0

One thing I've been struggling with in pChart is keeping the Y axis within the boundaries of the compass directions. In my case I am attempting to plot wind direction for a weather station, however the resulting charts always come out with a scale of 0-400. This prevents me from adding meaningful customizations such as defining horizontal bars at 0, 90, 180, and 270 degrees.

Some sample code to generate results...

<?php
include("pchart/class/pData.class.php");
include("pchart/class/pDraw.class.php");
include("pchart/class/pImage.class.php");

$points = array(0, 10, 90, 270, 345, 355);

$scale = array(
    'Mode' => SCALE_MODE_MANUAL,
    'ManualScale' => array(0=>array("Min"=>0,"Max"=>360)),
);

$data = new pData();
$data->addPoints($points, "dir");

$picture = new pImage(256, 128, $data, TRUE);
$picture->setFontProperties(array("FontName" => "pchart/fonts/GeosansLight.ttf","FontSize" =>10));
$picture->setGraphArea(40,10,216,108);
$picture->drawScale($scale);
$picture->drawPlotChart(array("PlotSize"=>1,"PlotBorder"=>TRUE,"BorderSize"=>1));
$picture->Render("wind.png");

echo "<img src='wind.png'>";

And the resulting chart... Wind direction chart results

As you can see, the Y axis does not honor the requested ManualScale settings despite the data points being well within the boundaries. I have tried use a Max of 359 and 360 (with 360 being the more correct choice since decimal values may be entered). I have also tried specifying scale MinDivHeight (divided by 5 to get the four compass directions) and/or Factors (testing with multiples of 90), but values are always overwritten in favor of a chart that spans 0-400. On the other hand, using a scale of 0-100 for percentage data works exactly as expected.

Am I missing something, or is this just a limitation of pChart that makes it impossible to perform this simple task?

1 Answers1

0

It turns out I was really close to an answer and didn't realize it. The "Factors" attribute is barely mentioned in the pChart wiki with no information on how to use it, but I had previously run across a discussion of it where the answer was to provide the value in the amount you wanted to split your axis by. That answer was only partially correct -- you must supply an array that also includes the number of times you want to split the axis.

So to answer my own question, I needed to add another line to the $scale array:

'Factors' => array(4,90)

With that line in place, setting Max to either 359 or 360 both resulted in a chart with a scale of 0-360, including horizontal breaks automatically created at 0, 90, 180, 270, and 360. Once I applied this line to my existing code it generated a wind direction plot exactly as desired.