I've been asked to add an interactive legend to my Multi-line chart. My Multi-line chart has a nest with two keys and it made things difficult for my legend. Here's an old EAMPLE I'm using for adding an interactive legend.
My issues:
1) I'm trying to display the top level key for the legend so that it shows system01, system02, system03 and so on instead of displaying the two keys together. For example, system01 0 and systmem01 2 become system01 so when it is clicked it will hide both lines.
2) After making the lines inactive, the tooltip still displays when mousing over the chart.
Here's my FIDDLE
Snippet for adding the Legend:
var chartBody = svg.append("g")
.attr("class", "chartbody")
.attr("clip-path", "url(#clip)");
for (var key in storages) {
if (storages.hasOwnProperty(key)) {
console.log("key:", key, [storages[key]]);
var capSeries = d3.line()
.x(function(d) {
return x(d.date); })
.y(function(d) {
return y(d.availableVolumeCapacity); })
.curve(d3.curveCardinal);
// mapping of data to line using the capSeries function
chartBody.append("path")
.data([storages[key]])
.attr("class", "line")
.attr("id", 'tag'+ key.replace(/\s+/g, ''))
.attr("d", capSeries)
.style("stroke", z(key));
}
}
// spacing for the legend
legendSpace = width/nest.length;
// Loop through each symbol / key
nest.forEach(function(d,i) {
// Add the Legend
svg.append("text")
.attr("x", (legendSpace/2)+i*legendSpace) // space legend
.attr("y", height + (margin.bottom/2)+ 5)
.attr("class", "legend") // style the legend
.style("fill", function() { // Add the colours dynamically
return d.z = z(d.key); })
.on("click", function(){
// Determine if current line is visible
var active = d.active ? false : true,
newOpacity = active ? 0 : 1;
// Hide or show the elements based on the ID
d3.select("#tag"+d.key.replace(/\s+/g, ''))
.transition().duration(100)
.style("opacity", newOpacity);
// Update whether or not the elements are active
d.active = active;
})
.text(d.key);
});
system01 0andsystmem01 2becomesystem01so when it is clicked it will hide both lines. - Clarkus978system01 0andsystem01 02so that it only displays a singlesystem01in the legend. Whensystem01is clicked on it will hide linessystem01 0andsystem01 2. - Clarkus978