d3 | reusable circle legend
This example shows a reusable SVG circle legend that maps numeric values to circle sizes using a square-root scale, with dashed guide lines and text labels for each value. Built with D3 v4, the `circleLegend` function accepts configuration options like domain, range, values, colors, and text padding, then renders circles, lines, and labels centered within a specified width and height. The legend is useful for bubble charts, allowing users to customize the suffix, text color, and circle stroke.
AI-generated descriptionneeded a simple circle legend for a bubble chart that I could pass in values and a domain
to initiate pass in a d3 selection to the circleLegend function and your dataset domain and values:
var l = circleLegend(d3.select('svg g#legend'))
.domain([0, 100])) // the dataset min and max
.range( [0, 80]) // the circle area/size mapping
.values( [8, 34, 89]) // pass in values (e.g. min,mean/median & max)
// optional
.width(500) // it centers to this
.height( 500) // it centers to this
.suffix('') // ability to pass in a suffix e.g. '%'
.circleColor( '#888') // stroke of the circles
.textPadding(40) // left padding on text
.textColor( '#454545') // the fill for text
// and render it
l.render()
note: the number of values you pass in will correlate to the number of circles
forked from <a href='http://bl.ocks.org/eesur/'>eesur</a>'s block: <a href='http://bl.ocks.org/eesur/7b63401950344f2dca1025d09cc7386d'>d3 | reusable circle legend</a>