d3 show labels only for ticks with data in a bar chart

前端 未结 1 576
我在风中等你
我在风中等你 2021-01-14 10:47

First, I\'m new with D3. :) I have a stacked bar chart. My y axis is a ordinal scale (state). In my data, I could have total=0 for some ticks. So, I just want to see the lab

相关标签:
1条回答
  • 2021-01-14 11:30

    You can use .tickFormat() to suppress the labels for those particular values. As the thing you want to check isn't part of the data that's available to the scale, you'll need to find it in your entire data:

    yAxis.tickFormat(function(d) {
        var val = 0;
        dataSet.forEach(function(item) {
            if(item.Date == d) val = item.total;
        });
        return val == 0 ? "" : d;
    });
    

    This will suppress the label (return "") if the total is 0 or the value can't be found in the data set. Complete demo here.

    0 讨论(0)
提交回复
热议问题