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.08k stars 1.02k forks source link

Pie: Fill color by label name? Callback function? #1370

Closed tony closed 3 years ago

tony commented 3 years ago

Is your feature request related to a problem? Please describe. There's been recent discussion in the tracker of Pie chart's getting an upgrade soon (animations, perhaps colors are already in?).

Though I'm not so sure if it's possible to fill colors based on the label now?

I have a color mapping (Programming Language -> Hex Codes based on GitHub colors) and a pie chart

image

Prior art: https://cv-react-v1.gitpull.com (see how programming language colors are synchronized, yellow for js, green for vim) image

Describe the solution you'd like

Having colors dynamically set via the label, via a callback function

I think https://nivo.rocks/storybook/?path=/story/pie--using-colors-from-data would do it? Is this in master? Currently live?

Describe alternatives you've considered I haven't found an example of it for nivo yet.

So far:

All other chart system's i've tried support coloring based on labels easily. Most use callbacks, other's use Array index ordering. If the color's index matches with the index of the value, the color is used.

Additional context

Related:

wyze commented 3 years ago

Which version of nivo are you on? The latest version supports a custom function for colors.

tony commented 3 years ago

@wyze I will re-read through the docs tomorrow evening - I didn't see anything about a custom color functions yet, I don't think.

I set up a staging example here: https://cv-react-v2.git-pull.com/dev/branch/v2-nivo/

Here's the code at the moment: https://github.com/tony/cv/blob/v2-nivo/packages/react/src/App.tsx#L182 / PR https://github.com/tony/cv/pull/1160

tony commented 3 years ago

@wyze Works! Thank you!

image

colors and sliceLabelsTextColor accepts a function now

        <div className="chartRow--donut">
          <ResponsivePie
            data={Object.entries(results.languageCount).map(
              ([languageName, count]) => {
                return {
                  id: languageName,
                  label: languageName,
                  value: count,
                };
              }
            )}
            colors={(item: { id: string }) => {
              return results.languages.find(
                (language) => language.id == item.id
              )?.ui?.backgroundColor;
            }}
            sliceLabelsTextColor={(item) => {
              return results.languages.find(
                (language) => language.id == item.id
              )?.ui?.color;
            }}
          />
        </div>
tony commented 3 years ago

@wyze

image

Tweaked more: https://cv-react-v2.git-pull.com/dev/branch/v2-nivo/

image

image