ホームページ > バックエンド開発 > PHPチュートリアル > PHP中用GD绘制饼图,gd绘制饼_PHP教程

PHP中用GD绘制饼图,gd绘制饼_PHP教程

WBOY
リリース: 2016-07-12 09:06:46
オリジナル
962 人が閲覧しました

PHP中用GD绘制饼图,gd绘制饼

PHP中用GD绘制饼图,绘制的类见代码:

<span>  1</span> <span>Class</span><span> Chart{
</span><span>  2</span>     <span>private</span> <span>$image</span>; <span>//</span><span> 定义图像</span>
<span>  3</span>     <span>private</span> <span>$title</span>; <span>//</span><span> 定义标题</span>
<span>  4</span>     <span>private</span> <span>$ydata</span>; <span>//</span><span> 定义Y轴数据</span>
<span>  5</span>     <span>private</span> <span>$xdata</span>; <span>//</span><span> 定义X轴数据</span>
<span>  6</span>     <span>private</span> <span>$color</span>; <span>//</span><span> 定义条形图颜色</span>
<span>  7</span>     <span>private</span> <span>$bgcolor</span>; <span>//</span><span> 定义图片背景颜色</span>
<span>  8</span>     <span>private</span> <span>$width</span>; <span>//</span><span> 定义图片的宽</span>
<span>  9</span>     <span>private</span> <span>$height</span>; <span>//</span><span> 定义图片的长</span>
<span> 10</span>     
<span> 11</span>     <span>/*</span>
<span> 12</span> <span>     * 构造函数 
</span><span> 13</span> <span>     * String title 图片标题
</span><span> 14</span> <span>     * Array xdata 索引数组,X轴数据
</span><span> 15</span> <span>     * Array ydata 索引数组,数字数组,Y轴数据
</span><span> 16</span>      <span>*/</span>
<span> 17</span>     <span>function</span> __construct(<span>$title</span>,<span>$xdata</span>,<span>$ydata</span><span>) {        
</span><span> 18</span>         <span>$this</span>->title = <span>$title</span><span>;
</span><span> 19</span>         <span>$this</span>->xdata = <span>$xdata</span><span>;
</span><span> 20</span>         <span>$this</span>->ydata = <span>$ydata</span><span>;
</span><span> 21</span>         <span>$this</span>->color = <span>array</span>('#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4'<span>);
</span><span> 22</span> <span>    }
</span><span> 23</span>     
<span> 24</span>     <span>/*</span>
<span> 25</span> <span>     * 公有方法,设置条形图的颜色 
</span><span> 26</span> <span>     * Array color 颜色数组,元素取值为'#058DC7'这种形式
</span><span> 27</span>      <span>*/</span>
<span> 28</span>     <span>function</span> setBarColor(<span>$color</span><span>){
</span><span> 29</span>         <span>$this</span>->color = <span>$color</span><span>;
</span><span> 30</span> <span>    }
</span><span> 31</span>     
<span> 32</span>     <span>/*</span>
<span> 33</span> <span>     * 绘制饼图
</span><span> 34</span>      <span>*/</span>
<span> 35</span>     <span>function</span><span> mkPieChart() {
</span><span> 36</span>         <span>$sum</span> = <span>array_sum</span>(<span>$this</span>->ydata); <span>//</span><span> 获取ydata所有元素之和</span>
<span> 37</span>         <span>$start</span> = 0; <span>//</span><span> 弧的开始角度</span>
<span> 38</span>         <span>$end</span> = 0; <span>//</span><span> 弧的结束角度</span>
<span> 39</span>         <span>$pieWidth</span> =  300; <span>//</span><span> 椭圆的长轴</span>
<span> 40</span>         <span>$pieHeight</span> = 220; <span>//</span><span> 椭圆的短轴</span>
<span> 41</span>         <span>$space</span> = 40; <span>//</span><span> 椭圆与小矩形的间距</span>
<span> 42</span>         <span>$margin</span> = 20; <span>//</span><span> 图片的边距</span>
<span> 43</span>         <span>$recWidth</span> = 20; <span>//</span><span> 小矩形的宽</span>
<span> 44</span>         <span>$recHeight</span> = 15; <span>//</span><span> 小矩形的高</span>
<span> 45</span>         <span>$titleHeight</span> = 50; <span>//</span><span> 标题区域的高
</span><span> 46</span> <span>        // 图片自适应宽与高</span>
<span> 47</span>         <span>$this</span>->width = <span>$pieWidth</span> + <span>$this</span>->arrayLengthMax(<span>$this</span>->xdata)*10*4/3 + <span>$space</span> + <span>$recWidth</span> +<span>$margin</span><span>;
</span><span> 48</span>         <span>$this</span>->height =  ((<span>$pieHeight</span> > <span>count</span>(<span>$this</span>->xdata)*25 ) ? <span>$pieHeight</span> : <span>count</span>(<span>$this</span>->xdata)*25) + <span>$titleHeight</span><span>;
</span><span> 49</span>         <span>//</span><span> 椭圆中心的坐标</span>
<span> 50</span>         <span>$cx</span> = <span>$pieWidth</span>/2+<span>$margin</span><span>;
</span><span> 51</span>         <span>$cy</span> = <span>$pieHeight</span>/2+<span>$titleHeight</span><span>;
</span><span> 52</span>         
<span> 53</span>         <span>$this</span>->image = imagecreatetruecolor(<span>$this</span>->width ,<span>$this</span>->height); <span>//</span><span> 准备画布</span>
<span> 54</span>         <span>$this</span>->bgcolor = imagecolorallocate(<span>$this</span>->image,255,255,255); <span>//</span><span> 图片的背景颜色</span>
<span> 55</span>         imagefill(<span>$this</span>->image,0,0,<span>$this</span>->bgcolor); <span>//</span><span> 填充背景
</span><span> 56</span>         
<span> 57</span> <span>        // 设置条形图的颜色</span>
<span> 58</span>         <span>$color</span> = <span>array</span><span>();
</span><span> 59</span>         <span>foreach</span>(<span>$this</span>->color <span>as</span> <span>$col</span><span>) {
</span><span> 60</span>             <span>$col</span> = <span>substr</span>(<span>$col</span>,1,<span>strlen</span>(<span>$col</span>)-1<span>);
</span><span> 61</span>             <span>$red</span> = <span>hexdec</span>(<span>substr</span>(<span>$col</span>,0,2<span>));
</span><span> 62</span>             <span>$green</span> = <span>hexdec</span>(<span>substr</span>(<span>$col</span>,2,2<span>));
</span><span> 63</span>             <span>$blue</span> = <span>hexdec</span>(<span>substr</span>(<span>$col</span>,4,2<span>));
</span><span> 64</span>             <span>$color</span>[] = imagecolorallocate(<span>$this</span>->image ,<span>$red</span>, <span>$green</span>, <span>$blue</span><span>);
</span><span> 65</span> <span>        }
</span><span> 66</span>         
<span> 67</span>         <span>//</span><span> 设置线段的颜色、字体的颜色、字体的路径</span>
<span> 68</span>         <span>$lineColor</span> = imagecolorallocate(<span>$this</span>->image ,0xcc,0xcc,0xcc<span>);
</span><span> 69</span>         <span>$fontColor</span> = imagecolorallocate(<span>$this</span>->image, 0x95,0x8f,0x8f<span>);
</span><span> 70</span>         <span>$fontPath</span> = 'font/simsun.ttc'<span>;
</span><span> 71</span>         
<span> 72</span>         <span>//</span><span> 绘制扇形弧 </span>
<span> 73</span>         <span>for</span>(<span>$i</span> = 0; <span>$i</span> < 10; <span>$i</span>++<span>) {
</span><span> 74</span>             <span>foreach</span>(<span>$this</span>->ydata <span>as</span> <span>$key</span> => <span>$val</span><span>) {
</span><span> 75</span>                 <span>$end</span> += 360*<span>$val</span>/<span>$sum</span><span>;
</span><span> 76</span>                 imagefilledarc(<span>$this</span>->image,<span>$cx</span>,<span>$cy</span>-<span>$i</span>,<span>$pieWidth</span>,<span>$pieHeight</span>, <span>$start</span>,<span>$end</span>,<span>$color</span>[<span>$key</span>%<span>count</span>(<span>$this</span>->color)],<span>IMG_ARC_PIE);        
</span><span> 77</span>                 <span>$start</span> = <span>$end</span><span>;                
</span><span> 78</span> <span>            }
</span><span> 79</span> <span>        }
</span><span> 80</span>         
<span> 81</span>         <span>//</span><span> 绘制小矩形及之后文字说明</span>
<span> 82</span>         <span>$x1</span> = <span>$pieWidth</span>+<span>$space</span><span>;
</span><span> 83</span>         <span>$y1</span> = <span>$titleHeight</span><span> ;
</span><span> 84</span>         <span>foreach</span>(<span>$this</span>->ydata <span>as</span> <span>$key</span> => <span>$val</span><span>) {
</span><span> 85</span>             imagefilledrectangle(<span>$this</span>->image,<span>$x1</span>,<span>$y1</span>,<span>$x1</span>+<span>$recWidth</span>,<span>$y1</span>+<span>$recHeight</span>,<span>$color</span>[<span>$key</span>%<span>count</span>(<span>$this</span>-><span>color)]);        
</span><span> 86</span>             imagettftext(<span>$this</span>->image,10,0,<span>$x1</span>+<span>$recWidth</span>+5,<span>$y1</span>+<span>$recHeight</span>-2,<span>$fontColor</span>,<span>$fontPath</span>,<span>$this</span>->xdata[<span>$key</span><span>]);
</span><span> 87</span>             <span>$y1</span> += <span>$recHeight</span> + 10<span>;            
</span><span> 88</span> <span>        }
</span><span> 89</span>         
<span> 90</span>         <span>//</span><span> 绘画标题</span>
<span> 91</span>         <span>$titleStart</span> = (<span>$this</span>->width - 5.5*<span>strlen</span>(<span>$this</span>->title))/2<span>;
</span><span> 92</span>         imagettftext(<span>$this</span>->image,11,0,<span>$titleStart</span>,20,<span>$fontColor</span>,<span>$fontPath</span>,<span>$this</span>-><span>title);
</span><span> 93</span>         
<span> 94</span>         <span>//</span><span> 输出图片</span>
<span> 95</span>         <span>header</span>("Content-Type:image/png"<span>);
</span><span> 96</span>         imagepng(<span>$this</span>-><span>image);
</span><span> 97</span> <span>    } 
</span><span> 98</span>     
<span> 99</span>     <span>/*</span>
<span>100</span> <span>     * 私有方法,求数组中元素长度最大的值 
</span><span>101</span> <span>     * Array arr 字符串数组,必须是汉字
</span><span>102</span>      <span>*/</span>
<span>103</span>     <span>private</span> <span>function</span> arrayLengthMax(<span>$arr</span><span>) {
</span><span>104</span>         <span>$length</span> = 0<span>;
</span><span>105</span>         <span>foreach</span>(<span>$arr</span> <span>as</span> <span>$val</span><span>) {
</span><span>106</span>             <span>$length</span> = <span>strlen</span>(<span>$val</span>) > <span>$length</span> ? <span>strlen</span>(<span>$val</span>) : <span>$length</span><span>;
</span><span>107</span> <span>        }
</span><span>108</span>         <span>return</span> <span>$length</span>/3<span>;
</span><span>109</span> <span>    } 
</span><span>110</span>     
<span>111</span>     <span>//</span><span> 析构函数</span>
<span>112</span>     <span>function</span><span> __destruct(){
</span><span>113</span>         imagedestroy(<span>$this</span>-><span>image);
</span><span>114</span> <span>    }
</span><span>115</span>  }
ログイン後にコピー

测试代码如下:

<span>1</span> <span>$xdata</span> = <span>array</span>('测试一','测试二','测试三','测试四','测试五','测试六','测试七','测试八','测试九'<span>);
</span><span>2</span> <span>$ydata</span> = <span>array</span>(89,90,90,23,35,45,56,23,56<span>);
</span><span>3</span> <span>$Img</span> = <span>new</span> Chart(<span>$title</span>,<span>$xdata</span>,<span>$ydata</span><span>);
</span><span>4</span> <span>$Img</span>->mkPieChart();
ログイン後にコピー

 

效果图如下:

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1063518.htmlTechArticlePHP中用GD绘制饼图,gd绘制饼 PHP中用GD绘制饼图,绘制的类见代码: 1 Class Chart{ 2 private $image ; // 定义图像 3 private $title ; // 定义标题 4 priv...
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート