1+ <?php
2+ /**
3+ * 用户图形化分析类
4+ * @authors Andy Chen ([email protected] ) 5+ * @date 2014-12-14 21:00:18
6+ * @version $Id$
7+ */
8+
9+ class userClass {
10+
11+ function __construct (){
12+ $ this ->fontfile = 'XHei.ttc ' ;
13+ }
14+
15+ /**
16+ * 用户图形化分析方法
17+ * @param int $speck_num 画多少个点
18+ * @param int $line_num 画多少条线
19+ * @param int $font_num 填充文字数量
20+ * @return img $im
21+ */
22+ public function userAnalyse ($ speck_num ,$ line_num ,$ font_num ){
23+ // 坐标宽高 coordinate
24+ $ coor_width = 2000 ;
25+ $ coor_height = 400 ;
26+ // 画布边距
27+ $ cre_left = 150 ;
28+ $ cre_right = 500 ;
29+ $ cre_top = 350 ;
30+ $ cre_bottom = 150 ;
31+ // 图柱宽
32+ $ con_width = 30 ;
33+ // x轴和y轴 宽
34+ $ con_line_wh = 2 ;
35+ // x轴和y轴 延伸
36+ $ con_line_plus = 100 ;
37+
38+ //画布
39+ $ cre_wh = $ coor_width + $ cre_left + $ cre_right ;
40+ $ cre_ht = $ coor_height + $ cre_top + $ cre_bottom ;
41+ $ im = imagecreatetruecolor ($ cre_wh , $ cre_ht );
42+
43+ //字体颜色
44+ $ font_color = imagecolorallocate ($ im , 24 , 24 , 24 );
45+
46+ // 画背景
47+ $ red = '244 ' ;
48+ $ green = '244 ' ;
49+ $ blue = '245 ' ;
50+ $ back_color = imagecolorallocate ($ im , $ red , $ green , $ blue );
51+ imagefilledrectangle ($ im ,0 ,0 ,$ cre_wh ,$ cre_ht ,$ back_color );
52+
53+ // X轴
54+ $ red = 4 ;
55+ $ green = 32 ;
56+ $ blue = 41 ;
57+ $ x1 = $ cre_left ;
58+ $ y1 = $ coor_height + $ cre_top ;
59+ $ x2 = $ coor_width + $ cre_left + $ con_line_plus ;
60+ $ y2 = $ coor_height + $ cre_top ;
61+ $ line_color = imagecolorallocate ($ im , $ red , $ green , $ blue ); //线条颜色
62+ imagesetthickness ($ im , $ con_line_wh );
63+ imageline ($ im , $ x1 , $ y1 , $ x2 , $ y2 , $ line_color );
64+
65+ // X轴 - 数值柱形图
66+ $ time_num = 24 ;
67+ $ space = $ coor_width / $ time_num ;
68+ for ($ i =1 ; $ i <= $ time_num ; $ i ++) {
69+ $ x1 = $ cre_left + $ space * $ i ;
70+ $ y1 = $ coor_height + $ cre_top - mt_rand (0 ,$ coor_height );
71+ $ x2 = $ cre_left + $ space * $ i ;
72+ $ y2 = $ coor_height + $ cre_top - $ con_line_wh ;
73+
74+ $ coor_red = mt_rand (0 ,255 );
75+ $ coor_green = mt_rand (0 ,255 );
76+ $ coor_blue = mt_rand (0 ,255 );
77+ $ coor_line_color = imagecolorallocate ($ im , $ coor_red , $ coor_green , $ coor_blue ); //线条颜色
78+
79+ imagesetthickness ($ im , $ con_width );
80+ imageline ($ im , $ x1 , $ y1 , $ x2 , $ y2 , $ coor_line_color );
81+
82+ // X轴 - 时间刻度(小时)
83+ if ($ i < 10 ) {
84+ $ font = '0 ' .$ i .':00 ' ;
85+ } else {
86+ $ font = $ i .':00 ' ;
87+ }
88+ imagefttext ($ im , 16 , 30 , $ x2 - 35 , $ y2 + 50 , $ font_color , $ this ->fontfile , $ font );
89+ }
90+
91+ // Y轴
92+ $ x1 = $ cre_left ;
93+ $ y1 = $ cre_top - $ con_line_plus ;
94+ $ x2 = $ cre_left ;
95+ $ y2 = $ coor_height + $ cre_top ;
96+ imagesetthickness ($ im , $ con_line_wh );
97+ imageline ($ im , $ x1 , $ y1 , $ x2 , $ y2 , $ line_color );
98+
99+ // Y轴 - 数值刻度
100+ $ value_num = 5 ;
101+ $ max_value = 4000 ;
102+ $ val = $ max_value / $ value_num ;
103+ $ space = $ coor_height / $ value_num ;
104+ for ($ i =0 ; $ i < $ value_num ; $ i ++) {
105+ $ x1 = $ cre_left ;
106+ $ y1 = $ cre_top + $ space * $ i ;
107+ $ x2 = $ cre_left + 10 ;
108+ $ y2 = $ cre_top + $ space * $ i ;
109+ imagesetthickness ($ im , $ con_line_wh );
110+ imageline ($ im , $ x1 , $ y1 , $ x2 , $ y2 , $ line_color );
111+
112+ // 画虚线
113+ $ x1 = $ cre_left + 20 ;
114+ $ x2 = $ coor_width + $ cre_left + 20 ;
115+ $ w = imagecolorallocate ($ im , 255 , 255 , 255 );
116+ $ red = imagecolorallocate ($ im , 0 , 0 , 0 );
117+ /* 5 个红色像素,5 个白色像素 */
118+ $ style = array ( $ red , $ red , $ red , $ red , $ red , $ w , $ w , $ w , $ w , $ w );
119+ imagesetstyle ($ im , $ style );
120+ imagesetthickness ($ im , 1 );
121+ imageline ($ im , $ x1 , $ y1 , $ x2 , $ y2 , IMG_COLOR_STYLED );
122+
123+ // Y轴 - 数值刻度数值
124+ $ font = $ val * ($ value_num - $ i );
125+ imagefttext ($ im , 16 , 0 , $ x1 - 75 , $ y1 + 5 , $ font_color , $ this ->fontfile , $ font );
126+ }
127+
128+ return $ im ;
129+ }
130+ }
131+
132+ header ("Cache-Control:max-age=1,s-maxage=1,no-cache,must-revalidate " );
133+ header ("Content-type:image/png;charset=utf8 " );
134+ $ obj = new userClass ;
135+ $ im = $ obj ->userAnalyse (1000 ,10 ,4 );
136+
137+ // 生成图片
138+ imagepng ($ im );
139+ // 销毁图片
140+ imagedestroy ($ im );
141+
142+
143+
144+
145+ ?>
0 commit comments