Q:

How to display the total value of a stacked bar chart in d3js

I've made this Plunkr where I would like to display the total value of each bar, it's currently displaying the total accumulated value of each slice of the bar (the black text) and the total value at the end, I only want to display the total value shown at the end of each bar.
I've commented out my attempt at this here
.text(function(d) {
                if (d["x1_"+TEAM] != maxValue){
                    "display", "none"; 
                    } else { return formatText(d["x1_"+TEAM])
                    }
                });

But the max value in maxValue is of course the max value of all bars combined.
Is a similar solution possible or do I have to sort the data in a completely new function?
Any help is appreciated!

A:

There are easier and better ways to create a selection with the total of each stacked bar. However, using your code and applying the minimum possible changes, just show the third object for each stacked bar:
.text(function(d, i) {
    if (i === 2) return formatText(d["x1_" + TEAM])
});

Or, if you want to do it without a magic number:
.text(function(d, i) {
    if (i === textOnBar4.data().length - 1) return formatText(d["x1_" + TEAM])
});

Here is your updated plunker: https://plnkr.co/edit/iovKsrx0Eg4pTUWTDUaA?p=preview

