amCharts 4(v4)在饼图中设置特定颜色

时间:2018-05-16 15:03:43

标签: graph amcharts beta

我正在测试这个框架Amcharts,我可以在amcharts v3中配置特定颜色,如下所示:


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

但我发现很难在Amcharts V4中做同样的事情......我尝试过这样的事情


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

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


或者


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

但最终不起作用......在这里冒险的人呢?

提前致谢。

3 个答案:

答案 0 :(得分:2)

colors是一个ColorSet对象,它是AmCharts v4中color个对象数组的包装器。您必须创建一个color个对象数组,将它们分配给ColorSet对象的list数组,然后将其分配给系列' colors财产:

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

答案 1 :(得分:1)

我发现了一种略有不同的方式:

  const myColors= [
    "#0000ff",
    "#ff0000",
    "#ffff00",
    "#ff00ff"
  ];
  series.columns.template.adapter.add("fill", (fill, target) => {
    return am4core.color(myColors[target.dataItem.index]);
  });

至少这适用于am4charts.ColumnSeries()对象。当然,列数必须等于颜色数。

答案 2 :(得分:1)

您可以这样做:

pieSeries.colors.list = [
    new am4core.color('#388E3C'),
    new am4core.color('#FBC02D'),
    new am4core.color('#0288D1'),
    new am4core.color('#F44336'),
    new am4core.color('#8E24AA'),
]

或者像@xorspark

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

文档在这里:https://www.amcharts.com/docs/v4/concepts/colors/#Manually_setting_color_sets