php生成柱状图

时间:2022-07-29 11:51:34

php生成柱状图

代码:

<?php
 include("../../jpgraph/jpgraph.php");
 include("../../jpgraph/jpgraph_bar.php");
 include("../../jpgraph/jpgraph_line.php");
 
 //我们需要一些数据
 $datay=array(31,44,49,40,24,47,12);
 
 //设置图形容器
 $graph=new Graph(600,300,"auto");
 $graph->img->SetMargin(60,30,30,40);
 $graph->SetScale("textlin");
 $graph->SetMarginColor("teal");
 $graph->SetShadow();
 
 //建立一个柱形
 $bplot=new BarPlot($datay);
 $bplot->SetWidth(0.6);
 
 //设置渐变填充的颜色
 $tcol=array(100,100,255);
 $fcol=array(255,100,100);
 $bplot->SetFillGradient($fcol,$tcol,GRAD_VERT);
 $bplot->SetFillColor("orange");
 $graph->Add($bplot);
 
 //设置图形标题
 $graph->title->Set("Sample Bargraph");
 $graph->title->SetColor("yellow");
 $graph->title->SetFont(FF_VERDANA,FS_BOLD,12);
 
 //设置坐标和标签
 $graph->xaxis->SetColor("black","white");
 $graph->yaxis->SetColor("black","white");
 
 //设置坐标字体
 $graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
 $graph->yaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
 $graph->yaxis->title->Set("Value Range");
 $graph->yaxis->title->SetColor("white");
 $graph->yaxis->title->SetFont(FF_VERDANA,FS_NORMAL,10);
 
 //设置x坐标的标题(颜色和字体)
 $graph->xaxis->title->Set("item Count");
 $graph->xaxis->title->SetColor("white");
 $graph->xaxis->title->SetFont(FF_VERDANA,FS_NORMAL,10);
 
 //最后,将图形发送到浏览器
 $graph->Stroke();
?>

结果截图:

php生成柱状图