简体   繁体   English

在RaphaelJS中自定义饼图

[英]Customizing Pie Chart in RaphaelJS

I am looking forward to customizing the Raphael JS Pie Chart ... 我期待自定义Raphael JS饼图 ...

Here is the code, http://jsfiddle.net/kcQbe/ 这是代码http://jsfiddle.net/kcQbe/

I have set 4 categories in it... 我在其中设置了4个类别...

1st of all i want the change the color fills or path's color on every category defined separately in JavaScript, for example like categories are looping into array which is called as data = [90, 90, 90, 90] here 90 is the degree at which we are defining the category's angle like that i want color should also b define separately and similar to that... may be like this fill = ["#000","#ccc","#ddd","#eee"] I could be wrong for that idea i am not sure... 首先,我要更改JavaScript中单独定义的每个类别的颜色填充或路径的颜色,例如,类似的类别循环进入数组,称为data = [90, 90, 90, 90]这里90是我们正在定义类别的角度,例如我想要的颜色,也应该b分别定义并且与之类似...可能就像这样fill = ["#000","#ccc","#ddd","#eee"]我不确定这个主意是错的...

Also on clicking it the color changes, i want that the color should remain the same as it is before clicking it... 另外在单击它时颜色会发生变化,我希望颜色应保持与单击之前相同。

Can anyone help it??? 谁能帮忙吗???

By going through the other answers by Jeff and Alex, created a working example - Demo 通过查看Jeff和Alex的其他答案,创建了一个有效的示例- 演示

I think this is your desired output 我认为这是您想要的输出

You probably should not be using Raphael JS if you don't know much about JavaScript. 如果您对JavaScript不太了解,则可能不应该使用Raphael JS。

To change the fill color, you have to modify this line: 要更改填充颜色,您必须修改以下行:

fill: "hsb(" + clr + ", .75, .8)"

Turn that into a function that tells it what color you want. 把它变成一个告诉你想要什么颜色的函数。 Currently, clr is a simple function given by 目前,clr是由

    clr = (a2 - a1) / 360;
    a1 = (a1 % 360) * Math.PI / 180;
    a2 = (a2 % 360) * Math.PI / 180;

To get rid of animation, remove the line "animate();" 要删除动画,请删除“ animate();”行。 (below data[i] *= 2) (在data [i] * = 2以下)

Simply update the Jeff answer: 只需更新Jeff答案:

For color: 对于颜色:

Pass color reference to function: r.customAttributes.segment = function (x, y, r, a1, a2, color) {... 将颜色引用传递给函数: r.customAttributes.segment = function (x, y, r, a1, a2, color) {...

Add color array var after data: var data = [90, 90, 90, 90], color = ['#ff0000', '#00ff00', '#0000ff', '#ff0000'], ... 在数据后添加颜色数组var: var data = [90, 90, 90, 90], color = ['#ff0000', '#00ff00', '#0000ff', '#ff0000'], ...

In the final loop: 在最后一个循环中:

            start = 0;
            for (i = 0; i < ii; i++) {
                var val = 360 / total * data[i];
                var col = color[i]; //Add color from array
                (function (i, val) {
                    //Here we pass the color (col)
                    paths.push(r.path().attr({segment: [200, 200, 1, start, start + val, col], stroke: "#fff"}).click(function () {
            ...

EDIT: 编辑:

an then (thanks to Matt Ball for the var props { part) 然后(感谢Matt Ball提供的var props {部分)

Change the r.customAttributes.segment = function (x, y, r, a1, a2, color) {... with this: 使用以下命令更改r.customAttributes.segment =函数(x,y,r,a1,a2,颜色){...:

                var flag = (a2 - a1) > 180;
                a1 = (a1 % 360) * Math.PI / 180;
                a2 = (a2 % 360) * Math.PI / 180;
                var props = {
                    path: [["M", x, y], ["l", r * Math.cos(a1), r * Math.sin(a1)], ["A", r, r, 0, +flag, 1, x + r * Math.cos(a2), y + r * Math.sin(a2)], ["z"]]
                };

                if (color) {
                    props.fill = color;
                }
                return props;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM