How to add jquery click event to gRaphael graphics?

半城伤御伤魂 提交于 2019-12-12 09:43:44

问题


I made a chart using g.Raphael:

$(function(){
    var r = Raphael("pieChart"),
            pie = r.piechart(320, 240, 100, [55, 20, 13, 32, 5, 1, 2, 10]);

            r.text(320, 100, "Interactive Pie Chart").attr({ font: "20px sans-serif" });
            $(pie.sector).click(function(){
                alert('hi');//not work!
            })

})

Later I added the click event to pie.sector, but my event is not work... any one know the right way to handle gRaphael with jQuery?


回答1:


Here you go

Iterate over you pie slices and add click handler to them

for(var index_i=0;index_i < pie.covers.items.length;index_i++){
    pie.covers.items[index_i].click(clickSomeSlice);
}


var clickSomeSlice = function () {
    alert("clicked on\t"+this.label[1].attrs.text);
};

Here a complete jsfiddle example




回答2:


Just select the pie

$(pie).click(function(){
    alert('hi');
})


来源:https://stackoverflow.com/questions/9767709/how-to-add-jquery-click-event-to-graphael-graphics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!