source: branches/feature-module-update/data/class/graph/SC_GraphPie.php @ 15635

Revision 15635, 6.9 KB checked in by nanasess, 17 years ago (diff)

未定義変数の修正

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7$SC_GRAPHPIE_DIR = realpath(dirname( __FILE__));
8require_once($SC_GRAPHPIE_DIR . "/SC_GraphBase.php");
9
10// 円グラフ生成クラス
11class SC_GraphPie extends SC_GraphBase{
12    var $cw;
13    var $ch;
14    var $cz;
15    var $cx;
16    var $cy;
17    var $arrLabel;
18    var $arrData;
19
20    // コンストラクタ
21    function SC_GraphPie($bgw = BG_WIDTH, $bgh = BG_HEIGHT, $left = PIE_LEFT, $top = PIE_TOP) {
22        parent::SC_GraphBase($bgw, $bgh, $left, $top);
23        // サイズ設定
24        $this->setSize(PIE_WIDTH, PIE_HEIGHT, PIE_THICK);
25        // 位置設定
26        $this->setPosition($this->left + ($this->cw / 2), $this->top + ($this->ch / 2));
27    }
28
29    // データを360°値に変換する
30    function getCircleData($array) {
31        $total = "";
32        $new_total = "";
33        if(!is_array($array)) {
34            return;
35        }
36        $arrRet = array();
37        foreach($array as $val) {
38            $total += $val;
39        }
40        if($total <= 0) {
41            return;
42        }
43        $rate = 360 / $total;
44        // ラベル表示用
45        $p_rate = 100 / $total;
46        $cnt = 0;
47        foreach($array as $val) {
48            $ret = round($val * $rate);
49            $new_total+= $ret;
50            $arrRet[] = $ret;
51            // パーセント表示用
52            $this->arrLabel[] = round($val * $p_rate) . " %";
53            $cnt++;
54        }
55        // 合計が360になるように補正しておく
56        $arrRet[0] -= $new_total - 360;
57        return $arrRet;
58    }
59
60    // 円の位置設定を行う
61    function setPosition($cx, $cy) {
62        $this->cx = $cx;
63        $this->cy = $cy;
64    }
65
66    // 円のサイズ設定を行う
67    function setSize($cw, $ch, $cz = 0) {
68        $this->cw = $cw;
69        $this->ch = $ch;
70        $this->cz = $cz;
71    }
72
73    // 影の描画
74    function drawShade() {
75        $move = 1;
76        for($i = ($this->cy + $this->cz); $i <= ($this->cy + $this->cz + ($this->cz * PIE_SHADE_IMPACT)); $i++) {
77            imagefilledarc($this->image, $this->cx + $move, $i, $this->cw, $this->ch, 0, 360, $this->shade_color, IMG_ARC_PIE);
78            $move += 0.5;
79        }
80    }
81
82    // データをセットする
83    function setData($arrData) {
84        $this->arrData = array_values($arrData);
85    }
86
87    // 円グラフを描画する
88    function drawGraph() {
89        $x = $this->cx;
90        $y = $this->cy;
91        $z = $this->cz;
92        $h = $this->ch;
93        $w = $this->cw;
94
95        // データの角度を取得する
96        $arrRad = $this->getCircleData($this->arrData);
97        $rd_max = count($arrRad);
98
99        // データが存在しない場合
100        if($rd_max <= 0) {
101            return;
102        }
103
104        // 影の描画
105        if($this->shade_on) {
106            $this->drawShade();
107        }
108
109        // 色数の取得
110        $c_max = count($this->arrColor);
111        $dc_max = count($this->arrDarkColor);
112
113        // 側面の描画
114        for ($i = ($y + $z - 1); $i >= $y; $i--) {
115            $start = 0;
116            for($j = 0; $j < $rd_max; $j++) {
117                // 角度が0度以上の場合のみ側面を描画する。
118                if($arrRad[$j] > 0) {
119                    $end = $start + $arrRad[$j];
120                    if($start == 0 && $end == 360) {
121                        // -90~270で指定すると円が描画できないので0~360に指定
122                        imagearc($this->image, $x, $i, $w, $h, 0, 360, $this->arrDarkColor[($j % $dc_max)]);
123                    } else {
124                        // -90°は12時の位置から開始するように補正している
125                        imagearc($this->image, $x, $i, $w, $h, $start - 90, $end - 90, $this->arrDarkColor[($j % $dc_max)]);
126                    }
127                    $start = $end;
128                }
129            }
130        }
131        // 底面の描画
132        imagearc($this->image, $x, $y + $z, $w, $h, 0, 180 , $this->flame_color);
133
134        // 上面の描画
135        $start = 0;
136        for($i = 0; $i < $rd_max; $i++) {
137            $end = $start + $arrRad[$i];
138            if($start == 0 && $end == 360) {
139                // -90~270で指定すると円が描画できないので0~360に指定
140                imagefilledarc($this->image, $x, $y, $w, $h, 0, 360, $this->arrColor[($i % $c_max)], IMG_ARC_PIE);
141            } else {
142                // -90°は12時の位置から開始するように補正している。
143                imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->arrColor[($i % $c_max)], IMG_ARC_PIE);
144            }
145            $start = $end;
146        }
147
148        // 上面の縁取り
149        $start = 0;
150        for($i = 0; $i < $rd_max; $i++) {
151            $end = $start + $arrRad[$i];
152            if($start == 0 && $end == 360) {
153                // -90~270で指定すると円が描画できないので0~360に指定
154                imagearc($this->image, $x, $y, $w, $h, 0, 360 , $this->flame_color);
155            }
156            // -90°は12時の位置から開始するように補正している。
157            imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->flame_color, IMG_ARC_EDGED|IMG_ARC_NOFILL);
158            $start = $end;
159        }
160
161        // 側面の縁取り
162        imageline($this->image, $x + ($w / 2), $y, $x + ($w / 2), $y + $z, $this->flame_color);
163        imageline($this->image, $x - ($w / 2), $y, $x - ($w / 2), $y + $z, $this->flame_color);
164        $start = 0;
165        for($i = 0; $i < $rd_max; $i++) {
166            $end = $start + $arrRad[$i];
167            // 前面のみ
168            if($end > 90 && $end < 270) {
169                list($ax, $ay) = lfGetArcPos($x, $y, $w, $h, $end);
170                // ラインのずれを補正する
171                if($end > 180) {
172                    $ax = $ax + 1;
173                }
174                imageline($this->image, $ax, $ay, $ax, $ay + $z, $this->flame_color);
175            }
176            $start = $end;
177        }
178
179        // ラベルの描画
180        $this->drawLabel($arrRad);
181        // 凡例の描画
182        $this->drawLegend(count($this->arrData));
183    }
184
185    // 円グラフのラベルを描画する
186    function drawLabel($arrRad) {
187        $rd_max = count($arrRad);
188        $start = 0;
189        for($i = 0; $i < $rd_max; $i++) {
190            $center = $start + ($arrRad[$i] / 2);
191            $end = $start + $arrRad[$i];
192            list($sx, $sy) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw / 1.5), ($this->ch / 1.5), $center);
193            list($ex, $ey) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw * 1.5), ($this->ch * 1.5), $center);
194            // 指示線の描画
195            imageline($this->image, $sx, $sy, $ex + 2, $ey - PIE_LABEL_UP, $this->flame_color);
196            $this->setText(FONT_SIZE, $ex - 10, $ey - PIE_LABEL_UP - FONT_SIZE, $this->arrLabel[$i], NULL, 0, true);
197            $start = $end;
198        }
199    }
200}
201?>
Note: See TracBrowser for help on using the repository browser.