amchartsamcharts5

Amcharts5 XY-Charts Change color of xlabel under columns


I have this XYChart that I have created with Amcharts5:

enter image description here

I want to change the colors of the xlabels, like this:

enter image description here

This is my code:

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>Graph</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width; initial-scale=1.0" />
</head>
<body>
<!-- Amcharts -->
    <script src="https://cdn.amcharts.com/lib/5/index.js"></script>
    <script src="https://cdn.amcharts.com/lib/5/xy.js"></script>
    <script src="https://cdn.amcharts.com/lib/5/themes/Animated.js"></script>
<!-- //Amcharts -->
<!-- Chart code ::per_month -->
    <script>
        am5.ready(function() {
            // Create root element
            // https://www.amcharts.com/docs/v5/getting-started/#Root_element
            var rootA = am5.Root.new("chartdiv_per_month");

            // Set themes
            // https://www.amcharts.com/docs/v5/concepts/themes/
            rootA.setThemes([
              am5themes_Animated.new(rootA)
            ]);



            // Create chart
            // https://www.amcharts.com/docs/v5/charts/xy-chart/
            var chartA = rootA.container.children.push(am5xy.XYChart.new(rootA, {
              panX: false,
              panY: false,
              layout: rootA.verticalLayout
            }));


            // Set data
            var data = [{
                  xlabelXYChart: "Aug",
                  value1: 52
                },{
                  xlabelXYChart: "Sep",
                  value1: 53
                },{
                  xlabelXYChart: "Oct",
                  value1: 54
                },{
                  xlabelXYChart: "Nov",
                  value1: 56
                },{
                  xlabelXYChart: "Dec",
                  value1: 56
                },{
                  xlabelXYChart: "Jan",
                  value1: 56
                }];

            
            // Create axes
            // https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
            var xAxis = chartA.xAxes.push(am5xy.CategoryAxis.new(rootA, {
                categoryField: "xlabelXYChart",
                renderer: am5xy.AxisRendererX.new(rootA, {
                    minGridDistance: 70,
                    minorGridEnabled: true
                }),
                tooltip: am5.Tooltip.new(rootA, {}),
                stroke: am5.color(0xFFFFFF),
                visible: true
            }));

            xAxis.data.setAll(data);
            var yAxis = chartA.yAxes.push(am5xy.ValueAxis.new(rootA, {
                renderer: am5xy.AxisRendererY.new(rootA, {}),
                stroke: am5.color(0xFFFFFF),
                visible: false
            }));

            // Add series
            // https://www.amcharts.com/docs/v5/charts/xy-chart/series/
            function makeSeries(name, fieldName) {
                var series = chartA.series.push(am5xy.ColumnSeries.new(rootA, {
                    name: name,
                    xAxis: xAxis,
                    yAxis: yAxis,
                    valueYField: fieldName,
                    categoryXField: "xlabelXYChart"
                }));
                series.columns.template.setAll({
                    tooltipText: "{categoryX} {name}: {valueY}",
                    width: am5.percent(90),
                    tooltipY: 0,
                    fill: "#2b4356", // fill colors of bars
                    stroke: "#67b7dc" // stroke around bars
                });

                series.data.setAll(data);


                // Make stuff animate on load
                // https://www.amcharts.com/docs/v5/concepts/animations/
                series.appear();


                series.bullets.push(function () {
                    return am5.Bullet.new(rootA, {
                        locationX: 0.5,
                        locationY: 1.25,
                        sprite: am5.Label.new(rootA, {
                            centerX: am5.p50,
                            centerY: am5.p50,
                            text: "{value1}",
                            fill: am5.color(0xbbd6e3), // Values over graph bars
                            populateText: true
                        })
                    });
                });

                //legendA.data.push(series);
            }
            makeSeries("Assets", "value1");

        }); // end am5.ready()
    </script>

    <div id="chartdiv_per_month" style="width: 100%;height: 160px;"></div>

<!-- //Chart code :: per_month -->
</body>
</html>

What parameter can help me change the color of the xlabel? I have tried looking at the manual for color, however it is not clear to me what parameter to change. I have also looked at AxisLabel and it says that "fill" is Text color. If this is correct, then where should I put fill in my code?


Solution

  • According to provided code, to implement required functionality of changing amcharts x-axis label color, the following code can be used:

    xAxis.get("renderer").labels.template.setAll({
      fill: '#ff0000'
    });
    

    The following code is updated code.

    <!DOCTYPE html>
    <html lang="en-US">
    <head>
    <title>Graph</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width; initial-scale=1.0" />
    </head>
    <body>
    <!-- Amcharts -->
    <script src="https://cdn.amcharts.com/lib/5/index.js"></script>
    <script src="https://cdn.amcharts.com/lib/5/xy.js"></script>
    <script src="https://cdn.amcharts.com/lib/5/themes/Animated.js"></script>
    <!-- //Amcharts -->
    <!-- Chart code ::per_month -->
    <script>
        am5.ready(function() {
            // Create root element
            // https://www.amcharts.com/docs/v5/getting-started/#Root_element
            var rootA = am5.Root.new("chartdiv_per_month");
    
            // Set themes
            // https://www.amcharts.com/docs/v5/concepts/themes/
            rootA.setThemes([            
             am5themes_Animated.new(rootA)
            ]);
    
            // Create chart
            // https://www.amcharts.com/docs/v5/charts/xy-chart/
            var chartA = rootA.container.children.push(am5xy.XYChart.new(rootA, {
              panX: false,
              panY: false,
              layout: rootA.verticalLayout
            }));
    
            // Set data
            var data = [{
                  xlabelXYChart: "Aug",
                  value1: 52
                },{
                  xlabelXYChart: "Sep",
                  value1: 53
                },{
                  xlabelXYChart: "Oct",
                  value1: 54
                },{
                  xlabelXYChart: "Nov",
                  value1: 56
                },{
                  xlabelXYChart: "Dec",
                  value1: 56
                },{
                  xlabelXYChart: "Jan",
                  value1: 56
                }];
            
            // Create axes
            // https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
            var xAxis = chartA.xAxes.push(am5xy.CategoryAxis.new(rootA, {
                categoryField: "xlabelXYChart",
                renderer: am5xy.AxisRendererX.new(rootA, {
                    minGridDistance: 70,
                    minorGridEnabled: true
                }),
                tooltip: am5.Tooltip.new(rootA, {}),
                stroke: am5.color(0xFFFFFF),
                visible: true
            }));
    
            xAxis.data.setAll(data); 
        xAxis.get("renderer").labels.template.setAll({
      fill: '#ff0000'
    });
            var yAxis = chartA.yAxes.push(am5xy.ValueAxis.new(rootA, {
                renderer: am5xy.AxisRendererY.new(rootA, {}),
                stroke: am5.color(0xFFFFFF),
                visible: false
            }));
    
            // Add series
            // https://www.amcharts.com/docs/v5/charts/xy-chart/series/
            function makeSeries(name, fieldName) {
                var series = chartA.series.push(am5xy.ColumnSeries.new(rootA, {
                    name: name,
                    xAxis: xAxis,
                    yAxis: yAxis,
                    valueYField: fieldName,
                    categoryXField: "xlabelXYChart"
                }));
                series.columns.template.setAll({
                    tooltipText: "{categoryX} {name}: {valueY}",
                    width: am5.percent(90),
                    tooltipY: 0,
                    fill: "#2b4356", // fill colors of bars
                    stroke: "#67b7dc" // stroke around bars
                });
    
                series.data.setAll(data);
    
                // Make stuff animate on load
                // https://www.amcharts.com/docs/v5/concepts/animations/
                series.appear();
                series.bullets.push(function () {
                    return am5.Bullet.new(rootA, {
                        locationX: 0.5,
                        locationY: 1.25,
                        sprite: am5.Label.new(rootA, {
                            centerX: am5.p50,
                            centerY: am5.p50,
                            text: "{value1}",
                            fill: am5.color(0xbbd6e3), // Values over graph bars
                            populateText: true
                        })
                    });
                });
                //legendA.data.push(series);
            }
            makeSeries("Assets", "value1");
    
        }); // end am5.ready()
    </script>
    
    <div id="chartdiv_per_month" style="width: 100%;height: 160px;"></div>
    
    <!-- //Chart code :: per_month -->
    </body>
    </html>