CSS3 simple donut chart
SVG for the win!
.item {
position: relative;
float: left;
}
.item h2 {
text-align:center;
position: absolute;
line-height: 125px;
width: 100%;
}
svg {
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
}
.circle_animation {
stroke-dasharray: 440; /* this value is the pixel circumference of the circle */
stroke-dashoffset: 440;
}
.html .circle_animation {
-webkit-animation: html 1s ease-out forwards;
animation: html 1s ease-out forwards;
}
.css .circle_animation {
-webkit-animation: css 1s ease-out forwards;
animation: css 1s ease-out forwards;
}
@-webkit-keyframes html {
to {
stroke-dashoffset: 80; /* 50% would be 220 (half the initial value specified above) */
}
}
@keyframes html {
to {
stroke-dashoffset: 80;
}
}
@-webkit-keyframes css {
to {
stroke-dashoffset: 160;
}
}
@keyframes css {
to {
stroke-dashoffset: 160;
}
}
<div class="item html">
<h2>HTML</h2>
<svg width="160" height="160" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Layer 1</title>
<circle class="circle_animation" r="69.85699" cy="81" cx="81" stroke-width="8" stroke="#6fdb6f" fill="none"/>
</g>
</svg>
</div>
<div class="item css">
<h2>CSS</h2>
<svg width="160" height="160" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Layer 1</title>
<circle class="circle_animation" r="69.85699" cy="81" cx="81" stroke-width="8" stroke="#69aff4" fill="none"/>
</g>
</svg>
</div>
JSFiddle version
Here is a version with background circles as requested in the comments:
.item {
position: relative;
float: left;
}
.item h2 {
text-align:center;
position: absolute;
line-height: 125px;
width: 100%;
}
svg {
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
}
.circle_animation {
stroke-dasharray: 440; /* this value is the pixel circumference of the circle */
stroke-dashoffset: 440;
}
.html .circle_animation {
-webkit-animation: html 1s ease-out forwards;
animation: html 1s ease-out forwards;
}
.css .circle_animation {
-webkit-animation: css 1s ease-out forwards;
animation: css 1s ease-out forwards;
}
@-webkit-keyframes html {
to {
stroke-dashoffset: 80; /* 50% would be 220 (half the initial value specified above) */
}
}
@keyframes html {
to {
stroke-dashoffset: 80;
}
}
@-webkit-keyframes css {
to {
stroke-dashoffset: 160;
}
}
@keyframes css {
to {
stroke-dashoffset: 160;
}
}
<div class="item html">
<h2>HTML</h2>
<svg width="160" height="160" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Layer 1</title>
<circle r="69.85699" cy="81" cx="81" stroke-width="8" stroke="#f2f2f2" fill="none"/>
<circle class="circle_animation" r="69.85699" cy="81" cx="81" stroke-width="8" stroke="#6fdb6f" fill="none"/>
</g>
</svg>
</div>
<div class="item css">
<h2>CSS</h2>
<svg width="160" height="160" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Layer 1</title>
<circle r="69.85699" cy="81" cx="81" stroke-width="8" stroke="#f2f2f2" fill="none"/>
<circle class="circle_animation" r="69.85699" cy="81" cx="81" stroke-width="8" stroke="#69aff4" fill="none"/>
</g>
</svg>
</div>
How does it work?
stroke-dasharray
is used to define the 'pattern' a dashed line uses (MDN). By providing a single value you create a pattern with a dash of 440px and a space of 440px. (440px is roughly the circumference of the circle).
stroke-dashoffset
effectively moves the starting point of the dash pattern (MDN).
A dash-offset
of 220 (half of the stroke-dasharray
) would produce a half-circle. 110 = quarter circle etc.
This answer is only possible because of Turnip's answer, but I made a few significant changes, and I'll explain how it works as well:
.donutContainer {
position: relative;
float: left;
}
.donutContainer h2 {
text-align:center;
position: absolute;
line-height: 125px;
width: 100%;
}
svg {
transform: rotate(-90deg);
}
.donut {
stroke-dasharray: 440;
-webkit-animation: donut 1s ease-out forwards;
animation: donut 1s ease-out forwards;
}
@-webkit-keyframes donut {
from {
stroke-dashoffset: 440;
}
}
@keyframes donut {
from {
stroke-dashoffset: 440;
}
}
<div class="donutContainer">
<h2>donut</h2>
<svg width="160" height="160" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Layer 1</title>
<circle id="circle" style="stroke-dashoffset: 160;/* 160 of 440 */" class="donut" r="69.85699" cy="81" cx="81" stroke-width="8" stroke="#69aff4" fill="none"/>
</g>
</svg>
</div>
<div class="donutContainer">
<h2>donut 2</h2>
<svg width="160" height="160" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Layer 1</title>
<circle id="circle" style="stroke-dashoffset: 220;/* 220 of 440 */" class="donut" r="69.85699" cy="81" cx="81" stroke-width="8" stroke="#FEC007" fill="none"/>
</g>
</svg>
</div>
Because the animation uses from
instead of to
to create the animation, browsers that don't support the animation will show the donut chart complete, instead of not at all. This also makes it possible to change the colored in portion of the donut chart with just inline CSS, and the same single CSS animation can work for any number of donut charts.
An explaination for the svg
stuff:
stroke-dasharray
: In this case, basically the total circumference of the circle.
stroke-dashoffset
: the portion of the circle that is colored in. Zero (0) means all colored in (100%), 440 (or whatever you set the circumference) for none of it colored in (0%)
Attributes on the circle
element:
r
: radius of the circle
cx
: "center X". the center of the circle (X coordinate from bottom left of svg
element)
cy
: "center Y". the center of the circle (Y coordinate from bottom left of svg
element)
stroke-width
: width of the stroke that will draw the donut
stroke
: color of the donut