Super simple layout function for arranging elements end to end
Note, I don't work in any scale here, just the values. Scale can be applied after the layout function
the value layedOut.accumulatedValue
can be used to set the max extent of the scales domain;
Created
June 23, 2017 12:52
-
-
Save tomgp/39e33b72589976c3305d3ad1aa3e0764 to your computer and use it in GitHub Desktop.
Simple end to end layout (for stacked area etc.)
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
const d = [1,2,3,4,5,6,7,8,9]; | |
const layedOut = d.reduce((acc, current)=>{ | |
acc.data.push({ | |
value:current, | |
start:acc.accumulatedValue, | |
}); | |
acc.accumulatedValue+=current; | |
return acc; | |
},{ | |
data:[], | |
accumulatedValue:0 | |
}); | |
console.log(layedOut.data); | |
/*result: | |
[ { value: 1, start: 0 }, | |
{ value: 2, start: 1 }, | |
{ value: 3, start: 3 }, | |
{ value: 4, start: 6 }, | |
{ value: 5, start: 10 }, | |
{ value: 6, start: 15 }, | |
{ value: 7, start: 21 }, | |
{ value: 8, start: 28 }, | |
{ value: 9, start: 36 } ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment