I am trying to get the value of clicked slice in Amcharts 4 3d pie chart. I am using this below code:
<!-- Styles -->
<style>
#chartdiv {
width: 100%;
height: 500px;
}
</style>
<!-- Resources -->
<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>
<!-- Chart code -->
<script>
am4core.ready(function() {
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
var chart = am4core.create("chartdiv", am4charts.PieChart3D);
chart.hiddenState.properties.opacity = 0; // this creates initial fade-in
chart.legend = new am4charts.Legend();
chart.data = [
{
country: "Lithuania",
litres: 501.9
},
{
country: "Czech Republic",
litres: 301.9
},
{
country: "Ireland",
litres: 201.1
},
{
country: "Germany",
litres: 165.8
},
{
country: "Australia",
litres: 139.9
},
{
country: "Austria",
litres: 128.3
},
{
country: "UK",
litres: 99
},
{
country: "Belgium",
litres: 60
},
{
country: "The Netherlands",
litres: 50
}
];
var pieSeries = chart.series.push(new am4charts.PieSeries3D());
pieSeries.dataFields.value = "litres";
pieSeries.dataFields.category = "country";
// AMCHART OFFICIAL DOCUMENTATION: https://www.amcharts.com/docs/v4/tutorials/one-pulled-slice-per-pie-chart/#Solution
pieSeries.slices.template.events.on("hit", function(ev) {
var series = ev.target.dataItem.component;
console.log(series);
});
}); // end am4core.ready()
</script>
<!-- HTML -->
<div id="chartdiv"></div>
amCharts
Now, as per Amcharts official documentation I added the hit
event. Neither I know how to get the value of the clicked slice nor I found any documentation/post regarding it.
Now, there are a couple of stackoverflow posts about the same but for Amcharts version 3 library and I am particularly looking for Amchart's version 4 library.
Kindly guide me here on how to get the value of clicked slice in Amcharts version 4 library.
Update: I am looking forward to get the property of country
of chart.data
.
It actually shows in the link you provided. It is merely a bit burried. To get the assigned value
property of the clicked slice, you can access the dataItem.value
of the current element ev.target
:
//AMCHART OFFICIAL DOCUMENTATION: https://www.amcharts.com/docs/v4/tutorials/one-pulled-slice-per-pie-chart/#Solution
pieSeries.slices.template.events.on("hit", function(ev){
console.log(ev.target.dataItem.value)
});