Smooth line class

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
pstng
Posts: 3
Joined: Wed Mar 21, 2007 7:56 pm

Smooth line class

Post by pstng » Wed Mar 21, 2007 8:06 pm

I've seen that many people asks for a method to draw smooth lines.
I've done a class that converts the datasets to datasets following bezier lines between the given points. It's like the method used in PEAR Image_Graph in PHP.
The class is the following:

Code: Select all

import java.awt.geom.*;
import org.jfree.data.xy.*;
import  org.jfree.data.time.*;
import java.util.*;

/**
 * Creates smooth curves from different datasets, interpolating 
 * points following Bezier curves
 * 
 * @author Roger Veciana
 * @version  0.1, 03/21/07
 */

public class SmoothLine {
	/** Total number of samples that the class will return with the dataset. */
	private int samples = 100;
	/** The smooth factor determinates how open are the bezier curves, and also how much do they fit to the data. */
	private double smooth_factor = 0.75;
	/** The points given by the user to be smoothed*/
	private Point2D[] points;

	/**
	 * Constructor from a Point2D array.
	 * @param points The Point2D array that will be used to get the reference points for the smooth line.
	 */
	public SmoothLine(Point2D[] points){
		this.points = points;
	}
	
	public SmoothLine(TimeSeries time_serie){
		
		//First, we set the Points2D array used to store the data
		points = new Point2D[time_serie.getItemCount()];
		//Fill the array
		for(int i = 0; i < time_serie.getItemCount(); i++){
		points[i] = new Point2D.Double(new Double(time_serie.getTimePeriod(i).getFirstMillisecond()), time_serie.getValue(i).doubleValue());
		}
	}
	
	/**
	 * Gets and returns a Point2D array with int samples that can be used to
	 * fill a serie, or whatever.
	 * @param points the array of points to interpolate the Bezier lines.
	 * @param samples number of sampels to return.
	 * 
	 * */
public Point2D[] getPoint2DArray() {
	
	 double x1=0;
     double y1=0;
     double x2=0;
     double y2=0;
     
     double x0;
     double y0;
     double x3;
     double y3;
     
     int samples_interval = Math.round(samples / (points.length-1)); 
    Point2D[] points_return = new Point2D[samples];
    int pos_return = 0; //we'll store the pointer in the points_return array here. 
    //We iterate between the different given points,
     //calculating the Bezier curves between them
	for(int i=0; i < points.length-1; i++){
		//the last period may have a different number of samples in order to fit the sample value
		if(i == points.length-2){
			samples_interval = samples - (samples_interval*(points.length-2));
		}
		x1=points[i].getX();
         x2=points[i+1].getX();
         y1=points[i].getY();
         y2=points[i+1].getY();
         if(i>0){
         x0=points[i-1].getX();
         y0=points[i-1].getY();
         }else {
         x0 = x1 - Math.abs(x2 - x1);
         y0 = y1;
         }
         if(i < points.length -2){
         x3=points[i+2].getX();
         y3=points[i+2].getY();
         } else {
         x3 = x1 + 2*Math.abs(x1 - x0);
         y3 = y1;
         }
         Point2D[] points_bezier =  CalculateBezierCurve(x0,y0,x1,y1,x2,y2,x3,y3, samples_interval);
         //Fill the return array
         for(int j = 0 ; j < points_bezier.length; j++){
        	 points_return[pos_return] = new Point2D.Double(points_bezier[j].getX(),points_bezier[j].getY()); 
        	 pos_return++;
         }
         
	}
	 

	return points_return;
}
/**
 * Takes the points, calculates the Bezier curves and creates a XYSeries.
 * @param serie_name The name of the serie that will appear in the Legend, for example.
 * @return the XYSerie ready to be used.
 * */
public XYSeries getXYSeries(String serie_name){
	XYSeries serie = new XYSeries(serie_name);
	Point2D[] points_bezier = getPoint2DArray();
	for(int j = 0 ; j < points_bezier.length; j++){
		
		serie.add(points_bezier[j].getX(),points_bezier[j].getY());
	}
	return serie;
}
/**
 * Takes the points stored and creates a TimeSeries with a smooth line. If the contructor is not the one with TimeSeries as the parameter, the x value has to be UnixTimestamp in milliseconds.
 * @param serie_name
 * @return a TimeSeries with the name given as parameter and a Second.class as the RegularTimePeriod, to be able to interpolate.
 */
public TimeSeries getTimeSeries(String serie_name){
	//First, we create the TimeSeries to return
	TimeSeries time_serie= new TimeSeries(serie_name,Second.class);
	
	//Gets the Bezier curves
	Point2D[] points_bezier = getPoint2DArray();
	
	for(int j = 0 ; j < points_bezier.length; j++){
		//System.out.println(j + " X --> " + points_bezier[j].getX());
		//time_serie.add(RegularTimePeriod.createInstance(Second.class,new Date((long)points_bezier[j].getX()),RegularTimePeriod.DEFAULT_TIME_ZONE),points_bezier[j].getY());
		time_serie.add(new Second(new Date((long)points_bezier[j].getX())),points_bezier[j].getY());
	}
	
	
	return time_serie;
}
/**
 * Calculates the Bezier curve between two points
 * The idea is taken from the PEAR Image_Graph Smoothline
 *
 * 
 * @param x0 the point "just before" (<code>null</code> permitted).
 * @param y0 the point "just before" (<code>null</code> permitted).
 * @param x1 the actual point to calculate the control points for (<code>null</code> NOT permitted).
 * @param y1 the actual point to calculate the control points for (<code>null</code> NOT permitted).
 * @param x2 the point "just after" (<code>null</code> NOT permitted).
 * @param y2 the point "just after" (<code>null</code> NOT permitted).
 * @param x3 the point "just after" x2 (<code>null</code> permitted). 
 * @param y4 the point "just after" y2 (<code>null</code> permitted).
 * @param samples_interval number of points generated between the given points
 * */
private Point2D[] CalculateBezierCurve(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, int samples_interval){
	
	
	
	double control_point_1_x;
    double control_point_1_y;
    double control_point_2_x;
    double control_point_2_y;
    
   
     //Calculate the control points for the cubic bezier line
   	 control_point_1_x =controlPoint(x0,x1,x2);
     control_point_1_y =controlPoint(y0,y1,y2);
                
     control_point_2_x = controlPoint(x3,x2,x1);
     control_point_2_y = controlPoint(y3,y2,y1);
    
    //System.out.println("control1: " + control_point_1_x + " -- " + control_point_1_y);
    //System.out.println("control2: " + control_point_2_x + " -- " + control_point_2_y);
    
    
    double cx = 3.0 * (control_point_1_x - x1);
    double bx = 3.0 * (control_point_2_x - control_point_1_x) - cx;
    double ax = x2 - x1 - cx - bx;
    
    double cy = 3.0 * (control_point_1_y - y1);
    double by = 3.0 * (control_point_2_y - control_point_1_y) - cy;
    double ay = y2 - y1 - cy - by;
    
    //Let's calculate all the ponits that follow the Bezier curve.
    Point2D[] points = new Point2D[samples_interval];
     for(int j = 0; j < samples_interval; j++){
   	 double t = j*(1.0/samples_interval);
   	 //System.out.println("j: " + j + " t: " + t + " samples_int: " + (samples_interval));
   	 double x = (ax * t * t * t) + (bx * t * t) + (cx * t) + x1;
   	 double y = (ay * t * t * t) + (by * t *t) + (cy * t) + y1;
   	 //System.out.println("x: " + x + " y: " + y + " t: " + t);
   	 points[j] = new Point2D.Double(x,y);
     }
     return points;
	}

    private double controlPoint(double p1, double p2, double factor){
	 	        
     double sa = p2 + smooth_factor * (p2 -p1);
     double sb = (p2+ sa)/2;
     double m = (p2+ factor)/2;
     return (sb + m)/2;
    }
    /**
     * Returns the numebr of samples used to calculate the smooth line. 
     * @return the number of samples.
     */
    public int getSamples(){
    	return this.samples;
    }
    /**
     * Sets the number of samples to be used when calculating the smooth line.
     * @param samples the new number of samples (by default is 100) 
     */
    public void setSamples(int samples){
    	this.samples = samples;
    }
    /**
     * Returns the smooth factor that is set to be used for calculating the Bezier curves.
     * @return the smooth factor.
     */
    public double getSmooth_factor(){
    	return this.smooth_factor;
    }
    /**
     * Sets the smooth factor that will be used to calculate the Bezier curves.
     * @param samples the new smooth factor (by default is 0.75) 
     */
    public void setSmooth_factor(double smooth_factor){
    	this.smooth_factor = smooth_factor;
    }
    /**
     * Returns either the points entered by the user (if the Point2D[] where passed through the contructor) or the calculated Point2D[] if another option was used.
     * @return The Point2d array that will be used to calculate the Bezier curves
     */
    public Point2D[] getGivenPoints(){
    	return this.points;
    }
}
You just have to put the points in a Point2D array or a TimeSeries (or add a constructor that converts whatever to a Plot2D array) and then use the get functions to retrieve the resulting dataset.

An example from the JFreeChart using a TimeSeries:

Code: Select all

* -------------------
* TimeSeriesDemo.java
* -------------------
* (C) Copyright 2002-2005, by Object Refinery Limited.
*
*/

import java.awt.Color;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import  org.jfree.chart.ChartFactory;
import  org.jfree.chart.ChartPanel;
import  org.jfree.chart.JFreeChart;
import  org.jfree.chart.axis.DateAxis;
import  org.jfree.chart.plot.XYPlot;
import  org.jfree.chart.renderer.xy.XYItemRenderer;
import  org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import  org.jfree.data.time.*;
import  org.jfree.data.time.TimeSeries;
import  org.jfree.data.time.TimeSeriesCollection;
import  org.jfree.data.xy.XYDataset;
import  org.jfree.ui.ApplicationFrame;
import  org.jfree.ui.RectangleInsets;
import  org.jfree.ui.RefineryUtilities;


/**
* An example of a time series chart. For the most part, default settings are
* used, except that the renderer is modified to show filled shapes (as well as
* lines) at each data point.
* <p>
* IMPORTANT NOTE: THIS DEMO IS DOCUMENTED IN THE JFREECHART DEVELOPER GUIDE.
* DO NOT MAKE CHANGES WITHOUT UPDATING THE GUIDE ALSO!!
*/
public class TimeSeriesDemo1 extends ApplicationFrame {
   /**
    * A demonstration application showing how to create a simple time series
    * chart. This example uses monthly data.
    *
    * @param title the frame title.
    */
   public TimeSeriesDemo1(String title) {
        super(title);
        XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(900, 670));
        chartPanel.setMouseZoomable(true, false);
        setContentPane(chartPanel);
  }
 /**
   * Creates a chart.
   *
   * @param dataset a dataset.
   *
   * @return A chart.
   */
 private static JFreeChart createChart(XYDataset dataset) {
      JFreeChart chart = ChartFactory.createTimeSeriesChart(
          "Legal & General Unit Trust Prices", // title
          "Date",             // x-axis label
          "Price Per Unit",   // y-axis label
          dataset,            // data
          true,               // create legend?
          true,               // generate tooltips?
          false               // generate URLs?
      );
      chart.setBackgroundPaint(Color.white);
      XYPlot plot = (XYPlot) chart.getPlot();
      plot.setBackgroundPaint(Color.lightGray);
      plot.setDomainGridlinePaint(Color.white);
      plot.setRangeGridlinePaint(Color.white);
      plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
      plot.setDomainCrosshairVisible(true);
      plot.setRangeCrosshairVisible(true);
      XYItemRenderer r = plot.getRenderer();
     /* if (r instanceof XYLineAndShapeRenderer) {
          XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
          renderer.setShapesVisible(true);
          renderer.setShapesFilled(true);
      }*/
      DateAxis axis = (DateAxis) plot.getDomainAxis();
      axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
      return chart;
 }
 /**
   * Creates a dataset, consisting of two series of monthly data.
   *
   * @return the dataset.
   */
 private static XYDataset createDataset() {
      TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
      s1.add(new Month(2, 2001), 181.8);
      s1.add(new Month(3, 2001), 167.3);
      s1.add(new Month(4, 2001), 153.8);
      s1.add(new Month(5, 2001), 167.6);
      s1.add(new Month(6, 2001), 158.8);
      s1.add(new Month(7, 2001), 148.3);
      s1.add(new Month(8, 2001), 153.9);
      s1.add(new Month(9, 2001), 142.7);
      s1.add(new Month(10, 2001), 123.2);
      s1.add(new Month(11, 2001), 131.8);
      s1.add(new Month(12, 2001), 139.6);
      s1.add(new Month(1, 2002), 142.9);
      s1.add(new Month(2, 2002), 138.7);
      s1.add(new Month(3, 2002), 137.3);
      s1.add(new Month(4, 2002), 143.9);
      s1.add(new Month(5, 2002), 139.8);
      s1.add(new Month(6, 2002), 137.0);
      s1.add(new Month(7, 2002), 132.8);
      TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);
      s2.add(new Month(2, 2001), 129.6);
      s2.add(new Month(3, 2001), 123.2);
      s2.add(new Month(4, 2001), 117.2);
      s2.add(new Month(5, 2001), 124.1);
      s2.add(new Month(6, 2001), 122.6);
      s2.add(new Month(7, 2001), 119.2);
      s2.add(new Month(8, 2001), 116.5);
      s2.add(new Month(9, 2001), 90.7);
      s2.add(new Month(10, 2001), 114.5);
      s2.add(new Month(11, 2001), 106.1);
      s2.add(new Month(12, 2001), 110.3);
      s2.add(new Month(1, 2002), 111.7);
      s2.add(new Month(2, 2002), 111.0);
      s2.add(new Month(3, 2002), 109.6);
      s2.add(new Month(4, 2002), 113.2);
      s2.add(new Month(5, 2002), 111.6);
      s2.add(new Month(6, 2002), 108.8);
      s2.add(new Month(7, 2002), 101.6);
/***************************************/
/* Create the smooth timeseries */
/***************************************/    

  SmoothLine sl = new SmoothLine(s2);
     sl.setSamples(300);
     sl.setSmooth_factor(0.5);
/**********************/
      TimeSeriesCollection dataset = new TimeSeriesCollection();
      dataset.addSeries(s1);
      dataset.addSeries(s2);
//Add the smooth timeseries
      dataset.addSeries(sl.getTimeSeries("Bezier"));
      dataset.setDomainIsPointsInTime(true);
      return dataset;
 }
 /**
   * Creates a panel for the demo (used by SuperDemo.java).
   *
   * @return A panel.
   */
 public static JPanel createDemoPanel() {
      JFreeChart chart = createChart(createDataset());
      return new ChartPanel(chart);
 }
 /**
   * Starting point for the demonstration application.
  *
   * @param args ignored.
   */
 public static void main(String[] args) {
      TimeSeriesDemo1 demo = new TimeSeriesDemo1("Time Series Demo 1");
      demo.pack();
      RefineryUtilities.centerFrameOnScreen(demo);
      demo.setVisible(true);
 }
} 
Be careful using Bezier lines, because some times the maximums or minimums are changed. This can be minimized decreasing the "Smooth factor" using setSmooth_factor(double factor). Anyway, the method shouldn't be used if the accuracy of the representation needs to be high. It's only to do fancy plots of simple data.

Locked