question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Help needed : colors in dynamic pie chart

See original GitHub issue

Hi,

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 :

image

Any place where I can find more info on this ?

Best regards and thanks in advance,

Davy

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
simonbrunelcommented, Feb 12, 2018

@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:

//////////// NOT IMPLEMENTED YET! //////////////////

backgroundColor: function(context) {
  return whateverColorLib.generate(context.dataIndex);
}

// or

backgroundColor: function() {
   return 'rgb('
      + Math.round(Math.random() *255) + ','
      + Math.round(Math.random() *255) + ','
      + Math.round(Math.random() *255) + ','
      + ')';
}

// or

var palette = [
 'rgb(255, 159, 64)',
 'rgb(255, 205, 86)',
 'rgb(0, 163, 51)',
 'rgb(54, 162, 235)',
 'rgb(153, 102, 255)',
 'rgb(201, 203, 207)',
 'rgb(0,0,255)'
]

backgroundColor: function(context) {
  return palette[context.dataIndex % palette.length];
}

I’m not sure Chart.js will ever implement default colors, I would rather see these theming features built in an external plugin.

0reactions
MrBean14commented, Feb 12, 2018

Thanks for all the help. It is working now. Will close this issue !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found