2017-04-25 1 views
0

Canvas JS 차트가 있는데 값이 0이면 레이블을 숨기고 싶습니다. 내 데이터 포인트 중 2 개가 0이 아닙니다. 내 자바 스크립트는 다음과 같습니다 자바 스크립트의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(); 

결과는 다음과 같습니다

Pie Chart

내가이 어떤 도움을 주셔서 감사합니다. 감사!

답변

1

차트 옵션을보고 indexLabel을 모든 해당 dataPoints에 대해 빈 문자열로 설정하여 dataPoints의 indexLabels을 0으로 y 값으로 숨길 수 있습니다.

차트를 렌더링하기 전에이 코드 스 니펫 즉 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 = ""; 
} 

희망이 있습니다.