RaphaelJs 饼图悬停动画

发布于 2024-12-19 07:34:18 字数 786 浏览 3 评论 0原文

我正在自定义下面链接的拉斐尔网站上给出的饼图 http://raphaeljs.com/pie.html

此图表显示悬停切片时的动画,此动画只需将切片扩展一点,

我想要的是将切片与我使用以下代码行的转换属性播放的图表分开

,但无法按照我的意愿制作。

p.mouseover(function () {
var xis= p.getBBox(true);
p.stop().animate({transform: "s1.1 1.1 "+ cx + " " + cy }, ms, "linear");
txt.stop().animate({opacity: 1}, ms, "linear");
}).mouseout(function () {
p.stop().animate({transform: ""}, ms, "linear");
txt.stop().animate({opacity: 0}, ms);
});

更改第 3 行的 cx 和 cy 实际上以相同的方式固定了每个切片的动画,也就是说,在悬停时每个切片都会不断改变位置。

http://imageshack.us/photo/my-images/690/sliced1。 png

有人请帮我解决这个问题

i am customizing the pie chart given on the raphael site below link
http://raphaeljs.com/pie.html

this chart shows animation when hover a slice, this animation simply expand the slice a little

what i want is to separate the slice from the chart

i played with the transform property of following lines of code but could not make it as i want.

p.mouseover(function () {
var xis= p.getBBox(true);
p.stop().animate({transform: "s1.1 1.1 "+ cx + " " + cy }, ms, "linear");
txt.stop().animate({opacity: 1}, ms, "linear");
}).mouseout(function () {
p.stop().animate({transform: ""}, ms, "linear");
txt.stop().animate({opacity: 0}, ms);
});

Changing the line 3's cx and cy actually fixed the animation for every slice in the same manner, that is, on hover every slice will change the position constantly.

http://imageshack.us/photo/my-images/690/sliced1.png

anyone please help me out to solve this problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不回头走下去 2024-12-26 07:34:18

如果我正确理解你的问题,那么当有人将鼠标悬停在切片上时,你希望切片与饼图完全断开连接。

为此,您需要平移该段,这样您就可以将 SVG 对象沿给定方向(即 x、y 坐标)移动。我不是 SVG 专业人士,所以我建议您自己研究一下它的完整功能;无论如何,要使用 Raphael 执行这些类型的操作,您可以使用 Element.transform,或者可以在 animate 调用中提供变换值。

第二个是您提供的示例中发生的情况,除了使用比例转换之外,如 transform 中的前导 s 所示:“s1.1 1.1.。

,您想要使用移动对象但不会使其变大的平移 - 它使用 t

在这里 将执行此操作:

        p.mouseover(function () {
            var distance = 20;
            var xShiftTo = distance*Math.cos(-popangle * rad);
            var yShiftTo = distance*Math.sin(-popangle * rad);
            p.stop().animate({transform: "t" + xShiftTo + " " + yShiftTo}, ms, "bounce");
            txt.stop().animate({opacity: 1}, ms, "bounce");
        }).mouseout(function () {
            p.stop().animate({transform: ""}, ms, "bounce");
            txt.stop().animate({opacity: 0}, ms);
        });

在示例中, distance 指切片应移动多远(因此请随意调整它),xShiftToyShiftTo 计算 x、y 值对象应该相对于它们当前所在的位置移动,请注意,这有点复杂 - 您需要计算出与饼图中心相距给定角度的 x、y 值。文本的定位也执行类似的操作。这个,所以我只是从那里得到了所需的数学知识。另外,我只保留了反弹动画,但您可以将其更改为线性或任何您想要的。希望有帮助。

If I understand your question correctly, you want the slice to completely disconnect from the pie chart when somebody hovers over it.

To do this, you want to translate the segment, which allows you to move an SVG object in a given direction, toward x, y co-ordinates. I'm no SVG pro, so I'd suggest taking a look into the full functionality of this yourself; regardless, to do these types of operations with Raphael, you can use the Element.transform, or can provide transform values in an animate call.

The second of these is what is happening in the example you provided, except a scale transformation is being used, as indicated by the leading s in transform: "s1.1 1.1.. A scale will make the object bigger.

Here, you want to use a translation which moves the object but doesn't make it bigger - it uses a t.

Here is a slightly edited block of code that will do this:

        p.mouseover(function () {
            var distance = 20;
            var xShiftTo = distance*Math.cos(-popangle * rad);
            var yShiftTo = distance*Math.sin(-popangle * rad);
            p.stop().animate({transform: "t" + xShiftTo + " " + yShiftTo}, ms, "bounce");
            txt.stop().animate({opacity: 1}, ms, "bounce");
        }).mouseout(function () {
            p.stop().animate({transform: ""}, ms, "bounce");
            txt.stop().animate({opacity: 0}, ms);
        });

In the example, distance refers to how far the slice should move away (so feel free to adjust this), xShiftTo and yShiftTo calculate the x, y values that the object should shift by, relative to where they currently are. Note that this is a little complicated - you need to figure out the x, y values at a given angle away from the pie centre. The positioning of the text also does something like this, so I just took the required maths from there. Also, I just left the bounce animation, but you can change that to linear or whatever you want. Hope that helps.

故笙诉离歌 2024-12-26 07:34:18

您可能应该使用 Raphael 中内置的 .hover。请参阅此处的文档:http://raphaeljs.com/reference.html#Element.hover

通过奥利的例子,我能够弄清楚大部分基本的动画原理。作为一个数学大师,这个例子有很多空白需要填补。这是一个功能齐全的版本(经过测试)。享受!

pie.hover(function () {
    // Stop all existing animation
    this.sector.stop();

    // Collect/Create variables
    var rad = Math.PI / 180;
    var distance = 50; // Amount in pixels to push graph segment out
    var popangle = this.sector.mangle; // Pull angle right out of segment object
    var ms = 300; // Time in milliseconds

    // Setup shift variables to move pie segments
    var xShiftTo = distance*Math.cos(-popangle * rad);
    var yShiftTo = distance*Math.sin(-popangle * rad);

    this.sector.animate({transform: "t" + xShiftTo + " " + yShiftTo}, ms, "linear");
}, function () {
    // Passing an empty transform property animates the segment back to its default location
    this.sector.animate({ transform: '' }, ms, "linear");
});

You should probably be using .hover which is built into Raphael. See documentation here: http://raphaeljs.com/reference.html#Element.hover

Working off of Oli's example I was able to figure out most of the basic animation principles. Not being a math guru there were a lot of gaps to fill in for the example. Here is a fully functioning version (tested). Enjoy!

pie.hover(function () {
    // Stop all existing animation
    this.sector.stop();

    // Collect/Create variables
    var rad = Math.PI / 180;
    var distance = 50; // Amount in pixels to push graph segment out
    var popangle = this.sector.mangle; // Pull angle right out of segment object
    var ms = 300; // Time in milliseconds

    // Setup shift variables to move pie segments
    var xShiftTo = distance*Math.cos(-popangle * rad);
    var yShiftTo = distance*Math.sin(-popangle * rad);

    this.sector.animate({transform: "t" + xShiftTo + " " + yShiftTo}, ms, "linear");
}, function () {
    // Passing an empty transform property animates the segment back to its default location
    this.sector.animate({ transform: '' }, ms, "linear");
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文