How to change colors in react charts

πŸ‘:0

The legend label will be correctly displayed if you define label on your dataset, this was mentioned by WhiteHat in his comment.

In order to obtain the background color below the lines (basically an area chart), you need to add fill: true to the dataset.

To make it work, you also need to import and register Filler from chart.js.

Please take a look at this Code Sandbox and see how it works.

πŸ‘:-1

Here’s how I added styled to my chart:

function MyChart({}) {
  const { data } = useChartConfig({
    height: 200,
    grouping: "primary",
    dataType: "ordinal",
  });

  const getSeriesStyle = React.useCallback((series) => {
    // Based off my chart bars
    const colorPalette = {
      series1: "#354657",
      series2: "#5597e2",
      series3: "#28A96C",
      series4: "#d44401",
      series5: "#ffe65b",
      series6: "#ffab1b",
    };

    return {
      fill: colorPalette[series.label],
    };
  }, []);

  return <Chart data={data} getSeriesStyle={getSeriesStyle} />;
}

Leave a comment