Created
August 16, 2022 13:35
-
-
Save nntrn/4d3e001490d4af3c0649e2df6a9f8bc1 to your computer and use it in GitHub Desktop.
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 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