javascript - 如何向使用 Raphael.js 创建的圆环图添加标记?

标签 javascript svg raphael donut-chart

我需要帮助向我使用 raphael.js 修改的圆环图脚本添加标记。除了动态生成三 Angular 形标记的方法外,我已经准备好了大部分内容。

JSFiddle:http://jsfiddle.net/aP7MK/73/

function donutChart(total, goal, avg){

    var paper = Raphael("canvas", 400, 400);
    paper.customAttributes.arc = function (xloc, yloc, value, total, R) {
        var alpha = 360 / total * value,
            a = (90 - alpha) * Math.PI / 180,
            x = xloc + R * Math.cos(a),
            y = yloc - R * Math.sin(a),
            path;
        if (total == value) {
            path = [
                ["M", xloc, yloc - R],
                ["A", R, R, 0, 1, 1, xloc - 0.01, yloc - R]
            ];
        } else {
            path = [
                ["M", xloc, yloc - R],
                ["A", R, R, 0, +(alpha > 180), 1, x, y]
            ];
        }
        return {
            path: path
        };
    };

    var backCircle = paper.circle(100, 100, 40).attr({
        "stroke": "#7BC2E5",
            "stroke-width": 14
    });

    var theArc = paper.path().attr({
        "stroke": "#f5f5f5",
            "stroke-width": 14,
        arc: [100, 100, 0, 100, 40]
    });


    //event fired on each animation frame
    eve.on("raphael.anim.frame.*", onAnimate);

    //text in the middle
    theText = paper.text(100, 100, "0%").attr({
        "font-size": 18,
            "fill": "#f5f5f5",
            "font-weight": "bold"
    });

    //the animated arc
    theArc.rotate(0, 100, 100).animate({
        arc: [100, 100, ((total/goal) * 100), 100, 40]
    }, 1900);


    //on each animation frame we change the text in the middle

    function onAnimate() {
        var howMuch = theArc.attr("arc");
        theText.attr("text", Math.floor(howMuch[2]) + "%");
    }
}

donutChart(80, 140, 40);

这是我最终要创建的内容:

我不担心样式,只需要 marker 元素方面的帮助,它将指示传递给 donutChart 函数的 avg 参数在图表中的位置。

最佳答案

正如@Ian 所说,您可以使用 path 绘制三 Angular 形:

// triangle
var tri = paper.path("M100 50 L90 40 L110 40 L100 50 Z");

参见 docs关于使用 path(它的命令)。

然后你需要旋转/平移(再次如@Ian 所说)但是 SVG 在这里帮助你提供 rotate该方法不仅需要旋转 Angular ,还需要旋转点的坐标(它会为您转换坐标)

tri.rotate((howMuch[2] - prev_percent) * 3.6, 100, 100);

这里唯一需要注意的是,您需要之前和当前百分比的差异。

工作 fiddle

关于javascript - 如何向使用 Raphael.js 创建的圆环图添加标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20232748/

相关文章:

javascript - 如何在搜索 vuejs 列表时添加加载图标

javascript - 我怎样才能填充已经穿过的svg路径?

javascript - 如何使用坐标系在dc.js中画线?

java - 使用蜡染缩放 SVG?

javascript - 将类添加到 .click 函数

javascript - 具有数千个路径/矩形的 RaphaelJS 的性能

java - 使用 Java 操作 Javascript

javascript - 在所选背景的右侧显示小图标

javascript - 如何将 div 标签的 id、名称和值传递给 JavaScript 函数

html - 为什么 MS Edge 上的路径描边宽度比其他浏览器大?