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