码迷,mamicode.com
首页 > Web开发 > 详细

php 生成饼状图,折线图,条形图 通用类 2

时间:2015-08-08 10:22:42      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

 生成饼状图,折线图,条形图通用的php类,这里使用的是国外的 HighCharts,前台页面别忘了调用HighCahrt  js

 HighCharts中文网站  http://www.hcharts.cn/index.php

<?php
/**
 * Created by PhpStorm.
 * User: WANG
 * Date: 2015/7/26
 * Time: 11:19
 */
namespace Org\Wechat;
class HighCharts
{
    /**
     * 获得js生成图表的报表
     * @param $id  dom元素id
     * @param array $data 图表数据
     * @param $type   图表类型    线形图:line      饼图:pie      条形图:column
     * @param string $title 图表主标题
     * @param string $subtitle 副标题
     * @return string
     */
    public function  getHighCharts($id, array $data, $type, $title = ‘‘, $subtitle = ‘‘, $yTitle = ‘‘)
    {
        $series = ‘‘;
        if (empty($data)) {
            $data = array(
                ‘categories‘ => array(
                    ‘data‘ => array(‘-‘)
                ),
                ‘xaxis‘ => array(
                    ‘type‘ => ‘category‘,
                    ‘boundaryGap‘ => ‘false‘,
                    ‘data‘ => array(‘‘)
                ),
                ‘series‘ => array(
                    array(
                        ‘name‘ => ‘-‘,
                        ‘type‘ => ‘line‘,
                        ‘itemStyle‘ => "{normal: {areaStyle: {type: ‘default‘}}}",
                        ‘data‘ => array()
                    ),
                )
            );
        }
        foreach ($data as $key => $value) {
            switch ($key) {
                case ‘categories‘:
                    $categories = ‘[‘;
                    foreach ($value as $k => $v) {
                        // categories: [‘Jan‘, ‘Feb‘, ‘Mar‘, ‘Apr‘, ‘May‘, ‘Jun‘,‘Jul‘, ‘Aug‘, ‘Sep‘, ‘Oct‘, ‘Nov‘, ‘Dec‘]
                        $categories = $categories . json_encode($v, JSON_UNESCAPED_UNICODE) . ‘,‘;
                    }
                    $categories = $categories . ‘]‘;
                    break;
                case ‘xaxis‘:
                    foreach ($value as $k => $v) {
                        switch ($k) {
                            case ‘type‘:
                                $xaxis[] = $k . ":‘" . $v . "‘";
                                break;
                            case ‘data‘:
                                $xaxis[] = $k . ‘:‘ . json_encode($v, JSON_UNESCAPED_UNICODE);
                                break;
                        }
                    }
                    $xaxis = ‘{‘ . implode(‘, ‘, $xaxis) . ‘}‘;
                    break;
                //{  name: ‘Tokyo‘,    data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6 ]}
                case ‘series‘:
                    foreach ($value as $list) {
                        $tmp = array();
                        foreach ($list as $k => $v) {
                            switch ($k) {
                                case ‘name‘:
                                case ‘type‘:
                                    $tmp[] = $k . ":‘" . $v . "‘";
                                    break;
                                case ‘itemStyle‘:
                                    $tmp[] = $k . ‘:‘ . $v;
                                    break;
                                case ‘data‘:
                                    $tmp[] = $k . ‘:‘ . json_encode($v, JSON_UNESCAPED_UNICODE);
                            }
                        }
                        $series[] = ‘{‘ . implode(‘, ‘, $tmp) . ‘}‘;
                    }
                    $series = implode(‘, ‘, $series);
                    break;
            }
        }
        if ($type !== ‘pie‘) {
            $str = "xAxis:{categories:$categories},";
        } else {
            $str = ‘‘;
        }

        $script = <<<EOT
<script type="text/javascript">
  $(function () {
    $(‘#$id‘).highcharts({
        title: {
            text: ‘$title‘,
            x: -20 //center
        },
        subtitle: {
            text: ‘$subtitle‘,
            x: -20
        },
        credits: {
                 enabled: false
         },
         $str
    yAxis: {
       title: {
        text: ‘$yTitle‘
            },
         plotLines: [{
        value: 0,
                width: 1,
                color: ‘#808080‘
            }]
        },
        tooltip: {
            valueSuffix: ‘%‘
        },
        legend: {
            layout: ‘vertical‘,
            align: ‘right‘,
            verticalAlign: ‘middle‘,
            borderWidth: 0
        },
        series: [$series]
    });
});
</script>
EOT;
        return $script;
    }
}

 

php 生成饼状图,折线图,条形图 通用类 2

标签:

原文地址:http://www.cnblogs.com/luoyangcn/p/4712556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!