JPGRAPH是PHP世界里一款非常方便的做各种图表的工作,柱状图、饼图、雷达图等,基本上对所有的图表无所不能。
在PEAR中,也有一款做图表的工具,叫image_graph,这个工具功能也是非常强大,但jpgraph和image_graph比起来的最大好处,就是有很多的例子和非常详细的文档说明,这就使jpgraph用起来是相当的方便。
最后决定选用jpgraph。
下面是一段代码。同时在用的过程中遇到了汉字乱码的问题,原因已经找到,也和大家说明一下。
如果您在使用jpgraph的过程遇到了什么问题,可以MAIL给我,大家一直探讨。同时,如果你有什么使用心得,也希望您能够发到LAMPER上,让更多的人分享你的经验。
- <?php
- include ("../jpgraph.php");
- include ("../jpgraph_line.php");
-
- $ydata = array(11,3,8,12,5,1,9,13,5,7);
- $y2data = array(354,200,265,99,111,91,198,225,293,251);
- $datax = array("2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01");
-
- // Create the graph. These two calls are always required
- $graph = new Graph(800,400,"auto");
- $graph->img->SetMargin(40,40,20,40);
-
- $graph->title->SetFont(FF_SIMSUN,FS_BOLD);
- $title = "展示pv/展示ip";
- $graph->title->Set($title);
-
- $graph->SetScale("textlin");
- $graph->SetY2Scale("lin");
-
- $graph->xaxis->title->Set("时间");
- $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
- $graph->xaxis->SetTickLabels($datax);
-
- $graph->yaxis->title->Set("展示pv");
- $graph->yaxis->SetColor("blue");
- $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
-
- $graph->y2axis->title->Set("展示IP");
- $graph->y2axis->SetColor("orange");
- $graph->y2axis->title->SetFont(FF_SIMSUN,FS_BOLD);
-
- $lineplot=new LinePlot($ydata);
- $lineplot->SetColor("blue");
- $lineplot->SetWeight(2);
- $lineplot->SetLegend("展示pv");
- $graph->legend->SetFont(FF_SIMSUN,FS_BOLD);
-
- $lineplot2=new LinePlot($y2data);
- $lineplot2->SetColor("orange");
- $lineplot2->SetWeight(2);
- $lineplot2->SetLegend("展示ip");
-
-
- $graph->Add($lineplot);
- $graph->AddY2($lineplot2);
-
- // Display the graph
- $graph->Stroke();
- ?>
注意SetFont方法,如果你的文件编码是gb2312,SetFont方法的第一个参数为FF_SIMSUN即可正常显示汉字
如果你的文件编码为utf-8,还需加一句$title = iconv("UTF-8 |