Home > Article > Backend Development > php顶用GD绘制折线图
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;">$seriesName</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;">$color</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;">$bgcolor</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;">$width</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 定义图片的宽</span><span style="color: #008080;"> 10</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;"> 11</span> <span style="color: #008080;"> 12</span> <span style="color: #008000;">/*</span><span style="color: #008080;"> 13</span> <span style="color: #008000;"> * 构造函数 </span><span style="color: #008080;"> 14</span> <span style="color: #008000;"> * String title 图片标题</span><span style="color: #008080;"> 15</span> <span style="color: #008000;"> * Array xdata 索引数组,X轴数据</span><span style="color: #008080;"> 16</span> <span style="color: #008000;"> * Array ydata 索引数组,数字数组,Y轴数据</span><span style="color: #008080;"> 17</span> <span style="color: #008000;"> * Array series_name 索引数组,数据系列名称</span><span style="color: #008080;"> 18</span> <span style="color: #008000;">*/</span><span style="color: #008080;"> 19</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: #800080;">$seriesName</span><span style="color: #000000;">) { </span><span style="color: #008080;"> 20</span> <span style="color: #800080;">$this</span>->title = <span style="color: #800080;">$title</span><span style="color: #000000;">;</span><span style="color: #008080;"> 21</span> <span style="color: #800080;">$this</span>->xdata = <span style="color: #800080;">$xdata</span><span style="color: #000000;">;</span><span style="color: #008080;"> 22</span> <span style="color: #800080;">$this</span>->ydata = <span style="color: #800080;">$ydata</span><span style="color: #000000;">;</span><span style="color: #008080;"> 23</span> <span style="color: #800080;">$this</span>->seriesName = <span style="color: #800080;">$seriesName</span><span style="color: #000000;">;</span><span style="color: #008080;"> 24</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;"> 25</span> <span style="color: #000000;"> }</span><span style="color: #008080;"> 26</span> <span style="color: #008080;"> 27</span> <span style="color: #008000;">/*</span><span style="color: #008080;"> 28</span> <span style="color: #008000;"> * 公有方法,设置条形图的颜色 </span><span style="color: #008080;"> 29</span> <span style="color: #008000;"> * Array color 颜色数组,元素取值为'#058DC7'这种形式</span><span style="color: #008080;"> 30</span> <span style="color: #008000;">*/</span><span style="color: #008080;"> 31</span> <span style="color: #0000ff;">function</span> setBarColor(<span style="color: #800080;">$color</span><span style="color: #000000;">){</span><span style="color: #008080;"> 32</span> <span style="color: #800080;">$this</span>->color = <span style="color: #800080;">$color</span><span style="color: #000000;">;</span><span style="color: #008080;"> 33</span> <span style="color: #000000;"> }</span><span style="color: #008080;"> 34</span> <span style="color: #008000;">/*</span><span style="color: #008080;"> 35</span> <span style="color: #008000;"> * 绘制折线图</span><span style="color: #008080;"> 36</span> <span style="color: #008000;">*/</span><span style="color: #008080;"> 37</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> paintLineChart() {</span><span style="color: #008080;"> 38</span> <span style="color: #800080;">$ydataNum</span> = <span style="color: #800080;">$this</span>->arrayNum(<span style="color: #800080;">$this</span>->ydata); <span style="color: #008000;">//</span><span style="color: #008000;"> 取得数据分组的个数</span><span style="color: #008080;"> 39</span> <span style="color: #800080;">$max</span> = <span style="color: #800080;">$this</span>->arrayMax(<span style="color: #800080;">$this</span>->ydata); <span style="color: #008000;">//</span><span style="color: #008000;"> 取得所有呈现数据的最大值</span><span style="color: #008080;"> 40</span> <span style="color: #800080;">$max</span> = (<span style="color: #800080;">$max</span> > 100)? <span style="color: #800080;">$max</span> : 100<span style="color: #000000;">;</span><span style="color: #008080;"> 41</span> <span style="color: #800080;">$multi</span> = <span style="color: #800080;">$max</span>/100; <span style="color: #008000;">//</span><span style="color: #008000;"> 如果最大数据是大于100的则进行缩小处理 </span><span style="color: #008080;"> 42</span> <span style="color: #800080;">$barHeightMulti</span> = 2.2; <span style="color: #008000;">//</span><span style="color: #008000;"> 条形高缩放的比例</span><span style="color: #008080;"> 43</span> <span style="color: #800080;">$lineWidth</span> = 50<span style="color: #000000;">;</span><span style="color: #008080;"> 44</span> <span style="color: #800080;">$chartLeft</span> = (1+<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$max</span>))*12; <span style="color: #008000;">//</span><span style="color: #008000;"> 设置图片左边的margin</span><span style="color: #008080;"> 45</span> <span style="color: #008080;"> 46</span> <span style="color: #800080;">$lineY</span> = 250; <span style="color: #008000;">//</span><span style="color: #008000;"> 初始化条形图的Y的坐标</span><span style="color: #008080;"> 47</span> <span style="color: #008000;"> // 设置图片的宽、高</span><span style="color: #008080;"> 48</span> <span style="color: #008000;"> //$this->width = $lineWidth*count($this->xdata) + $chartLeft - $lineWidth/1.6; </span><span style="color: #008080;"> 49</span> <span style="color: #008080;"> 50</span> <span style="color: #800080;">$margin</span> = 10; <span style="color: #008000;">//</span><span style="color: #008000;"> 小矩形描述右边margin</span><span style="color: #008080;"> 51</span> <span style="color: #800080;">$recWidth</span> = 20; <span style="color: #008000;">//</span><span style="color: #008000;"> 小矩形的宽</span><span style="color: #008080;"> 52</span> <span style="color: #800080;">$recHeight</span> = 15; <span style="color: #008000;">//</span><span style="color: #008000;"> 小矩形的高</span><span style="color: #008080;"> 53</span> <span style="color: #800080;">$space</span> = 20; <span style="color: #008000;">//</span><span style="color: #008000;"> 小矩形与条形图的间距</span><span style="color: #008080;"> 54</span> <span style="color: #800080;">$tmpWidth</span> = 0<span style="color: #000000;">;</span><span style="color: #008080;"> 55</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 设置图片的宽、高</span><span style="color: #008080;"> 56</span> <span style="color: #800080;">$lineChartWidth</span> = <span style="color: #800080;">$lineWidth</span>*<span style="color: #008080;">count</span>(<span style="color: #800080;">$this</span>->xdata) + <span style="color: #800080;">$chartLeft</span> - <span style="color: #800080;">$lineWidth</span>/1.6<span style="color: #000000;"> ;</span><span style="color: #008080;"> 57</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 两个系列数据以上的加上小矩形的宽</span><span style="color: #008080;"> 58</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$ydataNum</span> > 1<span style="color: #000000;">) {</span><span style="color: #008080;"> 59</span> <span style="color: #800080;">$tmpWidth</span> = <span style="color: #800080;">$this</span>->arrayLengthMax(<span style="color: #800080;">$this</span>->seriesName)*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;"> 60</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 61</span> <span style="color: #800080;">$this</span>->width = <span style="color: #800080;">$lineChartWidth</span> + <span style="color: #800080;">$tmpWidth</span><span style="color: #000000;">; </span><span style="color: #008080;"> 62</span> <span style="color: #008080;"> 63</span> <span style="color: #800080;">$this</span>->height = 300<span style="color: #000000;">; </span><span style="color: #008080;"> 64</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;"> 65</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;"> 66</span> <span style="color: #008080;"> 67</span> <span style="color: #008000;"> // 设置条形图的颜色</span><span style="color: #008080;"> 68</span> <span style="color: #800080;">$color</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();</span><span style="color: #008080;"> 69</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;"> 70</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;"> 71</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;"> 72</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;"> 73</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;"> 74</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;"> 75</span> <span style="color: #000000;"> }</span><span style="color: #008080;"> 76</span> <span style="color: #008080;"> 77</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 设置线段的颜色、字体的颜色、字体的路径</span><span style="color: #008080;"> 78</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;"> 79</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;"> 80</span> <span style="color: #800080;">$fontPath</span> = 'font/simsun.ttc'<span style="color: #000000;">;</span><span style="color: #008080;"> 81</span> <span style="color: #008080;"> 82</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;"> 83</span> <span style="color: #008080;"> 84</span> <span style="color: #008000;"> // 绘画图的分短线与左右边线</span><span style="color: #008080;"> 85</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;"> 86</span> imageline(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$chartLeft</span>-10,<span style="color: #800080;">$lineY</span>-<span style="color: #800080;">$barHeightMulti</span>*<span style="color: #800080;">$max</span>/5/<span style="color: #800080;">$multi</span>*<span style="color: #800080;">$i</span>,<span style="color: #800080;">$lineChartWidth</span>,<span style="color: #800080;">$lineY</span>-<span style="color: #800080;">$barHeightMulti</span>*<span style="color: #800080;">$max</span>/5/<span style="color: #800080;">$multi</span>*<span style="color: #800080;">$i</span>,<span style="color: #800080;">$lineColor</span><span style="color: #000000;">);</span><span style="color: #008080;"> 87</span> imagestring(<span style="color: #800080;">$this</span>->image,4,5,<span style="color: #800080;">$lineY</span>-<span style="color: #800080;">$barHeightMulti</span>*<span style="color: #800080;">$max</span>/5/<span style="color: #800080;">$multi</span>*<span style="color: #800080;">$i</span>-8,<span style="color: #008080;">floor</span>(<span style="color: #800080;">$max</span>/5*<span style="color: #800080;">$i</span>),<span style="color: #800080;">$fontColor</span><span style="color: #000000;">);</span><span style="color: #008080;"> 88</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 89</span> imageline(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$chartLeft</span>-10,30,<span style="color: #800080;">$chartLeft</span>-10,<span style="color: #800080;">$lineY</span>,<span style="color: #800080;">$lineColor</span><span style="color: #000000;">);</span><span style="color: #008080;"> 90</span> imageline(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$lineChartWidth</span>-1,30,<span style="color: #800080;">$lineChartWidth</span>-1,<span style="color: #800080;">$lineY</span>,<span style="color: #800080;">$lineColor</span><span style="color: #000000;">);</span><span style="color: #008080;"> 91</span> <span style="color: #800080;">$style</span> = <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$lineColor</span>,<span style="color: #800080;">$lineColor</span>,<span style="color: #800080;">$lineColor</span>,<span style="color: #800080;">$lineColor</span>,<span style="color: #800080;">$lineColor</span>,<span style="color: #800080;">$this</span>->bgcolor,<span style="color: #800080;">$this</span>->bgcolor,<span style="color: #800080;">$this</span>->bgcolor,<span style="color: #800080;">$this</span>->bgcolor,<span style="color: #800080;">$this</span>-><span style="color: #000000;">bgcolor);</span><span style="color: #008080;"> 92</span> imagesetstyle(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$style</span><span style="color: #000000;">);</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: #0000ff;">foreach</span>(<span style="color: #800080;">$this</span>->xdata <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;"> 96</span> <span style="color: #800080;">$lineX</span> = <span style="color: #800080;">$chartLeft</span> + 3 + <span style="color: #800080;">$lineWidth</span>*<span style="color: #800080;">$key</span><span style="color: #000000;">;</span><span style="color: #008080;"> 97</span> imageline(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$lineX</span>,30,<span style="color: #800080;">$lineX</span>,<span style="color: #800080;">$lineY</span>,<span style="color: #000000;">IMG_COLOR_STYLED);</span><span style="color: #008080;"> 98</span> <span style="color: #000000;"> }</span><span style="color: #008080;"> 99</span> <span style="color: #008080;">100</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 绘画图的折线</span><span style="color: #008080;">101</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;">102</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$ydataNum</span> == 1<span style="color: #000000;">) {</span><span style="color: #008080;">103</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 一个系列数据时</span><span style="color: #008080;">104</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$key</span> == <span style="color: #008080;">count</span>(<span style="color: #800080;">$this</span>->ydata) - 1 ) <span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #008080;">105</span> <span style="color: #800080;">$lineX</span> = <span style="color: #800080;">$chartLeft</span> + 3 + <span style="color: #800080;">$lineWidth</span>*<span style="color: #800080;">$key</span><span style="color: #000000;">;</span><span style="color: #008080;">106</span> <span style="color: #800080;">$lineY2</span> = <span style="color: #800080;">$lineY</span>-<span style="color: #800080;">$barHeightMulti</span>*(<span style="color: #800080;">$this</span>->ydata[<span style="color: #800080;">$key</span>+1])/<span style="color: #800080;">$multi</span><span style="color: #000000;">;</span><span style="color: #008080;">107</span> <span style="color: #008080;">108</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 画折线</span><span style="color: #008080;">109</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$key</span> == <span style="color: #008080;">count</span>(<span style="color: #800080;">$this</span>->ydata) - 2<span style="color: #000000;"> ) {</span><span style="color: #008080;">110</span> imagefilledellipse(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$lineX</span>+<span style="color: #800080;">$lineWidth</span>,<span style="color: #800080;">$lineY2</span>,10,10,<span style="color: #800080;">$color</span>[0<span style="color: #000000;">]);</span><span style="color: #008080;">111</span> <span style="color: #000000;"> }</span><span style="color: #008080;">112</span> imageline(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$lineX</span>,<span style="color: #800080;">$lineY</span>-<span style="color: #800080;">$barHeightMulti</span>*<span style="color: #800080;">$val</span>/<span style="color: #800080;">$multi</span>,<span style="color: #800080;">$lineX</span>+<span style="color: #800080;">$lineWidth</span>,<span style="color: #800080;">$lineY2</span>,<span style="color: #800080;">$color</span>[0<span style="color: #000000;">]);</span><span style="color: #008080;">113</span> imagefilledellipse(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$lineX</span>,<span style="color: #800080;">$lineY</span>-<span style="color: #800080;">$barHeightMulti</span>*<span style="color: #800080;">$val</span>/<span style="color: #800080;">$multi</span>,10,10,<span style="color: #800080;">$color</span>[0<span style="color: #000000;">]);</span><span style="color: #008080;">114</span> }<span style="color: #0000ff;">elseif</span>(<span style="color: #800080;">$ydataNum</span> > 1<span style="color: #000000;">) {</span><span style="color: #008080;">115</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 多个系列的数据时</span><span style="color: #008080;">116</span> <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$val</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$ckey</span> => <span style="color: #800080;">$cval</span><span style="color: #000000;">) {</span><span style="color: #008080;">117</span> <span style="color: #008080;">118</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$ckey</span> == <span style="color: #008080;">count</span>(<span style="color: #800080;">$val</span>) - 1 ) <span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #008080;">119</span> <span style="color: #800080;">$lineX</span> = <span style="color: #800080;">$chartLeft</span> + 3 + <span style="color: #800080;">$lineWidth</span>*<span style="color: #800080;">$ckey</span><span style="color: #000000;">;</span><span style="color: #008080;">120</span> <span style="color: #800080;">$lineY2</span> = <span style="color: #800080;">$lineY</span>-<span style="color: #800080;">$barHeightMulti</span>*(<span style="color: #800080;">$val</span>[<span style="color: #800080;">$ckey</span>+1])/<span style="color: #800080;">$multi</span><span style="color: #000000;">;</span><span style="color: #008080;">121</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 画折线</span><span style="color: #008080;">122</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$ckey</span> == <span style="color: #008080;">count</span>(<span style="color: #800080;">$val</span>) - 2<span style="color: #000000;"> ) {</span><span style="color: #008080;">123</span> imagefilledellipse(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$lineX</span>+<span style="color: #800080;">$lineWidth</span>,<span style="color: #800080;">$lineY2</span>,10,10,<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;">124</span> <span style="color: #000000;"> }</span><span style="color: #008080;">125</span> imageline(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$lineX</span>,<span style="color: #800080;">$lineY</span>-<span style="color: #800080;">$barHeightMulti</span>*<span style="color: #800080;">$cval</span>/<span style="color: #800080;">$multi</span>,<span style="color: #800080;">$lineX</span>+<span style="color: #800080;">$lineWidth</span>,<span style="color: #800080;">$lineY2</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;">126</span> imagefilledellipse(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$lineX</span>,<span style="color: #800080;">$lineY</span>-<span style="color: #800080;">$barHeightMulti</span>*<span style="color: #800080;">$cval</span>/<span style="color: #800080;">$multi</span>,10,10,<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;">127</span> <span style="color: #000000;"> }</span><span style="color: #008080;">128</span> <span style="color: #000000;"> }</span><span style="color: #008080;">129</span> <span style="color: #008080;">130</span> <span style="color: #000000;"> }</span><span style="color: #008080;">131</span> <span style="color: #008080;">132</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 绘画条形图的x坐标的值</span><span style="color: #008080;">133</span> <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$this</span>->xdata <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;">134</span> <span style="color: #800080;">$lineX</span> = <span style="color: #800080;">$chartLeft</span> + <span style="color: #800080;">$lineWidth</span>*<span style="color: #800080;">$key</span> + <span style="color: #800080;">$lineWidth</span>/3 - 20<span style="color: #000000;">;</span><span style="color: #008080;">135</span> imagettftext(<span style="color: #800080;">$this</span>->image,10,-65,<span style="color: #800080;">$lineX</span>,<span style="color: #800080;">$lineY</span>+15,<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;">136</span> <span style="color: #000000;"> } </span><span style="color: #008080;">137</span> <span style="color: #008080;">138</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 两个系列数据以上时绘制小矩形及之后文字说明</span><span style="color: #008080;">139</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$ydataNum</span> > 1<span style="color: #000000;">) {</span><span style="color: #008080;">140</span> <span style="color: #800080;">$x1</span> = <span style="color: #800080;">$lineChartWidth</span> + <span style="color: #800080;">$space</span><span style="color: #000000;">;</span><span style="color: #008080;">141</span> <span style="color: #800080;">$y1</span> = 20<span style="color: #000000;"> ;</span><span style="color: #008080;">142</span> <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$this</span>->seriesName <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;">143</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;">144</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>->seriesName[<span style="color: #800080;">$key</span><span style="color: #000000;">]);</span><span style="color: #008080;">145</span> <span style="color: #800080;">$y1</span> += <span style="color: #800080;">$recHeight</span> + 10<span style="color: #000000;">; </span><span style="color: #008080;">146</span> <span style="color: #000000;"> }</span><span style="color: #008080;">147</span> <span style="color: #000000;"> }</span><span style="color: #008080;">148</span> <span style="color: #008080;">149</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 绘画标题</span><span style="color: #008080;">150</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;">151</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;">152</span> <span style="color: #008080;">153</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 输出图片</span><span style="color: #008080;">154</span> <span style="color: #008080;">header</span>("Content-Type:image/png"<span style="color: #000000;">);</span><span style="color: #008080;">155</span> imagepng ( <span style="color: #800080;">$this</span>-><span style="color: #000000;">image );</span><span style="color: #008080;">156</span> <span style="color: #000000;"> }</span><span style="color: #008080;">157</span> <span style="color: #008080;">158</span> <span style="color: #008080;">159</span> <span style="color: #008000;">/*</span><span style="color: #008080;">160</span> <span style="color: #008000;"> * 私有方法,当数组为二元数组时,统计数组的长度 </span><span style="color: #008080;">161</span> <span style="color: #008000;"> * Array arr 要做统计的数组</span><span style="color: #008080;">162</span> <span style="color: #008000;">*/</span><span style="color: #008080;">163</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> arrayNum(<span style="color: #800080;">$arr</span><span style="color: #000000;">) {</span><span style="color: #008080;">164</span> <span style="color: #800080;">$num</span> = 0<span style="color: #000000;">;</span><span style="color: #008080;">165</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">)) {</span><span style="color: #008080;">166</span> <span style="color: #800080;">$num</span>++<span style="color: #000000;">;</span><span style="color: #008080;">167</span> <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> count(<span style="color: #800080;">$arr</span>); <span style="color: #800080;">$i</span>++<span style="color: #000000;">){</span><span style="color: #008080;">168</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">])) {</span><span style="color: #008080;">169</span> <span style="color: #800080;">$num</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">);</span><span style="color: #008080;">170</span> <span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #008080;">171</span> <span style="color: #000000;"> }</span><span style="color: #008080;">172</span> <span style="color: #000000;"> }</span><span style="color: #008080;">173</span> <span style="color: #000000;"> }</span><span style="color: #008080;">174</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$num</span><span style="color: #000000;">;</span><span style="color: #008080;">175</span> <span style="color: #000000;"> }</span><span style="color: #008080;">176</span> <span style="color: #008080;">177</span> <span style="color: #008000;">/*</span><span style="color: #008080;">178</span> <span style="color: #008000;"> * 私有方法,计算数组的深度 </span><span style="color: #008080;">179</span> <span style="color: #008000;"> * Array arr 数组</span><span style="color: #008080;">180</span> <span style="color: #008000;">*/</span><span style="color: #008080;">181</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> arrayDepth(<span style="color: #800080;">$arr</span><span style="color: #000000;">) {</span><span style="color: #008080;">182</span> <span style="color: #800080;">$num</span> = 0<span style="color: #000000;">;</span><span style="color: #008080;">183</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">)) {</span><span style="color: #008080;">184</span> <span style="color: #800080;">$num</span>++<span style="color: #000000;">;</span><span style="color: #008080;">185</span> <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> count(<span style="color: #800080;">$arr</span>); <span style="color: #800080;">$i</span>++<span style="color: #000000;">){</span><span style="color: #008080;">186</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">])) {</span><span style="color: #008080;">187</span> <span style="color: #800080;">$num</span> += <span style="color: #800080;">$this</span>->arrayDepth(<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]);</span><span style="color: #008080;">188</span> <span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #008080;">189</span> <span style="color: #000000;"> }</span><span style="color: #008080;">190</span> <span style="color: #000000;"> }</span><span style="color: #008080;">191</span> <span style="color: #000000;"> }</span><span style="color: #008080;">192</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$num</span><span style="color: #000000;">;</span><span style="color: #008080;">193</span> <span style="color: #000000;"> }</span><span style="color: #008080;">194</span> <span style="color: #008080;">195</span> <span style="color: #008000;">/*</span><span style="color: #008080;">196</span> <span style="color: #008000;"> * 私有方法,找到一组中的最大值 </span><span style="color: #008080;">197</span> <span style="color: #008000;"> * Array arr 数字数组</span><span style="color: #008080;">198</span> <span style="color: #008000;">*/</span><span style="color: #008080;">199</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> arrayMax(<span style="color: #800080;">$arr</span><span style="color: #000000;">) {</span><span style="color: #008080;">200</span> <span style="color: #800080;">$depth</span> = <span style="color: #800080;">$this</span>->arrayDepth(<span style="color: #800080;">$arr</span><span style="color: #000000;">);</span><span style="color: #008080;">201</span> <span style="color: #800080;">$max</span> = 0<span style="color: #000000;">;</span><span style="color: #008080;">202</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$depth</span> == 1<span style="color: #000000;">) {</span><span style="color: #008080;">203</span> <span style="color: #008080;">rsort</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">);</span><span style="color: #008080;">204</span> <span style="color: #800080;">$max</span> = <span style="color: #800080;">$arr</span>[0<span style="color: #000000;">]; </span><span style="color: #008080;">205</span> }<span style="color: #0000ff;">elseif</span>(<span style="color: #800080;">$depth</span> > 1<span style="color: #000000;">) {</span><span style="color: #008080;">206</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;">207</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$val</span><span style="color: #000000;">)) {</span><span style="color: #008080;">208</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->arrayMax(<span style="color: #800080;">$val</span>) > <span style="color: #800080;">$max</span><span style="color: #000000;">) {</span><span style="color: #008080;">209</span> <span style="color: #800080;">$max</span> = <span style="color: #800080;">$this</span>->arrayMax(<span style="color: #800080;">$val</span><span style="color: #000000;">);</span><span style="color: #008080;">210</span> <span style="color: #000000;"> }</span><span style="color: #008080;">211</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">212</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$val</span> > <span style="color: #800080;">$max</span><span style="color: #000000;">){</span><span style="color: #008080;">213</span> <span style="color: #800080;">$max</span> = <span style="color: #800080;">$val</span><span style="color: #000000;">;</span><span style="color: #008080;">214</span> <span style="color: #000000;"> }</span><span style="color: #008080;">215</span> <span style="color: #000000;"> } </span><span style="color: #008080;">216</span> <span style="color: #000000;"> } </span><span style="color: #008080;">217</span> <span style="color: #000000;"> }</span><span style="color: #008080;">218</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$max</span><span style="color: #000000;">;</span><span style="color: #008080;">219</span> <span style="color: #000000;"> }</span><span style="color: #008080;">220</span> <span style="color: #008080;">221</span> <span style="color: #008000;">/*</span><span style="color: #008080;">222</span> <span style="color: #008000;"> * 私有方法,求数组的平均值 </span><span style="color: #008080;">223</span> <span style="color: #008000;"> * Array arr 数字数组</span><span style="color: #008080;">224</span> <span style="color: #008000;">*/</span><span style="color: #008080;">225</span> <span style="color: #0000ff;">function</span> arrayAver(<span style="color: #800080;">$arr</span><span style="color: #000000;">) {</span><span style="color: #008080;">226</span> <span style="color: #800080;">$aver</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();</span><span style="color: #008080;">227</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;">228</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$val</span><span style="color: #000000;">)) {</span><span style="color: #008080;">229</span> <span style="color: #800080;">$aver</span> = <span style="color: #008080;">array_merge</span>(<span style="color: #800080;">$aver</span>,<span style="color: #800080;">$val</span><span style="color: #000000;">);</span><span style="color: #008080;">230</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{</span><span style="color: #008080;">231</span> <span style="color: #800080;">$aver</span>[] = <span style="color: #800080;">$val</span><span style="color: #000000;">;</span><span style="color: #008080;">232</span> <span style="color: #000000;"> }</span><span style="color: #008080;">233</span> <span style="color: #000000;"> }</span><span style="color: #008080;">234</span> <span style="color: #0000ff;">return</span> <span style="color: #008080;">array_sum</span>(<span style="color: #800080;">$aver</span>)/<span style="color: #008080;">count</span>(<span style="color: #800080;">$aver</span><span style="color: #000000;">);</span><span style="color: #008080;">235</span> <span style="color: #000000;"> }</span><span style="color: #008080;">236</span> <span style="color: #008080;">237</span> <span style="color: #008000;">/*</span><span style="color: #008080;">238</span> <span style="color: #008000;"> * 私有方法,求数组中元素长度最大的值 </span><span style="color: #008080;">239</span> <span style="color: #008000;"> * Array arr 字符串数组,必须是汉字</span><span style="color: #008080;">240</span> <span style="color: #008000;">*/</span><span style="color: #008080;">241</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;">242</span> <span style="color: #800080;">$length</span> = 0<span style="color: #000000;">;</span><span style="color: #008080;">243</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;">244</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;">245</span> <span style="color: #000000;"> }</span><span style="color: #008080;">246</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$length</span>/3<span style="color: #000000;">;</span><span style="color: #008080;">247</span> <span style="color: #000000;"> } </span><span style="color: #008080;">248</span> <span style="color: #008080;">249</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 析构函数</span><span style="color: #008080;">250</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __destruct(){</span><span style="color: #008080;">251</span> imagedestroy(<span style="color: #800080;">$this</span>-><span style="color: #000000;">image);</span><span style="color: #008080;">252</span> <span style="color: #000000;"> }</span><span style="color: #008080;">253</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>(<span style="color: #0000ff;">array</span>(29,30,45,54,65,45,76,23,54),<span style="color: #0000ff;">array</span>(89,60,90,23,35,45,56,23,56<span style="color: #000000;">));</span><span style="color: #008080;">3</span> <span style="color: #800080;">$color</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();</span><span style="color: #008080;">4</span> <span style="color: #800080;">$seriesName</span> = <span style="color: #0000ff;">array</span>("七月","八月"<span style="color: #000000;">);</span><span style="color: #008080;">5</span> <span style="color: #800080;">$title</span> = "测试数据"<span style="color: #000000;">;</span><span style="color: #008080;">6</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: #800080;">$seriesName</span><span style="color: #000000;">);</span><span style="color: #008080;">7 </span> <span style="color: #800080;">$Img</span>->paintLineChart();
效果图如下: