Last active
December 29, 2021 02:11
-
-
Save csssecrets/4696e4c6700fe9f346d8 to your computer and use it in GitHub Desktop.
Pie charts — with SVG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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: 32; | |
} | |
@keyframes grow { to { stroke-dasharray: 100 100 } } | |
.pie.animated circle { | |
animation: grow 2s infinite linear; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="pie">20%</div> | |
<div class="pie">60%</div> | |
<div class="pie animated">0%</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"view":"split-vertical","fontsize":"100","seethrough":"","prefixfree":"1","page":"css"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment