Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.8.0

Bug #7423 No bar for datasets with single value
Submitted: 2006-04-19 09:47 UTC Modified: 2012-01-27 14:42 UTC
From: christoffer at natlikan dot se Assigned:
Status: No Feedback Package: Image_Graph (version 0.7.2)
PHP Version: 5.1.1 OS: Linux 2.4.21
Roadmaps: (Not assigned)    
Subscription  


 [2006-04-19 09:47 UTC] christoffer at natlikan dot se (Christoffer Lund)
Description: ------------ When you have datasets with only one value no bar will show upp. I know it's kind of silly to have a graph with only one bar... but it's not my call. I've been trying to fix it and think I've come up with a solution. By changing the lines 705-707 in Axis.php from: if ($this->_axisValueSpan == 0) { $this->_delta = false; } elseif ($this->_type == IMAGE_GRAPH_AXIS_X) { to: /* if ($this->_axisValueSpan == 0) { $this->_delta = false; } elseif ($this->_type == IMAGE_GRAPH_AXIS_X) { */ if ($this->_type == IMAGE_GRAPH_AXIS_X) { In other words, by removing the first if-satement. Test script: --------------- <?php require_once 'Image/Graph.php'; // Create the graph $Graph =& Image_Graph::factory('graph', array(400, 300)); // Add a TrueType font $Font =& $Graph->addNew('font', 'Verdana'); // Set the font size to 11 pixels $Font->setSize(8); $Graph->setFont($Font); $Graph->add( Image_Graph::vertical( Image_Graph::factory('title', array('Bar Chart Sample', 12)), Image_Graph::vertical( $Plotarea = Image_Graph::factory('plotarea'), $Legend = Image_Graph::factory('legend'), 90 ), 5 ) ); $Legend->setPlotarea($Plotarea); // Create the dataset $Dataset =& Image_Graph::factory('random', array(1, 2, 15, false)); // Create the 1st plot as smoothed area chart using the 1st dataset $Plot =& $Plotarea->addNew('bar', array(&$Dataset)); // Set a line color $Plot->setLineColor('gray'); // Set a standard fill style $Plot->setFillColor('blue@0.2'); // Output the Graph $Graph->done(); ?> Expected result: ---------------- A graph with one centered bar. Actual result: -------------- A graph with no bar at all.

Comments

 [2006-04-24 23:26 UTC] alex_lai at hotmail dot com (Alex)
I want to provide my testing result for generating a graph with only one bar in it for v0.7.2. If the x-axis is a Image_Graph_Axis_Category or Image_Graph_Axis, the bar does get plotted. However, the bar is plotted right on the y-axis. If you change the color of the bar graph you will be able to view this. For a logarithmic axis, the single bar is generated correctly. There are existing work arounds for this behavior. If the x-axis is linear, Image_Graph_Axis, then force a minimum and maximum x-axis value and the bar will display nicely. If the x-axis is a category type, Image_Graph_Axis_Category, then add additional value to the dataset with x and y value (y's set to zero) in the desired order to have it appear on the x-axis. A possible appropriate behavior is to modify the code to let users input an offset value from the y-axis for the first value on the x-axis or vice versa, something like, setAxisOffset. This would also be beneficial for logarithmic axis as well. Alex
 [2007-09-30 15:31 UTC] srynoname (Stefan Mai)
hello, i have the same problem - a bar chart and only one value. i've read alexs post, but i just don't get it. i want the one bar i have to be displyed and using the whole width of the "chartboard"/plotarea. somebody can please post a sample code? thank you very much!
 [2008-03-03 19:09 UTC] adrianbj (Adrian Jones)
I just wanted to confirm that christoffer's solution worked for me - my single data point graphs now plot properly as expected. Thanks!
 [2009-11-20 05:57 UTC] ghhoriuchi (Takahiko Horiuchi)
For one graph, this fix resolved the problem. But for another graph, this fix caused the error "Division by zero in /usr/share/php/Image/Graph/Axis.php." So I changed the code from: function _calcDelta() { if ($this->_axisValueSpan == 0) { $this->_delta = false; } elseif ($this->_type == IMAGE_GRAPH_AXIS_X) { $this->_delta = (($this->_transpose ? $this->height() : $this->width()) - ($this->_axisPadding['low'] + $this->_axisPadding['high'])) / ($this->_axisValueSpan + ($this->_pushValues ? 1 : 0)); } else { $this->_delta = (($this->_transpose ? $this->width() : $this->height()) - ($this->_axisPadding['low'] + $this->_axisPadding['high'])) / ($this->_axisValueSpan + ($this->_pushValues ? 1 : 0)); } } to: function _calcDelta() { $div = $this->_axisValueSpan + ($this->_pushValues ? 1 : 0); $div = $div ? $div : 1; if ($this->_type == IMAGE_GRAPH_AXIS_X) { $this->_delta = (($this->_transpose ? $this->height() : $this->width()) - ($this->_axisPadding['low'] + $this->_axisPadding['high'])) / $div; } else { $this->_delta = (($this->_transpose ? $this->width() : $this->height()) - ($this->_axisPadding['low'] + $this->_axisPadding['high'])) / $div; } } I don't know what does _pushValues mean, but in my application, this code works well.
 [2011-08-28 11:24 UTC] doconnor (Daniel O'Connor)
-Status: Open +Status: Feedback
Is anyone able to provide a patch against the current source; with the proposed fixes? And reproduceable test steps (perhaps in the form of a unit test?)
 [2012-01-27 14:42 UTC] doconnor (Daniel O'Connor)
-Status: Feedback +Status: No Feedback