Heim > php教程 > php手册 > Hauptteil

PHP生成图表pChart入门教程

WBOY
Freigeben: 2016-06-13 10:37:49
Original
1036 Leute haben es durchsucht

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>
Nach dem Login kopieren
Quelle:php.cn
Erklärung dieser Website
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
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage