Home » 经验&资源分享 » 在ThinkPHP中封装pChart

在ThinkPHP中封装pChart

最近在使用ThinkPHP框架做项目,项目中需要生成一些图表,由于本人只对pChart的使用比较熟悉些,所以便想将pChart封装在ThinkPHP中使用。

在网上查到了一些方法,但是个人感觉用起来都不是很方便,在这说说我自己的使用方法。

首先下载pChart类和需要用到的字体文件,我这里有一个这些东西的一个打包,下载来就可以用了。

下载地址:http://pan.baidu.com/s/1zNS4U

然后将下载的文件解压出来,放到 项目目录\ThinkPHP\Extend\Vendor 文件夹下,如图

pChart

在在项目的Action文件夹中新建一个pageAciton.class.php,输入如下代码

<span id="lnum1" style="color: #606060;"> 1:</span> &lt;?php

<span id="lnum2" style="color: #606060;"> 2:</span>     <span style="color: #0000ff;">class</span> pageAction <span style="color: #0000ff;">extends</span> Action{

<span id="lnum3" style="color: #606060;"> 3:</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> index() {

<span id="lnum4" style="color: #606060;"> 4:</span>              $date1=<span style="color: #006080;">"2013-08-06"</span>;

<span id="lnum5" style="color: #606060;"> 5:</span>              $in1=<span style="color: #0000ff;">array</span>(1,0);

<span id="lnum6" style="color: #606060;"> 6:</span>              $out1=<span style="color: #0000ff;">array</span>(2,0);

<span id="lnum7" style="color: #606060;"> 7:</span>              $gates_name=<span style="color: #0000ff;">array</span>(<span style="color: #006080;">'支点咖啡一号门'</span>, <span style="color: #006080;">'支点咖啡二号门'</span>);

<span id="lnum8" style="color: #606060;"> 8:</span>              $chart=R(<span style="color: #006080;">'Chart/index'</span>,<span style="color: #0000ff;">array</span>($date1,$in1,$out1,$gates_name));

<span id="lnum9" style="color: #606060;"> 9:</span>              $gates_name-&gt;display();

<span id="lnum10" style="color: #606060;"> 10:</span>         }

<span id="lnum11" style="color: #606060;"> 11:</span>         <span style="color: #008000;">// 处理表单数据的方法</span>

<span id="lnum12" style="color: #606060;"> 12:</span>

<span id="lnum13" style="color: #606060;"> 13:</span>     }<span style="color: #008000;">//类定义 end</span>

<span id="lnum14" style="color: #606060;"> 14:</span> ?&gt;

然后新建chartAction.class.php,输入如下代码

<span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">class</span> chartAction <span style="color: #0000ff;">extends</span> Action{

<span id="lnum2" style="color: #606060;"> 2:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> index($date1,$in1,$out1,$gates_name){

<span id="lnum3" style="color: #606060;"> 3:</span>         <span style="color: #008000;">/* Create and populate the pData object */</span>

<span id="lnum4" style="color: #606060;"> 4:</span>         $MyData = <span style="color: #0000ff;">new</span> pData();

<span id="lnum5" style="color: #606060;"> 5:</span>         $MyData-&gt;addPoints($in1, <span style="color: #006080;">"进门数"</span>);

<span id="lnum6" style="color: #606060;"> 6:</span>         $MyData-&gt;addPoints($out1, <span style="color: #006080;">"出门数"</span>);

<span id="lnum7" style="color: #606060;"> 7:</span>         $MyData-&gt;setAxisName(0, <span style="color: #006080;">"人数"</span>);

<span id="lnum8" style="color: #606060;"> 8:</span>         $MyData-&gt;addPoints($gates_name , <span style="color: #006080;">"Labels"</span>);

<span id="lnum9" style="color: #606060;"> 9:</span>         $MyData-&gt;setSerieDescription(<span style="color: #006080;">"Labels"</span>, <span style="color: #006080;">"Months"</span>);

<span id="lnum10" style="color: #606060;"> 10:</span>         $MyData-&gt;setAbscissa(<span style="color: #006080;">"Labels"</span>);

<span id="lnum11" style="color: #606060;"> 11:</span>         $myPicture = <span style="color: #0000ff;">new</span> pImage(900, 430, $MyData);

<span id="lnum12" style="color: #606060;"> 12:</span>

<span id="lnum13" style="color: #606060;"> 13:</span>         $myPicture-&gt;drawGradientArea(0, 0, 900, 430, DIRECTION_VERTICAL, <span style="color: #0000ff;">array</span>(

<span id="lnum14" style="color: #606060;"> 14:</span>             <span style="color: #006080;">"StartR"</span> =&gt; 240,

<span id="lnum15" style="color: #606060;"> 15:</span>             <span style="color: #006080;">"StartG"</span> =&gt; 240,

<span id="lnum16" style="color: #606060;"> 16:</span>             <span style="color: #006080;">"StartB"</span> =&gt; 240,

<span id="lnum17" style="color: #606060;"> 17:</span>             <span style="color: #006080;">"EndR"</span> =&gt; 180,

<span id="lnum18" style="color: #606060;"> 18:</span>             <span style="color: #006080;">"EndG"</span> =&gt; 180,

<span id="lnum19" style="color: #606060;"> 19:</span>             <span style="color: #006080;">"EndB"</span> =&gt; 180,

<span id="lnum20" style="color: #606060;"> 20:</span>             <span style="color: #006080;">"Alpha"</span> =&gt; 100

<span id="lnum21" style="color: #606060;"> 21:</span>         ));

<span id="lnum22" style="color: #606060;"> 22:</span>         $myPicture-&gt;drawGradientArea(0, 0, 900, 430, DIRECTION_HORIZONTAL, <span style="color: #0000ff;">array</span>(

<span id="lnum23" style="color: #606060;"> 23:</span>             <span style="color: #006080;">"StartR"</span> =&gt; 240,

<span id="lnum24" style="color: #606060;"> 24:</span>             <span style="color: #006080;">"StartG"</span> =&gt; 240,

<span id="lnum25" style="color: #606060;"> 25:</span>             <span style="color: #006080;">"StartB"</span> =&gt; 240,

<span id="lnum26" style="color: #606060;"> 26:</span>             <span style="color: #006080;">"EndR"</span> =&gt; 180,

<span id="lnum27" style="color: #606060;"> 27:</span>             <span style="color: #006080;">"EndG"</span> =&gt; 180,

<span id="lnum28" style="color: #606060;"> 28:</span>             <span style="color: #006080;">"EndB"</span> =&gt; 180,

<span id="lnum29" style="color: #606060;"> 29:</span>             <span style="color: #006080;">"Alpha"</span> =&gt; 20

<span id="lnum30" style="color: #606060;"> 30:</span>         ));

<span id="lnum31" style="color: #606060;"> 31:</span>         $myPicture-&gt;setFontProperties(<span style="color: #0000ff;">array</span>(

<span id="lnum32" style="color: #606060;"> 32:</span>             <span style="color: #006080;">"FontName"</span> =&gt; <span style="color: #006080;">"C:\\WINDOWS\\Fonts\\simkai.ttf"</span>,

<span id="lnum33" style="color: #606060;"> 33:</span>             <span style="color: #006080;">"FontSize"</span> =&gt; 15,

<span id="lnum34" style="color: #606060;"> 34:</span>             <span style="color: #006080;">"R"</span> =&gt; 0,

<span id="lnum35" style="color: #606060;"> 35:</span>             <span style="color: #006080;">"G"</span> =&gt; 0,

<span id="lnum36" style="color: #606060;"> 36:</span>             <span style="color: #006080;">"B"</span> =&gt; 0

<span id="lnum37" style="color: #606060;"> 37:</span>         ));

<span id="lnum38" style="color: #606060;"> 38:</span>         <span style="color: #008000;">/*?????? */</span>

<span id="lnum39" style="color: #606060;"> 39:</span>         $myPicture-&gt;setGraphArea(50, 30, 900, 400);

<span id="lnum40" style="color: #606060;"> 40:</span>         $myPicture-&gt;drawScale(<span style="color: #0000ff;">array</span>(

<span id="lnum41" style="color: #606060;"> 41:</span>             <span style="color: #006080;">"CycleBackground"</span> =&gt; TRUE,

<span id="lnum42" style="color: #606060;"> 42:</span>             <span style="color: #006080;">"DrawSubTicks"</span> =&gt; TRUE,

<span id="lnum43" style="color: #606060;"> 43:</span>             <span style="color: #006080;">"GridR"</span> =&gt; 0,

<span id="lnum44" style="color: #606060;"> 44:</span>             <span style="color: #006080;">"GridG"</span> =&gt; 0,

<span id="lnum45" style="color: #606060;"> 45:</span>             <span style="color: #006080;">"GridB"</span> =&gt; 0,

<span id="lnum46" style="color: #606060;"> 46:</span>             <span style="color: #006080;">"GridAlpha"</span> =&gt; 10

<span id="lnum47" style="color: #606060;"> 47:</span>         ));

<span id="lnum48" style="color: #606060;"> 48:</span>         <span style="color: #008000;">/* ???? */</span>

<span id="lnum49" style="color: #606060;"> 49:</span>         $myPicture-&gt;setShadow(TRUE, <span style="color: #0000ff;">array</span>(

<span id="lnum50" style="color: #606060;"> 50:</span>             <span style="color: #006080;">"X"</span> =&gt; 1,

<span id="lnum51" style="color: #606060;"> 51:</span>             <span style="color: #006080;">"Y"</span> =&gt; 1,

<span id="lnum52" style="color: #606060;"> 52:</span>             <span style="color: #006080;">"R"</span> =&gt; 0,

<span id="lnum53" style="color: #606060;"> 53:</span>             <span style="color: #006080;">"G"</span> =&gt; 0,

<span id="lnum54" style="color: #606060;"> 54:</span>             <span style="color: #006080;">"B"</span> =&gt; 0,

<span id="lnum55" style="color: #606060;"> 55:</span>             <span style="color: #006080;">"Alpha"</span> =&gt; 10

<span id="lnum56" style="color: #606060;"> 56:</span>         ));

<span id="lnum57" style="color: #606060;"> 57:</span>         <span style="color: #008000;">/* ??? */</span>

<span id="lnum58" style="color: #606060;"> 58:</span>         $settings = <span style="color: #0000ff;">array</span>(

<span id="lnum59" style="color: #606060;"> 59:</span>             <span style="color: #006080;">"Floating0Serie"</span> =&gt; <span style="color: #006080;">"Floating 0"</span>,

<span id="lnum60" style="color: #606060;"> 60:</span>             <span style="color: #006080;">"Draw0Line"</span> =&gt; TRUE,

<span id="lnum61" style="color: #606060;"> 61:</span>             <span style="color: #006080;">"Gradient"</span> =&gt; TRUE,

<span id="lnum62" style="color: #606060;"> 62:</span>             <span style="color: #006080;">"DisplayPos"</span> =&gt; LABEL_POS_INSIDE,

<span id="lnum63" style="color: #606060;"> 63:</span>             <span style="color: #006080;">"DisplayValues"</span> =&gt; TRUE,

<span id="lnum64" style="color: #606060;"> 64:</span>             <span style="color: #006080;">"DisplayR"</span> =&gt; 255,

<span id="lnum65" style="color: #606060;"> 65:</span>             <span style="color: #006080;">"DisplayG"</span> =&gt; 255,

<span id="lnum66" style="color: #606060;"> 66:</span>             <span style="color: #006080;">"DisplayB"</span> =&gt; 255,

<span id="lnum67" style="color: #606060;"> 67:</span>             <span style="color: #006080;">"DisplayShadow"</span> =&gt; TRUE,

<span id="lnum68" style="color: #606060;"> 68:</span>             <span style="color: #006080;">"Surrounding"</span> =&gt; 10

<span id="lnum69" style="color: #606060;"> 69:</span>         );

<span id="lnum70" style="color: #606060;"> 70:</span>         $myPicture-&gt;drawBarChart($settings);

<span id="lnum71" style="color: #606060;"> 71:</span>         <span style="color: #008000;">/* ???? */</span>

<span id="lnum72" style="color: #606060;"> 72:</span>         $myPicture-&gt;setFontProperties(<span style="color: #0000ff;">array</span>(

<span id="lnum73" style="color: #606060;"> 73:</span>             <span style="color: #006080;">"FontName"</span> =&gt; <span style="color: #006080;">"C:\\WINDOWS\\Fonts\\simkai.ttf"</span>,

<span id="lnum74" style="color: #606060;"> 74:</span>             <span style="color: #006080;">"FontSize"</span> =&gt; 16

<span id="lnum75" style="color: #606060;"> 75:</span>         ));

<span id="lnum76" style="color: #606060;"> 76:</span>         $myPicture-&gt;drawText(150, 25, $date1 . <span style="color: #006080;">"客流进出人数"</span>, <span style="color: #0000ff;">array</span>(

<span id="lnum77" style="color: #606060;"> 77:</span>             <span style="color: #006080;">"R"</span> =&gt; 0,

<span id="lnum78" style="color: #606060;"> 78:</span>             <span style="color: #006080;">"G"</span> =&gt; 0,

<span id="lnum79" style="color: #606060;"> 79:</span>             <span style="color: #006080;">"B"</span> =&gt; 0

<span id="lnum80" style="color: #606060;"> 80:</span>         ));

<span id="lnum81" style="color: #606060;"> 81:</span>         <span style="color: #008000;">/* ???? */</span>

<span id="lnum82" style="color: #606060;"> 82:</span>         $myPicture-&gt;drawLegend(580, 12, <span style="color: #0000ff;">array</span>(

<span id="lnum83" style="color: #606060;"> 83:</span>             <span style="color: #006080;">"Style"</span> =&gt; LEGEND_NOBORDER,

<span id="lnum84" style="color: #606060;"> 84:</span>             <span style="color: #006080;">"Mode"</span> =&gt; LEGEND_HORIZONTAL

<span id="lnum85" style="color: #606060;"> 85:</span>         ));

<span id="lnum86" style="color: #606060;"> 86:</span>         <span style="color: #008000;">/* 输出图表 */</span>

<span id="lnum87" style="color: #606060;"> 87:</span>         $imageFile = <span style="color: #006080;">"example112.png"</span>;

<span id="lnum88" style="color: #606060;"> 88:</span>         $myPicture-&gt;Render($imageFile);

<span id="lnum89" style="color: #606060;"> 89:</span>     }

<span id="lnum90" style="color: #606060;"> 90:</span> }

最后在Tpl文件夹中新建page文件夹,在其中新建index.html,调用图片

<span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">img</span> <span style="color: #ff0000;">src</span><span style="color: #0000ff;">="/example.png"</span> <span style="color: #ff0000;">style</span><span style="color: #0000ff;">="max-width:100%;"</span><span style="color: #0000ff;">/&gt;</span>

最后的效果如图

example

Continue reading...
分享