Problem in getting the RegressionLine!

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Kishore

Problem in getting the RegressionLine!

Post by Kishore » Tue Oct 29, 2002 9:14 am

Hi ,
Thanks a lot for u'r Prompt Response.
here i am trying to modify the Overlaid plat demo to the regression.i've followed u'r suggestions.but, i am failed to get the second plot(i.e line containing the regression). and am also not able to define values for the X-axis.
could u please help to find out the mistake in this Application.
here is my code:
----------------------------------------------------------------------------------------
package com.jrefinery.chart.demo;

import java.awt.Color;
import com.jrefinery.chart.*;
import com.jrefinery.chart.tooltips.TimeSeriesToolTipGenerator;
import com.jrefinery.data.*;
import com.jrefinery.date.SerialDate;
import com.jrefinery.ui.ApplicationFrame;
import com.jrefinery.ui.RefineryUtilities;
import com.jrefinery.chart.data.LinearPlotFitAlgorithm;

public class Test extends ApplicationFrame {

XYDataset data,data2;

public Test(String title) {

super(title);

XYSeries series = new XYSeries("Test Data");
series.add(1, 1.0);
series.add(2, 1.1);
series.add(3, 1.3);
series.add(4, 1.5);
series.add(5, 1.7);
series.add(6, 1.8);
series.add(7, 1.9);
series.add(8, 2.0);
series.add(9, 2.1);
series.add(10,2.2);
series.add(11,2.3);
series.add(12,2.4);
series.add(12.1, 5.0);
series.add(12.3, 5.1);
series.add(12.5, 5.3);
series.add(13.4, 5.5);
series.add(13.8, 5.7);
series.add(14.1, 5.8);
series.add(14.7, 5.9);
series.add(15, 6.0);
series.add(15.9, 6.1);
series.add(16,6.2);
series.add(18,6.3);
series.add(20,6.4);

data = new XYSeriesCollection(series);

double [][] my2d=
{
{1,1.0},{2,1.1},{3,1.3},{4,1.5},{5,1.7},{6,1.8},{7,1.9},{8,2.0},{9,2.1},{10,2.2},{11,2.3},{12,2.4},
{12.1,5.0},{12.3,5.1},{12.5,5.3},{13.4,5.5},{13.8,5.7},{14.1,5.8},{14.7,5.9},{15,6.0},{15.9,6.1},{16,6.2},{18,6.3},{20,6.4}
};

JFreeChart chart = createOverlaidChart();
chart.getPlot().setBackgroundPaint(Color.yellow);
ChartPanel panel = new ChartPanel(chart, true, true, true, true, true);
panel.setPreferredSize(new java.awt.Dimension(270, 270));
setContentPane(panel);

double[] result=Regression.getOLSRegression(my2d);
LineFunction2D myf=new LineFunction2D(result[0],result[1]);
data2 =DatasetUtilities.sampleFunction2D(myf,1,20,10,"regression");
}

/**
* Creates an overlaid chart.
*
* @return the chart.
*/
private JFreeChart createOverlaidChart() {
// create subplot 1...
XYDataset data1 =data;
XYDotRenderer renderer1 = new XYDotRenderer();
XYPlot subplot1 = new XYPlot(data1, null, null, renderer1);

// create subplot 2...
XYItemRenderer renderer2 = new StandardXYItemRenderer();
XYPlot subplot2 = new XYPlot(data2, null, null, renderer2);
subplot2.setSeriesPaint(0, Color.green);

// make an overlaid plot and add the subplots...
ValueAxis domainAxis = new HorizontalDateAxis("X");
ValueAxis rangeAxis = new VerticalNumberAxis("Y");
OverlaidXYPlot plot = new OverlaidXYPlot(domainAxis, rangeAxis);
plot.add(subplot1);
plot.add(subplot2);

// return a new chart containing the overlaid plot...
return new JFreeChart("Linear Regression Example", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
}

/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
Test demo = new Test("Liner Regression Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
-----------------------------------------------------------------------------------
Thanks in advance,
Kishore.

Dave Gilbert

Re: Problem in getting the RegressionLine!

Post by Dave Gilbert » Wed Oct 30, 2002 10:22 am

Hi Kishore,

You create a new dataset (by sampling the regression function) AFTER you have created the overlaid chart. The new dataset is never assigned to the chart. You should probably move the last three lines in the constructor to just before the point where you call createOverlaidChart().

Regards,

DG.

Locked