JpGraph常用的几种图表
本文只是介绍了JpGraph生成常见的几种图表的方法,如果无法满足需求,可查看官方文档example中的例子。
柱状图1:
<?php include ("jpgraph/jpgraph.php"); include ("jpgraph/jpgraph_bar.php"); $data = array(19,23,34,38,45,67,71,78,85,87,96,145); //定义数组 $ydata = array("一","二","三","四","五","六","七","八","九","十","十一","十二"); $graph = new Graph(500,300); //创建新的Graph对象 $graph->SetScale("textlin"); $graph->SetShadow(); //设置阴影 $graph->img->SetMargin(40,30,40,50); //设置边距 $barplot = new BarPlot($data); //创建BarPlot对象 $barplot->SetFillColor('orange'); //设置颜色 $graph->Add($barplot); //将柱形图添加到图像中 $barplot->value->Show(); //设置显示数字 $graph->title->Set("CDN流量图"); //设置标题和X-Y轴标题 $graph->title->SetColor("red"); $graph->title->SetMargin(10); $graph->xaxis->title->Set("月份"); $graph->xaxis->title->SetMargin(5); $graph->xaxis->SetTickLabels($ydata); $graph->yaxis->title->Set("流 量(Mbits)"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置字体 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD); $graph->Stroke(); ?>
柱状图2:
<?php require_once ('jpgraph/jpgraph.php'); require_once ('jpgraph/jpgraph_bar.php'); $data1y=array(3,20,9,3,5,6); $data2y=array(12,2,1,7,5,4); // Create the graph. These two calls are always required $graph = new Graph(500,400); $graph->SetScale("textlin"); $graph->SetShadow(); $graph->img->SetMargin(40,30,20,40);//设置图形的边距 // Create the bar plots $b1plot = new BarPlot($data1y); $b1plot->SetFillColor("orange"); $b2plot = new BarPlot($data2y); $b2plot->SetFillColor("blue"); // Create the grouped bar plot $gbplot = new AccBarPlot(array($b1plot,$b2plot)); // ...and add it to the graPH $graph->Add($gbplot); $b1plot->value->Show(); $b2plot->value->Show(); //设置标题字体样式 $graph->title->Set("博客信息统计表"); $graph->xaxis->title->Set("X-标题"); $graph->yaxis->title->Set("Y-标题"); $mouth_arr=array("一月","二月","三月","四月","五月","六月"); $graph->xaxis->SetTickLabels($mouth_arr); $graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体 $graph->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->Stroke();
折线图:
<?php include ("jpgraph/jpgraph.php"); include ("jpgraph/jpgraph_line.php"); $data1 = array(523,634,371,278,685,587,490,256,398,545,367,577); //第一条曲线的数组 $data2 = array(19,23,34,38,45,67,71,78,85,87,90,96); //第二条曲线的数组 $graph = new Graph(600,300,"auto"); //创建新的Graph对象 $graph->SetScale("textlin"); $graph->SetShadow(); //设置图像的阴影样式 $graph->img->SetMargin(60,30,30,70); //设置图像边距(相对图片顶部) $graph->title->Set("CDN流量图"); //设置图像标题 $graph->title->SetMargin(15); //设置标题的边距 $lineplot1=new LinePlot($data1); //创建设置两条曲线对象 $lineplot2=new LinePlot($data2); $lineplot1->value->Show(); $lineplot1->value->SetColor("black"); $lineplot1->mark->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记 $lineplot1->mark->SetFillColor("red"); //设置填充的颜色 $lineplot1->mark->SetWidth(2); //设置圆形标记的直径为4像素 $lineplot1->SetColor("blue"); //设置折形颜色为蓝色 $lineplot1->SetCenter(); //在X轴的各坐标点中心位置绘制折线 $graph->Add($lineplot1); //将曲线放置到图像上 $lineplot1->value->Show(); $graph->Add($lineplot2); $lineplot2->value->Show(); $graph->xaxis->title->Set("月份"); //设置坐标轴名称 $graph->yaxis->title->Set("流 量(Gbits)"); $graph->xaxis->title->SetMargin(10); $graph->yaxis->title->SetMargin(10); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置字体 $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置x轴各值,传入一维数组 //参数:$gDateLocale->GetShortMonth() 底部为 Jan Feb... $mouth_arr=array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"); $graph->xaxis->SetTickLabels($mouth_arr); $graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体 $lineplot1->SetColor("red"); //设置颜色 $lineplot2->SetColor("blue"); $lineplot1->SetLegend("最大值"); //设置图例名称 $lineplot2->SetLegend("最小值"); $graph->legend->SetFont(FF_SIMSUN,FS_BOLD); $graph->legend->SetLayout(LEGEND_HOR); //设置图例样式和位置 $graph->legend->Pos(0.5,0.96,"center","bottom"); //设置下部lengend位置 $graph->Stroke(); //输出图像 ?>
饼状图
<?php require_once ('jpgraph/jpgraph.php'); require_once ('jpgraph/jpgraph_pie.php'); $data = array(89,23,34,38,45,67,71,78,85,87,90,96); $lable_data=array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"); $data1 = array(89,23,34,38,45,67,71,78,85,87,90,96); $graph = new PieGraph(500,450); $graph->SetShadow(); $graph->title->Set("CDN流量比例"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); $pieplot = new PiePlot($data); $pieplot->SetLegends($lable_data); $graph->legend->SetFont(FF_SIMSUN,FS_BOLD); $pieplot->SetSize(0.3); $pieplot->SetCenter(0.2,0.5); // Enable and set policy for guide-lines //显示指示线 /*$pieplot->SetGuideLines(true,false,true); $pieplot->SetGuideLinesAdjust(1.5);*/ /*$pieplot->SetGuideLines(); $pieplot->SetGuideLinesAdjust(3.0);*/ // Use absolute values (type==1) $pieplot->SetLabelType(PIE_VALUE_ABS); $pieplot->SetLabelPos(0.7); //设置数值跟圆的位置 // Label format $pieplot->value->SetFormat("%dMb"); //取代百分比显示具体数值 $pieplot->value->Show(); $pieplot1 = new PiePlot($data1); $pieplot1->SetLegends(array()); $graph->legend->SetFont(FF_SIMSUN,FS_BOLD); $pieplot1->SetSize(0.3); $pieplot1->SetCenter(0.5,0.5); // Use absolute values (type==1) $pieplot1->SetLabelType(PIE_VALUE_ABS); // 显示百分比 $pieplot1->SetLabelType(PIE_VALUE_PER); $pieplot1->value->Show(); $pieplot1->value->SetFont(FF_ARIAL,FS_NORMAL,9); $pieplot1->value->SetFormat('%2.2f%%'); $graph->Add($pieplot); $graph->Add($pieplot1); $graph->Stroke(); ?>
3D饼状图:
<?php include ("jpgraph/jpgraph.php"); include ("jpgraph/jpgraph_pie.php"); include ("jpgraph/jpgraph_pie3d.php"); $data = array(19,23,34,38,45,67,71,78,85,87,90,96); $graph = new PieGraph(550,400); $graph->SetShadow(); $graph->title->Set("CDN流量比例"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); $pieplot = new PiePlot3D($data); //创建PiePlot3D对象 $pieplot->SetCenter(0.5,0.4); //设置饼图中心的位置 $pieplot->SetLegends($gDateLocale->GetShortMonth()); //设置图例 //$pieplot->SetLabelType(PIE_VALUE_ABS); //$pieplot->SetLabelPos(0.7); //设置数值跟圆的位置 // Label format //$pieplot->value->SetFormat("%dMb"); //取代百分比显示具体数值 //$pieplot->value->Show(); $graph->Add($pieplot); $graph->Stroke(); ?>
浙公网安备 33010602011771号