Graph
[ class tree: Graph ] [ index: Graph ] [ all elements ]

Source for file SC_GraphBase.php

Documentation is available at SC_GraphBase.php

  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) 2000-2011 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. /** TTFフォントファイル */
  25. define("FONT_REALFILE"DATA_REALDIR "fonts/wlmaru20044.ttf");
  26.  
  27. /** フォントサイズ */
  28. define("FONT_SIZE"8);
  29.  
  30. /** タイトルフォントサイズ */
  31. define("TITLE_FONT_SIZE"11);
  32.  
  33. /** 背景幅 */
  34. define("BG_WIDTH"720);
  35.  
  36. /** 背景高さ */
  37. define("BG_HEIGHT"400);
  38.  
  39. /** 行間 */
  40. define("LINE_PAD"5);
  41.  
  42. /** フォント補正値(実際の描画幅/フォントサイズ) */
  43. define("TEXT_RATE"0.75);
  44.  
  45. // -----------------------------------------------------------------------------
  46. // 円グラフ
  47. // -----------------------------------------------------------------------------
  48. /** 円グラフ位置 */
  49. define("PIE_LEFT"200);
  50.  
  51. /** 円グラフ位置 */
  52. define("PIE_TOP"150);
  53.  
  54. /** 円グラフ幅 */
  55. define("PIE_WIDTH"230);
  56.  
  57. /** 円グラフ高さ */
  58. define("PIE_HEIGHT"100);
  59.  
  60. /** 円グラフ太さ */
  61. define("PIE_THICK"30);
  62.  
  63. /** 円グラフのラベル位置を上にあげる */
  64. define("PIE_LABEL_UP"20);
  65.  
  66. /** 値が大きいほど影が長くなる */
  67. define("PIE_SHADE_IMPACT"0.1);
  68.  
  69. // -----------------------------------------------------------------------------
  70. // 折れ線グラフ
  71. // -----------------------------------------------------------------------------
  72. /** Y軸の目盛り数 */
  73. define("LINE_Y_SCALE"10);
  74.  
  75. /** X軸の目盛り数 */
  76. define("LINE_X_SCALE"10);
  77.  
  78. /** 線グラフ位置 */
  79. define("LINE_LEFT"60);
  80.  
  81. /** 線グラフ位置 */
  82. define("LINE_TOP"50);
  83.  
  84. /** 線グラフ背景のサイズ */
  85. define("LINE_AREA_WIDTH"600);
  86.  
  87. /** 線グラフ背景のサイズ */
  88. define("LINE_AREA_HEIGHT"300);
  89.  
  90. /** 線グラフマークのサイズ */
  91. define("LINE_MARK_SIZE"6);
  92.  
  93. /** 目盛り幅 */
  94. define("LINE_SCALE_SIZE"6);
  95.  
  96. /** X軸のラベルの表示制限数 */
  97. define("LINE_XLABEL_MAX"30);
  98.  
  99. /** X軸のタイトルと軸の間隔 */
  100. define("LINE_XTITLE_PAD"-5);
  101.  
  102. /** Y軸のタイトルと軸の間隔 */
  103. define("LINE_YTITLE_PAD"15);
  104.  
  105. // -----------------------------------------------------------------------------
  106. //  棒グラフ
  107. // -----------------------------------------------------------------------------
  108. /** グラフと目盛りの間隔 */
  109. define("BAR_PAD"6);
  110.  
  111. // -----------------------------------------------------------------------------
  112. //  タイトルラベル
  113. // -----------------------------------------------------------------------------
  114. /** 背景枠との上幅 */
  115. define("TITLE_TOP"10);
  116.  
  117. // -----------------------------------------------------------------------------
  118. //  凡例
  119. // -----------------------------------------------------------------------------
  120. /** 背景枠との上幅 */
  121. define("LEGEND_TOP"10);
  122.  
  123. /** 背景枠との右幅 */
  124. define("LEGEND_RIGHT"10);
  125.  
  126. /**
  127.  * SC_Graph共通クラス.
  128.  *
  129.  * @package Graph
  130.  * @author LOCKON CO.,LTD.
  131.  * @version $Id: SC_GraphBase.php 20764 2011-03-22 06:26:40Z nanasess $
  132.  */
  133. class SC_GraphBase {
  134.  
  135.     // {{{ properties
  136.  
  137.     var $arrRGB;
  138.     var $arrColor;
  139.     var $arrDarkColor;
  140.     var $image;
  141.     var $left;
  142.     var $top;
  143.     var $shade_color;
  144.     var $flame_color;
  145.     var $shade_on;
  146.     var $text_color;
  147.     var $labelbg_color;
  148.     var $bgw;
  149.     var $bgh;
  150.     var $clabelbg_color;
  151.     var $title_color;
  152.     var $text_top;
  153.     var $mark_color;
  154.     var $arrLegend;
  155.  
  156.     /** グラフ背景 */
  157.     var $ARR_GRAPH_RGB;
  158.  
  159.     /** 背景色 */
  160.     var $ARR_BG_COLOR;
  161.  
  162.     /** 影の色 */
  163.     var $ARR_SHADE_COLOR;
  164.  
  165.     /** 縁の色 */
  166.     var $ARR_FLAME_COLOR;
  167.  
  168.     /** 文字色 */
  169.     var $ARR_TEXT_COLOR;
  170.  
  171.     /** ラベル背景 */
  172.     var $ARR_LABELBG_COLOR;
  173.  
  174.     /** 凡例背景 */
  175.     var $ARR_LEGENDBG_COLOR;
  176.  
  177.     /** タイトル文字色 */
  178.     var $ARR_TITLE_COLOR;
  179.  
  180.     /** グリッド線色 */
  181.     var $ARR_GRID_COLOR;
  182.  
  183.     // コンストラクタ
  184.     function SC_GraphBase($bgw$bgh$left$top{
  185.         $this->init();
  186.         // 画像作成
  187.         $this->bgw = $bgw;
  188.         $this->bgh = $bgh;
  189.         $this->image = imagecreatetruecolor($bgw$bgh);
  190.         // アンチエイリアス有効
  191.         if (function_exists('imageantialias')) imageantialias($this->imagetrue);
  192.         // 背景色をセット
  193.         imagefill($this->image00$this->lfGetImageColor($this->image$this->ARR_BG_COLOR));
  194.  
  195.         // 使用色の生成
  196.         $this->setColorList($this->ARR_GRAPH_RGB);
  197.         // グラフ描画位置の設定
  198.         $this->left = $left;
  199.         $this->top = $top;
  200.         $this->shade_color = $this->lfGetImageColor($this->image$this->ARR_SHADE_COLOR);
  201.         $this->flame_color = $this->lfGetImageColor($this->image$this->ARR_FLAME_COLOR);
  202.         $this->text_color = $this->lfGetImageColor($this->image$this->ARR_TEXT_COLOR);
  203.         $this->labelbg_color = $this->lfGetImageColor($this->image$this->ARR_LABELBG_COLOR);
  204.         $this->clabelbg_color = $this->lfGetImageColor($this->image$this->ARR_LEGENDBG_COLOR);
  205.         $this->title_color = $this->lfGetImageColor($this->image$this->ARR_TITLE_COLOR);
  206.         $this->grid_color $this->lfGetImageColor($this->image$this->ARR_GRID_COLOR);
  207.  
  208.         // 影あり
  209.         $this->shade_on = true;
  210.     }
  211.  
  212.     // リサンプル(画像を滑らかに縮小する)
  213.     function resampled({
  214.         $new_width $this->bgw * 0.8;
  215.         $new_height $this->bgh * 0.8;
  216.         $tmp_image imagecreatetruecolor($new_width$new_height);
  217.         if(imagecopyresampled($tmp_image$this->image0000$new_width$new_height$this->bgw$this->bgh)) {
  218.             $this->image = $tmp_image;
  219.         }
  220.     }
  221.  
  222.     // オブジェクトカラーの設定
  223.     function setColorList($arrRGB{
  224.         $this->arrRGB = $arrRGB;
  225.         $count count($this->arrRGB);
  226.         // 通常色の設定
  227.         for($i 0$i $count$i++{
  228.             $this->arrColor[$i$this->lfGetImageColor($this->image$this->arrRGB[$i]);
  229.         }
  230.         // 暗色の設定
  231.         for($i 0$i $count$i++{
  232.             $this->arrDarkColor[$i$this->lfGetImageDarkColor($this->image$this->arrRGB[$i]);
  233.         }
  234.     }
  235.  
  236.     // 影のありなし
  237.     function setShadeOn($shade_on{
  238.         $this->shade_on = $shade_on;
  239.     }
  240.  
  241.     // 画像を出力する
  242.     function outputGraph($header true$filename ""{
  243.         if($header{
  244.             header('Content-type: image/png');
  245.         }
  246.  
  247.         if ($filename != ""{
  248.             imagepng($this->image$filename);
  249.         }else{
  250.             imagepng($this->image);
  251.         }
  252.  
  253.         imagedestroy($this->image);
  254.     }
  255.  
  256.     // 描画時のテキスト幅を求める
  257.     function getTextWidth($text$font_size{
  258.         $text_len strlen($text);
  259.         $ret $font_size $text_len TEXT_RATE;
  260.         /*
  261.             ※正確な値が取得できなかったので廃止
  262.             // テキスト幅の取得
  263.             $arrPos = imagettfbbox($font_size, 0, FONT_REALFILE, $text);
  264.             $ret = $arrPos[2] - $arrPos[0];
  265.         */
  266.         return $ret;
  267.     }
  268.  
  269.     // テキストを出力する
  270.     function setText($font_size$left$top$text$color NULL$angle 0$labelbg false{
  271.         // 時計回りに角度を変更
  272.         $angle = -$angle;
  273.         // ラベル背景
  274.         if($labelbg{
  275.             $text_width $this->getTextWidth($text$font_size);
  276.             imagefilledrectangle($this->image$left 2$top 2$left $text_width 2$top $font_size 2$this->labelbg_color);
  277.         }
  278.         /*
  279.          * XXX EUC-JP にしないと Warning がでる.
  280.          *     --enable-gd-jis-conv も関係していそうだが, このオプションを
  281.          *     つけなくても出る.
  282.          *
  283.          *     Warning: imagettftext() [function.imagettftext]:
  284.          *     any2eucjp(): something happen in
  285.          *
  286.          *     PHP Bugs: #42218
  287.          *
  288.          *     http://www.php.net/imagettftext を見ると, UTF-8 にしろと
  289.          *     書いてあるのに...
  290.          *
  291.          */
  292.         $text mb_convert_encoding($text"EUC-JP"CHAR_CODE);
  293.         //$text = mb_convert_encoding($text, CHAR_CODE);
  294.         if($color != NULL{
  295.             ImageTTFText($this->image$font_size$angle$left$top $font_size$colorFONT_REALFILE$text);
  296.         else {
  297.             ImageTTFText($this->image$font_size$angle$left$top $font_size$this->text_colorFONT_REALFILE$text);
  298.         }
  299.     }
  300.  
  301.     // タイトルを出力する
  302.     function drawTitle($text$font_size TITLE_FONT_SIZE{
  303.         // 出力位置の算出
  304.         $text_width $this->getTextWidth($text$font_size);
  305.         $left ($this->bgw - $text_width2;
  306.         $top TITLE_TOP;
  307.         $this->setText($font_size$left$top$text$this->title_color);
  308.     }
  309.  
  310.     // ログを出力する
  311.     function debugPrint($text{
  312.         $text mb_convert_encoding($text"UTF-8"CHAR_CODE);
  313.         if(!isset($this->text_top)) {
  314.             $this->text_top = FONT_SIZE LINE_PAD;
  315.         }
  316.         // テキスト描画
  317.         ImageTTFText($this->imageFONT_SIZE0LINE_PAD$this->text_top$this->text_colorFONT_REALFILE$text);
  318.         $this->text_top += FONT_SIZE LINE_PAD;
  319.     }
  320.  
  321.     // カラーラベルを描画
  322.     function drawLegend($legend_max ""$clabelbg true{
  323.         // 凡例が登録されていなければ中止
  324.         if(count($this->arrLegend<= 0{
  325.             return;
  326.         }
  327.  
  328.         if($legend_max != ""{
  329.             $label_max $legend_max;
  330.         else {
  331.             $label_max count($this->arrLegend);
  332.         }
  333.  
  334.         $height_max 0;
  335.         $text_max 0;
  336.         $width_max 0;
  337.  
  338.         // 一番文字数が多いものを取得
  339.         for($i 0$i $label_max$i++{
  340.             $text_len strlen($this->arrLegend[$i]);
  341.             if($text_max $text_len{
  342.                 $text_max $text_len;
  343.             }
  344.             $height_max += FONT_SIZE LINE_PAD;
  345.         }
  346.         $width_max FONT_SIZE $text_max TEXT_RATE;
  347.  
  348.         //  カラーアイコンと文字間を含めた幅
  349.         $width_max += FONT_SIZE (LINE_PAD 2);
  350.         $left $this->bgw - $width_max LEGEND_RIGHT;
  351.         $top LEGEND_TOP;
  352.         // カラーラベル背景の描画
  353.         if($clabelbg{
  354.             $this->drawClabelBG($left LINE_PAD$top$left $width_max$top $height_max LINE_PAD);
  355.         }
  356.         $top += LINE_PAD;
  357.  
  358.         // 色数の取得
  359.         $c_max count($this->arrColor);
  360.         for($i 0$i $label_max$i++{
  361.             // カラーアイコンの表示
  362.             imagerectangle($this->image$left$top$left FONT_SIZE$top FONT_SIZE$this->flame_color);
  363.             imagefilledrectangle($this->image$left 1$top 1$left FONT_SIZE 1$top FONT_SIZE 1$this->arrColor[($i $c_max)]);
  364.             // ラベルの表示
  365.             $this->setText(FONT_SIZE$left FONT_SIZE LINE_PAD$top$this->arrLegend[$i]);
  366.             $top += FONT_SIZE LINE_PAD;
  367.         }
  368.     }
  369.  
  370.     // カラーラベル背景の描画
  371.     function drawClabelBG($left$top$right$bottom{
  372.         // 影の描画
  373.         if($this->shade_on{
  374.             imagefilledrectangle($this->image$left 2$top 2$right 2$bottom 2$this->shade_color);
  375.         }
  376.         // カラーラベル背景の描画
  377.         imagefilledrectangle($this->image$left$top$right$bottom$this->clabelbg_color);
  378.         imagerectangle($this->image$left$top$right$bottom$this->flame_color);
  379.     }
  380.  
  381.     // 凡例をセットする
  382.     function setLegend($arrLegend{
  383.         $this->arrLegend = array_values((array)$arrLegend);
  384.     }
  385.  
  386.     // }}}
  387.     // {{{ protected functions
  388.  
  389.     /**
  390.      * クラスの初期化を行う.
  391.      *
  392.      * 表示色をメンバ変数にセットする.
  393.      *
  394.      * @access protected
  395.      * @return void 
  396.      */
  397.     function init({
  398.         // 凡例背景
  399.         $this->ARR_LEGENDBG_COLOR = array(245,245,245);
  400.         // ラベル背景
  401.         $this->ARR_LABELBG_COLOR = array(255,255,255);
  402.         // グラフカラー
  403.         $this->ARR_GRAPH_RGB = array(
  404.                                array(200,50,50),
  405.                                array(50,50,200),
  406.                                array(50,200,50),
  407.                                array(255,255,255),
  408.                                array(244,200,200),
  409.                                array(200,200,255),
  410.                                array(50,200,50),
  411.                                array(255,255,255),
  412.                                array(244,244,244),
  413.                                );
  414.         // 影の色
  415.         $this->ARR_SHADE_COLOR = array(100,100,100);
  416.         // 縁の色
  417.         $this->ARR_FLAME_COLOR = array(000);
  418.         // 文字色
  419.         $this->ARR_TEXT_COLOR = array(000);
  420.         // 背景カラー
  421.         $this->ARR_BG_COLOR = array(255,255,255);
  422.         // タイトル文字色
  423.         $this->ARR_TITLE_COLOR = array(000);
  424.         // グリッド線色
  425.         $this->ARR_GRID_COLOR = array(200200200);
  426.         // マークの色
  427.         $this->ARR_MARK_COLOR array(130130255);
  428.     }
  429.  
  430.     /**
  431.      * 円の中心点と直径から弧の終端座標を算出する.
  432.      *
  433.      * @param integer $cx 中心点X座標
  434.      * @param integer $cy 中心点Y座標
  435.      * @param integer $r 半径
  436.      * @param integer $e 角度
  437.      * @return array 円の中心点と直径から弧の終端座標の配列
  438.      */
  439.     function lfGetArcPos($cx$cy$cw$ch$e{
  440.         // 三角関数用の角度を求める
  441.         $s 90 $e;
  442.         $r $cw 2;
  443.         // 位置を求める
  444.         $x $cx ($r cos(deg2rad($s)));
  445.         $y $cy (($r sin(deg2rad($s))) ($ch $cw));
  446.         return array(round($x)round($y));
  447.     }
  448.  
  449.     /** 画像にテキストを描画する */
  450.     function lfImageText($dst_image$text$font_size$left$top$font$arrRGB{
  451.         $color ImageColorAllocate($dst_image$arrRGB[0]$arrRGB[1]$arrRGB[2]);
  452.         $text mb_convert_encoding($text"UTF-8"CHAR_CODE);
  453.         // 表示角度
  454.         $angle 0;
  455.         // テキスト描画
  456.         ImageTTFText($dst_image$font_size$angle$left$top$color$font$text);
  457.     }
  458.  
  459.     /** 表示色の取得 */
  460.     function lfGetImageColor($image$array{
  461.         if(count($array!= 3{
  462.             return NULL;
  463.         }
  464.         $ret imagecolorallocate($image$array[0]$array[1]$array[2]);
  465.         return $ret;
  466.     }
  467.  
  468.     /** 影用表示色の取得 */
  469.     function lfGetImageDarkColor($image$array{
  470.         if(count($array!= 3{
  471.             return NULL;
  472.         }
  473.         $i 0;
  474.         foreach($array as $val{
  475.             $dark[$i$val 45;
  476.             if($dark[$i0{
  477.                 $dark[$i0;
  478.             }
  479.             $i++;
  480.         }
  481.         $ret imagecolorallocate($image$dark[0]$dark[1]$dark[2]);
  482.         return $ret;
  483.     }
  484. }
  485. ?>

Documentation generated on Fri, 24 Feb 2012 14:02:36 +0900 by Seasoft