Skip to content

Instantly share code, notes, and snippets.

@sulfo
Created February 15, 2023 14:36
Show Gist options
  • Save sulfo/8a518d9359d942ff7213a0cd3f48be32 to your computer and use it in GitHub Desktop.
Save sulfo/8a518d9359d942ff7213a0cd3f48be32 to your computer and use it in GitHub Desktop.
LYJEpBy
<div class="wrapper">
<h1>Answered Questions</h1>
<div class="pie-charts">
<div class="pieID--micro-skills pie-chart--wrapper">
<h2>Micro-Skills</h2>
<div class="pie-chart">
<div class="pie-chart__pie"></div>
<ul class="pie-chart__legend">
<li><em>Additive</em><span>642</span></li>
<li><em>Multiplicative</em><span>358</span></li>
</ul>
</div>
</div>
<div class="pieID--categories pie-chart--wrapper">
<h2>Categories</h2>
<div class="pie-chart">
<div class="pie-chart__pie"></div>
<ul class="pie-chart__legend">
<li><em>Horizontal</em><span>768</span></li>
<li><em>Vertical</em><span>232</span></li>
</ul>
</div>
</div>
<div class="pieID--operations pie-chart--wrapper">
<h2>Operations</h2>
<div class="pie-chart">
<div class="pie-chart__pie"></div>
<ul class="pie-chart__legend">
<li><em>Addition</em><span>486</span></li>
<li><em>Subtraction</em><span>156</span></li>
<li><em>Multiplication</em><span>215</span></li>
<li><em>Division</em><span>143</span></li>
</ul>
</div>
</div>
</div>
</div>
function sliceSize(dataNum, dataTotal) {
return (dataNum / dataTotal) * 360;
}
function addSlice(id, sliceSize, pieElement, offset, sliceID, color) {
$(pieElement).append("<div class='slice "+ sliceID + "'><span></span></div>");
var offset = offset - 1;
var sizeRotation = -179 + sliceSize;
$(id + " ." + sliceID).css({
"transform": "rotate(" + offset + "deg) translate3d(0,0,0)"
});
$(id + " ." + sliceID + " span").css({
"transform" : "rotate(" + sizeRotation + "deg) translate3d(0,0,0)",
"background-color": color
});
}
function iterateSlices(id, sliceSize, pieElement, offset, dataCount, sliceCount, color) {
var
maxSize = 179,
sliceID = "s" + dataCount + "-" + sliceCount;
if( sliceSize <= maxSize ) {
addSlice(id, sliceSize, pieElement, offset, sliceID, color);
} else {
addSlice(id, maxSize, pieElement, offset, sliceID, color);
iterateSlices(id, sliceSize-maxSize, pieElement, offset+maxSize, dataCount, sliceCount+1, color);
}
}
function createPie(id) {
var
listData = [],
listTotal = 0,
offset = 0,
i = 0,
pieElement = id + " .pie-chart__pie"
dataElement = id + " .pie-chart__legend"
color = [
"cornflowerblue",
"olivedrab",
"orange",
"tomato",
"crimson",
"purple",
"turquoise",
"forestgreen",
"navy"
];
color = shuffle( color );
$(dataElement+" span").each(function() {
listData.push(Number($(this).html()));
});
for(i = 0; i < listData.length; i++) {
listTotal += listData[i];
}
for(i=0; i < listData.length; i++) {
var size = sliceSize(listData[i], listTotal);
iterateSlices(id, size, pieElement, offset, i, 0, color[i]);
$(dataElement + " li:nth-child(" + (i + 1) + ")").css("border-color", color[i]);
offset += size;
}
}
function shuffle(a) {
var j, x, i;
for (i = a.length; i; i--) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
return a;
}
function createPieCharts() {
createPie('.pieID--micro-skills' );
createPie('.pieID--categories' );
createPie('.pieID--operations' );
}
createPieCharts();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
body {
font-family: "Open Sans", Arial;
background: #EEE;
text-align: center; }
body * {
font-weight: 300;
margin: 0;
padding: 0; }
@keyframes bake-pie {
from {
transform: rotate(0deg) translate3d(0, 0, 0); } }
.pie-chart {
font-family: "Open Sans", Arial; }
.pie-chart--wrapper {
width: 400px;
margin: 30px auto;
text-align: center; }
.pie-chart__pie, .pie-chart__legend {
display: inline-block;
vertical-align: top; }
.pie-chart__pie {
position: relative;
height: 200px;
width: 200px;
margin: 10px auto 35px; }
.pie-chart__pie::before {
content: "";
display: block;
position: absolute;
z-index: 1;
width: 100px;
height: 100px;
background: #EEE;
border-radius: 50%;
top: 50px;
left: 50px; }
.slice {
position: absolute;
width: 200px;
height: 200px;
clip: rect(0px, 200px, 200px, 100px);
animation: bake-pie 1s; }
.slice span {
display: block;
position: absolute;
top: 0;
left: 0;
background-color: black;
width: 200px;
height: 200px;
border-radius: 50%;
clip: rect(0px, 200px, 200px, 100px); }
.pie-chart__legend {
display: none;
list-style-type: none;
padding: 0;
margin: 0 auto;
background: #FFF;
padding: 0.75em 0.75em 0.05em;
font-size: 13px;
box-shadow: 1px 1px 0 #DDD, 2px 2px 0 #BBB;
text-align: left;
width: 65%; }
.pie-chart__legend li {
height: 1.25em;
margin-bottom: 0.7em;
padding-left: 0.5em;
border-left: 1.25em solid black; }
.pie-chart__legend em {
font-style: normal; }
.pie-chart__legend span {
float: right; }
.pie-charts {
display: flex;
flex-direction: row; }
@media (max-width: 500px) {
.pie-charts {
flex-direction: column; } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment