Nivo: Missing 'colorBy' type in BarSvgProps

Created on 20 Aug 2020  路  6Comments  路  Source: plouc/nivo

I created a ResponsiveBarChart and I added colors and colorBy props to adjust the colors of the bars. How every TypeScript compiler complains about colorBy prop being missed in the prop types. More specifically, colorBy prop is not in BarProps

I would love to open a PR for this but couldn't find a contribution guide so created this issue instead.

Screen Shot 2020-08-20 at 1 22 00 PM

Funny that there are two workarounds in the screenshot for different Typescript issues.

Most helpful comment

To achieve the same results you would do this:

+import { getOrdinalColorScale } from '@nivo/colors'


```diff
<ResponsiveBar
        margin={{ top: 32, right: 48, bottom: 56, left: 48 }}
-       colors={{ scheme: 'orange_red' }}
-       colorBy="index"
+       colors={getOrdinalColorScale({ scheme: 'orange_red' }, 'index')}
        layout="horizontal"

All 6 comments

This is deprecated, you should be using the colors prop. See https://github.com/plouc/nivo/pull/904#issuecomment-626407337

@wyze But if I take out colorBy from the below code scheme doesn't work. If I put it in and specify as colorBy="index" it works.

<ResponsiveBar
        margin={{ top: 32, right: 48, bottom: 56, left: 48 }}
        colors={{ scheme: 'orange_red' }}
        colorBy="index"
        layout="horizontal"
        keys={keys}
        padding={0.2}
        indexBy={indexBy}
        data={data.sort((a, b) => a.count - b.count)}
        animate={false}
        enableGridY={false}
        enableGridX
        labelFormat={labelValue =>
          ((
            <tspan x={-16}>{replaceThousandWithK(labelValue)}</tspan>
          ) as unknown) as string
        }
        borderRadius={2}
        tooltip={customTooltip}
        axisLeft={{
          renderTick,
        }}
        layers={['grid', 'bars', 'axes'] as BarLayerType[]} // This pushes axes in front of bars so tick labels stay over bars
      />

To achieve the same results you would do this:

+import { getOrdinalColorScale } from '@nivo/colors'


```diff
<ResponsiveBar
        margin={{ top: 32, right: 48, bottom: 56, left: 48 }}
-       colors={{ scheme: 'orange_red' }}
-       colorBy="index"
+       colors={getOrdinalColorScale({ scheme: 'orange_red' }, 'index')}
        layout="horizontal"

@wyze Thank you for helping, this works! Is there a part in the docs that explains how to use getOrdinalColorScale function? - or others if there are..

No I don't think so. I would imagine it would be here: https://nivo.rocks/guides/colors

Me too, but couldn't find it. Thanks so much for helping, again!

Hey @plouc - I just wanted to let you know about this thread. It might be very useful to have this info in https://nivo.rocks/guides/colors.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danpettay picture danpettay  路  3Comments

zhe1ka picture zhe1ka  路  3Comments

p45mark picture p45mark  路  3Comments

KENNYSOFT picture KENNYSOFT  路  3Comments

cedmax picture cedmax  路  4Comments