> php教程 > php手册 > PHP生成图表pChart

PHP生成图表pChart

WBOY
풀어 주다: 2016-06-06 19:46:43
원래의
3726명이 탐색했습니다.

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的工作流程:

PHP生成图表pChart

主要分为三步:

  1. * 读取用于生成图表数据(数据库、文件)
  2. * 设计图表样式(圆角、底色等)

 

下面看一个简单的柱状图表:

PHP生成图表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>
로그인 후 복사

这个是雷达效果的:

PHP生成图表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>$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,饼图:

PHP生成图表pChart

2, 双座标曲线图:

PHP生成图表pChart

3, 层叠柱状图:

PHP生成图表pChart

4, 多图表:

PHP生成图表pChart

图表的种类已经相当丰富了,具体图表设置请参考
http://pchart.sourceforge.net/documentation.php?topic=pChart
::源代码下载::

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