score:2

Accepted answer

well, i used the same node package in a project with different approach kinda work for me. almost all the charts take the same attributes.

basically, this attribute dataset={{ backgroundcolor: ['white', 'yellow' ], }} is all you need to colour each bar. you can either pass string or array of string to backgroundcolor.

the backgroundcolor in dataset takes two types of data string and array(object). typical examples of passing data are below.

  1. when you set backgroundcolor to a string, it applied the same colour to each bar. e.g backgroundcolor: 'red'

barchart - <barchart dataset={{ backgroundcolor: 'red', }} />

bar chart one color]

  1. when you set backgroundcolor to an array, it applied each colour in the array to each bar. e.g backgroundcolor: ['red', 'yellow'], then you create a loop of colours base on the data length.

column chart - <columnchart dataset={{ backgroundcolor: ['red', 'yellow' ], }} />

enter image description here

react implementation below:

/* eslint-disable no-plusplus */
 import react from 'react';
 import { columnchart, barchart } from 'react-chartkick';
 import { chartone } from '../common/chartdata';
 import 'chart.js';

 const monthlygrowth = () => {
    const handlebgcolors = () => {
       const firstcolor = "#a00b16", secondcolor = "#faa226";
       const arrofbgcolors = [];
       for (let i = 1; i <= chartone.length; i++) {
           if (i % 2 === 0) {
              arrofbgcolors.push(secondcolor)
           } else {arrofbgcolors.push(firstcolor)}
       }
       return arrofbgcolors;
   }

   return (
     <div classname="bukka-card uk-card-default bg-white pt-4 pb-2 mr-1 pl-3 pr-2 pl- 
       lg-5">
        <h2 classname="mt-2">4,500,000</h2>
        <barchart
           dataset={{ borderwidth: 0, width: 0, backgroundcolor: handlebgcolors(), }}
           data={chartone}
        />
      </div>
    )
}

export default monthlygrowth;

Related Query

More Query from same tag