Skip to content

Instantly share code, notes, and snippets.

@morten-olsen
Last active July 3, 2017 20:13
Show Gist options
  • Select an option

  • Save morten-olsen/317564f7b68723ff235f79d07a79e9d2 to your computer and use it in GitHub Desktop.

Select an option

Save morten-olsen/317564f7b68723ff235f79d07a79e9d2 to your computer and use it in GitHub Desktop.
const generateGraph = (data, {
includeZero = true,
useMin = true,
length = 10,
} = {}) => {
const chars = (includeZero ? ' ' : '') + '▁▂▃▄▅▆▇█';
const aggregated = length ? data.reduce((out, point, i) => {
const ri = Math.floor(i / (data.length / length));
out[ri] = out[ri] || [];
out[ri].push(point);
return out;
}, []).map(a => {
return a.reduce((out, p) => out + p, 0) / a.length;
}) : data;
const max = aggregated.reduce((max, c) => Math.max(max, c), 0);
const min = useMin ? aggregated.reduce((min, c) => Math.min(min, c), 99999999) : 0;
const normalized = aggregated.map(p => Math.round(((p / max) - min) * (chars.length - 1)));
return normalized.map(i => chars[i]).join('');
};
const data = new Array(30).fill(undefined).map(() => Math.random());
console.log(generateGraph(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment