source: branches/version-2_13_0/data/class/graph/SC_Graph_Pie.php @ 23126

Revision 23126, 7.5 KB checked in by m_uehara, 11 years ago (diff)

#2348 r23116 - r23125 をマージ

  • 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    public $cw;
28    public $ch;
29    public $cz;
30    public $cx;
31    public $cy;
32    public $arrLabel;
33    public $arrData;
34
35    // コンストラクタ
36    public 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    public 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
75        return $arrRet;
76    }
77
78    // 円の位置設定を行う
79    public function setPosition($cx, $cy)
80    {
81        $this->cx = $cx;
82        $this->cy = $cy;
83    }
84
85    // 円のサイズ設定を行う
86    public function setSize($cw, $ch, $cz = 0)
87    {
88        $this->cw = $cw;
89        $this->ch = $ch;
90        $this->cz = $cz;
91    }
92
93    // 影の描画
94    public function drawShade()
95    {
96        $move = 1;
97        for ($i = ($this->cy + $this->cz); $i <= ($this->cy + $this->cz + ($this->cz * PIE_SHADE_IMPACT)); $i++) {
98            imagefilledarc($this->image, $this->cx + $move, $i, $this->cw, $this->ch, 0, 360, $this->shade_color, IMG_ARC_PIE);
99            $move += 0.5;
100        }
101    }
102
103    // データをセットする
104    public function setData($arrData)
105    {
106        $this->arrData = array_values($arrData);
107    }
108
109    // 円グラフを描画する
110    public function drawGraph()
111    {
112        $x = $this->cx;
113        $y = $this->cy;
114        $z = $this->cz;
115        $h = $this->ch;
116        $w = $this->cw;
117
118        // データの角度を取得する
119        $arrRad = $this->getCircleData($this->arrData);
120
121        // データが存在しない場合
122        if (empty($arrRad)) {
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            foreach ($arrRad as $rad) {
139                // 角度が0度以上の場合のみ側面を描画する。
140                if ($rad > 0) {
141                    $end = $start + $rad;
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        foreach ($arrRad as $key => $rad) {
159            $end = $start + $rad;
160            // 開始・終了が同一値だと、(imagefilledarc 関数における) 0°から360°として動作するようなので、スキップする。
161            // XXX 値ラベルは別ロジックなので、実質問題を生じないと考えている。
162            if ($start == $end) {
163                continue 1;
164            }
165            // -90°は12時の位置から開始するように補正するもの。
166            // 塗りつぶし
167            imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->arrColor[($key % $c_max)], $style);
168            // FIXME 360°描画の場合、(imagefilledarc 関数における) 0°から360°として動作する。本来-90°から360°として動作すべき。
169            //       なお、360°と0°の組み合わせを考慮すると線が無いのも問題があるので、この処理をスキップする対応は不適当である。
170            // 縁取り線
171            imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->flame_color, IMG_ARC_EDGED|IMG_ARC_NOFILL);
172            $start = $end;
173        }
174
175        // 側面の縁取り
176        imageline($this->image, $x + ($w / 2), $y, $x + ($w / 2), $y + $z, $this->flame_color);
177        imageline($this->image, $x - ($w / 2), $y, $x - ($w / 2), $y + $z, $this->flame_color);
178        $start = 0;
179        foreach ($arrRad as $rad) {
180            $end = $start + $rad;
181            // 前面のみ
182            if ($end > 90 && $end < 270) {
183                list($ax, $ay) = $this->lfGetArcPos($x, $y, $w, $h, $end);
184                // ラインのずれを補正する
185                if ($end > 180) {
186                    $ax = $ax + 1;
187                }
188                imageline($this->image, $ax, $ay, $ax, $ay + $z, $this->flame_color);
189            }
190            $start = $end;
191        }
192
193        // ラベルの描画
194        $this->drawLabel($arrRad);
195        // 凡例の描画
196        $this->drawLegend(count($this->arrData));
197    }
198
199    // 円グラフのラベルを描画する
200    public function drawLabel($arrRad)
201    {
202        $start = 0;
203        foreach ($arrRad as $key => $rad) {
204            $center = $start + ($rad / 2);
205            $end = $start + $rad;
206            list($sx, $sy) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw / 1.5), ($this->ch / 1.5), $center);
207            list($ex, $ey) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw * 1.5), ($this->ch * 1.5), $center);
208            // 指示線の描画
209            imageline($this->image, $sx, $sy, $ex + 2, $ey - PIE_LABEL_UP, $this->flame_color);
210            $this->setText(FONT_SIZE, $ex - 10, $ey - PIE_LABEL_UP - FONT_SIZE, $this->arrLabel[$key], NULL, 0, true);
211            $start = $end;
212        }
213    }
214}
Note: See TracBrowser for help on using the repository browser.