懒惰的Highcharts钻研

时间:2014-11-07 12:59:19

标签: javascript highcharts

This JSFiddle demo显示了Highcharts明细的示例。当您单击图表中的某列时,系列将替换为与单击的列对应的向下钻取系列

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

例如,如果单击fruits列,将显示此数据

data: [
    ['Apples', 4],
    ['Oranges', 2]
]

请注意,必须预先创建所有钻取系列。在这种特殊情况下,只有3个向下钻系列,所以这不是一个大问题。但是,在我的情况下,大约有30个向下钻取系列,并且创建每个系列需要执行一些查询。有没有办法让钻取系列延迟加载,即钻取系列数据仅在用户点击其中一列时请求?

2 个答案:

答案 0 :(得分:6)

对于这种功能级别,我只是继续自己创建它。使用point.events.click回调进行ajax调用并替换系列:

plotOptions: {
  series: {
    point: {
      events: {
        click: function(event) {
          var chart = this.series.chart;
          var name = this.name;
          $.ajax({
            url: name + ".json",
            success: function(data) {
              swapSeries(chart,name,data);
            },
            dataType: "json"
          });
        }
      }
    }
  }
},

swapSeries的位置:

  function swapSeries(chart, name, data) {
    chart.series[0].remove();
    chart.addSeries({
      data: data,
      name: name,
      colorByPoint: true
    });
  }

这是一个有效的example

答案 1 :(得分:6)

Highcharts支持Lazy drilldown ,但它使用术语“async drilldown”。

$(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: []
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/drilldown.js"></script>

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