raphael.js - converting pie graph to donut graph

删除回忆录丶 提交于 2019-11-30 04:07:05

The way how one slice of the pie is created is like this:

  1. move to the center of the circle (cx,cy): "M", cx, cy
  2. draw a line until the edge, where the arc will start (x1,y1): "L", x1, y1
  3. draw an arc based on some mathematical calculations: "A", r, r, 0, +(endAngle - startAngle > 180), 0, x2, y2
  4. draw a line back to the middle of the circle: in this case "z" is used; it means move to origin(cx,cy)

and the slice(path) is ready.

In order to create the donut you need to modify how the path is composed. You need to have a path composed of 2 arcs(inside and outside) and 2 lines to complete it.

So first you need to find the starting point for the path, based on the radius of the imaginary empty circle that will be in the middle of the donut (with the radius rin). Lets call the coordinates of this point xx1 and yy1:

xx1 = cx + rin * Math.cos(-startAngle * rad)
yy1 = cy + rin * Math.sin(-startAngle * rad)

You start building the path from this point( "M", xx1, yy1 ); http://jsfiddle.net/EuMQ5/425/

Next step is to draw the line to the edge of the circle( "L", x1, y1 ). From there you will have to draw the outer arc( "A", r, r, 0, +(endAngle - startAngle > 180), 0, x2, y2 ) then one more line to the other edge of the inner arc( "L", xx2, yy2 ). To get the values for xx2 and yy2:

xx2 = cx + rin * Math.cos(-endAngle * rad)
yy2 = cy + rin * Math.sin(-endAngle * rad)  

and the last step is to complete the path by drawing the inner arc( "A", rin, rin, 0, +(endAngle - startAngle > 180), 1, xx1, yy1 ). And now you have a piece of a donut.

The fiddle is here.

**updated fiddle link

Why dont you just draw a circle at the center of the pie chart?

paper.add([
    {
        type: "circle",
        cx: 250,
        cy: 250,
        r: 150,
        fill: "#ffffff"
    },
]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!