How to get xy value of 2 points for slope calculation

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
nicksflok
Posts: 2
Joined: Sat Mar 05, 2016 10:09 am
antibot: No, of course not.

How to get xy value of 2 points for slope calculation

Post by nicksflok » Mon Mar 14, 2016 8:46 am

Hi all,

I have a SingleTimeSeriesDataset with the y set storing the price in data type double and x set storing the date in data type date.
I would like to calculate the slope between 2 points on the chart. And I find that I don't know how to get the distance between 2 dates so that I can calculate the slope.

For example,
The price on 2015-12-28 is 10000
The price on 2016-01-22 is 12000

I can calculate the y different is (12000-10000) but I don't know the x different between the date 2015-12-28 and 2016-01-22.
I am grateful if anyone can help, thanks!

Regards,
Nick

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: How to get xy value of 2 points for slope calculation

Post by paradoxoff » Tue Mar 15, 2016 2:56 pm

java.util.Date has the method getTime() which returns a long which represents the time in milliseconds since january 1st, 1970. The differences between the getTime() value of two Date instances is the time gap in milliseconds. Since this difference is not very handy, you might prefer to convert the time gap to days, which can be achieved by multiplication with 86400000. if youo use that value as denominator, the resulting slop will be in (currency change)/day.

nicksflok
Posts: 2
Joined: Sat Mar 05, 2016 10:09 am
antibot: No, of course not.

Re: How to get xy value of 2 points for slope calculation

Post by nicksflok » Fri Jun 10, 2016 10:38 am

I would like to refine my question as: How to get the x and y coordinate of 2 points on the chart so that I can calculate the slope between these 2 points?

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: How to get xy value of 2 points for slope calculation

Post by John Matthews » Sat Jun 11, 2016 9:58 am

One approach is to invoke Regression.getOLSRegression, passing it an XYDataset containing the two points; result[1] will be the slope. A complete example is shown here.

Locked