views:

15

answers:

1

I'm using a PHP graphing library from PEAR called Image_Graph to graph points of data in represented by bars. The graphs look fine when there's more than one datapoint, but when there's only one data point it looks like this:

http://img594.imageshack.us/img594/2944/screenshot20100715at528s.png

Has anyone else experienced this problem with PEAR's Image_Graph before? Does anyone know a fix? I have tried using the latest version of Image_Graph as well as a copy from SVN.

Here is my graphing code:

public function drawGraph() {
    $canvas =& Image_Canvas::factory('png', array('width' => 775, 'height' => 410, 'antialias' => true));
    $graph =& Image_Graph::factory('graph', $canvas);

    $font =& $graph->addNew('ttf_font', 'lib/fonts/Helvetica_Neue.ttf'); 
    $font->setSize(12);
    $graph->setFont($font);

    $plotarea =& $graph->addNew('plotarea');
    $dataset =& Image_Graph::factory('dataset');

    foreach ($this->getResults() as $res) {   
     $dataset->addPoint($res['name'], $res['value']);
    }

    $plot =& $plotarea->addNew('bar', &$dataset);

    $axisTitle = $this->_resultType->getName() . " (" . $this->_resultType->getUnits() . ")";
    $axisY =& $plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
    $axisY->setTitle($axisTitle, 'vertical');

    $filename = $this->_getFilename();
    return ($graph->done(array('tohtml' => 'true',
      'filename' => GRAPHS_DIR . $filename )));
}

I think this has got to be a bug with Image_Graph, but I'm not sure where it could be.

Thanks for your help!

A: 

Hi Zanneth, not sure if you still need the answer :)

If there is only one value Image_graph plots the bar above the Y-axis.

it would be nice to know what $this->getResults() contains, I suppose that 'name' is a string and that the array is something like array('name' => '33_Toshiba_pPVT_16GB', 'value' => 16.66). Having a string on the X axis will produce a Category X-Axis. You could add a dummy value after populating $dataset to solve this

if( count($this->getResults()) == 1 )
  $dataset->addPoint('', 0);

This should do it, but if the x-axis is linear ('name' is a number), then force a minimum and maximum x-axis value and the bar will display nicely.

$axisX =& $plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$axisX->forceMinimum(1);
$axisX->forceMaximum(3);

Alternatively, here is a fix if you want to edit the Image_Graph code http://pear.php.net/bugs/bug.php?id=7423

PS: I could only get your example working by replacing

$plot =& $plotarea->addNew('bar', &$dataset);

with

$plot =& $plotarea->addNew('bar', array(&$dataset));
silverskater