为什么我看不到Raphael.js饼图演示中的颜色?

时间:2013-05-14 22:14:55

标签: javascript raphael

我在http://jsfiddle.net/s6dEW/复制了拉斐尔饼图演示:

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, .75, 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": 3
                }),
                txt = paper.text(cx + (r + delta + 55) * Math.cos(-popangle * rad), cy + (r + delta + 25) * Math.sin(-popangle * rad), labels[j]).attr({
                    fill: bcolor,
                    stroke: "none",
                    opacity: 0,
                    "font-size": 20
                });
            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 = [],
        json = [{
            "units": "",
            "total": "1682.59",
            "lbs": "82500.00",
            "material": "stainless steel"
        }, {
            "units": "345",
            "total": "18076.19",
            "lbs": "49110.50",
            "material": "aluminum"
        }, {
            "units": "480",
            "total": "6793.75",
            "lbs": "6950.00",
            "material": "cardboard cores"
        }, {
            "units": "",
            "total": "353.44",
            "lbs": "3482.50",
            "material": "scrap metal"
        }]
    for (i = 0; i < json.length; i++) {
        labels.push(json[i].material);
        values.push(parseFloat(json[i].lbs));
    }
    Raphael("holder", 700, 700).pieChart(350, 350, 200, values, labels, "#fff");
});

但是,当我在我的网站(http://www.completerecycling.com/test)上放置相同的代码时,我会在Chrome中找到黑色饼图,在Firefox中使用透明的饼图。我在做什么不同的方式打破了配色方案?

1 个答案:

答案 0 :(得分:5)

这是因为您网站上的<base>代码。

Raphaël使用id引用填充颜色(每个彩色饼图的fill="url(#…)"属性)。

这些引用不适用于http://www.completerecycling.com/test,因为它们解析为http://www.completerecycling.com/test/(基础href)加#…颜色定义(而不是http://www.completerecycling.com/test#…)。

将基础href更改为http://www.completerecycling.com/test而不使用尾部斜杠,完全删除base标记或将http://www.completerecycling.com/test重定向到http://www.completerecycling.com/test/(工作正常)。