Published
Edited
Oct 26, 2018
Fork of Decimals
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const dividend = n;
// Maximum by spec is 20, but browsers may support higher values.
const wScale = 2;
const h = 80;

const w = steps * wScale;
const c = DOM.context2d(w, h, 1);
c.canvas.style.maxWidth = '100%';
c.fillStyle = `#fff`;
c.fillRect(0,0,w,h);

for(let x = 0; x < steps; x++) {
const digits = (n/(x+1)).toFixed(Math.min(100,decimals)).split('.').pop().split('');
const a = avg(digits) / 9;
c.fillStyle = `hsl(0,0%,${(1-a)*100|0}%)`;
c.fillRect(x * wScale, h - h * a, wScale, h);
}
return c.canvas;
function avg(arr) {
return sum(arr) / arr.length;
}
function sum(arr) {
let s = 0;
for(let v of arr) s += +v;
return s;
}
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more