TrendLines!

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

TrendLines!

Post by Kishore » Mon Oct 21, 2002 12:02 pm

Hi,
Is it possible to draw a Trend Line using This JFreeChart.
thanks,
Kishore.

Dave Gilbert

Re: TrendLines!

Post by Dave Gilbert » Tue Oct 22, 2002 9:11 am

Yes, you can use the Regression class to fit a linear or power regression to data from an XYDataset. From the results, you can create a LineFunction2D or a PowerFunction2D and then use the DatasetUtilities class to "sample" the function, creating another XYDataset.

Regards,

DG.

Kishore

How to get the start and end values of the Regression series

Post by Kishore » Tue Oct 29, 2002 6:23 am

Hello,
i want to use LinearRegression line. hear is my code.
//double dimensional array containing sample data
double [][] my2d=
{
{1,10},{2,11},{3,13},{4,15},{5,17},{6,18},{7,19},{8,20},{9,21},{10,22},{11,23},{12,24},
{12.1,10},{12.3,11},{12.5,13},{13.4,15},{13.8,17},{14.1,18},{14.7,19},{15,20},{15.9,21},{16,22},{18,23},{20,24}
};

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

DatasetUtilities.sampleFunction2D() function expects (fn,double start,double end,int samples,title) as arguments.
in this how do i get the values of start and end of the series.
is there a methods available to get the values of start and end values of the series,wcich i got it using my2d array.
Thanks in advance,
Kishore.

Dave Gilbert

Re: TrendLines!

Post by Dave Gilbert » Tue Oct 29, 2002 8:53 am

You can choose whatever start and end values you want, the resulting dataset will be defined only for the range you choose. In your example, you probably want the trend line defined for the same range as your data, 1 to 20.

Eventually, I'd like to add a feature to the XYPlot that can draw the Function2D instance directly on the chart, without having to worry about sampling it into a dataset.

Regards,

DG.

Justin Johnson

Re: TrendLines!

Post by Justin Johnson » Mon Nov 18, 2002 3:55 am

Dave,

Would you mind helping me out here? I can't remember much from my math classes. :-(

I have a VerticalXYBarChart, plotted over time, with dates on the x axis. It is plotting license usage over time. I have an overlaid plot so that I can add a line to the graph for the linear regression. The code is all in place, but I also am not sure what to use for start, end, and sample values. I tried anything for start and end, and the plot went way back in time to dates I don't have added to my dataset. I'm kind confused here. Any info you can give me would be greatly appreciated! :-)

Thanks.
Justin

Dave Gilbert

Re: TrendLines!

Post by Dave Gilbert » Mon Nov 18, 2002 10:09 am

The dates you see on the axis in JFreeChart are really numbers. Each date is represented as the number of milliseconds since midnight 1-Jan-1970 GMT, the encoding used by java.util.Date.

For your start and end values, create a Date object <= start, and another Date object >= end. Then use the getTime() method to extract the millisecond count for each one. Use these as your start and end values.

The 'sample' value controls how many points are created in between the start and end values. For a straight line you don't need many samples at all (2 should probably work). For a curve, you need to increase the number of samples, because each point is joined with a straight line and you want the curve to NOT look jagged.

Regards,

DG

Justin Johnson

Re: TrendLines!

Post by Justin Johnson » Mon Nov 18, 2002 3:07 pm

Great! Since I had a BasicTimeSeries, I did the following.

// create subplot 2...
// First, get a and b for the formula y = bx + a.
double[] ols = Regression.getOLSRegression(dataset,0);
Function2D curve = new LineFunction2D(ols[0], ols[1]);
// Now plot the line. We need to determine the starting and ending values for our function.
// We do this by finding the epoch number for the first datapoint we have and for the
// last datapoint we have, since that is how they are indexed.
TimePeriod startingTimePeriod = used.getTimePeriod(0);
TimePeriod endingTimePeriod = used.getTimePeriod(used.getItemCount() - 1);

// Convert the TimePeriod's into the epoch number, which is the number of
// milliseconds since midnight 1-Jan-1970 GMT, the encoding used by java.util.Date.
long startingEpoch = startingTimePeriod.getStart();
long endingEpoch = endingTimePeriod.getEnd();
// The sample value specifies how many data points are plotted for the linear
// regression. If you want a straight line, a sample value of 2 should be fine.
// If you want a curved line, you should specify a high value, because a straight
// line is drawn between each datapoint, so a high sample value will make it
// looked more curved instead of jagged.
int sampleValue = 2;

XYDataset regressionData = DatasetUtilities.sampleFunction2D(curve, startingEpoch, endingEpoch, sampleValue,
"Fitted Regression Line");


Thanks so much for your help!
Justin

Justin Johnson

Re: TrendLines!

Post by Justin Johnson » Mon Nov 18, 2002 8:10 pm

Dave,

Another question. Is there a way to plot polynomial trends?

Justin

Dave Gilbert

Re: TrendLines!

Post by Dave Gilbert » Mon Nov 18, 2002 11:41 pm

There's no code for fitting polynomials yet, but I hope that it can be incorporated in the same way as the existing linear and power regression code in the Regression class. It isn't high on my priority list right now, so I hope someone else will have a go at it...

Regards,

DG

Justin Johnson

Re: TrendLines!

Post by Justin Johnson » Tue Nov 19, 2002 3:08 am

Alright. I'll take a look at it and see what I can do.

Justin

Locked