susielu / d3-legend

A reusable d3 legend component.
http://d3-legend.susielu.com/
Apache License 2.0
727 stars 104 forks source link

Support for d3.scaleThreshold #43

Closed curran closed 8 years ago

curran commented 8 years ago

I tried making a color legend using a d3.scaleThreshold, and here's what the legend looks like:

image

Here's the relevant bit of code for setting up the scale and the legend:

var colorScale = d3.scaleThreshold()
  .domain([ 0, 1000, 2500, 5000, 10000 ])
  .range(["#ffffb2","#fed976","#feb24c","#fd8d3c","#f03b20","#bd0026"]);
var legend = d3.legendColor()
  .scale(colorScale);

A nice solution to this would:

susielu commented 8 years ago

Curran,

Thanks for submitting this. In the latest version 2.13.0 I have added the ability for you to pass a function in with the labels parameter and created a helper function to deal with threshold labels but you could create any custom function you want.

Here is an example: http://d3-legend.susielu.com/#color-threshold Documentation on the new function has been updated on the labels section of each legend type:

labels([string] or function(options)) Passing a string: Sets the legend labels to the array of strings passed to the legend. If the array is not the same length as the array the legend calculates, it merges the values and gives the calculated labels for the remaining items.

Passing a function: This function is called for each generated label and gives you the options:

Hope this helps, Susie

curran commented 8 years ago

Wow, this is fantastic! Thank you so much for your work on this.