因为在开发内部BI系统中需要画出统计图表,我选了Jpgraph 开源绘图工具实现需求。
之前实现过需求,没想到这次又花了很多时间回忆,各种搜索,真的是好记性不如烂笔头,
不会总结的人没有未来啊。
常用的三种图表样式:
|———— 3D饼图:
include ("jpgraph/jpgraph.php");include ("jpgraph/jpgraph_pie.php");include
("jpgraph/jpgraph_pie3d.php");$data
= array(218,100);$datatitle =
array(‘男‘,‘女‘);$title = "男女占比"; $width
= 490;$height = 350;$graph
= new PieGraph($width,$height,‘auto‘); $graph->SetShadow(); $graph->title->Set($title); $graph->title->SetFont(FF_SIMSUN,FS_BOLD,12); $p1 =
new PiePlot3D($data);//创建3D饼形图对象 $p1->ExplodeSlice(1); //分割线$p1->SetCenter(0.45); $graph->legend->SetFont(FF_SIMSUN,FS_BOLD,10);
//设置旁注字体$graph->legend->Pos(0.89,0.9,‘center‘,‘bottom‘);//设置旁注位置$graph->legend->SetFrameWeight(0); //图例文字外框边框border设置为0$graph->legend->SetFillColor(‘#FFFFFF‘); //图例文字框内的填充颜色$graph->legend->SetShadow(‘#C8C8C8‘, 0);$graph->legend->SetLayout(LEGEND_VERT);//旁注摆放形式[竖直] $p1->SetLegends($datatitle); $graph->Add($p1);$graph->Stroke();
|—————普通圆形图:
include ("jpgraph/jpgraph.php");include ("jpgraph/jpgraph_pie.php");$data
= array(218,100);$datatitle =
array(‘男‘,‘女‘);$title = "男女占比"; $width
= 490;$height = 350;$graph
= new PieGraph($width,$height,‘auto‘); $graph->SetShadow(); $graph->title->Set($title); $graph->title->SetFont(FF_SIMSUN,FS_BOLD,12); $graph->legend->SetFont(FF_SIMSUN,FS_BOLD,10);
//设置旁注字体$graph->legend->Pos(0.89,0.9,‘center‘,‘bottom‘);//设置旁注位置$graph->legend->SetFrameWeight(0); //图例文字外框边框border设置为0$graph->legend->SetFillColor(‘#FFFFFF‘); //图例文字框内的填充颜色$graph->legend->SetShadow(‘#C8C8C8‘, 0);$graph->legend->SetLayout(LEGEND_VERT);//旁注摆放形式[竖直]
// Create pie plot
$p1 = new
PiePlot($data);$p1->SetCenter(0.5,0.55);$p1->SetSize(0.3);
// Enable and set policy for guide-lines. Make labels line up vertically
$p1->SetGuideLines(true,false);$p1->SetGuideLinesAdjust(1.1);$p1->SetLegends($datatitle);
// Setup the labels
$p1->SetLabelType(PIE_VALUE_PER);$p1->value->Show();$p1->value->SetFont(FF_ARIAL,FS_NORMAL,9);$p1->value->SetFormat(‘%2.1f%%‘); $graph->legend->SetLayout(LEGEND_VERT); $graph->Add($p1);$graph->Stroke();
|————柱状图:
require_once (‘jpgraph/jpgraph.php‘);require_once
(‘jpgraph/jpgraph_bar.php‘); $datay = array(‘23‘,‘12‘,‘4‘,‘67‘,‘9‘);$datax
= array(‘0-10‘,‘11-50‘,‘51-100‘,‘101-500‘,‘500+‘);$title =
"综合排名"; $width=520;$height=380;
// Set the basic parameters of the graph
$graph = new
Graph($width,$height,‘auto‘);$graph->SetScale("textlin"); $top =
50;$bottom = 80;$left
= 80;$right = 25;$graph->Set90AndMargin($left,$right,$top,$bottom); $graph->xaxis->SetPos(‘min‘);
// Nice shadow
$graph->SetShadow();
// Setup title
$graph->title->Set($title);$graph->title->SetFont(FF_SIMSUN,FS_BOLD,10);
// Setup X-axis
$graph->xaxis->SetTickLabels($datax);
//
$graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD,12);
// Some extra margin looks nicer
$graph->xaxis->SetLabelMargin(5);
// Label align for X-axis
$graph->xaxis->SetLabelAlign(‘right‘,‘center‘);
// Add some grace to y-axis so the bars doesn‘t go
// all the way to the end of the plot area
$graph->yaxis->scale->SetGrace(20);
// Setup the Y-axis to be displayed in the bottom of the
// graph. We also finetune the exact layout of the title,
// ticks and labels to make them look nice.
$graph->yaxis->SetPos(‘max‘);
// First make the labels look right
$graph->yaxis->SetLabelAlign(‘center‘,‘top‘);$graph->yaxis->SetLabelFormat(‘%d‘);$graph->yaxis->SetLabelSide(SIDE_RIGHT);
// The fix the tick marks
$graph->yaxis->SetTickSide(SIDE_LEFT);
// Finally setup the title
$graph->yaxis->SetTitleSide(SIDE_RIGHT);$graph->yaxis->SetTitleMargin(35);
// Now create a bar pot
$bplot = new
BarPlot($datay);$bplot->SetFillColor("orange");$bplot->SetShadow();
//You can change the width of the bars if you like
//$bplot->SetWidth(0.5);
// We want to display the value of each bar at the top
$bplot->value->Show();$bplot->value->SetFont(FF_ARIAL,FS_BOLD,12);$bplot->value->SetAlign(‘left‘,‘center‘);$bplot->value->SetColor("black","darkred");$bplot->value->SetFormat(‘%.1f
mkr‘);
// Add the bar to the graph
$graph->Add($bplot);$graph->Stroke();
以上为常用的图表及对应的后端实现:给自己记忆和需要人的便捷;