php报表使用

时间:2025-01-21 09:04:08

php报表的使用:

1、到官网(http://jpgraph.net/)下载,建议下载jpgraph-3.0.7.tar.gz版本

2、解压后有两个文件夹

docportal:使用手册

src:报表核心文件

3、配置核心文件(对于src文件夹内容的操作)

1)创建一个jpgraph文件夹

2)将和Examples同级目录下的其它内容放在jpgraph文件夹下

3)将jpgraph放在Examples里

4)查找效果图。Examples里的每个php文件对应一个报表,可以在jpgraph11-3.0.7\docportal\chunkhtml\images查看报表的效果图,效果图的文件名和对应php文件的文件名相同

(如果你打开Examples文件夹里的某一个文件,你会发现有这样一句代码:require_once ('jpgraph/jpgraph.php')。但你却在该文件夹里没有找到jpgraph文件夹。其实jpgraph文件夹里的内容都放在和Examples同一目录下。因此你只要在Examples里创建一个jpgraph文件夹,并把和Examples同级目录的其它内容放到该文件夹里即可。)

4、引用方式:

1)直接访问php文件,如报表的效果图文件名为example27.1.png,则直接访问example27.1.php即可

2)作为图片引用,如<img alt="报表" src="example27.1.php" />

5、中文乱码处理

当你的文件为utf8时,会出现中文乱码,这时可使用iconv()将中文字符串转成gb2312。

例如:

$graph->title->Set(iconv("utf-8", "gb2312//ignore", "处理情况统计"));
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);

具体详见:http://blog.****.net/ms_x0828/article/details/5555864

6、案例分析

1)柱形图1

代码:

 <?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php'); $datay1=array(13,8,19);
$datay2=array(3,0,0);//多增加的数据 // Create the graph.
$graph = new Graph(650,450);//画布大小
//$graph->SetScale('textlin');
$graph->SetScale('textlin',-10,25);//设置y轴范围为5-75
$graph->yaxis->scale->ticks->Set(5);//设置y轴刻度为10
//$graph->xaxis->scale->ticks->Set(5);
$graph->xaxis->title->Set("X轴");
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD,14);
$graph->yaxis->title->Set("Y轴");
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD,14); $graph->SetMarginColor('white');//设置边框背景颜色
$graph->SetMargin(40,40,10,10);//设置图在边框中的位置 // Setup title
$graph->title->Set('Acc bar with gradient呵呵');//设置标题,默认的标题不支持中文
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,14); //设置字体类型和大小。第一个参数决定是否能显示中文。参数值可参考jpgraph_ttf.inc.php文件 // Create the first bar
$bplot = new BarPlot($datay1);
$bplot->SetFillGradient('AntiqueWhite2','AntiqueWhite4:0.8',GRAD_VERT);//设置柱体颜色
//$bplot->SetFillgradient('orange','darkred',GRAD_VER); //设置柱体颜色
$bplot->SetColor('orange');//柱体边界的颜色 // Create the second bar
$bplot2 = new BarPlot($datay2);
$bplot2->SetFillGradient('olivedrab1','olivedrab4',GRAD_VERT);//柱体中增加部分的颜色
$bplot2->SetColor('red');//柱体中增加部分的边框颜色 // And join them in an accumulated bar
$accbplot = new AccBarPlot(array($bplot,$bplot2));
$graph->Add($accbplot); $graph->Stroke();
?>

accbarframeex01.php

php报表使用

2)柱形图2
代码:

 <?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php'); // Some data
