@cortez.connelly
In d3.js, you can create a legend by following these steps:
1
|
<svg id="legend"></svg> |
1 2 3 4 5 |
var svg = d3.select("#legend");
var legend = svg.append("g")
.attr("class", "legend")
.attr("transform", "translate(20,20)");
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var data = [{label: "Category 1", color: "red"}, {label: "Category 2", color: "blue"}, {label: "Category 3", color: "green"}];
var items = legend.selectAll(".item")
.data(data)
.enter()
.append("g")
.attr("class", "item")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
items.append("rect")
.attr("width", 10)
.attr("height", 10)
.attr("fill", function(d) { return d.color; });
items.append("text")
.text(function(d) { return d.label; })
.attr("x", 15)
.attr("y", 10);
|
This is a basic example of how you can create a legend in d3.js. You can customize the legend further by adding additional elements, such as lines or circles, or by adjusting the positioning and styling of the elements.