Heim  >  Artikel  >  Backend-Entwicklung  >  PHP生成图表pChart入门教程_PHP教程

PHP生成图表pChart入门教程_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:39:171569Durchsuche

pChart是一个开源的图表生成库,主要涉及3个Class:pChart.class, pData.class, pCache.class,可生成20多种简单或复杂的图表,支持PNG,JPG,GIF通用图片格式。数据源可以来自于Database,CSV,当然也可以手写。使用该程序PHP需要开启GD服务,先来看看pChart的工作流程:

pChart

主要分为三步:

  1. * 读取用于生成图表数据(数据库、文件)
  2. * 设计图表样式(圆角、底色等)
  3. * 制作标签、题目、图例并生成图表

 

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

2009-8-6-13.13.35

代码如下:

<span style="color: blue"><?php </span><span style="color: green">// Standard inclusions   
 </span><span style="color: blue">include</span><span style="color: purple">(</span><span style="color: olive">"pChart/pData.class"</span><span style="color: purple">)</span>;
 <span style="color: blue">include</span><span style="color: purple">(</span><span style="color: olive">"pChart/pChart.class"</span><span style="color: purple">)</span>;

 <span style="color: green">// Dataset definition 
 </span><span style="color: blue">$DataSet </span><span style="color: purple">= </span><span style="color: blue">new </span>pData;
 <span style="color: green">//图表数据
 </span><span style="color: blue">$DataSet</span><span style="color: purple">-></span>AddPoint<span style="color: purple">(</span><span style="color: blue">array</span><span style="color: purple">(</span><span style="color: red">1</span><span style="color: purple">,</span><span style="color: red">4</span><span style="color: purple">,-</span><span style="color: red">3</span><span style="color: purple">,</span><span style="color: red">2</span><span style="color: purple">,-</span><span style="color: red">3</span><span style="color: purple">,</span><span style="color: red">3</span><span style="color: purple">,</span><span style="color: red">2</span><span style="color: purple">,</span><span style="color: red">1</span><span style="color: purple">,</span><span style="color: red">0</span><span style="color: purple">,</span><span style="color: red">7</span><span style="color: purple">,</span><span style="color: red">4</span><span style="color: purple">),</span><span style="color: olive">"Serie1"</span><span style="color: purple">)</span>;
 <span style="color: blue">$DataSet</span><span style="color: purple">-></span>AddPoint<span style="color: purple">(</span><span style="color: blue">array</span><span style="color: purple">(</span><span style="color: red">3</span><span style="color: purple">,</span><span style="color: red">3</span><span style="color: purple">,-</span><span style="color: red">4</span><span style="color: purple">,</span><span style="color: red">1</span><span style="color: purple">,-</span><span style="color: red">2</span><span style="color: purple">,</span><span style="color: red">2</span><span style="color: purple">,</span><span style="color: red">1</span><span style="color: purple">,</span><span style="color: red">0</span><span style="color: purple">,-</span><span style="color: red">1</span><span style="color: purple">,</span><span style="color: red">6</span><span style="color: purple">,</span><span style="color: red">3</span><span style="color: purple">),</span><span style="color: olive">"Serie2"</span><span style="color: purple">)</span>;
 <span style="color: blue">$DataSet</span><span style="color: purple">-></span>AddPoint<span style="color: purple">(</span><span style="color: blue">array</span><span style="color: purple">(</span><span style="color: red">4</span><span style="color: purple">,</span><span style="color: red">1</span><span style="color: purple">,</span><span style="color: red">2</span><span style="color: purple">,-</span><span style="color: red">1</span><span style="color: purple">,-</span><span style="color: red">4</span><span style="color: purple">,-</span><span style="color: red">2</span><span style="color: purple">,</span><span style="color: red">3</span><span style="color: purple">,</span><span style="color: red">2</span><span style="color: purple">,</span><span style="color: red">1</span><span style="color: purple">,</span><span style="color: red">2</span><span style="color: purple">,</span><span style="color: red">2</span><span style="color: purple">),</span><span style="color: olive">"Serie3"</span><span style="color: purple">)</span>;
 <span style="color: blue">$DataSet</span><span style="color: purple">-></span>AddAllSeries<span style="color: purple">()</span>;
 <span style="color: blue">$DataSet</span><span style="color: purple">-></span>SetAbsciseLabelSerie<span style="color: purple">()</span>;
 <span style="color: green">//数据图例
 </span><span style="color: blue">$DataSet</span><span style="color: purple">-></span>SetSerieName<span style="color: purple">(</span><span style="color: olive">"Microsoft"</span><span style="color: purple">,</span><span style="color: olive">"Serie1"</span><span style="color: purple">)</span>;
 <span style="color: blue">$DataSet</span><span style="color: purple">-></span>SetSerieName<span style="color: purple">(</span><span style="color: olive">"IBM"</span><span style="color: purple">,</span><span style="color: olive">"Serie2"</span><span style="color: purple">)</span>;
 <span style="color: blue">$DataSet</span><span style="color: purple">-></span>SetSerieName<span style="color: purple">(</span><span style="color: olive">"Google"</span><span style="color: purple">,</span><span style="color: olive">"Serie3"</span><span style="color: purple">)</span>;

 <span style="color: green">// Initialise the graph
 </span><span style="color: blue">$Test </span><span style="color: purple">= </span><span style="color: blue">new </span>pChart<span style="color: purple">(</span><span style="color: red">700</span><span style="color: purple">,</span><span style="color: red">230</span><span style="color: purple">)</span>;
 <span style="color: green">//设置图表尺寸、样式
 </span><span style="color: blue">$Test</span><span style="color: purple">-></span>setFontProperties<span style="color: purple">(</span><span style="color: olive">"Fonts/tahoma.ttf"</span><span style="color: purple">,</span><span style="color: red">8</span><span style="color: purple">)</span>;
 <span style="color: blue">$Test</span><span style="color: purple">-></span>setGraphArea<span style="color: purple">(</span><span style="color: red">50</span><span style="color: purple">,</span><span style="color: red">30</span><span style="color: purple">,</span><span style="color: red">680</span><span style="color: purple">,</span><span style="color: red">200</span><span style="color: purple">)</span>;
 <span style="color: blue">$Test</span><span style="color: purple">-></span>drawFilledRoundedRectangle<span style="color: purple">(</span><span style="color: red">7</span><span style="color: purple">,</span><span style="color: red">7</span><span style="color: purple">,</span><span style="color: red">693
<p align="left"></p>
<div style="display:none;">
<span id="url" itemprop="url">http://www.bkjia.com/PHPjc/486322.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/486322.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">pChart是一个开源的图表生成库,主要涉及3个Class: pChart.class , pData.class , pCache.class ,可生成20多种简单或复杂的图表,支持PNG,JPG,GIF通用...</span>
</div>
<div class="art_confoot"></div>
</span></span>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn