source: temp/trunk/html/test/naka/class/SC_GraphPie.php @ 1694

Revision 1694, 5.1 KB checked in by naka, 20 years ago (diff)

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2
3$SC_GRAPHPIE_DIR = realpath(dirname( __FILE__));
4require_once($SC_GRAPHPIE_DIR . "/SC_GraphBase.php");   
5
6// ±ß¥°¥é¥ÕÀ¸À®¥¯¥é¥¹
7class SC_GraphPie extends SC_GraphBase{
8    var $cw;
9    var $ch;
10    var $cz;
11    var $cx;
12    var $cy;
13    var $arrLabel;
14    var $arrData;
15   
16    // ¥³¥ó¥¹¥È¥é¥¯¥¿
17    function SC_GraphPie($bgw = BG_WIDTH, $bgh = BG_HEIGHT, $left = PIE_LEFT, $top = PIE_TOP) {
18        parent::SC_GraphBase($bgw, $bgh, $left, $top);
19        // ¥µ¥¤¥ºÀßÄê
20        $this->setSize(PIE_WIDTH, PIE_HEIGHT, PIE_THICK);
21        // °ÌÃÖÀßÄê
22        $this->setPosition($this->left + ($this->cw / 2), $this->top + ($this->ch / 2));
23    }
24   
25    // ¥Ç¡¼¥¿¤ò360¡ëÃͤËÊÑ´¹¤¹¤ë
26    function getCircleData($array) {
27        if(!is_array($array)) {
28            return;
29        }
30        $arrRet = array();
31        foreach($array as $val) {
32            $total += $val;         
33        }
34        if($total <= 0) {
35            return;
36        }       
37        $rate = 360 / $total;
38        // ¥é¥Ù¥ëɽ¼¨ÍÑ
39        $p_rate = 100 / $total;
40        $cnt = 0;
41        foreach($array as $val) {
42            $ret = round($val * $rate);
43            $new_total+= $ret;
44            $arrRet[] = $ret;
45            // ¥Ñ¡¼¥»¥ó¥Èɽ¼¨ÍÑ
46            $this->arrLabel[] = round($val * $p_rate) . " %";
47            $cnt++;
48        }
49        // ¹ç·×¤¬360¤Ë¤Ê¤ë¤è¤¦¤ËÊäÀµ¤·¤Æ¤ª¤¯
50        $arrRet[0] -= $new_total - 360;
51        return $arrRet;
52    }   
53       
54    // ±ß¤Î°ÌÃÖÀßÄê¤ò¹Ô¤¦
55    function setPosition($cx, $cy) {
56        $this->cx = $cx;
57        $this->cy = $cy;
58    }
59       
60    // ±ß¤Î¥µ¥¤¥ºÀßÄê¤ò¹Ô¤¦
61    function setSize($cw, $ch, $cz = 0) {
62        $this->cw = $cw;
63        $this->ch = $ch;
64        $this->cz = $cz;
65    }
66   
67    // ±Æ¤ÎÉÁ²è
68    function drawShade() {
69        $move = 1;
70        for($i = ($this->cy + $this->cz); $i <= ($this->cy + $this->cz + ($this->cz * PIE_SHADE_IMPACT)); $i++) {
71            imagefilledarc($this->image, $this->cx + $move, $i, $this->cw, $this->ch, 0, 360, $this->shade_color, IMG_ARC_PIE);
72            $move += 0.5;
73        }
74    }
75   
76    // ¥Ç¡¼¥¿¤ò¥»¥Ã¥È¤¹¤ë
77    function setData($arrData) {
78        $this->arrData = array_values($arrData);
79    }
80   
81    // ±ß¥°¥é¥Õ¤òÉÁ²è¤¹¤ë
82    function drawGraph() {
83        $x = $this->cx;
84        $y = $this->cy;
85        $z = $this->cz;
86        $h = $this->ch;
87        $w = $this->cw;
88       
89        $arrRad = $this->getCircleData($this->arrData);
90        $rd_max = count($arrRad);
91       
92        // ¥Ç¡¼¥¿¤¬Â¸ºß¤·¤Ê¤¤¾ì¹ç
93        if($rd_max <= 0) {
94            return;
95        }
96       
97        // ±Æ¤ÎÉÁ²è
98        if($this->shade_on) {
99            $this->drawShade();
100        }
101           
102        // ¿§¿ô¤Î¼èÆÀ
103        $c_max = count($this->arrColor);
104        $dc_max = count($this->arrDarkColor);
105       
106        // ¦Ì̤ÎÉÁ²è       
107        for ($i = ($y + $z - 1); $i >= $y; $i--) {
108            $start = 0;
109            for($j = 0; $j < $rd_max; $j++) {
110                $end = $start + $arrRad[$j];
111                if($start == 0 && $end == 360) {
112                    // -90¢·270¤Ç»ØÄꤹ¤ë¤È±ß¤¬ÉÁ²è¤Ç¤­¤Ê¤¤¤Î¤Ç0¢·360¤Ë»ØÄê
113                    imagearc($this->image, $x, $i, $w, $h, 0, 360, $this->arrDarkColor[($j % $dc_max)]);
114                } else {
115                    // -90¡ë¤Ï12»þ¤Î°ÌÃÖ¤«¤é³«»Ï¤¹¤ë¤è¤¦¤ËÊäÀµ¤·¤Æ¤¤¤ë
116                    imagearc($this->image, $x, $i, $w, $h, $start - 90, $end - 90, $this->arrDarkColor[($j % $dc_max)]);   
117                }           
118                $start = $end;
119            }
120        }
121        // ÄìÌ̤ÎÉÁ²è
122        imagearc($this->image, $x, $y + $z, $w, $h, 0, 180 , $this->flame_color);
123
124        // ¾åÌ̤ÎÉÁ²è
125        $start = 0;
126        for($i = 0; $i < $rd_max; $i++) {
127            $end = $start + $arrRad[$i];
128            if($start == 0 && $end == 360) {
129                // -90¢·270¤Ç»ØÄꤹ¤ë¤È±ß¤¬ÉÁ²è¤Ç¤­¤Ê¤¤¤Î¤Ç0¢·360¤Ë»ØÄê
130                imagefilledarc($this->image, $x, $y, $w, $h, 0, 360, $this->arrColor[($i % $c_max)], IMG_ARC_PIE);         
131            } else {
132                // -90¡ë¤Ï12»þ¤Î°ÌÃÖ¤«¤é³«»Ï¤¹¤ë¤è¤¦¤ËÊäÀµ¤·¤Æ¤¤¤ë¡£       
133                imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->arrColor[($i % $c_max)], IMG_ARC_PIE);
134            }
135            $start = $end;
136        }
137
138        // ¾åÌ̤αï¼è¤ê
139        $start = 0;
140        for($i = 0; $i < $rd_max; $i++) {
141            $end = $start + $arrRad[$i];
142            if($start == 0 && $end == 360) {
143                // -90¢·270¤Ç»ØÄꤹ¤ë¤È±ß¤¬ÉÁ²è¤Ç¤­¤Ê¤¤¤Î¤Ç0¢·360¤Ë»ØÄê
144                imagearc($this->image, $x, $y, $w, $h, 0, 360 , $this->flame_color);
145            }
146            // -90¡ë¤Ï12»þ¤Î°ÌÃÖ¤«¤é³«»Ï¤¹¤ë¤è¤¦¤ËÊäÀµ¤·¤Æ¤¤¤ë¡£
147            imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->flame_color, IMG_ARC_EDGED|IMG_ARC_NOFILL);
148            $start = $end;
149        }
150
151        // ¦Ì̤αï¼è¤ê
152        imageline($this->image, $x + ($w / 2), $y, $x + ($w / 2), $y + $z, $this->flame_color);
153        imageline($this->image, $x - ($w / 2), $y, $x - ($w / 2), $y + $z, $this->flame_color);
154        $start = 0;
155        for($i = 0; $i < $rd_max; $i++) {
156            $end = $start + $arrRad[$i];
157            // Á°Ì̤Τß
158            if($end > 90 && $end < 270) {
159                list($ax, $ay) = lfGetArcPos($x, $y, $w, $h, $end);
160                // ¥é¥¤¥ó¤Î¤º¤ì¤òÊäÀµ¤¹¤ë
161                if($end > 180) {
162                    $ax = $ax + 1;
163                }
164                imageline($this->image, $ax, $ay, $ax, $ay + $z, $this->flame_color);
165            }
166            $start = $end; 
167        }
168               
169        // ¥é¥Ù¥ë¤ÎÉÁ²è
170        $this->drawLabel($arrRad);
171        // ËÞÎã¤ÎÉÁ²è
172        $this->drawLegend(count($this->arrData));           
173    }
174   
175    // ±ß¥°¥é¥Õ¤Î¥é¥Ù¥ë¤òÉÁ²è¤¹¤ë
176    function drawLabel($arrRad) {
177        $rd_max = count($arrRad);
178        $start = 0;
179        for($i = 0; $i < $rd_max; $i++) {
180            $center = $start + ($arrRad[$i] / 2);
181            $end = $start + $arrRad[$i];
182            list($sx, $sy) = lfGetArcPos($this->cx, $this->cy, ($this->cw / 1.5), ($this->ch / 1.5), $center);
183            list($ex, $ey) = lfGetArcPos($this->cx, $this->cy, ($this->cw * 1.5), ($this->ch * 1.5), $center);
184            // »Ø¼¨Àþ¤ÎÉÁ²è
185            imageline($this->image, $sx, $sy, $ex + 2, $ey - PIE_LABEL_UP, $this->flame_color);
186            $this->setText(FONT_SIZE, $ex - 10, $ey - PIE_LABEL_UP - FONT_SIZE, $this->arrLabel[$i], NULL, 0, true);
187            $start = $end;
188        }
189    }   
190}
191
192?>
Note: See TracBrowser for help on using the repository browser.