Search code examples
amchartsamcharts4

Trying to set custom colours for a chart


I'm trying to add custom colours to a column chart, so each column has a different colour. I have the following code:

__this._chartColours = ['#2776BD', '#00A1D0','#00C195','#7ED321','#A8C600','#C9B600','#E3A600', '#F7941E', '#FC7149'];
__this._chart = am4core.create(__this._tileChartDiv[0], am4charts.XYChart);

if(result.chartDataMap != null)
{
    var colorSet = new am4core.ColorSet();
    var counter = 0;
    $.each(result.chartDataMap, function(xAxis, yAxis)
    {
        __this._dataProvider.push({"category": xAxis, "column-1": yAxis});
        __this._chart.colors.list.push(am4core.color(__this._chartColours[counter]));
    });
    __this._chart.data = __this._dataProvider;
    __this._chart.padding(40, 40, 40, 40);
    
    var categoryAxis = __this._chart.xAxes.push(new am4charts.CategoryAxis());
    categoryAxis.renderer.grid.template.location = 0;
    categoryAxis.dataFields.category = "category";
    categoryAxis.renderer.minGridDistance = 60;
    categoryAxis.title.text = result.xAxisTitle;

    var label = categoryAxis.renderer.labels.template;
    label.wrap = true;
    label.maxWidth = 120;

    var valueAxis = __this._chart.yAxes.push(new am4charts.ValueAxis());
    valueAxis.title.text = result.yAxisTitle;

    var series = __this._chart.series.push(new am4charts.ColumnSeries());
    series.dataFields.categoryX = "category";
    series.dataFields.valueY = "column-1";
    series.tooltipText = "{valueY.value}"
    series.columns.template.strokeOpacity = 0;

    __this._chart.cursor = new am4charts.XYCursor();
}

It renders the chart fine based on the dataprovider I create but it is not setting the colors. I got the colours code from here: https://www.amcharts.com/docs/v4/concepts/colors/. The XY Chart and derivative charts section

I tried to use a theme, but that didn't work either:

function am4themes_myTheme(target) 
{
    if (target instanceof am4core.ColorSet) 
    {
        $.each(__this._chartColours, function(index, item)
        {
            target.list.push(am4core.color(item));
        });                   
    }
}

am4core.useTheme(am4themes_myTheme);

It sets all the columns to the first colour. Then I tried adding a color property to the dataProvider for each column but again it sets them all to have the first colour.

I'm pretty much out of ideas.


Solution

  • There are a few issues here.

    First, if you want the chart to use only your colors, instead of appending to the default chart's ColorSet, you have to manually override it by assigning an array of Colors to chart.colors.list (instead of pushing values to it).

    Next, the column's color (fill) by default is based on its series. So even if you populate the chart's ColorSet, it's only each new series that will get a different color, not each column.

    To set an individual column's color it would be something like:

    column.fill = am4core.color("#2776BD");
    

    To get each column to have its own color, we can set that upon the column's first instantiation, i.e. on its template's inited event. Further, a column's dataItem will have a property/reference to its index, so we can use that with ColorSet's getIndex method to assign colors in sequence.

    So your final code might look something like this:

    __this._chart.colors.list = [
      am4core.color("#2776BD"),
      am4core.color("#00A1D0"),
      am4core.color("#00C195"),
      am4core.color("#7ED321"),
      am4core.color("#A8C600"),
      am4core.color("#C9B600"),
      am4core.color("#E3A600"),
      am4core.color("#F7941E"),
      am4core.color("#FC7149")
    ];
    series.columns.template.events.once("inited", function(event){
      event.target.fill = chart.colors.getIndex(event.target.dataItem.index);
    });
    

    Here's a fork of our Simple Column Chart demo with the above code and your custom colors:

    https://codepen.io/team/amcharts/pen/2ef06f392b347412c61bcdcd3439a5c6