2017-04-25 196 views
0

我有一個畫布JS圖表,如果值爲0,我想隱藏標籤。我的兩個數據點是非零的。我的JavaScript是:CanvasJS圖表隱藏標籤?

  var chart = new CanvasJS.Chart("chartContainer", 
      { 
       title:{ 
        text: "" 
       }, 
       legend: { 
        maxWidth: 350, 
        itemWidth: 120 
       }, 
       data: [ 
       { 
        type: "pie", 
        showInLegend: true, 
        toolTipContent: "${y} - #percent %", 
        dataPoints: [ 
         { y: debt, indexLabel: "Debt Payments" }, 
         { y: housing, indexLabel: "Housing" }, 
         { y: utilities, indexLabel: "Utilities" }, 
         { y: foodandsupplies, indexLabel: "Food and Supplies"}, 
         { y: transportation, indexLabel: "Transportation" }, 
         { y: health, indexLabel: "Health"}, 
         { y: otherDebts, indexLabel: "Other payments"} 
        ] 
       } 
       ] 
      }); 
      chart.render(); 

JavaScript的的結果是:

Pie Chart

我將不勝感激任何幫助。謝謝!

回答

1

通過遍歷圖表選項並將indexLabel設置爲所有這些數據點的空字符串,可以隱藏零點爲y值的dataPoints的indexLabels。

在渲染圖表之前添加此代碼段,即chart.render(),它應該可以正常工作。

for(var i = 0; i < chart.options.data[0].dataPoints.length; i++) { 
if(chart.options.data[0].dataPoints[i].y === 0) 
    chart.options.data[0].dataPoints[i].indexLabel = ""; 
} 

希望它有幫助。