在 AmCharts 中添加过滤/下拉菜单
Adding filtering / dropdown menu in AmCharts
我有一个数据库,其中包含 3 个不同国家(法国、意大利和西班牙)按工作类型划分的工人比例(我发明了这些数字)。
我想要一个带有 filtering/dropdown 菜单的条形图,允许选择在图中代表哪个国家/地区。请参阅下面的示例。
有人知道怎么做吗?
我有一个 CodePen,里面有我的数据和图形:https://codepen.io/European-DataLab/pen/LYpxqwG?editors=0010
我的代码是:
// 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;
您可以更改过滤器,方法是将系列的 valueY
字段更改为下拉列表中指定的值,然后在图表上调用 invalidateData
:
document.getElementById('filter').addEventListener('change', function(e) {
series1_1.dataFields.valueY = e.target.value;
chart.invalidateData();
});
演示:
// 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>
我有一个数据库,其中包含 3 个不同国家(法国、意大利和西班牙)按工作类型划分的工人比例(我发明了这些数字)。
我想要一个带有 filtering/dropdown 菜单的条形图,允许选择在图中代表哪个国家/地区。请参阅下面的示例。
有人知道怎么做吗?
我有一个 CodePen,里面有我的数据和图形:https://codepen.io/European-DataLab/pen/LYpxqwG?editors=0010
我的代码是:
// 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;
您可以更改过滤器,方法是将系列的 valueY
字段更改为下拉列表中指定的值,然后在图表上调用 invalidateData
:
document.getElementById('filter').addEventListener('change', function(e) {
series1_1.dataFields.valueY = e.target.value;
chart.invalidateData();
});
演示:
// 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>