Highcharts Dynamic Drilldown using json

Nb12se picture Nb12se · Mar 10, 2015 · Viewed 9k times · Source

Problem in having a dynamic drilldown. Can't figure the the code for getting those dynamic drilldown data for this chart using highcharts. Please help

<script type="text/javascript">//<![CDATA[ 

$(function () {

    // Create the chart
    $('#container').highcharts({
        chart: {
            type: 'pie',
            events: {
                drilldown: function (e) {
                    if (!e.seriesOptions) {

                        var chart = this,
                            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]
                                    ]
                                }
                            },
                            series = drilldowns[e.point.name];

                        // Show the loading label
                        chart.showLoading('Simulating Ajax ...');

                        setTimeout(function () {
                            chart.hideLoading();
                            chart.addSeriesAsDrilldown(e.point, series);
                        }, 1000);
                    }

                }
            }
        },
        title: {
            text: 'Async drilldown'
        },
        xAxis: {
            type: 'category'
        },

        legend: {
            enabled: false
        },
        lang: {
            drillUpText: '<< Terug naar {series.name}'
        },
        plotOptions: {
            series: {
                borderWidth: 0,
                dataLabels: {
                    enabled: true
                }
            }
        },

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

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

inside my series i already have a json data there for the name, y and for the value of the drilldown. having really hard time when it comes to the drilldown data inside 'Animals':{ name: 'Animals', data: ['Cows',2], ['Sheep', 3]]} . i need dynamic data for these please help help

Answer

Paweł Fus picture Paweł Fus · Mar 11, 2015

This part:

                if (!e.seriesOptions) {
                    var chart = this,
                        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]
                                ]
                            }
                        },
                        series = drilldowns[e.point.name];

                    // Show the loading label
                    chart.showLoading('Simulating Ajax ...');

                    setTimeout(function () {
                        chart.hideLoading();
                        chart.addSeriesAsDrilldown(e.point, series);
                    }, 1000);
                }

Is just an easy example of adding drilldowns dynamically. You can use AJAX calls, as suggestion in the comment:

                if (!e.seriesOptions) {
                    var chart = this,
                        series = drilldowns[e.point.name];

                    // Show the loading label
                    chart.showLoading('Loading...');

                    $.get('/my/url', e.point.name, function(data) {
                        chart.hideLoading();
                        chart.addSeriesAsDrilldown(e.point, data);
                        // where data is for example: 
                        // { name: 'Cars', data: [ ['Toyota', 1], ['Volkswagen', 2], ['Opel', 5] ] }
                    });
                }