Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php绘制条形图_Php_Bar Chart_Jpgraph - Fatal编程技术网

使用php绘制条形图

使用php绘制条形图,php,bar-chart,jpgraph,Php,Bar Chart,Jpgraph,我正在使用jpgraph库绘制条形图,但我正在找到一种方法来进行一些更改,例如: (a) X轴已经有文本a、B、C、D。如何添加另一组文本,例如,绘图a有3个条形图,我希望将每个条形图标记为给定的1、2、3 (b) 如何将每个条的值写在条的顶部 <?php // content="text/plain; charset=utf-8" require_once ('jpgraph/jpgraph.php'); require_once ('jpgraph/jpgraph_bar.php');

我正在使用
jpgraph
库绘制条形图,但我正在找到一种方法来进行一些更改,例如: (a) X轴已经有文本a、B、C、D。如何添加另一组文本,例如,绘图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();
?>

我仍然没有找到(a)的解决方案。

您可以通过
->legend='1'设置每个条形图上的图例

然后,您可以将图例布局设置为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();
输出:

首先,我想这不是你想要的?
$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();