Total height of Stacked Bar on tip of every rectangle in stacked Bar Chart using d3

后端 未结 1 1168
一整个雨季
一整个雨季 2020-12-12 02:48

I drawn a stackedbar Chart in which now I trying to place the Total value(I mean the yaxis value) on tip of the every rectangle. I have coded to fetch the details but here t

相关标签:
1条回答
  • 2020-12-12 03:22

    Here is what you need: First, we'll set fData as the data for the texts:

    layer.selectAll("text.rect")
        .data(fData)
        .enter()
        .append("text")
    

    But, as this dataset doesn't have the correct yScale value, we'll have to extract it using a filter:

    .attr("y", function (d) { 
        var filtered = fData.filter(function(e){ 
            return e.Total_Units == d.Total_Units
        });
        return y(filtered[0].Total_Lines) - 3; 
    })
    

    All together:

    layer.selectAll("text.rect")
        .data(fData)
        .enter().append("text")
        .attr("text-anchor", "middle")
        .attr("x", function(d) {
            return xScale(d.orders) + xScale.rangeBand() / 2;
        })
        .attr("y", function(d) {
            var filtered = fData.filter(function(e) {
                return e.Total_Units == d.Total_Units
            });
            return y(filtered[0].Total_Lines) - 3;
        })
        .text(function(d) {
            return d.Total_Units
        })
        .style("fill", "4682b4");
    

    Here is your fiddle: https://jsfiddle.net/wnwb6meh/

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