Search code examples
graphamchartsbeta

amCharts 4 (v4) Set specific colors in pie chart


I am testing this framework Amcharts and i get to configure specific colors in amcharts v3 like this:


pieChart = AmCharts.makeChart("chartdiv", {
      "type": "pie",
      "colors": ["#388E3C", "#FBC02D", "#0288d1", "#F44336", "#8E24AA"],
      -- other stuff
    });

But i am finding hard to make the same in Amcharts V4... i tried things like this


pieSeries.colors._list = am4core.color['#388E3C', '#FBC02D', '#0288D1', '#F44336', '#8E24AA']

pieSeries.slices.template.fill = am4core.color('#388E3C', '#FBC02D', '#0288D1', '#F44336', '#8E24AA')


or this


"series": [{
    "type": "PieSeries3D",
    "colors": ["#388E3C", "#FBC02D", "#0288d1", "#F44336", "#8E24AA"],
}]

but in the end does not work... anyone adventuring here too?

Thanks in advance.


Solution

  • colors is a ColorSet object, which is a wrapper for an array of color objects in AmCharts v4. You have to create an array of color objects, assign them to a ColorSet object's list array, then assign it to the series' colors property:

    var colorSet = new am4core.ColorSet();
    colorSet.list = ["#388E3C", "#FBC02D", "#0288d1", "#F44336", "#8E24AA"].map(function(color) {
      return new am4core.color(color);
    });
    pieSeries.colors = colorSet;