Nivo: Bar Chart - map color from object

Created on 12 Jun 2019  路  2Comments  路  Source: plouc/nivo

Hi,

Was just wondering if there is a way to set a color from object for each bar in a ResponsiveBar chart. I have the following Code, not sure what I'm doing wrong.

import React from 'react';
import { ResponsiveBar } from '@nivo/bar'

class BarChart extends React.Component {
    state = {
        data: [{
                "id": "Product",
                "label": "Product",
                "value": 0.6665,
                "color": "#008CCC"
            },
            {
                "id": "Technology",
                "label": "Technology",
                "value": 0.0396,
                "color": "#F15A22"
            },
            {
                "id": "All Services",
                "label": "All Services",
                "value": 0.2939,
                "color": "#05b1ff"
            }
        ]
    }

    componentWillReceiveProps(props) {
        console.log('props',props)
    }

    render() {
        const {data} = this.state
    return (
        <div style={{height:600}}>

        <ResponsiveBar
        data={data}
        // keys={data.map(i => i.id)}
        indexBy="id"
        margin={{ top: 50, right: 150, bottom: 80, left: 120 }}
        padding={0.3}
        groupMode="grouped"
        layout="horizontal"
        colors = {data.map(c => c.color)}
        colorBy = "id"
        defs={[
            {
                id: 'dots',
                type: 'patternDots',
                background: 'inherit',
                color: '#38bcb2',
                size: 4,
                padding: 1,
                stagger: true
            },
            {
                id: 'lines',
                type: 'patternLines',
                background: 'inherit',
                color: '#eed312',
                rotation: -45,
                lineWidth: 6,
                spacing: 10
            }
        ]}
        fill={[

        ]}
        borderColor={{ from: 'color', modifiers: [ [ 'brighter', '1.6' ] ] }}
        axisTop={null}
        axisRight={null}
        axisBottom={{
            tickSize: 5,
            tickPadding: 5,
            tickRotation: 0,
            legend: '',
            legendPosition: 'middle',
            legendOffset: 23
        }}
        axisLeft={{
            tickSize: 5,
            tickPadding: 5,
            tickRotation: 0,
            legend: '',
            legendPosition: 'middle',
            legendOffset: -40
        }}
        enableGridX={true}
        enableGridY={false}
        labelSkipWidth={12}
        labelSkipHeight={12}
        labelTextColor={{ from: 'color', modifiers: [ [ 'darker', 1.6 ] ] }}
        legends={[]}
        animate={true}
        motionStiffness={90}
        motionDamping={15}
    />
    </div>
    );
  }
}

export default BarChart;

Thank you

Most helpful comment

You should change the colorBy property to index. That will resolve your issue.

All 2 comments

You should change the colorBy property to index. That will resolve your issue.

Indeed that fixed my issue Thanks @wyze

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhe1ka picture zhe1ka  路  3Comments

danpettay picture danpettay  路  3Comments

p45mark picture p45mark  路  3Comments

tj picture tj  路  4Comments

pratikguru picture pratikguru  路  3Comments