I am currently using chartjs and I want to generate new random colors each time the page is refreshed. However, I need these colors to remain fixed and not change after a page refresh or reload. Below is the function I am working with:
getRandomRgb() {
var num = Math.round(0xffffff * Math.random());
var r = num >> 16;
var g = (num >> 8) & 255;
var b = num & 255;
return "rgb(" + r + ", " + g + ", " + b + ")";
}
This is where I implemented the function:
this.currentResponseItems.forEach((x) => {
x.Items.forEach((z) => {
if (!colorMap[z.Description]) {
colorMap[z.Description] = this.getRandomColor();
}
console.log(z.Description, " : ", colorMap[z.Description]);
z["phaseId"] = x.Id;
let zData = [];
let number = array.indexOf(z.phaseId);
if (z.Number != 0) {
zData[number] = z.Number;
this.barChartData.push({
data: zData,
stack: x.Id,
label: z.Description,
fill: false,
grouped: true,
idStatoAffiliazione: x.Id,
channelID: z.Id,
backgroundColor: colorMap[z.Description],
hoverBackgroundColor: colorMap[z.Description],
pointBackgroundColor: colorMap[z.Description],
pointBorderColor:colorMap[z.Description],
pointHoverBackgroundColor:colorMap[z.Description],
pointHoverBorderColor: colorMap[z.Description],
});
Below is the HTML code for the chartjs:
<div class="container-chart cursor-pointer" fxLayout="row" *ngIf="barChartData.length > 0" fxLayoutGap="20px">
<canvas baseChart fxFlex="100" [chartType]="'horizontalBar'" [datasets]="barChartData" [options]="barChartOptions" [labels]="barChartLabels" [colors]="graphColors" ></canvas>
<!-- <graph-legend fxFlex="20" [configs]="graphLegendConfig"></graph-legend> -->
</div>