【问题标题】:Raphaeljs adding click events to a slice of the Pie chartRaphaeljs 将点击事件添加到饼图的一部分
【发布时间】:2015-03-19 15:38:00
【问题描述】:

我需要响应拉斐尔饼图中每个切片上的点击事件。我以这种方式尝试过,但似乎没有这样做。我的代码只是下面代码中注释为“我的代码”的两行代码,直接来自 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>

【问题讨论】:

  • 在悬停事件中附加点击事件是否有原因?在悬停功能之外执行pie.onclick = clickEvent 似乎更正确
  • @ShanRobertson 我需要在每个切片上触发。切片具有子组,因此单击然后请求新数据,该数据扩展为不同的饼图。这可能会持续几个级别。基本上是向下钻取的情况。还是谢谢你的回复...

标签: javascript jquery css svg


【解决方案1】:

Raphaels 语法有点不同。

查看documentation

您的代码应如下所示:

u.click(clickEvent);

【讨论】:

  • 感谢您的建议,但这是我在阅读 Raphael 文档后尝试的第一件事。但它没有对此作出回应。尽管如此,我还是感谢您的帮助。我找到了解决方案,并会在我有时间的时候发布。我还得问问拉斐尔的创造者……
猜你喜欢
  • 2011-08-26
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 1970-01-01
  • 2014-04-22
相关资源
最近更新 更多