raphael演示饼图调整以添加自定义颜色

时间:2013-01-08 20:23:53

标签: javascript raphael

如何为此raphael饼图添加自定义颜色。 http://raphaeljs.com/pie.html它从HTML表中获取值并将它们插入饼图中。现在它使用以下内容来填充扇区。我认为它使用随机函数来选择十六进制颜色。我不确定。

bcolor = Raphael.hsb(start, 1, 1),
            p = sector(cx, cy, r, angle, angle + angleplus, {fill: "90-" + bcolor + "-" + color, stroke: stroke, "stroke-width": 2}),

我希望能够拉入一组设定的十六进制颜色。不应超过6个值。这是完整的代码。我也想设置自定义渐变,但现在不是那么重要。

Raphael.fn.pieChart = function (cx, cy, r, values, labels, stroke) {
    var paper = this,
        rad = Math.PI / 180,
        chart = this.set();
    function sector(cx, cy, r, startAngle, endAngle, params) {
        var x1 = cx + r * Math.cos(-startAngle * rad),
            x2 = cx + r * Math.cos(-endAngle * rad),
            y1 = cy + r * Math.sin(-startAngle * rad),
            y2 = cy + r * Math.sin(-endAngle * rad);
        return paper.path(["M", cx, cy, "L", x1, y1, "A", r, r, 0, +(endAngle - startAngle > 180), 0, x2, y2, "z"]).attr(params);
    }
    var angle = 0,
        total = 0,
        start = 0,
        process = function (j) {
            var value = values[j],
                angleplus = 360 * value / total,
                popangle = angle + (angleplus / 2),
                color = Raphael.hsb(start, .90, 1),
                ms = 500,
                delta = 30,
                bcolor = Raphael.hsb(start, 1, 1),
                p = sector(cx, cy, r, angle, angle + angleplus, {fill: "90-" + bcolor + "-" + color, stroke: stroke, "stroke-width": 2}),
                txt = paper.text(cx + (r + delta + 2) * Math.cos(-popangle * rad), cy + (r + delta + 5) * Math.sin(-popangle * rad), labels[j]).attr({fill: "#999999", stroke: "none", opacity: 1, "font-size": 13});
            p.mouseover(function () {
                p.stop().animate({transform: "s1.1 1.1 " + cx + " " + cy}, ms, "elastic");
                //txt.stop().animate({opacity: 1}, ms, "elastic");
            }).mouseout(function () {
                p.stop().animate({transform: ""}, ms, "elastic");
                //txt.stop().animate({opacity: 0}, ms);
            });
            angle += angleplus;
            chart.push(p);
            chart.push(txt);
            start += .1;
        };
    for (var i = 0, ii = values.length; i < ii; i++) {
        total += values[i];
    }
    for (i = 0; i < ii; i++) {
        process(i);
    }
    return chart;
};

$(function () {
    var values = [],
        labels = [];
    $("tr").each(function () {
        values.push(parseInt($("td", this).text(), 10));
        labels.push($("th", this).text());
    });
    $("table").hide();
    Raphael("holder-new", 400, 400).pieChart(100, 100, 50, values, labels, "#fff");
});

1 个答案:

答案 0 :(得分:4)

bcolor = Raphael.hsb(hue,saturation,value) 

将start的值作为hue。因此它从0开始,并为每个扇区递增.1。关于它没有任何随意性。要做你需要的,只需创建你的阵列。

var colors = ['red','green','blue','yellow','#ffaa00','#00ffaa','#aa00ff'];

设置数组的颜色(模数将j的值保持在颜色数组长度内)

bcolor = colors[j%colors.length]

并在您的细分中使用

p = sector(cx, cy, r, angle, angle + angleplus, 
    {fill: "90-" + bcolor + "-" + bcolor, stroke: stroke, "stroke-width": 1})

原始代码我使用了两种颜色(颜色和bcolor)来编辑它,而我在这里只是bcolor。如果你想要一个渐变,再加回另一种颜色。

这是显示结果的fiddle