How to dynamically set pie piece text colour on a C3 chart

peterc

I have a C3 pie chart similar to this online example. In my case I set the data.colors based on incoming external data.

My setup looks like the following (I pass in the colors to use)...

        this.pieChart = generate({
            data: {
              columns: columns,
              colors: colours,
              type: 'pie',
              onclick: (e) => {
                this.handlePieClick(e.id);
              },
            },
            bindto: '#pie-chart',
            tooltip: {
              show: false
            },
            transition: {
              duration: 1000
            },
            legend: {
              item: {
                onclick: id => {
                  this.handlePieClick(id);
                }
              }
            },
          });

My app has not control over these colors. When the colors are light, the white text is hard to see. I may have a mixture of light and dark colours. What I need to do is examine each color and then be able to set the text either white or black for each pie piece text color, however I can see no callback function I can override to do this.

Does anyone know a way to do this? The big thing being I have no control over the colors coming in, and need to set the text colors in code once I have the color data.

Thanks in advance for any help!

mgraham

Use the onrendered: option in the c3 configuration

In there, loop through the colours, and for each

  1. Construct a new colour that shows up against the pie colour

  2. Apply it to the text in the right segment using the class definitions (doable as the colour map and the pie segments element classes both contain the names of the data series)

http://jsfiddle.net/shxLfss3/2/

onrendered: function () {
    var colEntries = d3.entries(this.config.data_colors);
    colEntries.forEach (function (colEntry) {
        // get pie segment colour, make a contrasting colour from it
        var hsl = d3.hsl(colEntry.value);
        hsl.l = hsl.l > 0.5 ? 0 : 1;    // make black if light, white if dark
        var newCol = hsl.toString();
        // apply that color to the segmenbt's text
        var segment = d3.select(this.config.bindto+" .c3-chart-arc.c3-target-"+colEntry.key);
        segment.select("text").style("fill", newCol);
    }, this);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to dynamically set title in Pie Chart of Highcharts

From Dev

Change the Google pie Chart percentage text Colour

From Dev

VBA Pie Chart Legend - How to set legend text?

From Dev

VBA Pie Chart Legend - How to set legend text?

From Dev

Colour not rendering properly Pie Chart d3

From Dev

How to remove text from Pie chart section in IOS Swift 3?

From Dev

How to center chart title position dynamically inside pie chart in highcharts

From Dev

How to center chart title position dynamically inside pie chart in highcharts

From Dev

pie chart from json using c3 js

From Dev

How to set the numbers of labels displayed in a pie chart?

From Dev

How to set border for a pie chart in Sencha

From Dev

How to set specific color to the pie chart?

From Dev

How to dynamically set the colour of a 'div' element to match that of the colour picked on a colour widget using for loop?

From Dev

Set color in pie chart in c#.net

From Dev

How to set the text colour on a button? JAVA

From Dev

How to set a custom text highlighting colour

From Dev

How set color family to pie chart in chart.js

From Dev

How to create an array of objects dynamically in javascript for a pie chart

From Dev

How to update both the content and location of text labels on a D3 pie chart

From Dev

Converting pie chart to bar chart in pentaho dynamically

From Dev

How to update a D3 pie chart

From Dev

How to draw Normalize Stack chart using C3 chart?

From Dev

how to set two data label for pie chart using core plot

From Dev

Highcharts: How do I set dynamic data to the drilldown pie chart?

From Dev

Highcharts Pie Chart.How to set labels in two lines

From Dev

how to set two data label for pie chart using core plot

From Dev

How can I set circle size of the pie chart with jqplot?

From Dev

How to set pie chart wedge colors based on wedge values for highcharts?

From Dev

Python - How to change autopct text color to be white in a pie chart?

Related Related

  1. 1

    How to dynamically set title in Pie Chart of Highcharts

  2. 2

    Change the Google pie Chart percentage text Colour

  3. 3

    VBA Pie Chart Legend - How to set legend text?

  4. 4

    VBA Pie Chart Legend - How to set legend text?

  5. 5

    Colour not rendering properly Pie Chart d3

  6. 6

    How to remove text from Pie chart section in IOS Swift 3?

  7. 7

    How to center chart title position dynamically inside pie chart in highcharts

  8. 8

    How to center chart title position dynamically inside pie chart in highcharts

  9. 9

    pie chart from json using c3 js

  10. 10

    How to set the numbers of labels displayed in a pie chart?

  11. 11

    How to set border for a pie chart in Sencha

  12. 12

    How to set specific color to the pie chart?

  13. 13

    How to dynamically set the colour of a 'div' element to match that of the colour picked on a colour widget using for loop?

  14. 14

    Set color in pie chart in c#.net

  15. 15

    How to set the text colour on a button? JAVA

  16. 16

    How to set a custom text highlighting colour

  17. 17

    How set color family to pie chart in chart.js

  18. 18

    How to create an array of objects dynamically in javascript for a pie chart

  19. 19

    How to update both the content and location of text labels on a D3 pie chart

  20. 20

    Converting pie chart to bar chart in pentaho dynamically

  21. 21

    How to update a D3 pie chart

  22. 22

    How to draw Normalize Stack chart using C3 chart?

  23. 23

    how to set two data label for pie chart using core plot

  24. 24

    Highcharts: How do I set dynamic data to the drilldown pie chart?

  25. 25

    Highcharts Pie Chart.How to set labels in two lines

  26. 26

    how to set two data label for pie chart using core plot

  27. 27

    How can I set circle size of the pie chart with jqplot?

  28. 28

    How to set pie chart wedge colors based on wedge values for highcharts?

  29. 29

    Python - How to change autopct text color to be white in a pie chart?

HotTag

Archive