Highcharts异步钻取

时间:2014-01-14 08:06:26

标签: jquery highcharts

我正在关注制作向下钻取图表的http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/drilldown/async/链接。

我无法理解如何设置drilldowns = {}的值(从ajax调用动态) 和series = drilldowns[e.point.name];

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

我试图搜索.addSeriesAsDrilldown(e.point, series);

请帮助我如何通过$ .ajax调用实现向下钻取。

$(function () {    

// Create the chart
$('#container').highcharts({
    chart: {
        type: 'column',
        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
    },

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

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

});

2 个答案:

答案 0 :(得分:10)

您的示例:http://jsfiddle.net/S3j35/

使用e.point.name确定点击了哪个点以及您需要从服务器获取哪些数据。当AJAX出现时,只需添加新数据系列:

            drilldown: function (e) {
                if (!e.seriesOptions) {
                    // e.point.name is info which bar was clicked
                    chart.showLoading('Simulating Ajax ...');
                    $.get("path/to/place.html?name=" + e.point.name, function(data) {
                        /***
                        where data is this format:
                        data = {
                            name: 'Cars',
                            data: [
                                ['Toyota', 1],
                                ['Volkswagen', 2],
                                ['Opel', 5]
                            ]
                        }
                        ***/
                        chart.hideLoading();
                        chart.addSeriesAsDrilldown(e.point, data);
                    });
                }
            }

答案 1 :(得分:-1)

下面是使用ajax进行深入分析的工作代码:

<script type="text/javascript">
    // Create the chart
    Highcharts.chart('container', {
        chart: {
            type: 'column',
            events: {
                drilldown: function (e) {
                    if (!e.seriesOptions) {

                        var chart = this;
                            //calling ajax to load the drill down levels
                            chart.showLoading('Simulating Ajax ...');
                            $.get("ajax_response.php?name=" + e.point.name, function(data) {
                                chart.hideLoading();
                                chart.addSeriesAsDrilldown(e.point, jQuery.parseJSON(data));
                            });
                    }

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

        legend: {
            enabled: false
        },

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

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

</script>
<!-- language: lang-html -->

    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="https://code.highcharts.com/modules/drilldown.js"></script>

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




<!-- end snippet -->


Create ajax_response.php with below codes:

<?php
if($_REQUEST['name'] == "Cars") {
$response = array('name' => 'Cars', 'data'=>array(array('Toyota', 1), array('Volkswagen', 2), array('Opel', 5)));
}else if($_REQUEST['name'] == "Fruits"){
    $response = array('name' => 'Fruits', 'data'=>array(array('Apples', 5), array('Oranges', 7), array('Bananas', 2))); 
}else if($_REQUEST['name'] == "Animals"){
    $response = array('name' => 'Animals', 'data'=>array(array('Cows', 2), array('Sheep', 3))); 
}

echo json_encode($response);
?>