Help needed : colors in dynamic pie chart
See original GitHub issueHi,
I’m creating a pie chart but I don’t know the number of labels at moment of development. Data is taken from databases and calculated dynamically. Meaning today there can be 20 labels, tomorrow 30.
Currently I see that all pieces of the pie have the same color. I don’t know how to set the creation of the graph so that each label gets an owner color. It may also be dynamic but nice if all different.
My code now :
<script>
window.chartColors = {
red: 'rgb(255, 0, 0)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(0, 163, 51)',
blue: 'rgb(54, 162, 235)',
purple: 'rgb(153, 102, 255)',
grey: 'rgb(201, 203, 207)',
darkblue: 'rgb(0,0,255)'
};
var color = Chart.helpers.color;
var data_viewer = <?php echo json_encode($resultaten); ?>;
//alert(data_viewer);
var myObj = JSON.parse(data_viewer);
var totalen = [];
var labels = [];
var aantallen = [];
for (x in myObj) {
labels.push(x);
totalen.push(myObj[x].totaal);
aantallen.push(myObj[x].aantal);
}
var pieChartData = {
labels: labels,
datasets: [{
data: totalen,
backgroundColor: chartColors,
borderColor: window.chartColors.darkblue,
borderWidth: 1,
}]
};
var ctx = document.getElementById('myChart').getContext('2d');
var myPieChart = new Chart(ctx, {
type: 'pie',
data: pieChartData,
options: {
legend: {
position: 'top',
},
}
});
</script>
PS I have reused the color script from a bart chart I made before. So I hoped I could reuse.
Result is looking like this now :
Any place where I can find more info on this ?
Best regards and thanks in advance,
Davy
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Changing color of pie chart dynamically - OpenText Forums
I'm finding its difficult to display color in PIE chart or Bar chart dynamically and need your help. For eg: Class A: 10....
Read more >How to put dynamic colors for pie chart - chart js - Stack Overflow
this is my suggestion : Step 1 colors=[];. Step 2 for(let i=0;i<this.data.length;i++){ ...
Read more >Setting up colors dynamically of a pie chart based on ... - Telerik
We have a requirement to set up the colors of a pie chart dynamically based on the color code retrieved from the database....
Read more >Video: Customize a pie chart - Microsoft Support
Click the chart you want to change. · In the upper-right corner, next to the chart, click Chart Styles. · Click Color and...
Read more >Setting pie chart colors in a dynamically rendered chart
I'm using a pie chart and I want to set custom colors. I've figured out how to do that in the 'series' segment...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@MrBean14 As @JanOonk said, you can pick whatever color lib and generate yourself the palette you want based on how much data you have. However, it’s not very handy to work with arrays, we need to implement scriptable options in all controllers (currently only implemented in the bubble chart). Then it will be really easy to implement your use case:
I’m not sure Chart.js will ever implement default colors, I would rather see these theming features built in an external plugin.
Thanks for all the help. It is working now. Will close this issue !