Raphaeljs将点击事件添加到饼图的切片中

时间:2015-03-13 16:00:57

标签: javascript jquery css svg

我需要回复Raphael饼图中每个切片上的点击事件。我试过这种方式,但它似乎没有这样做。我的代码只有两行注释为"我的代码"在下面的代码中,它直接来自RaphaelJS的演示饼图...

        <script>
        var u = "";
        var r = "";
        window.onload = function () {

            r = Raphael("holder"),
                pie = r.piechart(320, 240, 100, [25, 20, 13, 32, 5, 21, 14, 10,41,16,12,18,16,14,12,13], 
                    { legend: ["%%.%% - Enterprise Users", "IE Users"],
                    legendpos: "west", href: ["http://raphaeljs.com", "http://g.raphaeljs.com"]
                    }
                );

            r.text(320, 100, "Interactive Pie Chart").attr({ font: "20px sans-serif" });

            pie.hover(function () {
                u = this;                 // My Code   
                u.onclick = clickEvent;   //  hook to the function 
               this.sector.stop();
                this.sector.scale(1.1, 1.1, this.cx, this.cy);  // Scale slice 

                if (this.label) {                               // Scale button and bolden text 
                    this.label[0].stop();
                    this.label[0].attr({ r: 7.5 });
                    this.label[1].attr({ "font-weight": 800 });
                }
            }, function () {
                this.sector.animate({ transform: 's1 1 ' + this.cx + ' ' + this.cy }, 500, "bounce");


                if (this.label) {
                    this.label[0].animate({ r: 5 }, 1500, "bounce");
                    this.label[1].attr({ "font-weight": 400 });
                }
            });

        };
        function clickEvent(){
            console.log("Clicked!")
        }
    </script>

1 个答案:

答案 0 :(得分:0)

Raphaels的语法略有不同。

选中此documentation

您的代码应如下所示:

u.click(clickEvent);