Using JavaScript, I am dynamically creating a graph based on a Bootstrap template.
This particular graph displays various appointment types and their corresponding values.
try {
//bar chart
var ctx = document.getElementById("ReferralTypeWeekly");
if (ctx) {
ctx.height = 200;
var myChart = new Chart(ctx, {
type: 'bar',
defaultFontFamily: 'Poppins',
data: {
labels: ["Apt Type 1", "Apt Type 2", "Apt Type 3", "Apt Type 4", "Apt Type 5", "Apt Type 6", "Apt Type 7", "Apt Type 8", "Apt Type 9", "Apt Type 10", "Apt Type 11"],
datasets: [
{
label: "Week 06",
data: [7, 31, 47, 41, 10, 42, 3, 19, 16, 24, 0, 26],
borderColor: "rgba(0, 123, 255, 0.9)",
borderWidth: "0",
backgroundColor: "rgba(0, 123, 255, 0.5)",
fontFamily: "Poppins"
},
{
label: "Week 07",
data: [10, 67, 112, 57, 21, 39, 9, 23, 30, 26, 9, 54],
borderColor: "rgba(0,0,0,0.09)",
borderWidth: "0",
backgroundColor: "rgba(123, 255, 0,0.5)",
fontFamily: "Poppins"
},
{
label: "Week 08",
data: [4, 47, 93, 58, 21, 29, 6, 10, 32, 30, 6, 33],
borderColor: "rgba(0,0,0,0.09)",
borderWidth: "0",
backgroundColor: "rgba(255, 0, 123,0.5)",
fontFamily: "Poppins"
}
]
},
options: {
legend: {
position: 'top',
labels: {
fontFamily: 'Poppins'
}
},
scales: {
xAxes: [{
ticks: {
fontFamily: "Poppins"
}
}],
yAxes: [{
ticks: {
beginAtZero: true,
fontFamily: "Poppins"
}
}]
}
}
});
}
} catch (error) {
console.log(error);
}
In the code snippet where it references:
borderColor: "rgba(0, 123, 255, 0.9)",
and
backgroundColor: "rgba(0, 123, 255, 0.5)",
I wish to generate these colors randomly from a predefined list, ensuring each set has a unique color but maintains consistency between borderColor and backgroundColor for each value.
Despite researching, I am unsure how to implement this feature.
Some of my other graphs involve multiple datasets (more than just two like in this example).
I eagerly await any suggestions on how to proceed.
Cheers!
UPDATE : CHECK OUT THIS IMAGE OF THE GRAPH https://i.sstatic.net/jeKyz.png