首页 > 代码库 > php报表使用

php报表使用

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)柱形图1

代码:

 1 <?php // content="text/plain; charset=utf-8" 2 require_once (‘jpgraph/jpgraph.php‘); 3 require_once (‘jpgraph/jpgraph_bar.php‘); 4  5  6 $datay1=array(13,8,19); 7 $datay2=array(3,0,0);//多增加的数据 8  9 // Create the graph.10 $graph = new Graph(650,450);//画布大小11 //$graph->SetScale(‘textlin‘);12 $graph->SetScale(‘textlin‘,-10,25);//设置y轴范围为5-7513 $graph->yaxis->scale->ticks->Set(5);//设置y轴刻度为1014 //$graph->xaxis->scale->ticks->Set(5);15 $graph->xaxis->title->Set("X轴");16 $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD,14); 17 $graph->yaxis->title->Set("Y轴");18 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD,14); 19 20 $graph->SetMarginColor(‘white‘);//设置边框背景颜色21 $graph->SetMargin(40,40,10,10);//设置图在边框中的位置22 23 // Setup title24 $graph->title->Set(‘Acc bar with gradient呵呵‘);//设置标题,默认的标题不支持中文25 $graph->title->SetFont(FF_SIMSUN,FS_BOLD,14);   //设置字体类型和大小。第一个参数决定是否能显示中文。参数值可参考jpgraph_ttf.inc.php文件26 27 // Create the first bar28 $bplot = new BarPlot($datay1);29 $bplot->SetFillGradient(‘AntiqueWhite2‘,‘AntiqueWhite4:0.8‘,GRAD_VERT);//设置柱体颜色30 //$bplot->SetFillgradient(‘orange‘,‘darkred‘,GRAD_VER); //设置柱体颜色31 $bplot->SetColor(‘orange‘);//柱体边界的颜色32 33 // Create the second bar34 $bplot2 = new BarPlot($datay2);35 $bplot2->SetFillGradient(‘olivedrab1‘,‘olivedrab4‘,GRAD_VERT);//柱体中增加部分的颜色36 $bplot2->SetColor(‘red‘);//柱体中增加部分的边框颜色37 38 39 // And join them in an accumulated bar40 $accbplot = new AccBarPlot(array($bplot,$bplot2));41 $graph->Add($accbplot);42 43 $graph->Stroke();44 ?>
accbarframeex01.php

 

 

2)柱形图2
代码:

 1 <?php // content="text/plain; charset=utf-8" 2 require_once (‘jpgraph/jpgraph.php‘); 3 require_once (‘jpgraph/jpgraph_bar.php‘); 4  5 // Some data 6 $datay=array(7,19,11,4,20); 7  8 // Create the graph and setup the basic parameters  9 $graph = new Graph(600,500,‘auto‘);    10 $graph->img->SetMargin(40,30,40,50);11 $graph->SetScale("textint");12 $graph->SetFrame(true,‘blue‘,1); //边框边界的颜色13 $graph->SetColor(‘lightblue‘);//柱体图背景颜色14 $graph->SetMarginColor(‘lightblue‘);//边框背景颜色15 16 // Setup X-axis labels17 $a = $gDateLocale->GetShortMonth();//x轴用月份显示18 $graph->xaxis->SetTickLabels($a);19 $graph->xaxis->SetFont(FF_FONT1);20 $graph->xaxis->SetColor(‘darkblue‘,‘black‘);//x轴的颜色21 22 // Setup "hidden" y-axis by given it the same color23 // as the background (this could also be done by setting the weight24 // to zero)25 $graph->yaxis->SetColor(‘lightblue‘,‘darkblue‘);//y轴颜色26 $graph->ygrid->SetColor(‘white‘);//y轴分割线的颜色27 28 // Setup graph title ands fonts29 $graph->title->Set(‘Using grace = 0测试‘);30 $graph->title->SetFont(FF_SIMSUN,FS_BOLD);//FF_SIMSUN显示中文31 $graph->xaxis->SetTitle(‘Year 2002‘,‘center‘);32 $graph->xaxis->SetTitleMargin(10);//x轴标题显示的位置33 $graph->xaxis->title->SetFont(FF_FONT2,FS_BOLD);34 35 // Add some grace to the top so that the scale doesn‘t36 // end exactly at the max value. 37 $graph->yaxis->scale->SetGrace(0);//修改y轴显示的最大的值38 39                               40 // Create a bar pot41 $bplot = new BarPlot($datay);42 $bplot->SetFillColor(‘darkblue‘);//柱体填充的颜色43 $bplot->SetColor(‘darkblue‘);//柱体边框颜色44 $bplot->SetWidth(0.5);//设置柱体的宽度,取值:0~145 $bplot->SetShadow(‘darkgray‘);//柱体阴影颜色46 47 48 /*显示和设置柱体上数字的样式*/49 $bplot->value->Show();50 $bplot->value->SetFont(FF_ARIAL,FS_NORMAL,9);//柱体上数字的字体和大小51 $bplot->value->SetFormat(‘$%d‘);//柱体上数字的格式52 $bplot->value->SetColor(‘darkred‘);//柱体上数字的颜色53 $bplot->value->SetAngle(45);//柱体上数字的倾斜度54 $graph->Add($bplot);55 56 // Finally stroke the graph57 $graph->Stroke();58 ?>
grace_ex0.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报表使用