隐藏 CanvasJS 图表的标签?

Hiding labels for CanvasJS chart?

我有一个 Canvas JS 图表,如果值为 0,我想隐藏标签。我的 2 个数据点是非零的。我的 JavaScript 是:

           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

如有任何帮助,我将不胜感激。谢谢!

您可以隐藏 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 = "";
}

希望对您有所帮助。