使用json的Highcharts Dynamic Drilldown

时间:2015-03-10 16:42:52

标签: javascript json dynamic highcharts drilldown

进行动态钻取时出现问题。无法使用highcharts计算获取此图表的动态钻取数据的代码。请帮忙

<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: []
        }
    });
});

在我的系列中我已经有一个json数据用于名称,y和下钻的值。在'Animals':{ name: 'Animals', data: ['Cows',2], ['Sheep', 3]]}内的钻取数据方面真的很难。 我需要这些动态数据请帮忙

2 个答案:

答案 0 :(得分:2)

这部分:

                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);
                }

只是动态添加下钻的简单示例。您可以使用AJAX调用作为注释中的建议:

                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] ] }
                    });
                }

答案 1 :(得分:1)

希望这有助于其他任何人。 你可以,如果你想这样做,虽然这不能解决你的问题,但是这个方法有效,将数据/ json分开并使用ajax调用数据。

  var chartSeriesData = [];

  var chartDrilldownData = [];

$.ajax({
type: 'GET',
url: 'db/jsondata.json',
success: function(data) {


    var agentJson = data;

    for (var i = 0;i <agentJson.agentinfo.length; i++)

        {

        var series_name = agentJson.agentinfo[i].name;
        var drill_id = agentJson.agentinfo[i].drilldown;
        var series_data = agentJson.agentinfo[i].y;

        var drill_data = agentJson.agentinfo[i].data;


              chartSeriesData.push({
                 name: series_name,
                 y: parseFloat(series_data),
                 drilldown : drill_id                                     
              }); 


         chartDrilldownData.push({
             data : drill_data,
             id: drill_id,
             name: series_name,

         });


        }

    /// END FOR LOOP


    $('#agentInfo').highcharts({

        credits: {
            enabled: false
        },
        colors: ['#4B2572', '#8E227D', '#7904e0', '#b726b5', '#64E572', '#ed9cdf', '#f704ce', '#780a84', '#8e4696', '#cd92d3', '#551c5b'],
        chart: {
            type: 'pie',
            backgroundColor:'rgba(255, 255, 255, 0.1)'
        },
        title: {
            text: 'Human Resources'
        },

        subtitle: {
            text: ''
        },
        plotOptions: {
            series: {
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '{point.name}: {point.y:.1f}%',
                    style: {
                        color: '#000',
                        fontWeight: 'bold',
                        fontSize: '12px',
                        textShadow: "0px #ffffff"

                     }
                }
            }
        },

        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/>'
        },
        series: [{
            name: 'Types',
            colorByPoint: true,
            data: chartSeriesData
        }],
        drilldown: {
             series: chartDrilldownData
            }
    });



}


});

json数据的结构如下:

                {
                    "agentinfo": [
                  {
                    "name": "Attendance",
                    "y": 86.32,
                    "drilldown": "Attendance",
                                        "data": [
                                        ["January",  80.13],
                                        ["Febuary",  99.2],
                                        ["March",  78.11],
                                        ["April", 89.33]
                                    ]
                  },
                  {
                    "name": "Absence",
                    "y": 4.03,
                    "drilldown": "Absence",
                                        "data": [
                                        ["January", 1.3],
                                        ["Febuary", 5.7],
                                        ["March", 9.6],
                                        ["April", 8.5]
                                    ]
                  },
                  {
                    "name": "Holidays",
                    "y": 10.38,
                    "drilldown" : "Holidays",
                                        "data": [
                                        ["January", 5],
                                        ["Febuary", 4.32],
                                        ["March", 3.68],
                                        ["April", 2.96]
                                    ]
                  }
                ]

                }