ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Problems plotting trend lines (PHP)
Posted by Chris on Jan-16-2018 18:49
Attachments:
Hello

I am trying to plot a short term trend line (blue) for the last 20 days, however it seems to be plotting the trend data the wrong way round. The short term trend should be up, not down.

This the print_r results of the yArray which has been split to extract the last 20 days data:

Array ( [0] => 1.34037 [1] => 1.344 [2] => 1.35022 [3] => 1.35022 [4] => 1.35139 [5] => 1.35132 [6] => 1.35949 [7] => 1.35113 [8] => 1.35525 [9] => 1.3568 [10] => 1.3568 [11] => 1.35768 [12] => 1.35709 [13] => 1.35322 [14] => 1.35126 [15] => 1.35415 [16] => 1.37276 [17] => 1.37276 [18] => 1.37334 [19] => 1.37946 )

This shows that the data is correct, but the short term trend line looks like it is pointing to the last value (1.37946) on the left hand side of the chart and not the right hand side.

This is the code used to generate the chart:

$xArraySplit = array_slice($xArray, -$recentTrend, $recentTrend);
$yArraySplit = array_slice($yArray, -$recentTrend, $recentTrend);

$plotAreaObj = $c->setPlotArea(70, 70, $chartWidth - $widthSpace, $chartHeight - $heightSpace);

$plotAreaObj->setGridColor(0xc0c0c0, 0xc0c0c0);

$legendObj = $c->addLegend(50, 30, false, "arialbd.ttf", 9);
$legendObj->setBackground(Transparent);

$textBoxObj = $c->addTitle($chartTitle, "timesbi.ttf", 15);
$textBoxObj->setBackground(0xccccff, 0x000000, glassEffect());

# Set the labels on the x axis. Rotate the labels by 45 degrees.
$labelsObj = $c->xAxis->setLabels($xArray);
$labelsObj->setFontAngle(45);

# Display 1 out of 30 labels on the x-axis.
$c->xAxis->setLabelStep(30);

# Add a line layer to the chart
$lineLayer = $c->addLineLayer($yArray);

$dataSetObj = $lineLayer->addDataSet($yArray, 0xcc9966, "Closing rates");
$dataSetObj->setDataSymbol(SquareSymbol, 7);

$trendLayerObj = $c->addTrendLayer($yArray, $longTrendColour, "Long Term Trend");

$trendEnd = count($xArray);
$trendStart = $trendEnd - $recentTrend;

$trendColor = $c->xZoneColor($trendStart, Transparent, $c->xZoneColor($trendEnd,$shortTrendColour, Transparent));

$recentTrendText = "Recent Trend (" . strval($recentTrend) . " days)";

$trendLayerObj2 = $c->addTrendLayer2($xArraySplit, $yArraySplit, $trendColor, "");

//$c->addTrendLayer("", $shortTrendColour,  $recentTrendText);

return $c;

Also, I cannot get the blue short term legend to work correctly because adding the empty trend layer (commented out) indexes the Y axis to zero which compresses the currency values to a straight line.

Can you suggest how I can add the legend using a different approach?

Any help/advice would be appreciated.

Thank you
05F7F921-73C6-4CAC-A188-970578D61610.png

  Re: Problems plotting trend lines (PHP)
Posted by Peter Kwan on Jan-17-2018 00:36
Hi Chris,

The error is in the $xArraySplit.

In your code, $xArray contains the axis labels. In ChartDirector, labels are just text for human reading, and have no other meaning and cannot be used as coordinates. If Axis.setLabels is used, the labels are just assumed to be at x = 0, 1, 2, 3, .... (the array index of the labels).

For the long-term trend layer, no x-coordinates is used too. So the points are assumed to be at x = 0, 1, 2, 3 ... (the array index). That's why the points match up with the labels.

In your x-zone color code, it uses array index as the x-coordinate too, and so it is at the expected position. For the short term trend layer, I suggest to use similar coordinates as your x-zone color, like

$trendLayerObj2 = $c->addTrendLayer($yArraySplit, $trendColor, "");
$trendLayerObj2->setXData2($trendStart, $trendEnd - 1);

Hopefully, this would generate the expected chart.

Regards
Peter Kwan

  Re: Problems plotting trend lines (PHP)
Posted by Chris on Jan-17-2018 00:47
Hi Peter

Many thanks. That works perfectly.

Could I trouble you to look at my second point about the short term legend. I tried your approach of creating a dummy empty trend layer but that forced the Y axis to plot from 0 which then compressed they Y data into a very narrow line as it hasn’t got a wide range.

Thank you.

  Re: Problems plotting trend lines (PHP)
Posted by Peter Kwan on Jan-17-2018 01:51
Hi Chris,

May be try to use an empty line layer instead of an empty trend layer.

Regards
Peter Kwan

  Re: Problems plotting trend lines (PHP)
Posted by Chris on Jan-17-2018 02:13
Hi Peter

Thank you for your suggestion, but unfortunately it has the same affect as the empty trend layer ie sets the start of the Y axis to 0 rather than making it data aware.

  Re: Problems plotting trend lines (PHP)
Posted by Peter Kwan on Jan-17-2018 12:06
Hi Chris,

I read your code again, and I noticed that the code to add the empty layer has an issue. The empty layer should use an empty array, like:

$c->addTrendLayer(array(), $shortTrendColour,  $recentTrendText);

Both addLineLayer and addTrendLayer should work if an empty array is used.

In addLineLayer/addTrendLayer, the first parameter is supposed to be an array of numbers. If it is not, ChartDirector will ask PHP to coerce it into an array of numbers, which is what most other standard PHP functions would do. For an empty string, PHP will coerce it into an array containing one number, which is zero. Since there is now a data value at 0, so ChartDirector auto-scaling needs to extend the axis to 0. A single point will not cause any line to be plotted, as it takes at least 2 points to draw a line. That's why you cannot see the zero point in the chart, but it still has an effect on the axis scale.

Hope this can help.

Regards
Peter Kwan

  Re: Problems plotting trend lines (PHP)
Posted by Chris on Jan-17-2018 18:05
Hi Peter

That solved it. Thank you very much for your help and explanations.

Your support is fantastic, and very helpful.