how to add trendline in JFreeChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
lakshmi
Posts: 10
Joined: Wed Mar 08, 2006 6:15 am

how to add trendline in JFreeChart

Post by lakshmi » Wed Mar 08, 2006 6:57 am

hi,

iam generating a plot using JFreeChart.
In my program, iam getting the values from the database(sql),from the retreived values i will generate the chart.iam using Timeseries plot and XYdataset.i have dates in the x-axis and numbers in the y-axis.for this program i got the plot. but now i need to add trend line for this plot.
i thought of using getlinerfit() method from org.jfree.data.statistics,but in that method we need to pass the data.since iam taking the values directly from the database,i cant pass the data.please can anyone suggest me how to add trendline for the plot.(the values will be in the database).

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Wed Mar 08, 2006 9:11 am

Take a look at RegressionDemo1.java which comes with the developers guide.

lakshmi
Posts: 10
Joined: Wed Mar 08, 2006 6:15 am

how to add trendline in JFreeChart

Post by lakshmi » Wed Mar 08, 2006 10:42 am

In regressiondemo1.java, they have created the sample dataset and have passed the vlues.

XYDataset regressionData = DatasetUtilities.sampleFunction2D(curve,
2.0, 11.0, 100, "Fitted Regression Line");

in the above method samplefunction, they are passing the starting and end values for the data as 2.0 and 11.0 respectively. but in my case iam taking the value from the database and moreover my x-axis is date, so how do i give that in the method?

develop
Posts: 296
Joined: Wed Mar 23, 2005 10:01 pm

Post by develop » Wed Mar 08, 2006 3:44 pm

i have drawn trendline on jfreechart using XYLineAnnotation. in this you have to give chart x and y co ordinates. you can convert dates int double

(double)date.getTime() and use this.

my requirement was slightly different. in my case user can draw his own trendline on mouse events. this way i get end points and draw XYLineAnnotation.

let me know if you need more information.
hope this helps

lakshmi
Posts: 10
Joined: Wed Mar 08, 2006 6:15 am

how to add trendline in JFreeChart

Post by lakshmi » Thu Mar 09, 2006 9:59 am

when i was converting dates to double it says inconvertable types.
iam using the below function to draw the regression line

public static XYDataset sampleFunction2D(Function2D f,
double start,
double end,
int samples,
Comparable seriesKey)

since my x-axix is date, i need to pass the start and end value as dates.
but it is not possible to convert dates into doubles.
can u please suggest me some idea to proceed.

develop
Posts: 296
Joined: Wed Mar 23, 2005 10:01 pm

Post by develop » Thu Mar 09, 2006 3:11 pm

you can use date as double value.

Date date = new Date();
double d1 = (double)date.getTime();

if you are using calendar.

Calendar cal = Calendar.getInstance();
double d1 = (double)cal.getTime().getTime(); // two times getTime() because cal.getTime() gives date and so need to use date.getTime() again.

also if you want to convert double date into real Date object you can do that also. (may be for testing)

Date newDate = new Date(new Double(date).longValue());

hope this helps

lakshmi
Posts: 10
Joined: Wed Mar 08, 2006 6:15 am

how to add trendline in JFreeChart

Post by lakshmi » Thu Mar 09, 2006 3:51 pm

thanks for ur suggestion.
i was able to convert the date into double
when i execute the program, it compiles but during the execution it says some error

C:\Program Files\Java\jdk1.5.0_06\bin>java RegressionDemo1
1.1361402E12
1.1361402E12
Exception in thread "main" java.lang.IllegalArgumentException: Requires 'start'
< 'end'.
at org.jfree.data.general.DatasetUtilities.sampleFunction2D(DatasetUtili
ties.java:501)
at RegressionDemo1.createChartPanel1(RegressionDemo1.java:187)
at RegressionDemo1.createContent(RegressionDemo1.java:69)
at RegressionDemo1.<init>(RegressionDemo1.java:59)
at RegressionDemo1.main(RegressionDemo1.java:271)

i dont know why it shows error even after i have changed to double.
the first two values are basically the start and end values which i have printed.

develop
Posts: 296
Joined: Wed Mar 23, 2005 10:01 pm

Post by develop » Thu Mar 09, 2006 3:55 pm

see the dates, they are same dates. start should be less than end. they can not be same.

for test purpose. you can use this.
double lowerBound = plot.getDomainAxis().getRange().getLowerBound();
double upperBound = plot.getDomainAxis().getRange().getUpperBound();

and give this values as start (lower) and end (upper). this should work.

also. make sure that start should always be less than end.

for test you can convert this back to dates and see excatly what dates they are.

let me know if you need more help.

lakshmi
Posts: 10
Joined: Wed Mar 08, 2006 6:15 am

how to add trendline in JFreeChart

Post by lakshmi » Mon Mar 13, 2006 4:14 am

my starting date is less than the end date.
but, iam getting the same error what i said before.

lakshmi
Posts: 10
Joined: Wed Mar 08, 2006 6:15 am

how to add trendline in JFreeChart

Post by lakshmi » Mon Mar 13, 2006 4:27 am

hi develop, i got the trendline.
thanks for ur help.

Locked