let ctx = document.querySelector("canvas").getContext("2d");
const results = [
{mood: "Angry", total: 1499, shade: "#0a9627"},
{mood: "Happy", total: 478, shade: "#960A2C"},
{mood: "Melancholic", total:332, shade: "#332E2E"},
{mood: "Gloomy", total: 195, shade: "#F73809"}
];
$(function(){
renderPie();
$('input').on('input', (e) => {
let clsNm = e.target.className,
moodVal = e.target.value,
resultObj = results.find(mood => mood.mood === clsNm);
resultObj.total = Number(moodVal);
renderPie();
console.log(clsNm, moodVal)
})
function renderPie() {
let sum = 0;
let totalNumberOfPeople = results.reduce((sum, {total}) => sum + total, 0);
let currentAngle = 0;
let i = 0;
console.log(results)
for (let moodValue of results) {
//calculating the angle the slice (portion) will take in the chart
let portionAngle = (moodValue.total / totalNumberOfPeople) * 2 * Math.PI;
//drawing an arc and a line to the center to differentiate the slice from the rest
ctx.beginPath();
ctx.arc(100, 100, 100, currentAngle, currentAngle + portionAngle);
currentAngle += portionAngle;
ctx.lineTo(100, 100);
//filling the slices with the corresponding mood's color
ctx.fillStyle = moodValue.shade;
ctx.fill();
// if (i === 1)
// break;
i++
}
ctx.beginPath();
ctx.arc(100, 100, 50, 0, 360);
ctx.fillStyle = '#ff0';
ctx.fill();
}
})
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.