PHP 生成直方图的类(仿Light Chart)

来源:互联网 发布:电力仿真软件下载 编辑:程序博客网 时间:2024/05/21 10:20
<?phpclass plchart {    var $chart;    var $data = array(        'title' => "title\ntest",        'group_count' => 4, //how many groups        'group_size' => 4, //size of each group        'group_desc' => array('1月份', '2月份', '3月份','333'), //describe text of each group, the size equal to group_count, other wise will be ingored or empty('')        'column_desc' => array('成交额', '销售额','不知道','我也不知道'), //describe text of each column in group, size same as group_size, other wise will be ingored or empty('')        'raw_data' => array(//size of raw_data is the same as group_size            array(34, 29, 55,33), //size of eache data is the same as group_count            array(45, 45, 61,44),            array(33,44,44,44),            array(33,44,44,44)        ),        'legend_y' => '万元',        'legend_x' => '日期',    );    //image parameter    var $width = 1200;    var $height = 600;    var $quality = 100;    var $bgcolor;    var $savefile = '';    //image parameter end    //image header parameter (title & legend)    var $header_height = 70;    var $header_bgcolor;    var $title_top_margin = 10;    var $title_left_margin = 10;    //字体    var $title_font_color;    var $title_font_size;    var $title_font_style;    //legend paramenter    var $legend_height = 50;    var $legend_width = 100;    var $legend_bgcolor;    var $legend_color_array = array(); //legend colors ,size same as group_size    var $each_legend_h = 10;    var $each_legend_w = 20;    var $each_legend_gap = 5;    //字体    var $legend_font_color;    var $legend_font_size;    var $legend_font_style;    //chart paramenter    var $y_legend_font_color;    var $y_legend_font_size;    var $y_legend_font_style;    var $x_legend_font_color;    var $x_legend_font_size;    var $x_legend_font_style;    var $ruler_line_color;    var $chart_y_left = 10; //y轴头部留下    var $chart_x_left = 10; //x轴头部留下    var $chart_left_margin = 70; //用来写纵坐标的刻度    var $chart_right_margin = 10;    var $chart_bottom_margin = 50; //用来写横坐标的说明    var $chart_left_font_color;    var $chart_left_font_size;    var $chart_left_font_style;    var $chart_bottom_font_color;    var $chart_bottom_font_size;    var $chart_bottom_font_style;    var $chart_top_font_color;    var $chart_top_font_size;    var $chart_top_font_style;    function __construct($w=1200, $h=600) {        $this->width = $w;        $this->height = $h;        $this->chart = @imagecreatetruecolor($this->width, $this->height) or die("Cannot Initialize new GD image stream");        $white = imagecolorallocate($this->chart, 255, 255, 255);        $black = imagecolorallocate($this->chart, 0, 0, 0);        $light = imagecolorallocate($this->chart, 244, 244, 244);        $red = imagecolorallocate($this->chart, 255, 0, 0);        $green = imagecolorallocate($this->chart, 0, 255, 0);        $blue = imagecolorallocate($this->chart, 0, 0, 255);        $ruler_color = imagecolorallocate($this->chart, 144, 144, 144);        $common_font_style = 'c:/windows/fonts/simhei.ttf';        $common_font_size = 12;        $this->bgcolor = $light;        $this->header_bgcolor = $white;        $this->title_font_color = $black;        $this->title_font_size = 18;        $this->title_font_style = $common_font_style;        $this->legend_bgcolor = $white;        $this->legend_color_array = array(            imagecolorallocate($this->chart, 0, 0, 255),            imagecolorallocate($this->chart, 0, 255, 0),            imagecolorallocate($this->chart, 255, 0, 0),            imagecolorallocate($this->chart, 144, 144, 144),        );        $this->legend_font_color = $black;        $this->legend_font_size = 10;        $this->legend_font_style = $common_font_style;        $this->y_legend_font_color = $green;        $this->y_legend_font_size = $common_font_size;        $this->y_legend_font_style = $common_font_style;        $this->x_legend_font_color = $green;        $this->x_legend_font_size = $common_font_size;        $this->x_legend_font_style = $common_font_style;        $this->ruler_line_color = $ruler_color;        $this->chart_left_font_color = $black;        $this->chart_left_font_size = $common_font_size;        $this->chart_left_font_style = $common_font_style;        $this->chart_bottom_font_color = $black;        $this->chart_bottom_font_size = 15;        $this->chart_bottom_font_style = $common_font_style;        $this->chart_top_font_color = $black;        $this->chart_top_font_size = $common_font_size;        $this->chart_top_font_style = $common_font_style;    }    function buildimage() {        imagefill($this->chart, 0, 0, $this->bgcolor); //fill the image with background color    }    function buildheader() {        //header start position (left-top point)        $startx = 0;        $starty = 0;        //height & width        $height = $this->header_height;        $width = $this->width - $startx;        imagefilledrectangle($this->chart, $startx, $starty, $startx + $width, $starty + $height, $this->header_bgcolor);        imagettftext($this->chart, $this->title_font_size, 0, $startx + $this->title_left_margin, $starty + $this->title_top_margin + $this->title_font_size, $this->title_font_color, $this->title_font_style, $this->data['title']);        $legend_startx = $this->width - $this->legend_width;        $legend_starty = $this->header_height - $this->legend_height;        $_sy = $legend_starty;        for ($i = 0; $i < $this->data['group_size']; $i++) {            imagefilledrectangle($this->chart, $legend_startx, $_sy, $legend_startx + $this->each_legend_w, $_sy + $this->each_legend_h, $this->legend_color_array[$i]);            imagettftext($this->chart, $this->legend_font_size, 0, $legend_startx + $this->each_legend_w + $this->each_legend_gap, $_sy + $this->each_legend_h, $this->legend_font_color, $this->legend_font_style, $this->data['column_desc'][$i]);            $_sy = $_sy + $this->each_legend_h + $this->each_legend_gap;        }    }    function buildchart() {        //获取图像原点        $originx = $this->chart_left_margin;        $originy = $this->height - $this->chart_bottom_margin;        // 取得图形宽度和高度 - get width and height of the graphics         $w = $this->width - $this->chart_left_margin - $this->chart_right_margin;        $h = $this->height - $this->header_height - $this->chart_bottom_margin;        //画出x轴与y轴        imageline($this->chart, $originx, $originy, $originx + $w, $originy, $this->ruler_line_color);     // x         imageline($this->chart, $originx, $originy, $originx, $originy - $h, $this->ruler_line_color);     // y        //imagefilledrectangle($this->chart,$posx,$posy-$h,$posx+$w,$posy,$this->legend_color_array[3]);        $maxvalue = 0;        foreach ($this->data['raw_data'] as $v)            foreach ($v as $v1)                $maxvalue = max($maxvalue, $v1);        //x轴每一个空隙的大小        $fieldnumberx = $this->data['group_count'] + $this->data['group_size'] * $this->data['group_count'] * 2 + 1;        $eachx = ($w - $this->chart_x_left) / $fieldnumberx;        //x标尺        /* for($i = 0; $i <= $fieldnumberx; $i ++)          imageline($this->chart, $originx+$eachx*$i, $originy, $originx+$eachx*$i, $originy - $h, $this->ruler_line_color); */        //计算y轴的单位长度与分割数        $multiple = floor(log10($maxvalue));     // 取得数值区间 - get data field         $field = floor($maxvalue / pow(10, $multiple)) + 1;     // 区间数 - number of unit fields         $fieldnumbery = $field > 5 ? $field : 5;     // 数据区域数量 - number of data fields        $eachy = ($h - $this->chart_y_left) / $fieldnumbery;        $topvaluey = $field * pow(10, $multiple);        //y标尺        for ($i = 0; $i <= $fieldnumbery; $i++) {            imageline($this->chart, $originx, $originy - $eachy * $i, $originx + $w, $originy - $eachy * $i, $this->ruler_line_color);            if($i != $fieldnumbery)                imagettftext($this->chart, $this->chart_left_font_size, 0, 0,                         $originy - $eachy * $i + $this->chart_left_font_size / 2, $this->chart_left_font_color,                         $this->chart_left_font_style, $i == 0 ? 0 : $topvaluey / $fieldnumbery * $i);            else                imagettftext($this->chart, $this->y_legend_font_size, 0, 0,                         $originy - $eachy * $i + $this->y_legend_font_size / 2,$this->y_legend_font_color,                         $this->y_legend_font_style, $this->data['legend_y']);        }                imagettftext($this->chart, $this->x_legend_font_size, 0, $originx + $w - $this->chart_x_left - $eachx,                 $originy + $this->x_legend_font_size + 3,$this->x_legend_font_color,$this->x_legend_font_style,$this->data['legend_x']);        // 生成条柱 - make columns         $group_w = $this->data['group_size'] * 2 * $eachx;        $chart_real_height = $h - $this->chart_y_left;        for ($i = 0; $i < $this->data['group_count']; $i++){            $_x = $originx + $eachx * ($i + 1) + $group_w * $i;            $each_data_key = $this->data['group_desc'][$i];            imagettftext($this->chart, $this->chart_bottom_font_size, 0, $_x, $originy + $this->chart_bottom_font_size + 3,                    $this->chart_bottom_font_color, $this->chart_bottom_font_style, $each_data_key);            for ($j = 0; $j < $this->data['group_size']; $j++) {                $each_originx = $originx + $eachx * ($i + 1) + $group_w * $i + $j * $eachx * 2;                $each_data_value = $this->data['raw_data'][$j][$i];                $each_height = $chart_real_height * ($each_data_value / $topvaluey);                imagefilledrectangle($this->chart, $each_originx, $originy - $each_height, $each_originx + 2 * $eachx, $originy, $this->legend_color_array[$j]);                imagettftext($this->chart, $this->chart_top_font_size, 0, $each_originx, $originy - $each_height,                         $this->chart_top_font_color, $this->chart_top_font_style,$each_data_value);            }        }                /*          // 生成条柱 - make columns          $paramkeys = array_keys($this->param);          for($j = 0; $j < $num; $j++)          {          $columnheight = ($h - $margin * 2) * ($this->param[$paramkeys[$j]] / $topvalue);     // 条柱高度 - column height          $columnx = $startx + $unitx / 4 + $unitx * $j;     // 条柱起点x坐标 - x coordinate of column          imagefilledrectangle($this->chart, $columnx, $starty - $columnheight, $columnx + $unitx / 2, $starty - 1, $this->colors[$j * 2]);     // 画条柱 - draw columns          } */    }    /*     * @todo  输出图像至浏览器或文件 - output the chart image to browser or file      */    function outputchart() {        $this->buildimage();        $this->buildheader();        $this->buildchart();        // 建立图标 - build chart         //$this->buildchart();         // 写入图形 - build graphics         //$this->buildgraphics();         // 写入注释 - build description         //$this->builddesc();         // 写入标题 - build title         //$this->buildtitle();         // 输出图像 - flush image         // 图像头信息 - header of the image file         header("Content-type: image/jpeg");        imagejpeg($this->chart, $this->savefile, $this->quality);        imagedestroy($this->chart);    }}/* * *******************************************************************************   演示文件demo.php: * ******************************************************************************** */$chart = new plchart();$data1 = array();$data2 = array();$mo = array();for($i = 0; $i < 31; $i++){    $data1[] = $i*20;    $data2[] = $i*20+4;    $mo[] = $i +1;}$chart->chart_left_margin = 50;$chart->data = array(    'title' => "title\ntest",    'group_count' => 31, //how many groups    'group_size' => 2, //size of each group    'group_desc' => $mo, //describe text of each group, the size equal to group_count, other wise will be ingored or empty('')    'column_desc' => array('成交额', '销售额'), //describe text of each column in group, size same as group_size, other wise will be ingored or empty('')    'raw_data' => array(//size of raw_data is the same as group_size        $data1, //size of eache data is the same as group_count        $data2,    ),    'legend_y' => '万元',    'legend_x' => '日期',);$chart->outputchart();?> 


原创粉丝点击