score:85
I believe that creation of color schemes is a whole science of its own. It makes sense to me that something like that has been left out of Chart.js, since there are more critical goals to pursue.
All colors used in chart examples in the docs are defined explicitly. And this two-month-old issue features a categorical response from a Chart.js member that default colors are not available in Chart.js 2.
So, you have three choices.
The first choice is to make some colors yourself. I guess you would not have asked the question, had you been into something like that (I know that the results would be horrible, if I did something like that).
The second choice is to look for color schemes and color scheme generators online and pick some color schemes that please you.
The third and interesting choice is to look for a JavaScript library that can produce color schemes at will.
There are a couple of alternative choices. A nice one, which is available under very permissive licensing, is the Colour Palette Generator. You may see it in action along Chart.js 2 here. The sample is also available below:
var ctx = document.getElementById("myChart");
var myData = [12, 19, 3, 5, 2, 3];
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ["First", "Second", "Third", "Fourth", "Fifth", "Sixth"],
datasets: [{
label: '# of Votes',
data: myData,
backgroundColor: palette('tol', myData.length).map(function(hex) {
return '#' + hex;
})
}]
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.min.js"></script>
<script src="https://raw.githubusercontent.com/google/palette.js/master/palette.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
How to use the library is described here. In short, you may create a 10-color palette from a specific color scheme using the following:
var seq = palette('tol-sq', 10);
The result is an array of hex strings (e.g. "eee8d5"). In order to use it where Chart.js is expecting colors, you may use map
to prepend "#" to each string, like in the example above.
score:2
Another alternative to add automatic color generation for Chart.js
is to use the plugin:
import autocolors from 'chartjs-plugin-autocolors';
Chart.register(autocolors);
const chart = new Chart(ctx, {
// ...
options: {
plugins: {
autocolors: {
mode: 'data'
}
}
}
});
Note: This plugin requires Chart.js 3.0 or later
score:21
I would actually recommend ChartsJS-Plugin-ColorSchemes. Simply importing it after chartjs will be enough for you to have automatic coloring but adding options will allow you to make custom palettes, for example.
...
options: {
plugins: {
colorschemes: {
scheme: 'tableau.Tableau20'
}
}
}
Source: stackoverflow.com
Related Query
- Automatic colors assignment in chart.js 2.x doesn't work anymore, used to work in v. 1.x
- Chartjs random colors for each part of pie chart with data dynamically from database
- chart.js Line chart with different background colors for each section
- Chart.js ng2-charts colors in pie chart not showing
- Can't get bar chart colors in Chart js working in React Js
- Change labels colors for radar chart js
- How do you set pie chart colors in angular-chart.js
- Chart.js Radar chart legend label font size doesn't work
- How do I put this on Real-Time? I already put (async: True) but it doesnt work
- Configure Pie Chart Colors Using Chart.js and PHP
- Minimum value for x Axis doesn't work for horizontal bar chart | ChartJS
- Line chart doesn't work with type time chart.js
- Populate colors on a Chart.js chart dynamically
- How to change the chart line or area colors according to the user need?
- Charts.js scales yaxes ticks min max doesnt work
- Code doesn't work after updating chart.js versioning
- Chart JS - Grid colors and gradient fill not showing
- Chart.js: Bar chart multiple colors
- angular-chartJs change chart background and highlight colors
- Charts.js - Colors for stacked bar on multi-series line/bar chart are not working
- Can the colors of bars in a bar chart be varied based on their value?
- Charts.js - Bar chart different colors for value intervals not working
- Chart.js : Controller for Scatter chart doesn't work for draw function
- Chart JS tooltips diffrent colors for label
- I cant get Legend to work for my chartjs donut chart
- Using Different Colors In a Chart
- Chart.js in Angular 10: Specified colors not shown in multi-series bar chart (instead random colors)
- Vue Chart JS options aren't used
- colors are not updated when updating chart
- Legend color not working with randomly generated background colors in chartjs pie chart
More Query from same tag
- chart.js and asynchronous loading
- How to draw lines between minimum and maximum value to average value in chartJS?
- Chartjs Stacked bar chart not displaying correctly
- ChartJS not working in Vue
- Set fix values on y-axis vue-chartjs
- Chart.js addData is undefined when using SignalR
- How to use a string returned from php by ajax as data for a chart.js chart?
- Chart.js v3 Fade Animation in delay
- angular chartjs line chart default options
- Chart.js line chart does not render data lines on iOS
- Angular-Charts - Pie Chart,show labels inside each slice of data
- What is the correct way to send 2 parameters in socket.io using lambdas? (I get undefined when printing in client and server)
- Chartkick-vue bar chart - "horizontalBar" is not a registered controller
- ChartJS : chartjs-plugin-datalabels The value is in the wrong place
- After rendering one chart when trying to render a new one chart.js gives "Check that a complete date adapter is provided."
- Updating Chartjs Line chart from database
- Options: plugins not functioning
- How to disable event during a state change
- How to implement dynamic data on chartjs?
- How to displays item names from the database using a monthly or yearly chart PHP MySQL
- How to install Chart.js and angular-chart.js (Error: Chart.js library needs to be included)
- how to dynamicly change the json data in chartjs
- Chart.js 2.1.2 Bar Chart Animation Issue
- Radar chart - show values near vertices in chart.js
- chartjs xaxis ticks with round values from shifted input data
- JavaScript. Find the sum of a certain parameter in an array to produce a popup if that sum == 0
- Chart.js multicolored fill sections
- chart.js scale xaxis with date, strange renderer
- How to update chart.js using Javascript
- How to make chartJs stacked bar always rounded when data is 0?