处理不同点击的电影切片

Akh*_*oul 5 javascript amcharts pie-chart

我有一个不同切片的AmPiechart.我想对每个切片的单击进行不同的操作.

PieChart.addListener("clickSlice", handleClickPie);

function handleClickPie(e) {

        }
Run Code Online (Sandbox Code Playgroud)

我想做的事情如下:

如果ClickSliceNo == 1:Action1

如果ClickSliceNo == 2:Action2

如果ClickSliceNo == 3:Action3

我如何在handleClickPie()函数中处理它?

mg1*_*075 1

是的,正如您在评论中指出的那样,您可以访问切片项目的属性,然后根据需要处理这些属性。

这是一个例子:


JS

var chart = AmCharts.makeChart("chartdiv", {
    "type": "pie",
    "theme": "light",
    "dataProvider": [{
        "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
    }],
    "valueField": "litres",
    "titleField": "country",
    "balloon": {
        "fixedPosition": true
    },
    "listeners": [{
        "event": "clickSlice",
        "method": myCustomClick
    }]
});

function myCustomClick(e) {
    // to see the full api, log out "e"
    // console.log(e);
    var country = e.dataItem.dataContext.country;
    if (country === "Lithunia") {
        alert("Lithuania: the home of amCharts.");
    } else if (country === "Germany") {
        alert("Munich is a city in Germany.");
    } else if (country === "Austria") {
        alert("Skiing in Austria is awesome.");
    } else {
        alert("You have clicked " + country + ".");
    }
}
Run Code Online (Sandbox Code Playgroud)

CSS

#chartdiv {
    width: 100%;
    height: 500px;
    font-size: 11px;
}
Run Code Online (Sandbox Code Playgroud)

超文本标记语言

<div id="chartdiv"></div>
Run Code Online (Sandbox Code Playgroud)