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.
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"
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 will be determined by attribute "data-style-padding"
on the legend element.
Defaults to 5.
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 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 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.
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"
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 will be determined by attribute "data-style-padding"
on the legend element.
Defaults to 5.
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 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
I tried using d3.legend.js with d3 v4 but ran into a problem. So I ported index.html to use d3 v4 to see if the problem was in my code or in d3.legend.js. See https://gist.github.com/louking/6491d18c9cef56011971be76a85cde8e (http://bl.ocks.org/louking/6491d18c9cef56011971be76a85cde8e for running version) - the problem is at line 47 of d3.legend.js Uncaught
TypeError: Cannot read property '0' of undefined
.I'm not that familiar with d3 (or the changes from v3) to understand this error -- any ideas?
Also posted at http://stackoverflow.com/questions/38774855/d3-legend-js-with-d3-v4 because it's not clear author is responding to comments here.
thanks