score:6
Accepted answer
You can see a working example on this jsFiddle. Every time you run the code, you'll get different colors.
Here is one of the result :
Using Chart.js plugins, you can create your own backgroundColor
& borderColor
properties and then assign them to the chart :
var randomColorPlugin = {
// We affect the `beforeUpdate` event
beforeUpdate: function(chart) {
var backgroundColor = [];
var borderColor = [];
// For every data we have ...
for (var i = 0; i < chart.config.data.datasets[0].data.length; i++) {
// We generate a random color
var color = "rgba(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ",";
// We push this new color to both background and border color arrays
// .. a lighter color is used for the background
backgroundColor.push(color + "0.2)");
borderColor.push(color + "1)");
}
// We update the chart bars color properties
chart.config.data.datasets[0].backgroundColor = backgroundColor;
chart.config.data.datasets[0].borderColor = borderColor;
}
};
You can see a working example on this jsFiddle. Every time you run the code, you'll get different colors.
Here is one of the result :
Source: stackoverflow.com
Related Query
- applying background-color in bar chart using chart.js
- How to use set the color for each bar in a bar chart using chartjs?
- Change the background color of tooltip dynamically using chart color
- How to draw multiple color bar in a bar chart using chart.js
- How to make the background color of the chart as gradient using Chart.js
- The colors of the bar chart in chart.js is not showing. I tried background color and fill color but none of it worked
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- Chartjs Bar Chart add background color to value labels
- Background color does not work when trying to create my data before using scatter chart with chart.js
- Angular 4: Different color for bar in bar chart dynamically using ChartJS
- how to create bar chart with group and sam color for each group using chart.js?
- Chart area background color chartjs
- Chart.js line chart set background color
- How to save Chart JS charts as image without black background using blobs and filesaver?
- Overlapping Bar Chart using Chart.js
- How to create stacked bar chart using react-chartjs-2?
- Chart.js bar chart : Grid color and hide label
- set background color to save canvas chart
- Different color for each column in angular-chartjs bar chart
- angular-chart.js bar chart with 100% opacity color
- Chartjs - show elements in all datasets on hover using bar chart
- How to set a full length background color for each bar in chartjs bar
- Gradient color for each bar in a bar graph using ChartJS
- set y-axis scale manually in a bar chart using angular-chart.js
- X-axis multiple colored label for bar chart using chart.js
- Multiple stacked bar chart using ChartJs
- ChartJS click on bar and change it's background color
- chart.js bar chart color change based on value
- Chart.js - Color specific parts of the background in a line chart
- unable to add background color to the canvas using jspdf and chartjs
More Query from same tag
- Weather graph using Angular 2, chart.js and json data
- ng2 chart multiple chart update
- How to shrink the height and increase the line width of a custom Chartjs horizontal line?
- mysql sum query for chartjs
- How to import Patternomaly into Ionic 2 project?
- chartjs bar chart align the legend to left position
- Getting the HTML code of a chart created by chart.js
- ChartJs DrawBorder() Configuration not working
- Radar chart Display legend
- Contrast chart.js datalabels colors with the background
- custom tooltips with react-chartjs-2 library
- chart.js not displayed in angular2 (rc1)
- chart.js plotting timeseries
- How to create a chart.js scatter chart with data from two lists
- Charts JS: Doughnut chart and hiding tooltip for specific data index within each dataset
- PrimeNg bar chart how to show a label for the y-axis
- Placeholder on canvas of ChartJs and Angular
- Unable to make the x-zoom to work when xAxes type is different from 'time'
- I want to change color of individual bar of bar graph
- Hide/show chart.js charts
- How to update the chart dynamically with vue-chartjs?
- How do i use chartjs in react project properly?
- Chartjs, scatter with For-Loop
- Not all date data shows on x axis line chart
- Add rectangle fill to line chart
- Can't Customize Chart.js
- How to align ticks label in chartJS 3.x?
- Laravel determine the date from database if MONDAY-SUNDAY
- Chart JS 2.x: How to show a tooltip in a timeline chart?
- Multiple Chart.js Charts in Partial Views Overwriting Each Other