|
Cannot display decimal numbers on multiline chart (PHP) |
Posted by Mick on Dec-12-2014 18:56 |
|
Hello
I want to display two lines on a chart, each of which may represent 6, 12 or 18
datapoints, depending on the user's choice. Here is my code:
//new chart 530w x 300h
$c = new XYChart(530, 300);
$c->setPlotArea(40, 0, 480, 280);
//add legend
$legendObj = $c->addLegend(50, 30, true, "arialbd.ttf", 9);
$legendObj->setBackground(Transparent);
//title for y axis
$c->yAxis->setTitle("Value on Loan as % of Market Cap");
//x axis labels (months)
$c->xAxis->setLabels($dateArray);
//add line layer to chart
$layer = $c->addLineLayer2();
//set line width to 2 px
$layer->setLineWidth(2);
//Add the data sets to the line layer.
//FTSE index (always displayed)
$layer->addDataSet($indexArray, $colorIndex, $nameIndex);
//stock 1
if(!empty($stock1Array)){
$layer->addDataSet($stock1Array, $colorStock1, $nameStock1);
}
//stock 2
if(!empty($stock2Array)){
$layer->addDataSet($stock2Array, $colorStock2, $nameStock2);
}
//sector
if(!empty($sectorArray)){
$layer->addDataSet($sectorArray, $colorSector, $nameSector);
}
//Output the chart
header("Content-type: image/png");
print($c->makeChart2(PNG));
On the current run, $indexArray has the values: 3.99, 4.01, 3.93, 4.18, 4.19, 4.26 and
$stock1Array has the values: 3.12, 2.66, 2.41, 2.17, 2.33, 2.21.
Neither line is displayed on the graph and no scale is displayed on the y axis. However,
I have discovered that if I use integer values for each array, both lines will display
correctly, as will the y axis scale.
Another problem is that the labels are not correctly displayed. They are pulled from
$dateArray, which also may contain 6, 12 or 18 elements, being the names of the
months over the user's chosen period. Whether I choose a period of 6 months, 12 or 18,
I only ever see 6 labels displayed, although I have checked that the array does contain
appropriate values. |
Re: Cannot display decimal numbers on multiline chart (PHP) |
Posted by Peter Kwan on Dec-13-2014 01:24 |
|
Hi Mick,
To diagnose the problem, please use the following code to display your data directly on
the chart:
//new chart 530w x 300h
$c = new XYChart(530, 300);
$c->setPlotArea(40, 0, 480, 280);
//add legend
$legendObj = $c->addLegend(50, 30, true, "arialbd.ttf", 9);
$legendObj->setBackground(Transparent);
//title for y axis
$c->yAxis->setTitle("Count=". (count($dataArray)) . ", [6]=" . $dateArray[6]);
//x axis labels (months)
$c->xAxis->setLabels($dateArray);
//add line layer to chart
$layer = $c->addLineLayer2();
//set line width to 2 px
$layer->setLineWidth(2);
//Add the data sets to the line layer.
//FTSE index (always displayed)
$layer->addDataSet($indexArray, $colorIndex, $nameIndex);
$c->addTitle("[" . $indexArray[0] . "] [" . $indexArray[2] . "]");
//stock 1
if(!empty($stock1Array)){
$layer->addDataSet($stock1Array, $colorStock1, $nameStock1);
}
//stock 2
if(!empty($stock2Array)){
$layer->addDataSet($stock2Array, $colorStock2, $nameStock2);
}
//sector
if(!empty($sectorArray)){
$layer->addDataSet($sectorArray, $colorSector, $nameSector);
}
//Output the chart
header("Content-type: image/png");
print($c->makeChart2(PNG));
With the above code, you should see the array size of the $dateArray in the y-axis title,
as well as the 7th element of the array (if it exists). Also, the chart title should contain
the first and third element of the $indexArray. This can help to confirm what are in those
arrays or if the values actually exist.
Please kindly let me know what is the result (eg. by attaching the chart produced).
Regards
Peter Kwan |
Re: Cannot display decimal numbers on multiline chart (PHP) |
Posted by Mick on Dec-13-2014 02:59 |
|
Hello, Peter
Thank you for that. As you can see from the attached graphic, the arrays must be empty.
That's with the graphic placed into the page as follows:
<tr>
<td style="padding-left: 70px;" class="noBorder">
<img src = "crest/newchart.php" />
</td>
</tr>
(though how the months come to be set as labels on the x-axis when the $dateArray is
empty is a mystery).
However, if I have the following instead:
<tr>
<td style="padding-left: 70px;" class="noBorder">
<?php include "crest/newchart.php"?>
</td>
</tr>
with breakpoints set in newchart.php, I can clearly see that the arrays are not empty.
I used ChartDirector fairly extensively a couple of years ago and was greatly impressed by
its ability to produce any kind of chart I might want. Now, for some reason, I don't seem
to be able to create even the simplest chart, so I'm a little embarrassed to be asking for
help with what should surely be quite straightforward. So thank you for your patience
-Mick
|
Re: Cannot display decimal numbers on multiline chart (PHP) |
Posted by Peter Kwan on Dec-13-2014 03:49 |
|
Hi Mick,
There is a typo mistake in my code. The count($dataArray) should be count($dateArray).
The explains why the array appears to be empty yet you have labels.
According to your code, to trouble-shoot your code, you can just point your browser
directly to:
http://your.webserver.com/path/to/crest/newchart.php
For trouble-shooting, there is no need to "include" it in another page.
If you "include" it in another page, the script works differently compared to using <img>. For
example, if you use <img>, it is a separate HTTP request, and according to your code, there
will be no GET or POST parameters. If you use "include", the included script will see the GET
or POST parameters of the containing PHP script. It will also inherts the containing PHP
script's variable scope (which means it would see the variables in the containing script.) If
your data (eg. $indexArray) is defined in the containing script, it would not be visible by the
"crest/newchart.php" if "<img>" is used, but will be visible if "include" is used.
So for proper trouble-shooting, we must make sure the "crest/newchart.php" is run without
being included in any other script. The best way is to just point the browser directly to
"crest/newchart.php".
Hope this can help.
Regards
Peter Kwan |
Re: Cannot display decimal numbers on multiline chart (PHP) |
Posted by Mick on Dec-13-2014 08:33 |
|
My word, Peter, I grow ever more stupid.
Thank you.
Mick |
|