source: branches/version-2_12-dev/data/class/graph/SC_Graph_Pie.php @ 22567

Revision 22567, 7.6 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24// 円グラフ生成クラス
25class SC_Graph_Pie extends SC_Graph_Base_Ex
26{
27    var $cw;
28    var $ch;
29    var $cz;
30    var $cx;
31    var $cy;
32    var $arrLabel;
33    var $arrData;
34
35    // コンストラクタ
36    function __construct($bgw = BG_WIDTH, $bgh = BG_HEIGHT, $left = PIE_LEFT, $top = PIE_TOP)
37    {
38        parent::__construct($bgw, $bgh, $left, $top);
39        // サイズ設定
40        $this->setSize(PIE_WIDTH, PIE_HEIGHT, PIE_THICK);
41        // 位置設定
42        $this->setPosition($this->left + ($this->cw / 2), $this->top + ($this->ch / 2));
43    }
44
45    // データを360°値に変換する
46    function getCircleData($array)
47    {
48        $total = '';
49        $new_total = '';
50        if (!is_array($array)) {
51            return;
52        }
53        $arrRet = array();
54        foreach ($array as $val) {
55            $total += $val;
56        }
57        if ($total <= 0) {
58            return;
59        }
60        $rate = 360 / $total;
61        // ラベル表示用
62        $p_rate = 100 / $total;
63        $cnt = 0;
64        foreach ($array as $val) {
65            $ret = round($val * $rate);
66            $new_total+= $ret;
67            $arrRet[] = $ret;
68            // パーセント表示用
69            $this->arrLabel[] = round($val * $p_rate) . ' %';
70            $cnt++;
71        }
72        // 合計が360になるように補正しておく
73        $arrRet[0] -= $new_total - 360;
74        return $arrRet;
75    }
76
77    // 円の位置設定を行う
78    function setPosition($cx, $cy)
79    {
80        $this->cx = $cx;
81        $this->cy = $cy;
82    }
83
84    // 円のサイズ設定を行う
85    function setSize($cw, $ch, $cz = 0)
86    {
87        $this->cw = $cw;
88        $this->ch = $ch;
89        $this->cz = $cz;
90    }
91
92    // 影の描画
93    function drawShade()
94    {
95        $move = 1;
96        for ($i = ($this->cy + $this->cz); $i <= ($this->cy + $this->cz + ($this->cz * PIE_SHADE_IMPACT)); $i++) {
97            imagefilledarc($this->image, $this->cx + $move, $i, $this->cw, $this->ch, 0, 360, $this->shade_color, IMG_ARC_PIE);
98            $move += 0.5;
99        }
100    }
101
102    // データをセットする
103    function setData($arrData)
104    {
105        $this->arrData = array_values($arrData);
106    }
107
108    // 円グラフを描画する
109    function drawGraph()
110    {
111        $x = $this->cx;
112        $y = $this->cy;
113        $z = $this->cz;
114        $h = $this->ch;
115        $w = $this->cw;
116
117        // データの角度を取得する
118        $arrRad = $this->getCircleData($this->arrData);
119        $rd_max = count($arrRad);
120
121        // データが存在しない場合
122        if ($rd_max <= 0) {
123            return;
124        }
125
126        // 影の描画
127        if ($this->shade_on) {
128            $this->drawShade();
129        }
130
131        // 色数の取得
132        $c_max = count($this->arrColor);
133        $dc_max = count($this->arrDarkColor);
134
135        // 側面の描画
136        for ($i = ($y + $z - 1); $i >= $y; $i--) {
137            $start = 0;
138            for ($j = 0; $j < $rd_max; $j++) {
139                // 角度が0度以上の場合のみ側面を描画する。
140                if ($arrRad[$j] > 0) {
141                    $end = $start + $arrRad[$j];
142                    if ($start == 0 && $end == 360) {
143                        // -90~270で指定すると円が描画できないので0~360に指定
144                        imagearc($this->image, $x, $i, $w, $h, 0, 360, $this->arrDarkColor[($j % $dc_max)]);
145                    } else {
146                        // -90°は12時の位置から開始するように補正している
147                        imagearc($this->image, $x, $i, $w, $h, $start - 90, $end - 90, $this->arrDarkColor[($j % $dc_max)]);
148                    }
149                    $start = $end;
150                }
151            }
152        }
153        // 底面の描画
154        imagearc($this->image, $x, $y + $z, $w, $h, 0, 180 , $this->flame_color);
155
156        // 上面の描画
157        $start = 0;
158        for ($i = 0; $i < $rd_max; $i++) {
159            $end = $start + $arrRad[$i];
160            if ($start == 0 && $end == 360) {
161                // -90~270で指定すると円が描画できないので0~360に指定
162                imagefilledarc($this->image, $x, $y, $w, $h, 0, 360, $this->arrColor[($i % $c_max)], IMG_ARC_PIE);
163            } else {
164                // -90°は12時の位置から開始するように補正している。
165                imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->arrColor[($i % $c_max)], IMG_ARC_PIE);
166            }
167            $start = $end;
168        }
169
170        // 上面の縁取り
171        $start = 0;
172        for ($i = 0; $i < $rd_max; $i++) {
173            $end = $start + $arrRad[$i];
174            if ($start == 0 && $end == 360) {
175                // -90~270で指定すると円が描画できないので0~360に指定
176                imagearc($this->image, $x, $y, $w, $h, 0, 360 , $this->flame_color);
177            }
178            // -90°は12時の位置から開始するように補正している。
179            imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->flame_color, IMG_ARC_EDGED|IMG_ARC_NOFILL);
180            $start = $end;
181        }
182
183        // 側面の縁取り
184        imageline($this->image, $x + ($w / 2), $y, $x + ($w / 2), $y + $z, $this->flame_color);
185        imageline($this->image, $x - ($w / 2), $y, $x - ($w / 2), $y + $z, $this->flame_color);
186        $start = 0;
187        for ($i = 0; $i < $rd_max; $i++) {
188            $end = $start + $arrRad[$i];
189            // 前面のみ
190            if ($end > 90 && $end < 270) {
191                list($ax, $ay) = $this->lfGetArcPos($x, $y, $w, $h, $end);
192                // ラインのずれを補正する
193                if ($end > 180) {
194                    $ax = $ax + 1;
195                }
196                imageline($this->image, $ax, $ay, $ax, $ay + $z, $this->flame_color);
197            }
198            $start = $end;
199        }
200
201        // ラベルの描画
202        $this->drawLabel($arrRad);
203        // 凡例の描画
204        $this->drawLegend(count($this->arrData));
205    }
206
207    // 円グラフのラベルを描画する
208    function drawLabel($arrRad)
209    {
210        $rd_max = count($arrRad);
211        $start = 0;
212        for ($i = 0; $i < $rd_max; $i++) {
213            $center = $start + ($arrRad[$i] / 2);
214            $end = $start + $arrRad[$i];
215            list($sx, $sy) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw / 1.5), ($this->ch / 1.5), $center);
216            list($ex, $ey) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw * 1.5), ($this->ch * 1.5), $center);
217            // 指示線の描画
218            imageline($this->image, $sx, $sy, $ex + 2, $ey - PIE_LABEL_UP, $this->flame_color);
219            $this->setText(FONT_SIZE, $ex - 10, $ey - PIE_LABEL_UP - FONT_SIZE, $this->arrLabel[$i], NULL, 0, true);
220            $start = $end;
221        }
222    }
223}
Note: See TracBrowser for help on using the repository browser.