Skip to content

Instantly share code, notes, and snippets.

@soggybag
Created November 8, 2017 18:20
Show Gist options
  • Save soggybag/65ad5a2221241d6761ba024df253bb04 to your computer and use it in GitHub Desktop.
Save soggybag/65ad5a2221241d6761ba024df253bb04 to your computer and use it in GitHub Desktop.
Creates a graph from an array of objects. Colors bars rainbow of colors.
makeBars() {
// Get an array of timers tiemr.time is a utc
const timers = this.props.timers
// use reduce to get the max value to normalize times
const max = timers.reduce((acc, timer) => {
return Math.max(acc, timer.time)
}, 0)
// generate an array of bars
return this.props.timers.map((timer, index) => {
const h = timer.time / max * 100 // normalize the times to 0 to 100
const c = 360 / timers.length // Cycle through all colors
const color = `hsl(${c * index}, 100%, 50%)` // Use HSL to set the color
return <TimeGraphBar key={index} width={10} height={h} backgroundColor={color} />
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment