Cannot use attr with an object in D3 v4

From D3 v4 onwards you cannot use objects to set attr or style anymore. To do so, you have to reference the mini library D3-selection-multi:

<script src="https://d3js.org/d3-selection-multi.v0.4.min.js"></script>

After doing that, change your code from attr to attrs (yes, like a plural):

var textLabels = labelGroups.append("text").attrs({
    //mind the 's' here-------------------------ˆ
});

Do the same for the styles: it should be styles, as a plural, not style.

If you don’t want to change all this, simply do as the “regular” way: set x, y, text-anchor and class in separate attr.

Here is the selection-multi documentation: https://github.com/d3/d3-selection-multi/blob/master/README.md#selection_attrs

Leave a Comment