【问题标题】:Highcharts async drilldown (3 level) - drillup not workingHighcharts 异步向下钻取(3 级) - 向上钻取不起作用
【发布时间】:2019-05-23 05:38:52
【问题描述】:

我在 highcharts 中创建了一个异步向下钻取(Treemap1 到 Treemap2 到折线图)。每次下钻从服务器获取数据。向下钻取功能按预期工作。从 Treemap2 向上钻取到 Treemap1 也正在运行。

但是当我尝试从折线图向上钻取到 Treemap2 时,我得到一个错误:

Uncaught TypeError: Cannot read property 'x' of null 

在此钻取期间,我检查了系列数据,但它是空的。

这是向下钻取事件的代码:

drilldown(e){

    let drilldown_point = e.point;
    let chart = this;
    let current_level = 0;

    if(chart.drilldownLevels){
        current_level = chart.drilldownLevels.length;
    }

    //api call to get drill down data based on current level of drilldown
    if(current_level == 0){
        level_0_point_name = e.point.name;
        chart.showLoading('Loading ...');
        Api()
        .get('/dataFromServer')
        .then(response => {

            //get sub series data and set drilldown = true for 2nd  level drilldown
            let sub_series_data = response.data;
            sub_series_data.map(value => value.drilldown = true);

            //create series for sub treemap
            let sub_series = {
                type: 'treemap',
                data: sub_series_data,
            };

            //Update title and subtitle of subchart
            chart.update({
                title: {
                    text: "1st Level Drilldown"
                },
                subtitle: {
                    text: ""
                }

            });

            chart.addSeriesAsDrilldown(drilldown_point, sub_series);
            chart.applyDrilldown();
            chart.hideLoading();
        });
    }
    else if(current_level == 1){
        // console.log(this)
        level_1_point_name = e.point.name;
        chart.showLoading('Loading ...');
        Api()
        .get('/dataFromServer')
        .then(response => {

            //get sub series data and set drilldown = true for 2nd  level drilldown
            let sub_series_data = response.data;
            let x_axis_categories = sub_series_data.map(value => value.name)

            //create series for sub treemap
            let sub_series = {
                type: 'line',
                name: 'LineChart',
                colorByPoint: true,
                data: sub_series_data,
            };

            //Update title and subtitle of subchart
            chart.update({
                title: {
                    text: "2nd Level Drilldown"
                },
                subtitle: {
                    text: ""
                },
                yAxis: {
                    title: {
                        text: 'Number'
                    }
                },
                xAxis: {
                    title: {
                        text: 'X-axis title'
                    },
                    categories: x_axis_categories
                }

            });

            chart.addSeriesAsDrilldown(drilldown_point, sub_series);
            chart.applyDrilldown();
            chart.hideLoading();
        });
    }

}

这里是问题的小提琴链接 - https://jsfiddle.net/gaurav_neema/gkdoa9jr/7/

我已将代码简化为 2 个级别。向下钻取到任何节点,然后再次按下返回按钮以向上钻取。

这个错误的原因是什么?

【问题讨论】:

  • 您能否在带有示例数据的 jsfiddle 等在线代码编辑器中重现此问题?
  • 这里是小提琴链接...jsfiddle.net/gaurav_neema/gkdoa9jr/7 向下钻取到任何节点并尝试再次单击返回按钮以向上钻取。

标签: javascript highcharts


【解决方案1】:

你可以从我在here 上找到的this fiddle 得到一个想法。您必须在 Drillup() 事件下更新聊天。以下是小提琴的示例代码。

Highcharts.chart('container', {

  chart: {
    events: {
      drilldown: function(e) {
        console.log(this)
        var chart = this,
          drilldowns = chart.userOptions.drilldown.series,
          series = [];
        Highcharts.each(drilldowns, function(p, i) {
          if (p.id === e.point.name) {
            chart.addSingleSeriesAsDrilldown(e.point, p);
          }
        });
        chart.applyDrilldown();
        chart.update({
          chart: {
            type: 'column'
          }
        })
      },
      drillup: function() {
        let chart = this;
        console.log(this)
        chart.update({
          chart: {
            type: 'treemap',
          }
        })
      }
    }
  },
  
  legend: {
    enabled: true
  },

  series: [{
    animation: false,
    type: "treemap",
    data: [{
      id: 'B',
      name: 'Bananas',
      color: "#ECE100"
    }, {
      name: 'Anne',
      parent: 'B',
      value: 3,
      drilldown: true
    }, {
      name: 'Rick',
      parent: 'B',
      value: 10,
      drilldown: 'BRickSales'
    }, {
      name: 'Peter',
      parent: 'B',
      value: 1

    }]
  }],

  drilldown: {
    series: [{
      name: 'Ricks quotes',
      id: 'Rick',
      type: 'column',
      data: [
        ['v11', 30],
        ['v8', 17],
        ['v9', 8],
        ['v10', 5],
        ['v7', 3]
      ]
    }, {
      name: 'Rick Calls',
      id: 'Rick',
      type: 'line',
      data: [
        ['v11', 50],
        ['v8', 40],
        ['v9', 60],
        ['v10', 65],
        ['v7', 73]
      ]
    }, {
      name: 'Anne quotes',
      id: 'Anne',
      type: 'column',
      data: [
        ['v11', 2],
        ['v8', 7],
        ['v9', 3],
        ['v10', 5],
        ['v7', 3]
      ]
    }, {
      name: 'Anne Calls',
      id: 'Anne',
      type: 'line',
      data: [
        ['v11', 50],
        ['v8', 40],
        ['v9', 60],
        ['v10', 65],
        ['v7', 73]
      ]
    }]
  },

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多