2013-04-04 73 views
0

所有欄值如何刪除一個類別時,所有等於一個類別欄值0Highcharts條形圖刪除類別時類別等於0

教廷:(http://jsfiddle.net/no1uknow/EJFsH/1/

如果只有一個酒吧等於0,它不應該刪除欄。 但是,如果所有3條等於0,那麼該類別應該被刪除。

chart = new Highcharts.Chart({ 
     chart: { 
      renderTo: 'container', 
      type: 'column' 
     }, 
     title: { 
      text: 'Stacked column chart' 
     }, 
     xAxis: { 
      categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas'] 
     }, 
     yAxis: { 
      min: 0, 
      title: { 
       text: 'Total fruit consumption' 
      }, 
      stackLabels: { 
       enabled: true, 
       style: { 
        fontWeight: 'bold', 
        color: (Highcharts.theme && Highcharts.theme.textColor) || 'blue' 
       } 
      } 
     }, 
     legend: { 
      align: 'right', 
      x: -100, 
      verticalAlign: 'top', 
      y: 20, 
      floating: true, 
      backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'gray', 
      borderColor: '#CCC', 
      borderWidth: 1, 
      shadow: false 
     }, 
     tooltip: { 
      formatter: function() { 
       return '<b>'+ this.x +'</b><br/>'+ 
        this.series.name +': '+ this.y +'<br/>'+ 
        'Total: '+ this.point.stackTotal; 
      } 
     }, 
     plotOptions: { 
      column: { 
       stacking: 'normal', 
       dataLabels: { 
        enabled: true, 
        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white' 
       } 
      } 
     }, 
     series: [{ 
      name: 'John', 
      data: [5, 3, 4, 7, 2] 
     }, { 
      name: 'Jane', 
      data: [2, 2, 3, 2, 1] 
     }, { 
      name: 'Joe', 
      data: [3, 4, 4, 2, 5] 
     }] 
    }); 
}); 

回答

1

您需要初始化像這樣的圖表前,對數據進行過濾:

var data = [{ 
    name: 'Year 1800', 
    data: [0, 0, 0, 0, 0] 
}, { 
    name: 'Year 1900', 
    data: [0, 156, 947, 408, 6] 
}, { 
    name: 'Year 2008', 
    data: [0, 914, 4054, 732, 34] 
}]; 

data = $.grep(data, function (category) { 
    return $.grep(category.data, function (item) { 
     return item > 0; 
    }).length > 0; 
}); 

http://jsfiddle.net/EJFsH/2/

+0

謝謝賈斯汀這是一個很棒的解決方案,我沒有想到... – no1uknow 2013-08-21 17:02:21

1

的問題是,當你想動態刪除類別(通過將其設定setCategories函數),你也應該關心數據序列中的元素。換句話說,當你想修改類別時,你也應該刪除數據點。在使用highchart中的數據之前,最好先準備好沒有零的數據。

+0

謝謝塞巴斯蒂安我結束了重建我的數據之前手,因爲你建議...賈斯汀上面有一個很棒的解決方案,但我已經發布了我的代碼。 ..再次感謝! – no1uknow 2013-08-21 17:03:27