how to add trendline in JFreeChart
how to add trendline in JFreeChart
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).
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).
how to add trendline in JFreeChart
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?
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?
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
(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
how to add trendline in JFreeChart
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.
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.
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
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
how to add trendline in JFreeChart
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.
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.
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.
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.
how to add trendline in JFreeChart
my starting date is less than the end date.
but, iam getting the same error what i said before.
but, iam getting the same error what i said before.
how to add trendline in JFreeChart
hi develop, i got the trendline.
thanks for ur help.
thanks for ur help.