javascript – 如何在使用Raphael.js创建的圆环图中添加标记?

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

JSfiddlehttp://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,1,xloc - 0.01,yloc - R]
            ];
        } else {
            path = [
                ["M",+(alpha > 180),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,40]
    });


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

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

    //the animated arc
    theArc.rotate(0,100).animate({
        arc: [100,((total/goal) * 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);

这是我最终要创建的内容

我并不担心样式,只需要帮助标记元素,这将表示传递给donutChart函数的avg参数位于图表中的位置.

解决方法

正如@Ian所说,您可以使用路径绘制三角形:
// triangle
var tri = paper.path("M100 50 L90 40 L110 40 L100 50 Z");

请参阅docs关于使用路径(其命令).

然后你需要旋转/翻译(再次像@Ian所说)但是SVG帮助你提供rotate方法,它不仅需要旋转角度,还需要旋转点的坐标(它为你转换坐标)

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

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

工作fiddle

相关文章

什么是深拷贝与浅拷贝?深拷贝与浅拷贝是js中处理对象或数据...
前言 今天复习了一些前端算法题,写到一两道比较有意思的题:...
最近在看回JavaScript的面试题,this 指向问题是入坑前端必须...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面