What I'm trying to do is create a simple donut chart. I'm currently using CSS(3) only but I don't know if it's possible without javascript.
What I have so far: http://jsfiddle.net/aBP5Q/
HTML:
<div class="donut-container" style="background: #9C0;">
<div class="donut-inner">
<div class="donut-label">HTML</div>
</div>
</div>
CSS:
.donut-container {
width: 150px;
height: 150px;
float: left;
-webkit-border-radius: 75px;
-moz-border-radius: 75px;
border-radius: 75px;
margin-right: 20px;
}
.donut-inner {
width: 134px;
height: 134px;
position: relative;
top: 8px;
left: 8px;
background: #FFF;
-webkit-border-radius: 65px;
-moz-border-radius: 65px;
border-radius: 65px;
}
.donut-label {
line-height: 130px;
text-align: center;
font-size: 20px;
}
I would like to display the green and blue colors as the precentage. So no green is 0% and full green (360 degrees) is 100%. Maybe even with a simple animation when the chart is loaded if its possible.
Your help is much appreciated.
In case you need to create a donut chart animation (just plain css) and also need multiple colors for it then check the codepen example I have created.
http://codepen.io/hilar47/pen/RprXev
i wrote this as a comment reply but it was too long :
hm... well, here is a fiddle for the second circle http://jsfiddle.net/LgtV2/ .... it has 3 pie parts. the first circle (100%) has 5 parts. you should play with the fiddle to learn how it works and so you can replicate it. i've never done this before and am just looking at link San posted, but it looks like this just uses multiple Divs with the css3 TRANSFORM to form the curves, and the pseudo selectors :before and :after for animations. The animations occur as the page itself is loading... EG: :before div1 loads it has a transform of 5, it loads and has a transform of 8, :after it loads it has a transform of 11.
code:
SVG for the win!
JSFiddle version
Here is a version with background circles as requested in the comments:
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 thestroke-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:
Because the animation uses
from
instead ofto
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 circlecx
: "center X". the center of the circle (X coordinate from bottom left ofsvg
element)cy
: "center Y". the center of the circle (Y coordinate from bottom left ofsvg
element)stroke-width
: width of the stroke that will draw the donutstroke
: color of the donutI have modified a snippet I found on the web to make a simple doughnut chart using only HTML and CSS, here is the result:
Decided to post it here as an alternative to the other answers. Cheers!