d3.legend example (d3 v4)
This example demonstrates a reusable legend component for D3 v4 charts, built as a drop-in `.call(d3.legend)` on a `g` element. The legend scans the visualization for elements with a `data-legend` attribute, using those titles as unique entries (deduplicated via a set) and deriving colors from each item's fill or stroke (or an explicit `data-legend-color`). Item order is determined by the top of each element’s bounding box, overridable with `data-legend-pos`. The box padding defaults to 5 and adjusts via `data-style-padding`, while sizing uses `em` units based on font size. The example also demonstrates dynamic updates: after one second, the font size is changed and `d3.legend` is called again to show how the legend adapts.
AI-generated descriptiond3.legend
d3.legend is a quick hack to add a legend to a d3 chart. Simply add a g and .call(d3.legend). Any elements that have a title set in the "data-legend" attribute will be included when d3.legend is called. Each title will appear only once (even when multiple items define the same data-legend) as the process uses a set based on a existing names, not an array of all items.
Color
By default the color in the legend will try to match the fill attribute or the stroke attribute of the relevant items. Color can be explicitly defined by attribute "data-legend-color"
Order
The order of items in the legend will be sorted using the top of the bounding box for each included item. The order can be explicitly defined by attribute "data-legend-pos"
Padding
Padding will be determined by attribute "data-style-padding" on the legend element.
Defaults to 5.
Size
Size of the box is determined by font size, as items are placed using "em" and the frame around the items is based on the bounding box.
This Example
This example takes an existing Gist and adds a legend by defining data-legend for each series and calling d3.legend on a "g" element. To show the font-adjustment I change font-size after one second and call d3.legend again.
d3.legend
d3.legend is a quick hack to add a legend to a d3.graph. Simply add a g and .call(d3.legend). Any elements that has a title set in the "data-legend" attribute set will be included, when d3.legend is called. Each title will appear only once as the process uses a set based on a existing names, not an array of all items.
Color
By default the color in the legend will try to match the fill attribute or the stroke attribute of the relevant items. Color can be explicitly defined by attribute "data-legend-color"
Order
The order of items in the legend will be sorted using the top of the bounding box for each included item. The order can be explicitly defined by attribute "data-legend-pos"
Padding
Padding will be determined by attribute "data-style-padding" on the legend element.
Defaults to 5.
Size
Size of the box is determined by font size, as items are placed using "em" and the frame around the items is based on the bounding box.
This Example
This example takes an existing Gist and adds a legend by defining data-legend for each series:
.attr("data-legend",function(d) { return d.name})
Adding the legend is as simple as:
legend = svg.append("g")
.attr("class","legend")
.attr("transform","translate(50,30)")
.style("font-size","12px")
.call(d3.legend)
To show the font-adjustment I change font-size after one second and call d3.legend again.
##Comments on the original gist This line chart is constructed from a TSV file storing the daily average temperatures of New York, San Francisco and Austin over the last year. The chart employs conventional margins and a number of D3 features:
- d3.tsv - load and parse data
- d3.time.format - parse dates
- d3.time.scale - x-position encoding
- d3.scale.linear - y-position encoding
- d3.scale.category10, a d3.scale.ordinal - color encoding
- d3.extent, d3.min and d3.max - compute domains
- d3.keys - compute column names
- d3.svg.axis - display axes
- d3.svg.line - display line shape
forked from <a href='http://bl.ocks.org/louking/'>louking</a>'s block: <a href='http://bl.ocks.org/louking/6491d18c9cef56011971be76a85cde8e'>d3.legend example (d3 v4)</a>
forked from <a href='http://bl.ocks.org/GerardoFurtado/'>GerardoFurtado</a>'s block: <a href='http://bl.ocks.org/GerardoFurtado/93703aa76f8fe0d33c6149cc1a586d24'>d3.legend example (d3 v4)</a>