plouc / nivo

nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
https://nivo.rocks
MIT License
13.11k stars 1.03k forks source link

Choropleth: Support fill color by value or label #2606

Open fabio-carvalho88 opened 3 months ago

fabio-carvalho88 commented 3 months ago

Is your feature request related to a problem? Please describe. https://github.com/plouc/nivo/issues/2068

Describe the solution you'd like The goal is to have control over the color used for each country value.

Describe alternatives you've considered

I've tried using the following props:

  1. defs + match

Defining a pattern/color and then applying a match condition. The problem here is that it seems to only work with id and I want to be able to do it with values.

  1. fillColor

Passing a callback to apply color based on the current country label (name).

  fillColor={function ({ label }) {
          console.log("Label: ", label);
          if (
            tier1.find(({ id }) => id.toLowerCase() === label.toLowerCase())
          ) {
            return "#42108B";
          } else if (
            tier2.find(({ id }) => id.toLowerCase() === label.toLowerCase())
          ) {
            return "#7522E0"; // tier 2
          } else if (
            tier3.find(({ id }) => id.toLowerCase() === label.toLowerCase())
          ) {
            return "#EFE6FD"; // tier 3
          } else {
            return ""; 
          }
        }}