Skip to content

Instantly share code, notes, and snippets.

@DavidSanwald
Created September 26, 2018 12:37
Show Gist options
  • Save DavidSanwald/3694379f80190a4bf0b45180d541ba10 to your computer and use it in GitHub Desktop.
Save DavidSanwald/3694379f80190a4bf0b45180d541ba10 to your computer and use it in GitHub Desktop.
do not directly apply scales to data, compose them with accessor functions instead
// accessor functions to decouple from data shape
const x = d => d.date
const y = d => d.score
const xScale = d3.scaleTime()
.range([0, width])
.domain(xExtent)
const yScale = d3.scaleLinear()
.range([height, 0])
.domain(yDomainPadded)
// compose scaling with accessors of path generator
const line =d3.line()
.x(d => xScale(x(d)))
.y(d => yScale(y(d)))
/* do not scale data directly
const data = rawData.map(datum=>({...datum, x: xScale(datum.date),y: yScale(datum.score)}))* /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment