source: branches/dev/html/admin/total/class/SC_GraphBase.php @ 167

Revision 167, 6.7 KB checked in by naka, 19 years ago (diff)
Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7/*
8$SC_GRAPHPIE_DIR = realpath(dirname( __FILE__));
9require_once($SC_GRAPHPIE_DIR . "/config.php");
10require_once($SC_GRAPHPIE_DIR . "/lib.php");   
11*/
12require_once(realpath(dirname( __FILE__)) . "/config.php");
13require_once(realpath(dirname( __FILE__)) . "/lib.php");   
14
15// SC_Graph¶¦ÄÌ¥¯¥é¥¹
16class SC_GraphBase {
17    var $arrRGB;
18    var $arrColor;
19    var $arrDarkColor;
20    var $image;
21    var $left;
22    var $top;
23    var $shade_color;
24    var $flame_color;
25    var $shade_on;
26    var $text_color;
27    var $labelbg_color;
28    var $bgw;
29    var $bgh;
30    var $clabelbg_color;
31    var $title_color;
32    var $text_top;
33    var $mark_color;
34    var $arrLegend;
35   
36    // ¥³¥ó¥¹¥È¥é¥¯¥¿
37    function SC_GraphBase($bgw = BG_WIDTH, $bgh = BG_HEIGHT, $left, $top) {
38        global $ARR_GRAPH_RGB;
39        global $ARR_BG_COLOR;
40        global $ARR_SHADE_COLOR;
41        global $ARR_FLAME_COLOR;
42        global $ARR_TEXT_COLOR;
43        global $ARR_LABELBG_COLOR;
44        global $ARR_LEGENDBG_COLOR;
45        global $ARR_TITLE_COLOR;
46        global $ARR_GRID_COLOR;
47       
48        // ²èÁüºîÀ®
49        $this->bgw = $bgw;
50        $this->bgh = $bgh; 
51        $this->image = imagecreatetruecolor($bgw, $bgh);
52        // ¥¢¥ó¥Á¥¨¥¤¥ê¥¢¥¹Í­¸ú
53        if (function_exists("imageantialias")) imageantialias($this->image, true);
54        // ÇØ·Ê¿§¤ò¥»¥Ã¥È
55        imagefill($this->image, 0, 0, lfGetImageColor($this->image, $ARR_BG_COLOR));
56       
57        // »ÈÍÑ¿§¤ÎÀ¸À®
58        $this->setColorList($ARR_GRAPH_RGB);
59        // ¥°¥é¥ÕÉÁ²è°ÌÃÖ¤ÎÀßÄê
60        $this->left = $left;
61        $this->top = $top;
62        $this->shade_color = lfGetImageColor($this->image, $ARR_SHADE_COLOR);
63        $this->flame_color = lfGetImageColor($this->image, $ARR_FLAME_COLOR);
64        $this->text_color = lfGetImageColor($this->image, $ARR_TEXT_COLOR);
65        $this->labelbg_color = lfGetImageColor($this->image, $ARR_LABELBG_COLOR);
66        $this->clabelbg_color = lfGetImageColor($this->image, $ARR_LEGENDBG_COLOR);
67        $this->title_color = lfGetImageColor($this->image, $ARR_TITLE_COLOR);
68        $this->grid_color = lfGetImageColor($this->image, $ARR_GRID_COLOR);
69           
70        // ±Æ¤¢¤ê
71        $this->shade_on = true;
72    }
73   
74    // ¥ê¥µ¥ó¥×¥ë
75    function resampled() {
76        $tmp_image = imagecreatetruecolor($this->bgw, $this->bgh);
77        if(imagecopyresampled($tmp_image, $this->image, 0, 0, $this->bgw, $this->bgh, $this->bgw, $this->bgh)) {
78            $this->image = $tmp_image;
79        }
80    }
81   
82   
83    // ¥ª¥Ö¥¸¥§¥¯¥È¥«¥é¡¼¤ÎÀßÄê
84    function setColorList($arrRGB) {
85        $this->arrRGB = $arrRGB;
86        $count = count($this->arrRGB);
87        // Ä̾￧¤ÎÀßÄê
88        for($i = 0; $i < $count; $i++) {
89            $this->arrColor[$i] = lfGetImageColor($this->image, $this->arrRGB[$i]);
90        }
91        // °Å¿§¤ÎÀßÄê
92        for($i = 0; $i < $count; $i++) {
93            $this->arrDarkColor[$i] = lfGetImageDarkColor($this->image, $this->arrRGB[$i]);
94        }       
95    }
96   
97    // ±Æ¤Î¤¢¤ê¤Ê¤·
98    function setShadeOn($shade_on) {
99        $this->shade_on = $shade_on;
100    }
101   
102    // ²èÁü¤ò½ÐÎϤ¹¤ë
103    function outputGraph($header = true, $filename = "") {
104        if($header) {
105            header('Content-type: image/png');
106        }
107       
108        if ($filename != "") {
109            imagepng($this->image, $filename);
110        }else{
111            imagepng($this->image);
112        }
113
114        imagedestroy($this->image);
115    }
116
117    // ÉÁ²è»þ¤Î¥Æ¥­¥¹¥ÈÉý¤òµá¤á¤ë
118    function getTextWidth($text, $font_size) {
119        $text_len = strlen($text);
120        $ret = $font_size * $text_len * TEXT_RATE; 
121        /*
122            ¢¨Àµ³Î¤ÊÃͤ¬¼èÆÀ¤Ç¤­¤Ê¤«¤Ã¤¿¤Î¤ÇÇÑ»ß
123            // ¥Æ¥­¥¹¥ÈÉý¤Î¼èÆÀ
124            $arrPos = imagettfbbox($font_size, 0, FONT_PATH, $text);
125            $ret = $arrPos[2] - $arrPos[0];
126        */
127        return $ret;
128    }
129   
130    // ¥Æ¥­¥¹¥È¤ò½ÐÎϤ¹¤ë
131    function setText($font_size, $left, $top, $text, $color = NULL, $angle = 0, $labelbg = false) {
132        // »þ·×²ó¤ê¤Ë³ÑÅÙ¤òÊѹ¹
133        $angle = -$angle;       
134        // ¥é¥Ù¥ëÇØ·Ê
135        if($labelbg) {
136            $text_width = $this->getTextWidth($text, $font_size);
137            imagefilledrectangle($this->image, $left - 2, $top - 2, $left + $text_width + 2, $top + $font_size + 2, $this->labelbg_color);
138        }
139        //$text = mb_convert_encoding($text, "UTF-8", CHAR_CODE);
140        $text = mb_convert_encoding($text, CHAR_CODE);
141        if($color != NULL) {
142            ImageTTFText($this->image, $font_size, $angle, $left, $top + $font_size, $color, FONT_PATH, $text);
143        } else {
144            ImageTTFText($this->image, $font_size, $angle, $left, $top + $font_size, $this->text_color, FONT_PATH, $text);         
145        }
146    }
147   
148    // ¥¿¥¤¥È¥ë¤ò½ÐÎϤ¹¤ë
149    function drawTitle($text, $font_size = TITLE_FONT_SIZE) {
150        // ½ÐÎϰÌÃ֤λ»½Ð
151        $text_width = $this->getTextWidth($text, $font_size);
152        $left = ($this->bgw - $text_width) / 2;
153        $top = TITLE_TOP;
154        $this->setText($font_size, $left, $top, $text, $this->title_color);     
155    }
156   
157    // ¥í¥°¤ò½ÐÎϤ¹¤ë
158    function debugPrint($text) {
159        $text = mb_convert_encoding($text, "UTF-8", CHAR_CODE);
160        if(!isset($this->text_top)) {
161            $this->text_top = FONT_SIZE + LINE_PAD;
162        }       
163        // ¥Æ¥­¥¹¥ÈÉÁ²è
164        ImageTTFText($this->image, FONT_SIZE, 0, LINE_PAD, $this->text_top, $this->text_color, FONT_PATH, $text);
165        $this->text_top += FONT_SIZE + LINE_PAD;
166    }
167       
168    // ¥«¥é¡¼¥é¥Ù¥ë¤òÉÁ²è
169    function drawLegend($legend_max = "", $clabelbg = true) {
170        // ËÞÎ㤬ÅÐÏ¿¤µ¤ì¤Æ¤¤¤Ê¤±¤ì¤ÐÃæ»ß
171        if(count($this->arrLegend) <= 0) {
172            return;
173        }       
174       
175        if($legend_max != "") {
176            $label_max = $legend_max;
177        } else {
178            $label_max = count($this->arrLegend);
179        }
180
181        $height_max = 0;
182        $text_max = 0;
183        $width_max = 0;
184       
185        // °ìÈÖʸ»ú¿ô¤¬Â¿¤¤¤â¤Î¤ò¼èÆÀ
186        for($i = 0; $i < $label_max; $i++) {
187            $text_len = strlen($this->arrLegend[$i]);
188            if($text_max < $text_len) {
189                $text_max = $text_len;
190            }
191            $height_max += FONT_SIZE + LINE_PAD;
192        }
193        $width_max = FONT_SIZE * $text_max * TEXT_RATE;     
194
195        //  ¥«¥é¡¼¥¢¥¤¥³¥ó¤Èʸ»ú´Ö¤ò´Þ¤á¤¿Éý
196        $width_max += FONT_SIZE + (LINE_PAD * 2);   
197        $left = $this->bgw - $width_max - LEGEND_RIGHT;
198        $top = LEGEND_TOP;
199        // ¥«¥é¡¼¥é¥Ù¥ëÇØ·Ê¤ÎÉÁ²è
200        if($clabelbg) {
201            $this->drawClabelBG($left - LINE_PAD, $top, $left + $width_max, $top + $height_max + LINE_PAD);
202        }
203        $top += LINE_PAD;
204               
205        // ¿§¿ô¤Î¼èÆÀ
206        $c_max = count($this->arrColor);
207        for($i = 0; $i < $label_max; $i++) {           
208            // ¥«¥é¡¼¥¢¥¤¥³¥ó¤Îɽ¼¨
209            imagerectangle($this->image, $left, $top, $left + FONT_SIZE, $top + FONT_SIZE, $this->flame_color);
210            imagefilledrectangle($this->image, $left + 1, $top + 1, $left + FONT_SIZE - 1, $top + FONT_SIZE - 1, $this->arrColor[($i % $c_max)]);
211            // ¥é¥Ù¥ë¤Îɽ¼¨
212            $this->setText(FONT_SIZE, $left + FONT_SIZE + LINE_PAD, $top, $this->arrLegend[$i]);
213            $top += FONT_SIZE + LINE_PAD;
214        }
215    }
216   
217    // ¥«¥é¡¼¥é¥Ù¥ëÇØ·Ê¤ÎÉÁ²è
218    function drawClabelBG($left, $top, $right, $bottom) {
219        // ±Æ¤ÎÉÁ²è
220        if($this->shade_on) {
221            imagefilledrectangle($this->image, $left + 2, $top + 2, $right + 2, $bottom + 2, $this->shade_color);
222        }
223        // ¥«¥é¡¼¥é¥Ù¥ëÇØ·Ê¤ÎÉÁ²è
224        imagefilledrectangle($this->image, $left, $top, $right, $bottom, $this->clabelbg_color);
225        imagerectangle($this->image, $left, $top, $right, $bottom, $this->flame_color);
226    }
227   
228    // ËÞÎã¤ò¥»¥Ã¥È¤¹¤ë
229    function setLegend($arrLegend) {
230        $this->arrLegend = array_values((array)$arrLegend);
231    }
232
233}
234
235?>
Note: See TracBrowser for help on using the repository browser.