highcharts PHP中使用

官网

https://www.hcharts.cn/demo/highcharts

html

<div id="container" style="min-width:400px;height:400px"></div>

js

Highcharts.chart('container', {
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false,
        type: 'pie'
    },
    title: {
        text: '2018年1月浏览器市场份额'
    },
    tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                }
            }
        }
    },
    series: [{
        name: 'Brands',
        colorByPoint: true,
        data: [{
            name: 'Chrome',
            y: 61.41,
            sliced: true,
            selected: true
        }, {
            name: 'Internet Explorer',
            y: 11.84
        }, {
            name: 'Firefox',
            y: 10.85
        }, {
            name: 'Edge',
            y: 4.67
        }, {
            name: 'Safari',
            y: 4.18
        }, {
            name: 'Sogou Explorer',
            y: 1.64
        }, {
            name: 'Opera',
            y: 1.6
        }, {
            name: 'QQ',
            y: 1.2
        }, {
            name: 'Other',
            y: 2.61
        }]
    }]
});

这里的数据通常都是从数据库查询处理出来的。
而它的格式是json的格式。
所以通过ajax获取比较方便一些。

public function get_series_data() {
        if ($date = $_POST['date']) {
            $sql = 'select count(*) as count ,appid from tf_bag_lucky_log where is_receive=1 and add_time > '.strtotime($date." 00:00").' and add_time < '.strtotime($date ." 23:59").' group by appid order by count desc';
        } else {
            $sql = 'select count(*) as count ,appid from tf_bag_lucky_log where is_receive=1 group by appid order by count desc';
        }
        // 统计
        $data_list = Db::query($sql);
        $series_data = [];
        foreach ($data_list as $k=>&$v) {
            $xcx_info = Db::name('xcx')->where('appid',$v['appid'])->find();
            if ($k == 0) {
                $series_data[$k] = [
                    'name' => $xcx_info['name'],
                    'y' => $v['count'],
                    'sliced' => true,
                    'selected' => true,
                ];
            } else {
                $series_data[$k] = [
                    'name' => $xcx_info['name'],
                    'y' => $v['count'],
                ];
            }
        }

        $this->json->setErr(0, '操作成功');
        $this->json->setAttr('data', $series_data);
        $this->json->Send();
}

js改造

showContainer(date);
function showContainer(date) {
            $.ajax({
                url: "get_series_data",
                data: {
                    "date"          : date,
                },
                type: "POST",
                dataType: "json",
                success: function (data) {
                    Highcharts.chart('container', {
                        chart: {
                            plotBackgroundColor: null,
                            plotBorderWidth: null,
                            plotShadow: false,
                            type: 'pie'
                        },
                        title: {
                            text: '金猪中奖来自小程序'
                        },
                        tooltip: {
                            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: false
                                },
                                showInLegend: true
                            }
                        },
                        series: [{
                            name: '占比',
                            colorByPoint: true,
                            data: data.data,
                            // data: [{
                            //     name: 'Chrome',
                            //     y: 1000,
                            //     sliced: true,
                            //     selected: true
                            // }, {
                            //     name: 'Internet Explorer',
                            //     y: 11.84
                            // }, {
                            //     name: 'Firefox',
                            //     y: 10.85
                            // }, {
                            //     name: 'Edge',
                            //     y: 4.67
                            // }, {
                            //     name: 'Safari',
                            //     y: 4.18
                            // }, {
                            //     name: 'Other',
                            //     y: 7.05
                            // }]
                        }],
                    });
                },
                error: function () {
                    alert("网络错误");
                }
});

422101-20190130125629736-1628042908.png

highcharts 非常灵活,非常方便。ajax,json获取数据,效果刚刚的。

转载于:https://www.cnblogs.com/jiqing9006/p/10337648.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值