Search code examples
javascriptamchartsamcharts4

Adding filtering / dropdown menu in AmCharts


I have a database with the share of workers by type of jobs for 3 different countries - France, Italy and Spain - (I invented the numbers).

I would like to have a bar chart with a filtering/dropdown menu that allow selecting which country to represent on the figure. See the example below.

Does anyone know how to do it ?

I have a CodePen with my data and figure: https://codepen.io/European-DataLab/pen/LYpxqwG?editors=0010

My code is:

// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);

// Add data
chart.data = [
  {
    "group": "High blue collar",
    "France": 10.5,
    "Italy": 9.8,
    "Spain": 11
  },
  {
    "group": "Low blue collar",
    "France": 26,
    "Italy": 22.5,
    "Spain": 23.9
  },
  {
    "group": "High white collar",
    "France": 36.8,
    "Italy": 35,
    "Spain": 33.9
  },
  {
    "group": "Low white collar",
    "France": 28.3,
    "Italy": 17,
    "Spain": 36
  }
];

// Create axis X
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "group";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 1;
categoryAxis.renderer.labels.template.rotation = -45;
categoryAxis.renderer.labels.template.verticalCenter = "top";
categoryAxis.renderer.labels.template.horizontalCenter = "right";
categoryAxis.renderer.grid.template.disabled = true;

// Create axis Y
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.title.text = "%";


// Create series for column
var series1_1 = chart.series.push(new am4charts.ColumnSeries());
series1_1.dataFields.valueY = "France";
series1_1.dataFields.categoryX = "group";
series1_1.strokeWidth = 0;
series1_1.fillOpacity = 0.7;

filtering menu


Solution

  • You can change the filter by changing the series' valueY field to the value specified in your dropdown and then calling invalidateData on the chart:

    document.getElementById('filter').addEventListener('change', function(e) {
      series1_1.dataFields.valueY = e.target.value;
      chart.invalidateData();
    });
    

    Demo:

    // Create chart instance
    var chart = am4core.create("chartdiv", am4charts.XYChart);
    
    // Add data
    chart.data = [
      {
        "group": "High blue collar",
        "France": 10.5,
        "Italy": 9.8,
        "Spain": 11
      },
      {
        "group": "Low blue collar",
        "France": 26,
        "Italy": 22.5,
        "Spain": 23.9
      },
      {
        "group": "High white collar",
        "France": 36.8,
        "Italy": 35,
        "Spain": 33.9
      },
      {
        "group": "Low white collar",
        "France": 28.3,
        "Italy": 17,
        "Spain": 36
      }
    ];
    
    // Create axis X
    var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
    categoryAxis.dataFields.category = "group";
    categoryAxis.renderer.grid.template.location = 0;
    categoryAxis.renderer.minGridDistance = 1;
    categoryAxis.renderer.labels.template.rotation = -45;
    categoryAxis.renderer.labels.template.verticalCenter = "top";
    categoryAxis.renderer.labels.template.horizontalCenter = "right";
    categoryAxis.renderer.grid.template.disabled = true;
    
    // Create axis Y
    var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
    valueAxis.title.text = "%";
    
    
    // Create series for column
    var series1_1 = chart.series.push(new am4charts.ColumnSeries());
    series1_1.dataFields.valueY = "France";
    series1_1.dataFields.categoryX = "group";
    series1_1.strokeWidth = 0;
    series1_1.fillOpacity = 0.7;
    
    document.getElementById('filter').addEventListener('change', function(e) {
      series1_1.dataFields.valueY = e.target.value;
      chart.invalidateData();
    });
    #chartdiv {
      width: 100%;
      height: 500px;
    }
    <script src="https://www.amcharts.com/lib/4/core.js"></script>
    <script src="https://www.amcharts.com/lib/4/charts.js"></script>
    <script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
    <select id="filter">
      <option>France</option>
      <option>Italy</option>
      <option>Spain</option>
    </select>
    <div id="chartdiv"></div>