susielu / d3-legend

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

Custom legend labels for quantile scale #29

Closed drapadubok closed 8 years ago

drapadubok commented 8 years ago

Hi, thank you so much for this package, helped me a lot! However, I have a small question, it seems that I'm missing some simple idea about creating custom legend labels. Right now I have labels that look like '40 to 102', but I would like to have something like '≤ 102'. For example, I have:

var colorScale = d3.scale.quantile()
  .domain([d3.min(values), d3.max(values)])
  .range(colorbrewer.Blues[8]);

// I thought that I could just map the values to format I wanted:
var newLabels = colorScale.quantiles().map( function(i) {
  return ' ≤ ' + i;
 });

var legend = d3.legend.color()
  .cells(newLabels)
  .scale(colorScale);

svg.select(".legendQuant")
  .call(legend);

However, when I do this, nothing changes, I still have the same '40 to 102' type of labels. Can you suggest how I can troubleshoot this?

Thank you!

drapadubok commented 8 years ago

Now that I've read into the source, I have a hunch that only the linear scale is supported with custom cell labels, am I correct? I can try to extend the functionality to add the custom labels to other scale, but maybe you have some ideas that can help on that front?

susielu commented 8 years ago

There is a labels properly. You can send it the output of your newLabels function:

// I thought that I could just map the values to format I wanted: var newLabels = colorScale.quantiles().map( function(i) { return ' ≤ ' + i; });

var legend = d3.legend.color() .cells(newLabels) .scale(colorScale) .labels(newLabels)

Let me know if you have any issues with getting that to work.

Thanks, Susie

drapadubok commented 8 years ago

Awesome, this worked out perfectly, thank you!