Within Nadieh Bremer's coding structure, this is the method she employs for aligning the labels:
.attr("x", function (d, i) {
return rScale(maxValue * cfg.labelFactor) * Math.cos(angleSlice * i - Math.PI / 2); })
.attr("y", function (d, i) {
return rScale(maxValue * cfg.labelFactor) * Math.sin(angleSlice * i - Math.PI / 2); })
When dealing with text-only labels, positioning them in the center can be achieved easily using:
.attr("text-anchor", "middle")
However, when it comes to images, there isn't a direct text-anchor
equivalent. In the case of an image, the x
and y
coordinates point to the top-left corner. As seen in the provided screenshot, all small squares have their top-left corners right at the center:
https://i.sstatic.net/CmZbE.png
To properly position the images, we need to shift them upwards and towards the left until the centers are aligned with where the top left corners were initially set. Since all images share the legend
class (each being 60x60 pixels), this adjustment can be made by:
d3.selectAll(".legend").attr("transform", "translate(-30,-30)");
An alternative approach would be to preset the correct values for x
and y
as detailed in ego2dot0's response.