使用 php 绘制 bargraph-jpgraph
Posted
技术标签:
【中文标题】使用 php 绘制 bargraph-jpgraph【英文标题】:Plot bargraph-jpgraph using php 【发布时间】:2020-08-07 22:26:30 【问题描述】:我正在使用jpgraph
库绘制条形图,但我正在寻找一种方法来进行一些更改,例如:
(a) X 轴已经有文本 A、B、C、D。如何添加另一组文本,例如 Plot A 有 3 个条,我想将每个条标记为 1、2、3,如给定
(b) 如何在条形顶部写入每个条形的值?
<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
$data1y=array(47,80,40,116);
$data2y=array(61,30,82,105);
$data3y=array(115,50,70,93);
// Create the graph. These two calls are always required
$graph = new Graph(350,200,'auto');
$graph->SetScale("textlin");
$theme_class=new UniversalTheme;
$graph->SetTheme($theme_class);
$graph->yaxis->SetTickPositions(array(0,30,60,90,120,150), array(15,45,75,105,135));
$graph->SetBox(false);
$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array('A','B','C','D'));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);
// Create the bar plots
$b1plot = new BarPlot($data1y);
$b2plot = new BarPlot($data2y);
$b3plot = new BarPlot($data3y);
// Create the grouped bar plot
$gbplot = new GroupBarPlot(array($b1plot,$b2plot,$b3plot));
// ...and add it to the graPH
$graph->Add($gbplot);
$b1plot->SetColor("white");
$b1plot->SetFillColor("#cc1111");
$b2plot->SetColor("white");
$b2plot->SetFillColor("#11cccc");
$b3plot->SetColor("white");
$b3plot->SetFillColor("#1111cc");
$graph->title->Set("Bar Plots");
// Display the graph
$graph->Stroke();
?>
编辑: 我通过在代码末尾添加(b)找到了解决方案:
$b1plot->value->Show();
$b2plot->value->Show();
$b3plot->value->Show();
我仍然没有找到 (a) 的解决方案。
【问题讨论】:
对于a),我猜a legend 不是你想要的? 【参考方案1】:您可以这样做获得栏顶部的值:
$b1plot->value->Show();<br>
$b1plot->value->SetFormat( "1" );<br>
$b2plot->value->Show();<br>
$b2plot->value->SetFormat( "2" );<br>
$b3plot->value->Show();<br>
$b3plot->value->SetFormat( "3" );<br>
您还可以将文本的位置设置为栏的中心:
$b1plot->SetValuePos( "center" );<br>
$b2plot->SetValuePos( "center" );<br>
$b3plot->SetValuePos( "center" );<br>
【讨论】:
答案清晰而有条理,对未来的用户很有帮助。我对您的答案没有批评,而是提出改进建议:如果您按照带有反映您建议的功能的条形图图形遵循示例代码,这将很有帮助,与原始问题和@user 的答案保持一致: 5215806(迪奥戈戈麦斯)。这将帮助未来的用户了解您的建议的期望。这不是对您的回答的批评,只是一种改进。【参考方案2】:您可以通过->legend = '1';
在每个bar_plot上设置图例
然后您可以将图例布局设置为 1 列,并为图表添加右边距,以便图例有空间。
这是我的工作代码:
$data1y=array(47,80,40,116);
$data2y=array(61,30,82,105);
$data3y=array(115,50,70,93);
// Create the graph. These two calls are always required
$graph = new Graph(350,200,'auto');
$graph->SetScale("textlin");
$theme_class=new UniversalTheme;
$graph->SetTheme($theme_class);
$graph->yaxis->SetTickPositions(array(0,30,60,90,120,150, 180, 210), array(15,45,75,105,135));
$graph->SetBox(false);
$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array('A','B','C','D'));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);
// Create the bar plots
$b1plot = new BarPlot($data1y);
$b2plot = new BarPlot($data2y);
$b3plot = new BarPlot($data3y);
// Create the grouped bar plot
$gbplot = new GroupBarPlot(array($b1plot,$b2plot,$b3plot));
// ...and add it to the graPH
$graph->Add($gbplot);
$b1plot->SetColor("white");
$b1plot->SetFillColor("#cc1111");
$b1plot->value->Show();
$b1plot->legend = '1';
$b2plot->SetColor("white");
$b2plot->SetFillColor("#11cccc");
$b2plot->value->Show();
$b2plot->legend = '2';
$b3plot->SetColor("white");
$b3plot->SetFillColor("#1111cc");
$b3plot->value->Show();
$b3plot->legend = '3';
$graph->title->Set("Bar Plots");
$graph->SetMargin(40,80,40,40);
$graph->legend->Pos(0.05,0.5, 'right', 'center');
$graph->legend->SetColumns(1);
// Display the graph
$graph->Stroke();
输出:
【讨论】:
以上是关于使用 php 绘制 bargraph-jpgraph的主要内容,如果未能解决你的问题,请参考以下文章