Skip to content

Instantly share code, notes, and snippets.

@tomgp
Created June 23, 2017 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomgp/39e33b72589976c3305d3ad1aa3e0764 to your computer and use it in GitHub Desktop.
Save tomgp/39e33b72589976c3305d3ad1aa3e0764 to your computer and use it in GitHub Desktop.
Simple end to end layout (for stacked area etc.)

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;

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