Skip to content

Instantly share code, notes, and snippets.

@nntrn
Created August 16, 2022 13:35
Show Gist options
  • Save nntrn/4d3e001490d4af3c0649e2df6a9f8bc1 to your computer and use it in GitHub Desktop.
Save nntrn/4d3e001490d4af3c0649e2df6a9f8bc1 to your computer and use it in GitHub Desktop.
function piechart(data, radius=10) {
var k = Object.keys(data)
var v = Object.values(data)
var r = radius
d = r * 2;
M = Math;
R = M.round;
p = [];
for (y = 0; y < d; y++) {
p[y] = [];
for (x = 0; x < d; x++) {
p[y][x] = ' '
}
}
t = 0;
i = -1;
for (f = 0; f < 1; f += 1 / (r * 25)) {
if (f > t) {
t += v[++i]
}
a = M.PI * 2 * f;
for (j = 0; j < r; j++){
p[R(M.sin(a) * j) + r][R(M.cos(a) * j) + r] = k[i]
}
}
s = "";
for (y = 0; y < d; y++) {
for (x = 0; x < d; x++){
s += `${p[y][x]} `;
}
s += "\n";
}
return s.split('\n')
}
pie = piechart({ A: .3, B: .5, C: .2 }, 5).map(e=>'// ' + e).join('\n')
copy(pie)
console.log(pie)
//
// B B B C C
// B B B B C C C
// B B B B B C C C C
// B B B B C C C C C
// B B B B C C C C C
// B B B B B A A A A
// B B B B A A A A A
// B B B A A A A
// B B A A A
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment