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

WBOY
풀어 주다: 2016-07-13 17:39:17
원래의
1567명이 탐색했습니다.

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