SHARE
TWEET

Untitled

a guest Aug 20th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // An array of discrete values (possibly dynamically generated)
  2. var discreteValues = ['John', 'Jack', 'Jill', 'Jane', 'Jim', 'Joan'];
  3. // 1st scale establishes relationship between continuous [0,1] and
  4. // the array index of the discrete values
  5. var preColor = d3.scaleLinear()
  6.   .domain([0, discreteValues.length - 1])
  7.   .range([0, 1]);
  8. console.log(preColor(discreteValues.indexOf('John'))) // 0
  9. console.log(preColor(discreteValues.indexOf('Jack'))) // 0.2
  10. console.log(preColor(discreteValues.indexOf('Joan'))) // 1.0
  11. // 2nd scale relates a discrete entity name index to a color via the 1st
  12. // scale. We get a continuous value from the entity index going into the 1st
  13. // scale, that continuous value is then mapped to a color using the 2nd scale
  14. var color = i => { return d3.scaleSequential(d3.interpolateRainbow)(preColor(i)); };
  15. console.log(color(discreteValues.indexOf('John'))) // rgb(110, 64, 170)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top