Skip to content

Instantly share code, notes, and snippets.

@bernard-leech
Created September 11, 2016 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bernard-leech/506abce9e2f744225dfa9c87e6865277 to your computer and use it in GitHub Desktop.
Save bernard-leech/506abce9e2f744225dfa9c87e6865277 to your computer and use it in GitHub Desktop.
Pie charts — with SVG
/**
* Pie charts — with SVG
*/
.pie {
width: 100px;
height: 100px;
display: inline-block;
margin: 10px;
transform: rotate(-90deg);
}
svg {
background: yellowgreen;
border-radius: 50%;
}
circle {
fill: yellowgreen;
stroke: #655;
stroke-width: 9;
}
@keyframes grow { to { stroke-dasharray: 100 100 } }
.pie.animated circle {
animation: grow 2s infinite linear;
}
<div class="pie">20%</div>
<div class="pie">60%</div>
<div class="pie animated">0%</div>
function $$(selector, context) {
context = context || document;
var elements = context.querySelectorAll(selector);
return Array.prototype.slice.call(elements);
}
$$('.pie').forEach(function(pie) {
var p = parseFloat(pie.textContent);
var NS = "http://www.w3.org/2000/svg";
var svg = document.createElementNS(NS, "svg");
var circle = document.createElementNS(NS, "circle");
var title = document.createElementNS(NS, "title");
circle.setAttribute("r", 16);
circle.setAttribute("cx", 16);
circle.setAttribute("cy", 16);
circle.setAttribute("stroke-dasharray", p + " 100");
svg.setAttribute("viewBox", "0 0 32 32");
title.textContent = pie.textContent;
pie.textContent = '';
svg.appendChild(title);
svg.appendChild(circle);
pie.appendChild(svg);
});
{"view":"split-vertical","fontsize":"90","seethrough":"","prefixfree":"1","page":"css"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment