2016-03-30 25 views
5

我有一個AmPiechart不同切片。我想對每個切片的單獨點擊執行不同的操作。處理不同點擊的ampiechart切片

PieChart.addListener("clickSlice", handleClickPie); 

function handleClickPie(e) { 

     } 

我想要做的事,如:

如果ClickSliceNo == 1:措施1

如果ClickSliceNo == 2:措施2

如果ClickSliceNo == 3:措施3

我如何處理handleClickPie()函數?

+0

發現:e.dataItem.value == 「SliceName」 –

回答

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 + "."); 
    } 
} 

CSS

#chartdiv { 
    width: 100%; 
    height: 500px; 
    font-size: 11px; 
} 

HTML

<div id="chartdiv"></div>