d3.legend example (d3 v4)
This example demonstrates a reusable legend component for D3 v4, added to a multi-series line chart of daily average temperatures for New York, San Francisco, and Austin. The legend is generated by calling `d3.legend` on a `g` element, automatically collecting items from any marks with a `data-legend` attribute, and matching colors from their fill or stroke. The legend supports explicit control over item order and color via `data-legend-pos` and `data-legend-color`, with padding and size configurable through `data-style-padding` and font-size. The visualization highlights how the legend responds to font-size changes, as the author re-invokes `d3.legend` after one second to demonstrate dynamic re-layout. Rendered in SVG using d3 v4, the example builds on a line chart of daily temperatures for New York, San Francisco, and Austin, loaded from a TSV file, with the legend drawn inside an SVG `<g>` element.
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>