Попытка выделить текущую линию пути, захватив pathID текущего пути и присвоив ему class .highlight , который составляет stroke-width: 2px
.
console.log показывает текущий путь , но не хочет назначать класс. Пожалуйста помоги. Что я здесь не так делаю?
Codepen
//Generate group (main)
let groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.on("mouseover", function(d) {
d3.select(this)
.call(revealCityName)
})
.on("mouseout", hideCityName)
//Path (path #, path line)
groups
.append("path").classed("line", true)
.attr("id", function(d) {
console.log(d.country)
if (d && d.length != 0) {
return d.country.replace(/ |,|\./g, '_');
}
})
.attr("d", d => line(d.emissions))
.classed("unfocused", true)
.style("stroke-width", d =>
d.country === "China" ? 1 : 0.2
)
.on("mouseover", highlightPath);
//Text (contries on the left y axis)
groups
.append("text")
.datum(function(d) {
return { country: d.country, value: d.emissions[d.emissions.length - 1]};
})
.attr("transform", function(d) {
if (d.value) {
return "translate(" + xScale(d.value.year) + "," + yScale(d.value.amount) + ")";
} else {
return null;
}
})
.attr("x", 3)
.attr("dy", 4)
.style("font-size", 10)
.style("font-family", "Abril Fatface")
.text(d => d.country)
.classed("hidden", function(d) {
if (d.value && d.value.amount > 700000) {
return false
} else {
return true;
}
})
И функция, в которой я пытаюсь назначить класс:
function highlightPath(d) {
let pathID = d3.select(this).attr("id");
console.log(pathID);
let currentId = d3.select(this).select("path#" + pathID)
console.log("Current ID: ",currentId)
.classed("highlight", true);
}