Chartjs-Javascript How to give color using conditional statement based on json array state value

1👍

Try like below

Keep a color map and iterate using map to get the mapped colors list.

const colorMap = {
    A: "yellow",
    B: "green",
    C: "gray"
};

let chartColors = {
    yellow: "rgb(244, 208, 104)", // A
    green: "#4CC26F", // B
    gray: "#EBEBEB" // C
};

const Function = {
    datasets: [
        {
            backgroundColor: functionStatus.map((colorCode) => chartColors[colorMap[colorCode]]),
            data: functionInfo // Lactobacillus Lactobacillus Glacagon toothpaste toothpaste Glacagon
        }
    ]
};

Leave a comment