最近要把MySQL的数据取出来然后展示,自然少不得这些东西.
引入了文件.
然后写数据.
设置图表大小.
设置图例:
我们从Example0分析.
<?php // content="text/plain; charset=utf-8"
require_once ('../jpgraph.php');
require_once ('../jpgraph_line.php');
// Some data
$ydata = array(11,3,8,12,5,1,9,13,5,7);
// Create the graph. These two calls are always required
$graph = new Graph(350,250);
$graph->SetScale('textlin');
// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');
// Add the plot to the graph
$graph->Add($lineplot);
// Display the graph
$graph->Stroke();
?>
引入了文件.
然后写数据.
$ydata = array(11,3,8,12,5,1,9,13,5,7);
设置图表大小.
$graph = new Graph(350,250);
刻度类型:
$graph->SetScale('textlin');
比如强制如此设置.
$graph->SetScale('textlin',0,200);
注入y轴数据,设置颜色.
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');
$slineplot=new LinePlot($sydata);
$slineplot->SetColor('red');
$graph->Add($lineplot);
$graph->Add($slineplot);
设置图例:
$lineplot->SetLegend('Label 1');
$slineplot->SetLegend('Label 2');
更多解释:
<?php
include ("../jpgraph.php"); //必须要引用的文件
include ("../jpgraph_line.php"); //包含曲线图文件
// y 轴数据,以数组形式赋值
$ydata = array(11,3,8,12,5,1,9,13,5,7);
// 创建 Graph 类,350 为宽度,250 长度,auto:表示生成的缓存文件名是该文件的文件名+扩展名(.jpg .png .gif ……)
$graph = new Graph(350,250,"auto");
// 设置刻度类型,x轴刻度可作为文本标注的直线刻度,y轴为直线刻度
$graph->SetScale("textlin");
// 创建坐标类,将y轴数据注入
$lineplot=new LinePlot($ydata);
// y 轴连线设定为蓝色
$lineplot->SetColor("blue");
// 坐标类注入图标类
$graph->Add($lineplot);
// 显示图
$graph->Stroke(); ?>
<?php
$ydata = array(11,3,8,12,5,1,9,13,5,7);
// x 轴数据,作为 x 轴标注
$xdata = array('2007-3-01','2007-3-03','2007-3-05','2007-3-07','2007-3-09','2007-3-01','2007-3-11','2007-3-13','2007-3-15');
$graph = new Graph(350,150,"auto");
$graph->SetScale("textlin");
// 设置图表大标题
$graph->title->Set(‘Example’);
$lineplot = new LinePlot($ydata);
$graph->Add($lineplot);
// 设置图表灰度四周边距,顺序为左右上下
$graph->img->SetMargin(60,20,20,50);
// 设置输出文件格式为 jpg,缺省为 png
$graph->img->SetImgFormat( "jpeg");
// y 轴坐标描点形状为菱形
$lineplot-> mark->SetType(MARK_DIAMOND );
// 加入 x 轴标注
$graph->xaxis->SetTickLabels($xdata);
// 定位 x 轴标注垂直位置应在最下方
$graph->xaxis->SetPos("min");
// 设置 x 轴标注文字为斜体,粗体,6号小字
$graph->xaxis->SetFont(FF_ARIAL,FS_BOLD,6);
// 设置 x 轴标注文字 45 度倾斜。注:前面 SetFont 必须为 FF_ARIAL
$graph->xaxis->SetLabelAngle(45);
// x 轴刻度间隔为 2
$graph->xaxis->SetTextLabelInterval(2);
// 标题和 y 轴标题字体为标准字体
$graph->title->SetFont(FF_FONT1,FS_BOLD,2);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
// y 轴坐标点连线为蓝色加深加粗
$lineplot->SetColor("blue:0.5");
$lineplot->SetWeight(2);
$graph->yaxis->SetColor("black");
$graph->yaxis->SetWeight(2);
// 阴影效果
$graph->SetShadow();
// 显示图形
$graph->Stroke(); ?>
require_once('include/jpgraph.php'); //Graph类
require_once('include/jpgraph_line.php'); //LinePlot 类
$aAxisType = 'intlin'; //第一个int是X轴类型第2个lin是Y轴类型
$yScaleMin = 0; //Y轴最小值, 如果不需要设置,可以为空,或者NULL,下同
$yScaleMax = 11; //Y轴最大值
$xScaleMin = 1; //X轴最大值
$xScaleMax = 24; //X轴最大值
$unitType = '月份';
$chartDataTmpArr = array('1' => '1', '2' => '0', '3' => '4', '4' => '5', '5' => '7', '6' => '8', '7' => '4', '8' => '6', '9' => '8', '10' => '7', '11' => '4', '12' => '5', '13' => '4', '14' => '6', '15' => '4', '16' => '2', '17' => '2', '18' => '2', '19' => '2', '20' => '7', '21' => '6', '22' => '1', '23' => '4', '24' => '4');
$chartDataTmpArr2 = array('1' => '2', '2' => '0', '3' => '2', '4' => '2', '5' => '8', '6' => '9', '7' => '5', '8' => '7', '9' => '9', '10' => '7', '11' => '4', '12' => '7', '13' => '2', '14' => '4', '15' => '5', '16' => '6', '17' => '0', '18' => '3', '19' => '6', '20' => '5', '21' => '8', '22' => '9', '23' => '4', '24' => '4');
$graph = new Graph($width = 780 , $height = 400); //创建新的Graph对象
$graph->SetScale($aAxisType, $yScaleMin, $yScaleMax, $xScaleMin, $xScaleMax); //设置刻度模式SetScale($aAxisType,$aYMin=1,$aYMax=1,$aXMin=1,$aXMax=1)
$graph->img->SetMargin(40, 20, 50, 50) ; //设置图表边界
$title = date("Y-m-d H:i:s")."统计图";
$graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置图表标题的字体类型
$graph->title->Set($wholeTitle);
$graph->title->SetMargin (20);
$graph->xaxis->title->SetFont(FF_SIMSUN);
$graph->xaxis->title->Set($unitType);
$graph->xaxis->title->SetMargin(14);
$graph->legend->SetLineWeight(2); //设置图例文字的字体
$lineplot = new LinePlot($chartDataTmpArr); //创建要画的一地条曲线的对象
$lineplot->SetWeight(2); //曲线的线宽
$lineplot->SetLegend("进入");//设置图例文字
$graph->legend->Pos( 0.025, 0.08, "right","center" ); //图例文字框的位置 0.025,0.08 是以右上角为基准的,0.025是距左右距离,0.08是上下距离。
$graph->legend->SetLayout('1'); //如果有两条曲线,他们的图例文字并排放置。
$graph->legend->SetFrameWeight(0); //图例文字外框边框border设置为0
$graph->legend->SetFillColor('#C8C8C8'); //图例文字框内的填充颜色
$graph->legend->SetShadow('#C8C8C8', 0); //设置图例文字框的阴影部分颜色和border
$lineplot->SetColor("orange"); //设置曲线颜色
$graph->Add($lineplot); //在统计图上绘制曲线
$lineplot2 = new LinePlot($chartDataTmpArr2); //创建一条新的曲线对象
$lineplot2->SetLegend("出去");
$lineplot2->SetWeight(2);
$lineplot2->SetColor("blue");
$graph->Add($lineplot2);
$filename = 'images/strock/'.time().'.png';
$graph->Stroke($filename);
转帖地址:http://www.lijingquan.net/php-jpgraph.html