Highcharts-7-下钻图制作

2021-03-04 14:58:46 浏览数 (1)

Highcharts-7—下钻图形

本文中只讲解一个图形的制作:下钻图

下钻表示的是通过层级的方式来展示数据,比如我们想查看国内人口数的占比情况,我们可以先看各个省份的情况,接着我们想看具体某个省中各个地级市的占比,这就是通过下钻方式实现。

代码

代码语言:javascript复制
# -*- coding: utf-8 -*-

from highcharts import Highchart
H = Highchart(width=850, height=400)

# 第一层级的数据
data = [{  # 第一层级的数据
            'name': "Microsoft Internet Explorer", # 名字
            'y': 56.33,  # 数据
            'drilldown': "Microsoft Internet Explorer"  # 下钻的名字
        }, {
            'name': "Chrome",
            'y': 24.030000000000005,
            'drilldown': "Chrome"
        }, {
            'name': "Firefox",
            'y': 10.38,
            'drilldown': "Firefox"
        }, {
            'name': "Safari",
            'y': 4.77,
            'drilldown': "Safari"
        }, {
            'name': "Opera",
            'y': 0.9100000000000001,
            'drilldown': "Opera"
        }, {
            'name': "Proprietary or Undetectable",
            'y': 0.2,
            'drilldown': None
        }]

# 第二层级的数据
# data_1的全部数值加起来就是data中第一个元素的值
data_1 = [  # 对一个第一层级的子数据
    ["v11.0", 24.13],
    ["v8.0", 17.2],
    ["v9.0", 8.11],
    ["v10.0", 5.33],
    ["v6.0", 1.06],
    ["v7.0", 0.5]
]

data_2 = [
    ["v40.0", 5],
    ["v41.0", 4.32],
    ["v42.0", 3.68],
    ["v39.0", 2.96],
    ["v36.0", 2.53],
    ["v43.0", 1.45],
    ["v31.0", 1.24],
    ["v35.0", 0.85],
    ["v38.0", 0.6],
    ["v32.0", 0.55],
    ["v37.0", 0.38],
    ["v33.0", 0.19],
    ["v34.0", 0.14],
    ["v30.0", 0.14]
]

data_3 = [
    ["v35", 2.76],
    ["v36", 2.32],
    ["v37", 2.31],
    ["v34", 1.27],
    ["v38", 1.02],
    ["v31", 0.33],
    ["v33", 0.22],
    ["v32", 0.15]
]

data_4 = [
    ["v8.0", 2.56],
    ["v7.1", 0.77],
    ["v5.1", 0.42],
    ["v5.0", 0.3],
    ["v6.1", 0.29],
    ["v7.0", 0.26],
    ["v6.2", 0.17]
]

data_5 = [
    ["v12.x", 0.34],
    ["v28", 0.24],
    ["v27", 0.17],
    ["v29", 0.16]
]

options = {
    'chart': {
        'type': 'column'  # 表的类型:柱状图
    },
    'title': {  # 主标题
        'text': 'Browser market shares. January, 2015 to May, 2015'
    },
    'subtitle': {  # 副标题
        'text': 'Click the columns to view versions. Source: <a href="http://netmarketshare.com">netmarketshare.com</a>.'
    },
    'xAxis': {  # x轴
        'type': 'category'
    },
    'yAxis': {  # y轴
        'title': {
            'text': 'Total percent market share'
        }

    },
    'legend': {  # 图例
        'enabled': False
    },
    'plotOptions': {
        'series': {
            'borderWidth': 0,
            'dataLabels': {
                'enabled': True,
                'format': '{point.y:.1f}%'
            }
        }
    },
    'tooltip': {
        'headerFormat': '<span style="font-size:11px">{series.name}</span><br>',
        'pointFormat': '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
    },
}

# 添加配置项
H.set_dict_options(options)

# 添加主数据
H.add_data_set(data, 'column', "Brands", colorByPoint= True)

# 添加下钻数据
# add_drilldown_data_set(data, series_type, id, **kwargs)
H.add_drilldown_data_set(data_1, 'line', 'Microsoft Internet Explorer',name='Microsoft Internet Explorer' )
H.add_drilldown_data_set(data_2, 'column', 'Chrome', name='Chrome')
H.add_drilldown_data_set(data_3, 'pie', 'Firefox', name='Firefox')   # pie表示下钻图形
H.add_drilldown_data_set(data_4, 'column', 'Safari', name='Safari')
H.add_drilldown_data_set(data_5, 'column', 'Opera', name='Opera')

H

效果

整体效果
下钻效果

当我们选择其中某个图形进行点击的时候,会展示其下面的图形和数据,比如我们选择第一个:

下钻的图形展示的是折线图,因为我们指定的是line类型:

如果我们想回到主图中,点击右上角的如图位置:

选择第3个图形,我们选择的是饼图pie,看下实际的效果:

这便是下钻图表的效果?

问题

问题出现

问题:目前在jupyter notebook中的时候使用的是python-highcharts运行的结果不能下钻到下一层级中

问题所在

打印出返回的源码

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <link href="https://www.highcharts.com/highslide/highslide.css" rel="stylesheet" />
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/highcharts.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/highcharts-more.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/modules/heatmap.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/modules/exporting.js"></script>
        <script type="text/javascript" src="http://code.highcharts.com/modules/drilldown.js"></script>
    </head>
    <body style="margin:0;padding:0">
                <div id="container" style="width:850px;height:400px;">Loading....</div>
    <script>
        $(function(){
            Highcharts.setOptions({"global": {}, "lang": {}});
            var option = {"chart": {"renderTo": "container", "width": 850, "height": 400, "type": "column"}, "colors": {}, "credits": {"enabled": false}, "drilldown": {}, "exporting": {}, "labels": {}, "legend": {"enabled": false}, "loading": {}, "navigation": {}, "pane": {}, "plotOptions": {"series": {"borderWidth": 0, "dataLabels": {"enabled": true, "format": "{point.y:.1f}%"}}}, "series": {}, "subtitle": {"text": "Click the columns to view versions. Source: <a href="http://netmarketshare.com">netmarketshare.com</a>."}, "title": {"text": "Browser market shares. January, 2015 to May, 2015"}, "tooltip": {"headerFormat": "<span style="font-size:11px">{series.name}</span><br>", "pointFormat": "<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>"}, "xAxis": {"type": "category"}, "yAxis": {"title": {"text": "Total percent market share"}}};
            var drilldowndata = [{"data": [["v11.0", 24.13], ["v8.0", 17.2], ["v9.0", 8.11], ["v10.0", 5.33], ["v6.0", 1.06], ["v7.0", 0.5]], "type": "line", "name": "Microsoft Internet Explorer", "id": "Microsoft Internet Explorer"}, {"data": [["v40.0", 5], ["v41.0", 4.32], ["v42.0", 3.68], ["v39.0", 2.96], ["v36.0", 2.53], ["v43.0", 1.45], ["v31.0", 1.24], ["v35.0", 0.85], ["v38.0", 0.6], ["v32.0", 0.55], ["v37.0", 0.38], ["v33.0", 0.19], ["v34.0", 0.14], ["v30.0", 0.14]], "type": "column", "name": "Chrome", "id": "Chrome"}, {"data": [["v35", 2.76], ["v36", 2.32], ["v37", 2.31], ["v34", 1.27], ["v38", 1.02], ["v31", 0.33], ["v33", 0.22], ["v32", 0.15]], "type": "pie", "name": "Firefox", "id": "Firefox"}, {"data": [["v8.0", 2.56], ["v7.1", 0.77], ["v5.1", 0.42], ["v5.0", 0.3], ["v6.1", 0.29], ["v7.0", 0.26], ["v6.2", 0.17]], "type": "column", "name": "Safari", "id": "Safari"}, {"data": [["v12.x", 0.34], ["v28", 0.24], ["v27", 0.17], ["v29", 0.16]], "type": "column", "name": "Opera", "id": "Opera"}];
            option.drilldown.series = drilldowndata;
            var chart = new Highcharts.Chart(option);
            var data = [{"data": [{"name": "Microsoft Internet Explorer", "y": 56.33, "drilldown": "Microsoft Internet Explorer"}, {"name": "Chrome", "y": 24.030000000000005, "drilldown": "Chrome"}, {"name": "Firefox", "y": 10.38, "drilldown": "Firefox"}, {"name": "Safari", "y": 4.77, "drilldown": "Safari"}, {"name": "Opera", "y": 0.9100000000000001, "drilldown": "Opera"}, {"name": "Proprietary or Undetectable", "y": 0.2, "drilldown": null}], "type": "column", "name": "Brands", "colorByPoint": true}];
            var dataLen = data.length;
            for (var ix = 0; ix < dataLen; ix  ) {
                chart.addSeries(data[ix]);
            }
    });
        </script>
    </body>
</html>

找了半天没有发现问题所在,后来在stack overflow上有人遇到了同样的问题:https://www.coder.work/article/5773029

问题解决

有人提出了问题的解决方案,原来模块版本的不统一:

代码语言:javascript复制
<script type="text/javascript" src="https://code.highcharts.com/modules/drilldown.js"></script>
改成:
<script type="text/javascript" src="https://code.highcharts.com/6/modules/drilldown.js"></script>

我们上面src中的两个链接发现:

  • 第一个是新的版本
  • 第二个是旧的版本

按照建议修改html代码之后就能够正常显示下钻的图形。

待解决

目前显示下钻图形是通过前端的html代码实现的,在jupyter notebook如何直接在线显示图形还是没有解决?

感觉是下载highcharts的版本时候,模块文件要统一下。后续跟进这个问题。

0 人点赞