I have this code, I need to make it dynamic
<svg width="240" height="240" viewBox="0 0 240 240"><circle cx="120"
cy="120" r="120" fill="#ebeced"></circle><path d="M 120.0,120.0 L 120.0,0.0 A
120.0,120.0 0 1 1 75.71350260559888,8.471052418954017 Z" fill="#58d6a6"></path>
<circle cx="120" cy="120" r="100" fill="#ffffff"></circle></svg>
<svg width="240" height="240" viewBox="0 0 240 240"><circle cx="120" cy="120"
r="120" fill="#ebeced"></circle><path d="M 120.0,120.0 L 120.0,0.0 A 120.0,120.0
0 1 1 55.59949741795174,18.744998804110452 Z" fill="#58d6a6"></path><circle
cx="120" cy="120" r="100" fill="#ffffff"></circle></svg>
I leave 2 examples, I do not find a relationship, as it should vary.
That graph now means 90% of X, I want to make it dynamic for any percentage.
Here is my progress:
function circlePath(cx, cy, r){
var variables="M" + cx + "," + cy + "m" + (-r) + ",0a" + r + "," + r + " 0 1,0 " + (r * 2) + ",0a" + r + "," + r + " 0 1,0 " + (-r * 2) + ",0";;
var pathEl = document.createElementNS("http://www.w3.org/2000/svg", "path");
pathEl.setAttribute(variables);
document.querySelector('svg').appendChild(pathEl);
}
circlePath(120,120,120);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg class="circulo" width="240" height="240" viewBox="0 0 240 240"><circle cx="120"
cy="120" r="120" fill="#ebeced"></circle ><path d="M 120.0,120.0 L 120.0,0.0 A
120.0,120.0 0 1 1 75.71350260559888,8.471052418954017 Z" fill="#58d6a6"></path>
<circle cx="120" cy="120" r="100" fill="#ffffff"></circle></svg>
I relied on: https://stackoverflow.com/questions/5737975/circle-drawing-with-svgs-arc-path
and here an example: http://jsfiddle.net/godly/zL5Su/
But it does not work!
First of all, a bit of trigonometry ( courtesy of Paul Dixon ): to obtain the point inside a circumference from a certain angle, you must apply the following formula:
where
cx
ycy
are the coordinates of the center of the circle,r
is the radius, anda
is the angle you want expressed in radians. We are going to use that to calculate the destination position of the arc that we are going to create.And you also have to take another important part into account: the angle starts counting from the X axis (as indicated in the following graphic), while the percentage you want to show starts counting from the Y axis. So you're going to have to correct the angle π/2 radians:
Taking this into account, to calculate the destination point of the arc, the calculations that will have to be done will be like this (it can be simplified, but I have left it like this so that the correction can be seen better and already in radians):
And now the fun part: the value that is going to go inside
d
thepath
one you want the percentage to represent. It would be something like this:Where:
<cx>
: is the X coordinate of the center of the circumference<cy>
: is the Y coordinate of the center of the circumference<r>
: is the radius of the circle<modificador de arco>
: this will be the three digits (with values 0 or 1), which will depend on the value of the angle: if it is more than 180 it will be "0 1.1" and if it is not it will be "1 0.1". ( Daniel Pataki has a very good post in English, in which he explains it better while teaching how to create a graph).<x>
: is the valuex
you calculated with the formulas above.<y>
: is the valuey
you calculated with the formulas above.From there it can be deduced that it
<cx - r>
is the X coordinate of the center of the circumference minus the radius.Here is an example with the code working:
Note: I have cheated a bit and the code fails for the 100% percentage. You could add a little condition for that case where the circle is painted red instead of calculating arcs and such.
You could also solve it using:
stroke-dasharray
: which allows us to specify the length of the script.stroke-dashoffset
: which allows us to specify how much to subtract from the length of the script.transition
: which allows us to define the transition between two states of an element.transform
: which allows us, usingrotate
, to rotate the circle and position the start of the bar at the top.So we have to:
The radius (
r
) of the circle is90
.The formula to calculate the perimeter (
P
) of a circumference:2π * r
We set
stroke-dasharray
equal to565.48
, which is the perimeter (P
).We set
stroke-dashoffset
equal to0
(do not subtract anything), which generates a hyphen of100%
the established length.If we wanted to generate a dash from the
30%
(x
) of the perimeter of the circumference, we would have to calculate how much to subtract from the length of the dash:demonstration: