PHP中利用GD实现的柱状图,phpgd实现柱状图_PHP教程

WBOY
Libérer: 2016-07-12 09:07:15
original
806 Les gens l'ont consulté

PHP中利用GD实现的柱状图,phpgd实现柱状图

PHP中利用GD实现的柱状图,自己写的一个画柱状图的类,上代码。

<span>  1</span> <?<span>php
</span><span>  2</span>  <span>Class</span><span> Chart{
</span><span>  3</span>     <span>private</span> <span>$image</span>; <span>//</span><span> 定义图像</span>
<span>  4</span>     <span>private</span> <span>$title</span>; <span>//</span><span> 定义标题</span>
<span>  5</span>     <span>private</span> <span>$ydata</span>; <span>//</span><span> 定义Y轴数据</span>
<span>  6</span>     <span>private</span> <span>$xdata</span>; <span>//</span><span> 定义X轴数据</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>*/</span>
<span> 18</span>     <span>function</span> __construct(<span>$title</span>,<span>$xdata</span>,<span>$ydata</span><span>) {        
</span><span> 19</span>         <span>$this</span>->title = <span>$title</span><span>;
</span><span> 20</span>         <span>$this</span>->xdata = <span>$xdata</span><span>;
</span><span> 21</span>         <span>$this</span>->ydata = <span>$ydata</span><span>;
</span><span> 22</span>         <span>$this</span>->color = <span>array</span>('#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4'<span>);
</span><span> 23</span> <span>    }
</span><span> 24</span>     
<span> 25</span>     <span>/*</span>
<span> 26</span> <span>     * 公有方法,设置条形图的颜色 
</span><span> 27</span> <span>     * Array color 颜色数组,元素取值为'#058DC7'这种形式
</span><span> 28</span>      <span>*/</span>
<span> 29</span>     <span>function</span> setBarColor(<span>$color</span><span>){
</span><span> 30</span>         <span>$this</span>->color = <span>$color</span><span>;
</span><span> 31</span> <span>    }
</span><span> 32</span>     
<span> 33</span>     <span>/*</span>
<span> 34</span> <span>     * 公有方法,画条形图 
</span><span> 35</span>      <span>*/</span>
<span> 36</span>     <span>function</span><span> mkBarChart(){
</span><span> 37</span>         <span>$ydataNum</span> = <span>$this</span>->arrayNum(<span>$this</span>->ydata); <span>//</span><span> 取得数据分组的个数</span>
<span> 38</span>         <span>$max</span> = <span>$this</span>->arrayMax(<span>$this</span>->ydata); <span>//</span><span> 取得所有呈现数据的最大值</span>
<span> 39</span>         <span>$multi</span> = (<span>$max</span> > 100)? <span>$max</span>/100 : 1; <span>//</span><span> 如果最大数据是大于100的则进行缩小处理,获取</span>
<span> 40</span>         <span>$barHeightMulti</span> = 2.2; <span>//</span><span> 条形高缩放的比例</span>
<span> 41</span>         <span>$barWidth</span> = (16 - 2*(<span>$ydataNum</span> - 1)) > 10 ? (16 - 2*(<span>$ydataNum</span> - 1)) : 10; <span>//</span><span> 条的宽</span>
<span> 42</span>         <span>$barSpace</span> = 16; <span>//</span><span> 条之间的间距</span>
<span> 43</span>         <span>$chartLeft</span> = (1+<span>strlen</span>(<span>$max</span>))*12; <span>//</span><span> 设置图片左边的margin</span>
<span> 44</span>         
<span> 45</span>         <span>$barY</span> = 250; <span>//</span><span> 初始化条形图的Y的坐标
</span><span> 46</span> <span>        // 设置图片的宽、高</span>
<span> 47</span>         <span>$this</span>->width = (<span>$ydataNum</span>*<span>$barWidth</span> + <span>$barSpace</span>)*<span>count</span>(<span>$this</span>->xdata) + <span>$chartLeft</span><span>; 
</span><span> 48</span>         <span>$this</span>->height = 300<span>; 
</span><span> 49</span>         <span>$this</span>->image = imagecreatetruecolor(<span>$this</span>->width ,<span>$this</span>->height); <span>//</span><span> 准备画布</span>
<span> 50</span>         <span>$this</span>->bgcolor = imagecolorallocate(<span>$this</span>->image,255,255,255); <span>//</span><span> 图片的背景颜色
</span><span> 51</span>         
<span> 52</span> <span>        // 设置条形图的颜色</span>
<span> 53</span>         <span>$color</span> = <span>array</span><span>();
</span><span> 54</span>         <span>foreach</span>(<span>$this</span>->color <span>as</span> <span>$col</span><span>) {
</span><span> 55</span>             <span>$col</span> = <span>substr</span>(<span>$col</span>,1,<span>strlen</span>(<span>$col</span>)-1<span>);
</span><span> 56</span>             <span>$red</span> = <span>hexdec</span>(<span>substr</span>(<span>$col</span>,0,2<span>));
</span><span> 57</span>             <span>$green</span> = <span>hexdec</span>(<span>substr</span>(<span>$col</span>,2,2<span>));
</span><span> 58</span>             <span>$blue</span> = <span>hexdec</span>(<span>substr</span>(<span>$col</span>,4,2<span>));
</span><span> 59</span>             <span>$color</span>[] = imagecolorallocate(<span>$this</span>->image ,<span>$red</span>, <span>$green</span>, <span>$blue</span><span>);
</span><span> 60</span> <span>        }
</span><span> 61</span>         
<span> 62</span>         <span>//</span><span> 设置线段的颜色、字体的颜色、字体的路径</span>
<span> 63</span>         <span>$lineColor</span> = imagecolorallocate(<span>$this</span>->image ,0xcc,0xcc,0xcc<span>);
</span><span> 64</span>         <span>$fontColor</span> = imagecolorallocate(<span>$this</span>->image, 0x95,0x8f,0x8f<span>);
</span><span> 65</span>         <span>$fontPath</span> = 'font/simsun.ttc'<span>;
</span><span> 66</span>         
<span> 67</span>         imagefill(<span>$this</span>->image,0,0,<span>$this</span>->bgcolor); <span>//</span><span> 绘画背景
</span><span> 68</span>         
<span> 69</span> <span>        // 绘画图的分短线与左右边线</span>
<span> 70</span>         <span>for</span>(<span>$i</span> = 0; <span>$i</span> < 6; <span>$i</span>++<span> ) {
</span><span> 71</span>             imageline(<span>$this</span>->image,<span>$chartLeft</span>-10,<span>$barY</span>-<span>$barHeightMulti</span>*<span>$max</span>/5/<span>$multi</span>*<span>$i</span>,<span>$this</span>->width,<span>$barY</span>-<span>$barHeightMulti</span>*<span>$max</span>/5/<span>$multi</span>*<span>$i</span>,<span>$lineColor</span><span>);
</span><span> 72</span>             imagestring(<span>$this</span>->image,4,5,<span>$barY</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> 73</span> <span>        }        
</span><span> 74</span>         imageline(<span>$this</span>->image,<span>$chartLeft</span>-10,30,<span>$chartLeft</span>-10,<span>$barY</span>,<span>$lineColor</span><span>);
</span><span> 75</span>         imageline(<span>$this</span>->image,<span>$this</span>->width-1,30,<span>$this</span>->width-1,<span>$barY</span>,<span>$lineColor</span><span>);
</span><span> 76</span>         
<span> 77</span>         <span>//</span><span> 绘画图的条形</span>
<span> 78</span>         <span>foreach</span>(<span>$this</span>->ydata <span>as</span> <span>$key</span> => <span>$val</span><span>) {
</span><span> 79</span>             <span>if</span>(<span>$ydataNum</span> == 1<span>) {
</span><span> 80</span>                 <span>//</span><span> 一个系列数据时</span>
<span> 81</span>                 <span>$barX</span> = <span>$chartLeft</span> + 3 + (<span>$barWidth</span>+<span>$barSpace</span>)*<span>$key</span><span>;
</span><span> 82</span>                 imagefilledrectangle(<span>$this</span>->image,<span>$barX</span>,<span>$barY</span>-<span>$barHeightMulti</span>*<span>$val</span>/<span>$multi</span>,<span>$barX</span>+<span>$barWidth</span>,<span>$barY</span>,<span>$color</span>[<span>$key</span>%<span>count</span>(<span>$this</span>-><span>color)]);
</span><span> 83</span>             }<span>elseif</span>(<span>$ydataNum</span> > 1<span>) {
</span><span> 84</span>                 <span>//</span><span> 多个系列的数据时</span>
<span> 85</span>                 <span>$cbarSpace</span> = <span>$barSpace</span> + <span>$barWidth</span>*(<span>$ydataNum</span>-1<span>);
</span><span> 86</span>                 <span>foreach</span>(<span>$val</span> <span>as</span> <span>$ckey</span> => <span>$cval</span><span>) {
</span><span> 87</span>                     <span>$barX</span> = <span>$chartLeft</span> + 3 + <span>$barWidth</span>*<span>$key</span> + <span>$ckey</span>*(<span>$cbarSpace</span>+<span>$barWidth</span><span>);
</span><span> 88</span>                     imagefilledrectangle(<span>$this</span>->image,<span>$barX</span>,<span>$barY</span>-<span>$barHeightMulti</span>*<span>$cval</span>/<span>$multi</span>,<span>$barX</span>+<span>$barWidth</span>,<span>$barY</span>,<span>$color</span>[<span>$key</span>%<span>count</span>(<span>$this</span>-><span>color)]);
</span><span> 89</span> <span>                }
</span><span> 90</span> <span>            }
</span><span> 91</span>             
<span> 92</span> <span>        }
</span><span> 93</span>                 
<span> 94</span>         <span>//</span><span> 绘画条形图的x坐标的值</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>$barX</span> = <span>$chartLeft</span> + (<span>$ydataNum</span>*<span>$barWidth</span>+<span>$barSpace</span>)*<span>$key</span> + <span>$ydataNum</span>*<span>$barWidth</span>/3<span>;
</span><span> 97</span>             imagettftext(<span>$this</span>->image,10,-45,<span>$barX</span>,<span>$barY</span>+15,<span>$fontColor</span>,<span>$fontPath</span>,<span>$this</span>->xdata[<span>$key</span><span>]);
</span><span> 98</span> <span>        }
</span><span> 99</span>         
<span>100</span>         <span>//</span><span> 绘画标题</span>
<span>101</span>         <span>$titleStart</span> = (<span>$this</span>->width - 5.5*<span>strlen</span>(<span>$this</span>->title))/2<span>;
</span><span>102</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>103</span>         
<span>104</span>         <span>//</span><span> 输出图片</span>
<span>105</span>         <span>header</span>("Content-Type:image/png"<span>);
</span><span>106</span>         imagepng ( <span>$this</span>-><span>image );
</span><span>107</span> <span>    }
</span><span>108</span>     
<span>109</span>     <span>/*</span>
<span>110</span> <span>     * 私有方法,当数组为二元数组时,统计数组的长度 
</span><span>111</span> <span>     * Array arr 要做统计的数组
</span><span>112</span>      <span>*/</span>
<span>113</span>     <span>private</span> <span>function</span> arrayNum(<span>$arr</span><span>) {
</span><span>114</span>          <span>$num</span> = 0<span>;
</span><span>115</span>          <span>if</span>(<span>is_array</span>(<span>$arr</span><span>)) {
</span><span>116</span>             <span>$num</span>++<span>;
</span><span>117</span>             <span>for</span>(<span>$i</span> = 0; <span>$i</span> < <span>count</span>(<span>$arr</span>); <span>$i</span>++<span>){
</span><span>118</span>                 <span>if</span>(<span>is_array</span>(<span>$arr</span>[<span>$i</span><span>])) {
</span><span>119</span>                     <span>$num</span> = <span>count</span>(<span>$arr</span><span>);
</span><span>120</span>                     <span>break</span><span>;
</span><span>121</span> <span>                }
</span><span>122</span> <span>            }
</span><span>123</span> <span>         }
</span><span>124</span>          <span>return</span> <span>$num</span><span>;
</span><span>125</span> <span>    }
</span><span>126</span>     
<span>127</span>     <span>/*</span>
<span>128</span> <span>     * 私有方法,计算数组的深度 
</span><span>129</span> <span>     * Array arr 数组
</span><span>130</span>      <span>*/</span>
<span>131</span>     <span>private</span> <span>function</span> arrayDepth(<span>$arr</span><span>) {
</span><span>132</span>          <span>$num</span> = 0<span>;
</span><span>133</span>          <span>if</span>(<span>is_array</span>(<span>$arr</span><span>)) {
</span><span>134</span>             <span>$num</span>++<span>;
</span><span>135</span>             <span>for</span>(<span>$i</span> = 0; <span>$i</span> < <span>count</span>(<span>$arr</span>); <span>$i</span>++<span>){
</span><span>136</span>                 <span>if</span>(<span>is_array</span>(<span>$arr</span>[<span>$i</span><span>])) {
</span><span>137</span>                     <span>$num</span> += <span>$this</span>->arrayDepth(<span>$arr</span>[<span>$i</span><span>]);
</span><span>138</span>                     <span>break</span><span>;
</span><span>139</span> <span>                }
</span><span>140</span> <span>            }
</span><span>141</span> <span>         }
</span><span>142</span>          <span>return</span> <span>$num</span><span>;
</span><span>143</span> <span>    }
</span><span>144</span>     
<span>145</span>     <span>/*</span>
<span>146</span> <span>     * 私有方法,找到一组中的最大值 
</span><span>147</span> <span>     * Array arr 数字数组
</span><span>148</span>      <span>*/</span>
<span>149</span>      <span>private</span> <span>function</span> arrayMax(<span>$arr</span><span>) {
</span><span>150</span>         <span>$depth</span> = <span>$this</span>->arrayDepth(<span>$arr</span><span>);
</span><span>151</span>         <span>$max</span> = 0<span>;
</span><span>152</span>         <span>if</span>(<span>$depth</span> == 1<span>) {
</span><span>153</span>             <span>rsort</span>(<span>$arr</span><span>);
</span><span>154</span>             <span>$max</span> = <span>$arr</span>[0<span>];        
</span><span>155</span>         }<span>elseif</span>(<span>$depth</span> > 1<span>) {
</span><span>156</span>             <span>foreach</span>(<span>$arr</span> <span>as</span> <span>$val</span><span>) {
</span><span>157</span>                 <span>if</span>(<span>is_array</span>(<span>$val</span><span>)) {
</span><span>158</span>                     <span>if</span>(<span>$this</span>->arrayMax(<span>$val</span>) > <span>$max</span><span>) {
</span><span>159</span>                         <span>$max</span> = <span>$this</span>->arrayMax(<span>$val</span><span>);
</span><span>160</span> <span>                    }
</span><span>161</span>                 }<span>else</span><span>{                    
</span><span>162</span>                     <span>if</span>(<span>$val</span> > <span>$max</span><span>){
</span><span>163</span>                         <span>$max</span> = <span>$val</span><span>;
</span><span>164</span> <span>                    }
</span><span>165</span> <span>                }    
</span><span>166</span> <span>            }            
</span><span>167</span> <span>        }
</span><span>168</span>         <span>return</span> <span>$max</span><span>;
</span><span>169</span> <span>    }
</span><span>170</span>     
<span>171</span>     <span>function</span> arrayAver(<span>$arr</span><span>) {
</span><span>172</span>         <span>$aver</span> = <span>array</span><span>();
</span><span>173</span>         <span>foreach</span>(<span>$arr</span> <span>as</span> <span>$val</span><span>) {
</span><span>174</span>             <span>if</span>(<span>is_array</span>(<span>$val</span><span>)) {
</span><span>175</span>                 <span>$aver</span> = <span>array_merge</span>(<span>$aver</span>,<span>$val</span><span>);
</span><span>176</span>             }<span>else</span><span>{
</span><span>177</span>                 <span>$aver</span>[] = <span>$val</span><span>;
</span><span>178</span> <span>            }
</span><span>179</span> <span>        }
</span><span>180</span>         <span>return</span> <span>array_sum</span>(<span>$aver</span>)/<span>count</span>(<span>$aver</span><span>);
</span><span>181</span>         
<span>182</span> <span>    }
</span><span>183</span>     <span>//</span><span> 析构函数</span>
<span>184</span>     <span>function</span><span> __destruct(){
</span><span>185</span>         imagedestroy(<span>$this</span>-><span>image);
</span><span>186</span> <span>    }
</span><span>187</span> <span> }
</span><span>188</span> ?><br /><br />这个类可以实现画一个系列的柱状图和多个系列的柱状图,如下:
Copier après la connexion
一个系列的柱状图
Copier après la connexion
PHP中利用GD实现的柱状图,phpgd实现柱状图_PHP教程















多个系列的柱状图

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1063027.htmlTechArticlePHP中利用GD实现的柱状图,phpgd实现柱状图 PHP中利用GD实现的柱状图,自己写的一个画柱状图的类,上代码。 1 ? php 2 Class Chart{ 3 private $ima...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal