PHP生成图表pChart
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的工作流程:
主要分为三步:
- * 读取用于生成图表数据(数据库、文件)
- * 设计图表样式(圆角、底色等)
下面看一个简单的柱状图表:
代码如下:
<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="/static/imghw/default1.png" data-src="'</span><span>.</span><span>$imageFile</span><span>.</span><span>'" class="lazy" alt="PHP生成图表pChart" ><span>.</span><span>$imageFile</span><span>.</span><span>'">'</span>; <span>?></span></span></span>
这个是雷达效果的:
代码:
<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="/static/imghw/default1.png" data-src="'</span><span>.</span><span>$imageFile</span><span>.</span><span>'" class="lazy" alt="PHP生成图表pChart" ><span>.</span><span>$imageFile</span><span>.</span><span>'">'</span>; <span>?></span></span></span>
再看几个其他的效果
1,饼图:
2, 双座标曲线图:
3, 层叠柱状图:
4, 多图表:
图表的种类已经相当丰富了,具体图表设置请参考
http://pchart.sourceforge.net/documentation.php?topic=pChart
::源代码下载::

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

CakePHP でデータベースを操作するのは非常に簡単です。この章では、CRUD (作成、読み取り、更新、削除) 操作について理解します。
