Anyone know how to create a circle "progressbar" in svg? I need to specify the percentage of the circle, så that a color grows in the shape of a cake.
The growing can be static as long as I have a attribute to change its current status.
Anyone know how to create a circle "progressbar" in svg? I need to specify the percentage of the circle, så that a color grows in the shape of a cake.
The growing can be static as long as I have a attribute to change its current status.
Following is the idea I used to use. With a bit of modification in css
and animation
tag we can achieve more effects for intuitive user experiences.
---SAMPLE CODE----
svg{
}
.over{
-webkit-animation: rotator 1.5s ease-in-out infinite;
stroke-dasharray: 107,38;
}
.bag{
position: absolute;
}
@-webkit-keyframes rotator {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.container{
}
<div class="container">
<svg class="bag" height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="#F8BBD0" stroke-width="3" fill="none">
</circle>
</svg>
<svg class="over" height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="#E91E63" stroke-width="3" fill="none" >
<animate attributeType="CSS" attributeName="stroke-dasharray" from="1,254" to="247,56" dur="5s" repeatCount="indefinite" />
</circle>
</svg>
</div>
Hope you were looking for something kind of this. :)
Thanks, boldewyn.
To answer my own question, I found the following solution:
One can use the following path in template:
<path id="progress" fill="none" stroke="#ffffff" d="" stroke-width="10"/>
And use this function from Raphael js-framework to update x and y. If total is 100, value is the percentage of progress:
function updateState (value, total, R) {
var center;
var alpha = 360 / total * value,
a = (90 - alpha) * Math.PI / 180,
x = 300 + R * Math.cos(a),
y = 300 - R * Math.sin(a),
path;
if (total == value) {
path = "M"+ 300 +","+ (300 - R) +" A"+ R+","+ R+","+ 0+","+ 1+","+ 1+","+ 299.99+","+ 300 - R;
} else {
if(alpha > 180) {
center = 1;
} else {
center = 0;
}
path = "M"+ 300+","+ (300 - R) +" A"+ R+","+ R+","+ 0+"," + center +","+ 1+","+ x+","+ y;
}
return path;
}
The returned path variable is the value for the d attribute on the path element.
This works perfect, if your browser supports SVG Full with the Elliptical Arc command for the path-element. In my case I only have SVG tiny, so this wont work for me :(
Use this self implemented method in JavaScript for example here percentage=85
HTML Code:
<p style="position:absolute;margin-left:95px;margin-top:50px;" id="percentage">0 %</p>
<svg style="position:absolute" id="svg_test" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" cx="110" cy="60" r="50" fill="none" stroke="#e4e4e4" stroke-width="2"></circle>
<path id="svgpath" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" fill="none" stroke="#16a6b6" d="M60,60 A50,50 0 0,1 160,60" stroke-width="2"></path>
</svg>
<circle style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" cx="110" cy="60" r="50" fill="none" stroke="#e4e4e4" stroke-width="2"></circle>
<path id="svgpath" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" fill="none" stroke="#16a6b6" d="M60,60 A50,50 0 0,1 160,60" stroke-width="2"></path>
</svg>
jQuery Code:
percentage=85
if(percentage>=50)
{
flag=1;
}
var per=0;
$(svg).animate({ 'to': 1 }, {
duration: 2000,
step: function(pos, fx) {
//var offset = (pos);
if(pos<0.5)
{
if(percentage>=50)
{
per=180;
$('#percentage').html(Math.round(pos*100)+" %");
}
else
{
per=(percentage*180/50);
$('#percentage').html(Math.round(percentage*pos*2)+" %");
}
endx=110-50*Math.cos(0+(Math.PI/180)*per*(pos*2));
endy=60-50*Math.sin(0+(Math.PI/180)*per*(pos*2));
svg.setAttribute('d',current_dx+endx+","+endy);
}
else if(pos>=0.5 && pos!=1 && flag==1)
{
per=((percentage-50)*180/50);
$('#percentage').html(Math.round(50+(percentage-50)*(pos-0.5)*2)+" %");
endx=110+50*Math.cos(0+(Math.PI/180)*per*(pos-0.5)*2);
endy=60+50*Math.sin(0+(Math.PI/180)*per*(pos-0.5)*2);
svg.setAttribute('d',current_d+endx+","+endy);
}
}
});
Demo : Click Here
Shamelessly copy and pasting from the specification:
<path d="M275,175 v-150 a150,150 0 0,0 -150,150 z"
fill="yellow" stroke="blue" stroke-width="5" />
The path uses the "elliptical arc" command to draw a partial circle. You can either draw several of them, each describing a different circular section, or you give one of them an ID and reference it with <use xlink:href="#ID" />
. Then you can rotate the <use/>
. Draw as many of them as you need for granularity (e.g., 100 sectors allow you steps of 0% to 100%).
To colorize them, just set the fill=""
attribute of each single sector to the fitting value.
Some time ago, I was needing one so I studied a lot to get it done. All you need is a simple SVG markup with the right coordinates, a bit of CSS for it and a little bit of JS (to allow change the % of the progress bar). But at same time you can generate the SVG on the back-end with the right coordinates based on the % you want and unless your progress cannot be read-only, that would work as well.
This is the implementation: http://codepen.io/leandroico/pen/zwIHl
And this is the sample of the SVG markup:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<g class="arcs">
<circle cx="50" cy="50" r="49" class="outer-circle" />
<path d="M50 1 A 49 49, 0, 1, 1, 7.06 73.61, L 50 50 z" class="outer-arc" id="arc1" />
<circle cx="50" cy="50" r="35" class="inner-circle" />
<path d="M50 15 A 35 35, 0, 1, 1, 19.33 66.86" class="inner-arc" id="arc2" />
</g>
<g class="circles">
<circle cx="50" cy="50" r="49" class="outer-circle" />
<circle cx="50" cy="50" r="35" class="inner-circle" />
</g>
<text x="50" y="58" id="percentage-label">67%</text>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="225" height="225">
<g>
<circle cx="112" cy="112" r="95"></circle>
<path id="path" d="M 112,17 A 95,95 0 0,0 112,17"></path>
</g>
</svg>
<script type="text/javascript">
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
var angleInRadians = angleInDegrees * Math.PI / 180.0;
var x = centerX + radius * Math.cos(angleInRadians);
var y = centerY + radius * Math.sin(angleInRadians);
return [x, y];
}
function drawCircle(elm, centerX, centerY, radius, percentage) {
var angle = (360 * (percentage / 100)) % 360;
var start = polarToCartesian(centerX, centerY, radius, -90);
var end = polarToCartesian(centerX, centerY, radius, -(angle + 90));
var large = percentage < 50 ? 0 : 1;
var length = (2 * Math.PI * radius) * (percentage / 100);
console.log(start, end, angle);
elm.setAttribute('d', 'M ' + start[0] + ',' + start[1] + ' A ' + radius + ',' + radius + ' ' + 0 + ' ' + large + ',' + 0 + ' ' + end[0] + ',' + end[1]);
elm.setAttribute('stroke-dasharray', length);
elm.setAttribute('stroke-dashoffset', length);
}
drawCircle(document.getElementById('path'), 112, 112, 95, 47);
</script>