javascript - 从 highcharts 图表中删除所有系列数据的正确方法?

标签 javascript charts highcharts

更新:这是一个显示问题的 jsfiddle:http://jsfiddle.net/pynju/1/

单击星期一的蓝色列。当详细 View 加载时,请注意 01-07 有 3 列(预期为 2)。单击最高的栏以返回原始 View 。请注意,xAxis 上的标签没有被删除。

===============

我有一个包含 2 个系列的条形图,并排显示为成对的条形。

series: [{
         showInLegend: false,
         data: dowChartData
      },{
         showInLegend: false,
         data: avgUserDowChartData
      }],

.

dowChartData = [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   {
                y: 98.74,
                color: '#0072ff',
                drilldown: {
                   name: 'Category Engagement - Sunday',
                   categories: ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23'],
                   data: [0,637,0,0,0,173,48.54,48.54,0,0,0,0,0,0,102.24,166.36,706.59,699.18,298.32,184.14,97.08,1539,0,1224.56],
                   color: '#0072ff',
                   data2: [506.80686467275,354.56354558498,333.25158689567,234.19283190879,234.82132336088,220.03247578171,222.86420797556,218.14034615202,170.42559544164,171.54776353196,249.24788461442,345.14915669555,206.65543589797,243.38811965637,367.02593304906,378.83677778129,467.45739743621,424.26264387522,639.60922934374,679.71299714907,373.26353846375,480.94380626458,551.82326068362,466.77469230724],
                   color2: '#C00'
                }
             }
AND SIMILAR

.

avgUserDowChartData = [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         {
                y: 142.35,
                color: '#C00'
             },
AND SIMILAR

初始数据为星期数据,X轴为:周日-周一-周二-周三-周四-周五-周六

初始系列有一个带有新数据和数据 2 的向下钻取元素 (见上文)

以下钻演示代码为例,我有以下代码:

column: {
              borderWidth: 0,
            cursor: 'pointer',
            point: {
               events: {
                  click: function(event) {
                     var drilldown = this.drilldown;
                     if (drilldown) { // drill down
                        setChart(dowChart, drilldown.name, drilldown.categories, drilldown.data, drilldown.color, drilldown.data2, drilldown.color2);
                     } else { // restore
                        setChart(dowChart, '', dowCategories, dowChartData);
                     }
                  }
               }
            },

设置图表处理程序:

function setChart(chart, name, categories, data, color, data2, color2) {
      chart.xAxis[0].setCategories(categories);
//      chart.series[0].remove();
      for (var i = 0; i < chart.series.length; i++) {
          chart.series[i].remove();
      }
      chart.addSeries({
         showInLegend: false,
         name: name,
         data: data,
         color: color || 'white'
      });
      if (typeof(data2) != undefined && data2.length > 0) {
          chart.addSeries({
             showInLegend: false,
             name: name,
             data: data2,
             color: color2 || 'white'
          });
      }
   }

初始图表显示完美无缺: initial display

当您单击任何蓝色条(具有向下钻取的数据集)时,前 7 个 x 轴项目会变得不稳定: drill down - broken display

就好像初始数据集没有被代码删除一样:

for (var i = 0; i < chart.series.length; i++) {
          chart.series[i].remove();
      }

当您单击任何条形图以重置为原始数据集/系列时: reset data to original set - broken display

所以...很明显,我使用的删除系列代码不起作用。什么是完全删除图表上的数据以及我每次需要根据点击的内容显示的 2 个系列的最佳方法?

最佳答案

尝试删除所有图表系列,

while(chart.series.length > 0)
    chart.series[0].remove(true);

它对我有用。代码

for (var i = 0; i < chart.series.length; i++)

不会工作,因为每次调用 remove()chart.series.length 都会减少。这样,i 将永远不会达到预期的长度。希望这会有所帮助。

关于javascript - 从 highcharts 图表中删除所有系列数据的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6604291/

相关文章:

highcharts - 图表显示了非常小的饼图

javascript - 选择文档模式 8 时,High Chart Legends 旋转在 IE 10,11 上不起作用

javascript - 用 true 或 false 初始化变量是否确定了 Javascript 中内容的方向?

javascript - 将形状加载到 d3

javascript - 如何在jquery中输入之前删除标签

php - 使用mysql数据生成Google图表

javascript - 如何在 d3 日历热图中显示像 github 热图这样的日期?

javascript - 带有外部 CSV 的 Highchart(在外部域中)

javascript - Firestore - 将数组传递给 arrayUnion()

javascript - 在javascript中将字符串更改为数字