$datay=array(7,19,11,4,20); // Create the graph and setup the basic parameters
$graph = new Graph(600,500,'auto');
$graph->img->SetMargin(40,30,40,50);
$graph->SetScale("textint");
$graph->SetFrame(true,'blue',1); //边框边界的颜色
$graph->SetColor('lightblue');//柱体图背景颜色
$graph->SetMarginColor('lightblue');//边框背景颜色 // Setup X-axis labels
$a = $gDateLocale->GetShortMonth();//x轴用月份显示
$graph->xaxis->SetTickLabels($a);
$graph->xaxis->SetFont(FF_FONT1);
$graph->xaxis->SetColor('darkblue','black');//x轴的颜色 // Setup "hidden" y-axis by given it the same color
// as the background (this could also be done by setting the weight
// to zero)
$graph->yaxis->SetColor('lightblue','darkblue');//y轴颜色
$graph->ygrid->SetColor('white');//y轴分割线的颜色 // Setup graph title ands fonts
$graph->title->Set('Using grace = 0测试');
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);//FF_SIMSUN显示中文
$graph->xaxis->SetTitle('Year 2002','center');
$graph->xaxis->SetTitleMargin(10);//x轴标题显示的位置
$graph->xaxis->title->SetFont(FF_FONT2,FS_BOLD); // Add some grace to the top so that the scale doesn't
// end exactly at the max value.
$graph->yaxis->scale->SetGrace(0);//修改y轴显示的最大的值 // Create a bar pot
$bplot = new BarPlot($datay);
$bplot->SetFillColor('darkblue');//柱体填充的颜色
$bplot->SetColor('darkblue');//柱体边框颜色
$bplot->SetWidth(0.5);//设置柱体的宽度,取值:0~1
$bplot->SetShadow('darkgray');//柱体阴影颜色 /*显示和设置柱体上数字的样式*/
$bplot->value->Show();
$bplot->value->SetFont(FF_ARIAL,FS_NORMAL,9);//柱体上数字的字体和大小
$bplot->value->SetFormat('$%d');//柱体上数字的格式
$bplot->value->SetColor('darkred');//柱体上数字的颜色
$bplot->value->SetAngle(45);//柱体上数字的倾斜度
$graph->Add($bplot); // Finally stroke the graph
$graph->Stroke();
?>

grace_ex0.php

php报表使用

3)折线图

代码:

<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php'); // Some (random) data
$ydata = array(11.1,3,8,12,5,1,9,13,5,7); // Size of the overall graph
$width=650;
$height=550; // Create the graph and set a scale.
// These two calls are always required
$graph = new Graph($width,$height);
$graph->SetScale('intlin');
$graph->SetShadow(); // Setup margin and titles
$graph->SetMargin(40,20,20,40);
$graph->title->Set('Calls per operator');//标题
$graph->subtitle->Set('(March 12, 2008)');//副标题
$graph->xaxis->title->Set('Operator x轴');
$graph->yaxis->title->Set('# of calls y轴'); $graph->yaxis->title->SetFont( FF_SIMSUN , FS_BOLD );
$graph->xaxis->title->SetFont( FF_SIMSUN , FS_BOLD ); $graph->yaxis->SetColor('blue'); // 折线
$lineplot=new LinePlot($ydata);
$lineplot->SetColor( 'blue' );
$lineplot->SetWeight( 2 ); // 折线图颜色
$lineplot->mark->SetType(MARK_UTRIANGLE); /*折线处的标记*/
$lineplot->mark->SetColor('blue');
$lineplot->mark->SetFillColor('red'); $lineplot->value->Show();//在折线处显示数据 // Add the plot to the graph
$graph->Add($lineplot); // Display the graph
$graph->Stroke();
?>

example3.3.php

php报表使用

4)多条折线图

