[英]How to get the coordinates of mouse click on a slice of pie-chart : Amcharts
[英]How to get the value of clicked slice in Amcharts 4 pie chart
我試圖在 Amcharts 4 3d 餅圖中獲取單擊切片的值。 我正在使用以下代碼:
<!-- 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
現在,根據 Amcharts 官方文檔,我添加了hit
事件。 我既不知道如何獲取單擊切片的值,也沒有找到任何有關它的文檔/帖子。
現在,有一些關於相同的 stackoverflow 帖子,但對於 Amcharts 版本 3 庫,我特別在尋找 Amchart 版本 4 庫。
請在此處指導我如何獲取 Amcharts 版本 4 庫中單擊切片的值。
更新:我期待獲得chart.data
country
的屬性。
它實際上顯示在您提供的鏈接中。 它只是有點隱蔽。 要獲取單擊切片的value
屬性,您可以訪問當前元素ev.target
的dataItem.value
:
//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)
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.