Home > php教程 > php手册 > body text

PHP生成图表pChart入门教程

WBOY
Release: 2016-06-13 10:37:49
Original
1035 people have browsed it

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

主要分为三步:

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

 

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

代码如下:

<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
</span></span>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template