<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php'); // $dateUtils = new DateScaleUtils();
// Some (random) data
$ydata = array(11,3,8,12,5,1,9,13,5,7);
$y2data = array(1,5,1,22,5,9,1,3,15,17);
//x轴显示日期
$xdata=array("2015-01-01","2015-01-02","2015-01-03","2015-01-04","2015-01-05","2015-01-06","2015-01-07","2015-01-08","2015-01-09","2015-01-10"); //报表长宽
$width=800;
$height=400; // Create the graph and set a scale.
// These two calls are always required
$graph = new Graph($width,$height);
$graph->SetScale('intlin');
// $graph->SetShadow();
$graph->SetFrame(false); // Setup margin and titles
// $graph->SetMargin(40,20,10,40);
$graph->img->SetMargin(60,140,70,80); //折线图在框中的位置
$graph->title->Set(iconv("utf-8","gb2312","报警情况统计表"));
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,20);
// $graph->subtitle->Set('(March 12, 2008)');
$graph->xaxis->title->Set(iconv("utf-8","gb2312","日期"));
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD,15);
$graph->yaxis->title->Set(iconv("utf-8","gb2312","每日消息数"));
$graph->yaxis->title->SetFont( FF_SIMSUN , FS_BOLD,15 );
// $graph->yaxis->SetColor('blue');//y轴颜色 //*****************x轴数据设置********************
//x轴显示日期
// $xdata=array("2015-01-01","2015-01-02","2015-01-03","2015-01-04","2015-01-05","2015-01-06","2015-01-07","2015-01-08","2015-01-09","2015-01-10");
// $xdata = $gDateLocale->GetShortMonth();//x轴用月份显示
$graph->xaxis->SetTickLabels($xdata);
// $graph->xaxis->SetLabelMargin(15);//$xdata数据离x轴的距离
$graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,10);
$graph->xaxis->SetLabelAngle(45); // x轴数据倾斜的角度
$graph->xaxis->SetTitleMargin(45);//x轴标题显示的位置 /********************************设置折线**************************************/
$lineplot=new LinePlot($ydata);
$lineplot2=new LinePlot($y2data); $lineplot->SetColor( 'blue' );
$lineplot2->SetColor( 'green' ); $lineplot->SetWeight( 2 ); // Two pixel wide
$lineplot2->SetWeight( 2 ); // Two pixel wide //设置图例
$lineplot->SetLegend("Task");
$lineplot2->SetLegend(iconv("utf-8","gb2312","其它"));
$graph->legend->SetFont(FF_SIMSUN,FS_NORMAL); //设置图例字体
// $graph->legend->SetFont(FF_FONT2,FS_NORMAL);
$graph->legend->SetLineSpacing(10); //图例中的间距
$graph->legend->Pos(0.01,0.1,"right","top");
$graph->legend->SetLineWeight(2);//图例这种线条宽度
$graph->legend->SetMarkAbsSize(20);//图例大小 //折线图标设置
// MARK_IMG_BEVEL,MARK_IMG_DIAMOND,MARK_IMG_STAR,MARK_IMAGE_SQUARE,MARK_IMAGE_LBALL,MARK_IMAGE_MBALL,MARK_IMG_LPUSHPIN,MARK_IMG_PUSHPIN,MARK_IMG_SPUSHPIN
$lineplot->mark->SetType(MARK_IMG_STAR,'yellow',0.9);
$lineplot2->mark->SetType(MARK_IMG_DIAMOND,'red',0.5); // Add the plot to the graph
$graph->Add($lineplot);
$graph->Add($lineplot2); // Display the graph
$graph->Stroke();
?>

  php报表使用

5)饼状图

<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_pie.php');
require_once ('jpgraph/jpgraph_pie3d.php'); $data = array(,,); $graph = new PieGraph(,);
// $graph->SetShadow(); //设置饼状图阴影
$graph->SetFrame(false);//设置饼状图边框 $graph->title->Set(iconv("utf-8","gb2312","处理情况统计表"));
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,); $p1 = new PiePlot3D($data);
$p1->SetAngle(); //饼状图的倾斜程度
$p1->SetSize(0.5); //饼状图的大小
$p1->SetCenter(0.5); //饼状图的位置 $legends = array(iconv("utf-8","gb2312","未接收"),iconv("utf-8","gb2312","已接收未解决"),iconv("utf-8","gb2312","已解决")); //图例
$graph->legend->SetFont(FF_SIMSUN,FS_NORMAL); //设置图例字体
$p1->SetLegends($legends);
// $p1->SetLabelPos(0.5); //饼状图中数据的位置
$graph->Add($p1);
$graph->Stroke();

php报表使用