向下钻取两个步骤,在Highcharts中进行多项选择

时间:2015-11-20 10:39:38

标签: jquery json highcharts drilldown

我是这个社区的新手,所以我会尽我所能。 我正在尝试使用饼图中的Highcharts进行深入分析。事实是我想开发一些允许我的代码:

  • 首次点击选择多个图表(或饼图中的切片)
  • 在第二次点击向下钻取饼图以查看下钻系列。

这里有我的代码(我只能在完成下钻时执行多项选择,而不是之前):

$(function () {
var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container',
        type: 'pie'

    },
    plotOptions: {
        series: {
            point: {
                events: {
                    click: function (event) {
                        this.slice(null);
                    }
                }
            }
        }
    },
    series: [{
            name: 'Things',
            colorByPoint: true,
            data: [{
                    name: 'Animals',
                    y: 5,
                    drilldown: 'animals'
                }, {
                    name: 'Fruits',
                    y: 2,
                    drilldown: 'fruits'
                }, {
                    name: 'Cars',
                    y: 4,
                    drilldown: 'cars'
                }]
        }],
    drilldown: {
        series: [{
                id: 'animals',
                data: [
                    ['Cats', 4],
                    ['Dogs', 2],
                    ['Cows', 1],
                    ['Sheep', 2],
                    ['Pigs', 1]
                ]
            }, {
                id: 'fruits',
                data: [
                    ['Apples', 4],
                    ['Oranges', 2]
                ]
            }, {
                id: 'cars',
                data: [
                    ['Toyota', 4],
                    ['Opel', 2],
                    ['Volkswagen', 2]
                ]
            }]
        }
    });
});

感谢您的帮助伙伴们!

1 个答案:

答案 0 :(得分:1)

使用async drilldownAPI)可以添加更多系列作为明细。检查点的状态以及它是否已被选中,这意味着单击了选定的点并且应该执行向下钻取。

$(function () {

    // Create the chart
    $('#container').highcharts({
        chart: {
            type: 'column',
            events: {
                drilldown: function (e) {
                    if (!e.seriesOptions && e.point.selected) {
                        var chart = this,
                            points = chart.getSelectedPoints(),
                            drilldowns = {
                                'Animals': {
                                    name: 'Animals',
                                    data: [
                                        ['Cows', 2],
                                        ['Sheep', 3]
                                    ]
                                },
                                    'Fruits': {
                                    name: 'Fruits',
                                    data: [
                                        ['Apples', 5],
                                        ['Oranges', 7],
                                        ['Bananas', 2]
                                    ]
                                },
                                    'Cars': {
                                    name: 'Cars',
                                    data: [
                                        ['Toyota', 1],
                                        ['Volkswagen', 2],
                                        ['Opel', 5]
                                    ]
                                }
                            };

                        Highcharts.each(points, function (p) {
                            chart.addSingleSeriesAsDrilldown(p, drilldowns[p.name]);
                        });
                        chart.applyDrilldown();
                    }

                },
                drillup: function (e) {
                    var chart = this,
                        points = [];
                    setTimeout(function () {
                        points = chart.getSelectedPoints();

                        Highcharts.each(points, function (p) {
                            // unselect points from previous drilldown
                            p.selected = false;
                            p.setState('', true);
                        });
                    }, 0);
                }
            }
        },
        title: {
            text: 'Async drilldown'
        },
        xAxis: {
            type: 'category'
        },

        legend: {
            enabled: false
        },

        plotOptions: {
            series: {
                borderWidth: 0,
                dataLabels: {
                    enabled: true
                }
            }
        },

        series: [{
            allowPointSelect: true,
            name: 'Things',
            colorByPoint: true,
            data: [{
                name: 'Animals',
                y: 5,
                drilldown: true
            }, {
                name: 'Fruits',
                y: 2,
                drilldown: true
            }, {
                name: 'Cars',
                y: 4,
                drilldown: true
            }]
        }],

        drilldown: {
            series: []
        }
    });
});

JSFiddle:http://jsfiddle.net/7hmn093d/2/