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