当只有一个数据点时,PEAR Image_Graph 无法正确绘制图表
我正在使用 PEAR 中名为 Image_Graph 的 PHP 图形库来绘制由条形表示的数据点。当有多个数据点时,图表看起来不错,但当只有一个数据点时,图表看起来像这样:
http://img594.imageshack.us/img594/2944/screenshot20100715at528s.png
有没有其他人在 PEAR 的 Image_Graph 上遇到过这个问题?有谁知道修复方法吗?我尝试使用最新版本的 Image_Graph 以及 SVN 的副本。
这是我的图形代码:
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 )));
}
我认为这一定是 Image_Graph 的错误,但我不确定它可能在哪里。
感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定您是否还需要答案:)
如果只有一个值 Image_graph 会在 Y 轴上方绘制条形图。
很高兴知道 $this->getResults() 包含什么,我认为 'name' 是一个字符串,数组类似于
array('name' => '33_Toshiba_pPVT_16GB', 'value ' => 16.66)
。 X 轴上有一个字符串将产生一个类别 X 轴。您可以在填充 $dataset 后添加一个虚拟值来解决这个问题
这应该可以做到,但是如果 x 轴是线性的(“名称”是一个数字),则强制使用最小和最大 x 轴值,并且条形图将显示很好。
或者,如果您想编辑 Image_Graph 代码,这里有一个修复方法
http://pear.php.net/bugs/bug.php?id= 7423
PS:
我只能通过替换来让你的例子
工作
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
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.
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
with