Skip to content

Instantly share code, notes, and snippets.

@1wheel
Last active August 29, 2015 14:11
Show Gist options
  • Save 1wheel/97e538c098e1a310224e to your computer and use it in GitHub Desktop.
Save 1wheel/97e538c098e1a310224e to your computer and use it in GitHub Desktop.
d3.scale.category20() randomizer
//paste in the console to swap out the d3.scale.category20() pallet
var oldColors = oldColors ? oldColors : d3.scale.category20().range()
var newColors = oldColors.map(function(){
return '#'+Math.floor(Math.random()*16777215).toString(16) })
oldColors.forEach(function(str, i){
//update inline color style
d3.selectAll('[style*="' + str + '"]')
.style('fill', newColors[i])
.style('background-color', newColors[i])
//update fill attr
d3.selectAll('[fill*="' + str + '"]')
.attr('fill', newColors[i])
})
//save new pallet - press up arrow and enter to run again
oldColors = newColors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment