'How to label one specific scatter point (not all points) using d3 or regular javascript?
The annotation should read “min y-value: ” where is the minimum y. I wrote the following codes which labels the same smallest y value to all points but I want the label only displaying on the one scatter point who has the smallest y value
Please help!
var min = d3.min(dataset, function(d) { return d3.min(d); });
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) {return "min y-value"+min;})
.attr("x", function(d) {return xScale(d[0]);})
.attr("y", function(d) {return yScale(d[1]);})
Solution 1:[1]
You could add a filter in order to only show the text on certain data elements.
// If you really want the min of y-values - is it
// possible that you meant d[1] in the following min function?
var min = d3.min(dataset, function(d) { return d[1]; });
svg.selectAll("text")
.data(dataset)
.enter()
.filter(function(d) { return d3.min(d) > min })
.append("text")
.text(function(d) {return "min y-value"+min;})
.attr("x", function(d) {return xScale(d[0]);})
.attr("y", function(d) {return yScale(d[1]);})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | likle |