pChart是一个开源的图表生成库,主要涉及3个Class:pChart.class, pData.class, pCache.class,可生成20多种简单或复杂的图表,支持PNG,JPG,GIF通用图片格式。数据源可以来自于Database,CSV,当然也可以手写。使用该程序PHP需要开启GD服务,先来看看pChar
pChart是一个开源的图表生成库,主要涉及3个Class:pChart.class, pData.class, pCache.class,可生成20多种简单或复杂的图表,支持PNG,JPG,GIF通用图片格式。数据源可以来自于Database,CSV,当然也可以手写。使用该程序PHP需要开启GD服务,先来看看pChart的工作流程:
主要分为三步:
下面看一个简单的柱状图表:
代码如下:
<span><?php </span><span>// Standard inclusions </span><span>include</span><span>(</span><span>"pChart/pData.class"</span><span>)</span>; <span>include</span><span>(</span><span>"pChart/pChart.class"</span><span>)</span>; <span>// Dataset definition </span><span>$DataSet </span><span>= </span><span>new </span>pData; <span>//图表数据 </span><span>$DataSet</span><span>-></span>AddPoint<span>(</span><span>array</span><span>(</span><span>1</span><span>,</span><span>4</span><span>,-</span><span>3</span><span>,</span><span>2</span><span>,-</span><span>3</span><span>,</span><span>3</span><span>,</span><span>2</span><span>,</span><span>1</span><span>,</span><span>0</span><span>,</span><span>7</span><span>,</span><span>4</span><span>),</span><span>"Serie1"</span><span>)</span>; <span>$DataSet</span><span>-></span>AddPoint<span>(</span><span>array</span><span>(</span><span>3</span><span>,</span><span>3</span><span>,-</span><span>4</span><span>,</span><span>1</span><span>,-</span><span>2</span><span>,</span><span>2</span><span>,</span><span>1</span><span>,</span><span>0</span><span>,-</span><span>1</span><span>,</span><span>6</span><span>,</span><span>3</span><span>),</span><span>"Serie2"</span><span>)</span>; <span>$DataSet</span><span>-></span>AddPoint<span>(</span><span>array</span><span>(</span><span>4</span><span>,</span><span>1</span><span>,</span><span>2</span><span>,-</span><span>1</span><span>,-</span><span>4</span><span>,-</span><span>2</span><span>,</span><span>3</span><span>,</span><span>2</span><span>,</span><span>1</span><span>,</span><span>2</span><span>,</span><span>2</span><span>),</span><span>"Serie3"</span><span>)</span>; <span>$DataSet</span><span>-></span>AddAllSeries<span>()</span>; <span>$DataSet</span><span>-></span>SetAbsciseLabelSerie<span>()</span>; <span>//数据图例 </span><span>$DataSet</span><span>-></span>SetSerieName<span>(</span><span>"Microsoft"</span><span>,</span><span>"Serie1"</span><span>)</span>; <span>$DataSet</span><span>-></span>SetSerieName<span>(</span><span>"IBM"</span><span>,</span><span>"Serie2"</span><span>)</span>; <span>$DataSet</span><span>-></span>SetSerieName<span>(</span><span>"Google"</span><span>,</span><span>"Serie3"</span><span>)</span>; <span>// Initialise the graph </span><span>$Test </span><span>= </span><span>new </span>pChart<span>(</span><span>700</span><span>,</span><span>230</span><span>)</span>; <span>//设置图表尺寸、样式 </span><span>$Test</span><span>-></span>setFontProperties<span>(</span><span>"Fonts/tahoma.ttf"</span><span>,</span><span>8</span><span>)</span>; <span>$Test</span><span>-></span>setGraphArea<span>(</span><span>50</span><span>,</span><span>30</span><span>,</span><span>680</span><span>,</span><span>200</span><span>)</span>; <span>$Test</span><span>-></span>drawFilledRoundedRectangle<span>(</span><span>7</span><span>,</span><span>7</span><span>,</span><span>693</span><span>,</span><span>223</span><span>,</span><span>5</span><span>,</span><span>240</span><span>,</span><span>240</span><span>,</span><span>240</span><span>)</span>; <span>$Test</span><span>-></span>drawRoundedRectangle<span>(</span><span>5</span><span>,</span><span>5</span><span>,</span><span>695</span><span>,</span><span>225</span><span>,</span><span>5</span><span>,</span><span>230</span><span>,</span><span>230</span><span>,</span><span>230</span><span>)</span>; <span>$Test</span><span>-></span>drawGraphArea<span>(</span><span>255</span><span>,</span><span>255</span><span>,</span><span>255</span><span>,</span><span>TRUE</span><span>)</span>; <span>$Test</span><span>-></span>drawScale<span>(</span><span>$DataSet</span><span>-></span>GetData<span>(),</span><span>$DataSet</span><span>-></span>GetDataDescription<span>(),</span>SCALE_NORMAL<span>,</span><span>150</span><span>,</span><span>150</span><span>,</span><span>150</span><span>,</span><span>TRUE</span><span>,</span><span>0</span><span>,</span><span>2</span><span>,</span><span>TRUE</span><span>)</span>; <span>$Test</span><span>-></span>drawGrid<span>(</span><span>4</span><span>,</span><span>TRUE</span><span>,</span><span>230</span><span>,</span><span>230</span><span>,</span><span>230</span><span>,</span><span>50</span><span>)</span>; <span>// Draw the 0 line </span><span>$Test</span><span>-></span>setFontProperties<span>(</span><span>"Fonts/MankSans.ttf"</span><span>,</span><span>6</span><span>)</span>; <span>$Test</span><span>-></span>drawTreshold<span>(</span><span>0</span><span>,</span><span>143</span><span>,</span><span>55</span><span>,</span><span>72</span><span>,</span><span>TRUE</span><span>,</span><span>TRUE</span><span>)</span>; <span>// Draw the bar graph //柱状图要使用drawBarGraph() </span><span>$Test</span><span>-></span>drawBarGraph<span>(</span><span>$DataSet</span><span>-></span>GetData<span>(),</span><span>$DataSet</span><span>-></span>GetDataDescription<span>(),</span><span>TRUE</span><span>,</span><span>80</span><span>)</span>; <span>// Finish the graph //制作图例、标题、字体等属性 </span><span>$Test</span><span>-></span>setFontProperties<span>(</span><span>"Fonts/MankSans.ttf"</span><span>,</span><span>10</span><span>)</span>; <span>$Test</span><span>-></span>drawLegend<span>(</span><span>596</span><span>,</span><span>150</span><span>,</span><span>$DataSet</span><span>-></span>GetDataDescription<span>(),</span><span>255</span><span>,</span><span>255</span><span>,</span><span>255</span><span>)</span>; <span>$Test</span><span>-></span>setFontProperties<span>(</span><span>"Fonts/MankSans.ttf"</span><span>,</span><span>10</span><span>)</span>; <span>$Test</span><span>-></span>drawTitle<span>(</span><span>50</span><span>,</span><span>22</span><span>,</span><span>"Example"</span><span>,</span><span>50</span><span>,</span><span>50</span><span>,</span><span>50</span><span>,</span><span>585</span><span>)</span>; <span>//生成图表 </span><span>$imageFile </span><span>= </span><span>"example12.png"</span>; <span>$Test</span><span>-></span>Render<span>(</span><span>$imageFile</span><span>)</span>; <span>echo </span><span>'<img src="'</span><span>.</span><span>%24imageFile</span><span>.</span><span>'" alt="PHP生成图表pChart" >'</span>; <span>?></span></span>
这个是雷达效果的:
代码:
<span><?php </span><span>// Standard inclusions </span><span>include</span><span>(</span><span>"pChart/pData.class"</span><span>)</span>; <span>include</span><span>(</span><span>"pChart/pChart.class"</span><span>)</span>; <span>// Dataset definition </span><span>$DataSet </span><span>= </span><span>new </span>pData; <span>$DataSet</span><span>-></span>AddPoint<span>(</span><span>array</span><span>(</span><span>"Memory"</span><span>,</span><span>"Disk"</span><span>,</span><span>"Network"</span><span>,</span><span>"Slots"</span><span>,</span><span>"CPU"</span><span>),</span><span>"Label"</span><span>)</span>; <span>$DataSet</span><span>-></span>AddPoint<span>(</span><span>array</span><span>(</span><span>6</span><span>,</span><span>4</span><span>,</span><span>7</span><span>,</span><span>4</span><span>,</span><span>5</span><span>),</span><span>"Serie1"</span><span>)</span>; <span>$DataSet</span><span>-></span>AddPoint<span>(</span><span>array</span><span>(</span><span>2</span><span>,</span><span>3</span><span>,</span><span>5</span><span>,</span><span>2</span><span>,</span><span>4</span><span>),</span><span>"Serie2"</span><span>)</span>; <span>$DataSet</span><span>-></span>AddSerie<span>(</span><span>"Serie1"</span><span>)</span>; <span>$DataSet</span><span>-></span>AddSerie<span>(</span><span>"Serie2"</span><span>)</span>; <span>$DataSet</span><span>-></span>SetAbsciseLabelSerie<span>(</span><span>"Label"</span><span>)</span>; <span>$DataSet</span><span>-></span>SetSerieName<span>(</span><span>"Reference"</span><span>,</span><span>"Serie1"</span><span>)</span>; <span>$DataSet</span><span>-></span>SetSerieName<span>(</span><span>"Tested computer"</span><span>,</span><span>"Serie2"</span><span>)</span>; <span>// Initialise the graph </span><span>$Test </span><span>= </span><span>new </span>pChart<span>(</span><span>400</span><span>,</span><span>400</span><span>)</span>; <span>$Test</span><span>-></span>setFontProperties<span>(</span><span>"Fonts/tahoma.ttf"</span><span>,</span><span>8</span><span>)</span>; <span>$Test</span><span>-></span>drawFilledRoundedRectangle<span>(</span><span>7</span><span>,</span><span>7</span><span>,</span><span>393</span><span>,</span><span>393</span><span>,</span><span>5</span><span>,</span><span>240</span><span>,</span><span>240</span><span>,</span><span>240</span><span>)</span>; <span>$Test</span><span>-></span>drawRoundedRectangle<span>(</span><span>5</span><span>,</span><span>5</span><span>,</span><span>395</span><span>,</span><span>395</span><span>,</span><span>5</span><span>,</span><span>230</span><span>,</span><span>230</span><span>,</span><span>230</span><span>)</span>; <span>$Test</span><span>-></span>setGraphArea<span>(</span><span>30</span><span>,</span><span>30</span><span>,</span><span>370</span><span>,</span><span>370</span><span>)</span>; <span>$Test</span><span>-></span>drawFilledRoundedRectangle<span>(</span><span>30</span><span>,</span><span>30</span><span>,</span><span>370</span><span>,</span><span>370</span><span>,</span><span>5</span><span>,</span><span>255</span><span>,</span><span>255</span><span>,</span><span>255</span><span>)</span>; <span>$Test</span><span>-></span>drawRoundedRectangle<span>(</span><span>30</span><span>,</span><span>30</span><span>,</span><span>370</span><span>,</span><span>370</span><span>,</span><span>5</span><span>,</span><span>220</span><span>,</span><span>220</span><span>,</span><span>220</span><span>)</span>; <span>// Draw the radar graph //要使用drawRadarAxis()生成雷达效果 </span><span>$Test</span><span>-></span>drawRadarAxis<span>(</span><span>$DataSet</span><span>-></span>GetData<span>(),</span><span>$DataSet</span><span>-></span>GetDataDescription<span>(),</span><span>TRUE</span><span>,</span><span>20</span><span>,</span><span>120</span><span>,</span><span>120</span><span>,</span><span>120</span><span>,</span><span>230</span><span>,</span><span>230</span><span>,</span><span>230</span><span>)</span>; <span>$Test</span><span>-></span>drawFilledRadar<span>(</span><span>$DataSet</span><span>-></span>GetData<span>(),</span><span>$DataSet</span><span>-></span>GetDataDescription<span>(),</span><span>50</span><span>,</span><span>20</span><span>)</span>; <span>// Finish the graph </span><span>$Test</span><span>-></span>drawLegend<span>(</span><span>15</span><span>,</span><span>15</span><span>,</span><span>$DataSet</span><span>-></span>GetDataDescription<span>(),</span><span>255</span><span>,</span><span>255</span><span>,</span><span>255</span><span>)</span>; <span>$Test</span><span>-></span>setFontProperties<span>(</span><span>"Fonts/tahoma.ttf"</span><span>,</span><span>10</span><span>)</span>; <span>$Test</span><span>-></span>drawTitle<span>(</span><span>0</span><span>,</span><span>22</span><span>,</span><span>"Example"</span><span>,</span><span>50</span><span>,</span><span>50</span><span>,</span><span>50</span><span>,</span><span>400</span><span>)</span>; <span>$imageFile </span><span>= </span><span>"example8.png"</span>; <span>$Test</span><span>-></span>Render<span>(</span><span>$imageFile</span><span>)</span>; <span>echo </span><span>'<img src="'</span><span>.</span><span>%24imageFile</span><span>.</span><span>'" alt="PHP生成图表pChart" >'</span>; <span>?></span></span>
再看几个其他的效果
1,饼图:
2, 双座标曲线图:
3, 层叠柱状图:
4, 多图表:
图表的种类已经相当丰富了,具体图表设置请参考
http://pchart.sourceforge.net/documentation.php?topic=pChart
::源代码下载::