Last active
July 3, 2017 20:13
-
-
Save morten-olsen/317564f7b68723ff235f79d07a79e9d2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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