|
Trend Line Forecast |
Posted by Mike B on Apr-11-2012 05:42 |
|
I am creating a trend line chart to display server disk space usage over time using Ruby. I
would like to know if it is possible for ChartDirector to forecast the date at which the disk
capacity is projected to reach a certain threshold so that I can display it on the chart. |
Re: Trend Line Forecast |
Posted by Peter Kwan on Apr-12-2012 01:59 |
|
Hi Mike,
When you ask ChartDirector to compute a trend line (using addTrendLayer), you can also ask ChartDirector for the trend line parameters, so that you code can determine when will the trend line reaches a certain value.
For a linear trend line, the equation is:
y = mx + c
The slope m can be retrieved using TrendLayer.getSlope, while the intercept c can be retrieved using TrendLayer.getIntercept. For a given y value, you can compute x as :
x = (y - c) / m
If the x-coordinates of your chart is really date/time (which means they are Ruby Time objects or values created with chartTime or chartTime2), the x computed above will be represented as clock seconds elapsed since 01-01-0001 00:00:00. You may use ChartDirector::getChartYMD to convert that value to yyyymmdd.
If you would like to display the above value in the chart, you may consider to add a scatter layer with just one point, which is the (x, y) value computed above. This ensures the axis is auto-scaled to include that point. You may also add a y-axis mark line (using Axis.addMark) and an x-axis mark line to visualize the threshold.
Hope this can help.
Regards
Peter Kwan |
Re: Trend Line Forecast |
Posted by pdeline on Sep-16-2012 22:28 |
|
Hey Peter -
I would like to follow up on this. I have implemented a chart that includes two lines. One
represents the amount of data used. The Y-values in this set (yData1) result in a slope of
0. The other line represents the maximum amount of storage available. For my example,
the Y-value in this set (yData2) is always 200. This chart displays just fine until I add the
TrendLayer.
When I set my X-values (xData) to be increments of 1, the chart appears as I expect.
When I set them to be increments of 2, the X-axis seems to double in length. For example,
when X-values go from 0 to 40 incrementing by 2, the X-axis displays the labels (0 to 40)
and the two lines correctly, but the X-axis extends to double the expected size. If I change
the increment to 3, the X-axis extends to triple the expected size.
This only occurs when I add the TrendLayer. I have also tested it with other Y data sets
the result in a non-zero slope with the same results. I have uploaded my test code as well
as the two images that illustrate the results.
Thanks in advance.
|
Re: Trend Line Forecast |
Posted by Peter Kwan on Sep-18-2012 00:40 |
|
Hi pdeline,
The issue is because the xData array is being used in two different ways - as labels, and as x-coordinates.
When you use xAxis.setLabels(xData), the xData are used as labels. The labels have no meaning at all to ChartDirector and is just for human reading. For example, you can change the xData to contain the labels ["Apple", "Oranges", "Water", ...], and the chart will still be exactly the same, except the x-axis labels are changed. For these labels, ChartDirector will put them at x = 0, 1, 2, 3, ... irrespective of what the labels are. For your case, as you have 21 labels, so they will be at x = 0, 1, 2, .... 20.
For the addLineLayer2, as you have not provided any x-coordinates to the line layer (using Layer.setXData), ChartDirector will assume the x-coordinates are x = 0, 1, 2, 3, ... This matches with the labels, which is also assumed to be at x = 0, 1, 2, 3, .... That's why the line looks correct.
For the addTrendLayer2, you do provide the x-coordinates as the first parameter to addTrendLayer2. So the data points for the addTrendLayer2 is at x = 0, 2, 4, .... 40. In other words, the x-range of the trend layer is much wider than that of the labels or line layer.
To solve the problem, there are two methods:
(a) Do not provide the x-coordinates to addTrendLayer2. Use addTrendLayer instead, which does not take x-coordinates. In this case, the data points for the trend layer will be at x = 0, 1, 2, 3 ... the same as that for the line layer and the labels.
or
(b) Do not use the xData as labels. Remove the line "xAxis.setLabels(xData)". Add the line lineLayer.setXData(xData) to use the xData as the x-coordinates of the data points for the line layer. In this case, the x-axis will be auto-scaled like the y-axis, with the labels automatically determined.
Hope this can help.
Regards
Peter Kwan |
|