title: Specifically translating data into colors in D3.js
body: <p>I have just started with D3.js, and I'm reading through this: <a href="http://alignedleft.com/tutorials/d3" rel="nofollow">http://alignedleft.com/tutorials/d3</a></p>
  
  <p>In the section on creating a bar chart, the author gives a very simple example of using the current data in the loop to specify the RGB value of the rectangle's fill color. That's very useful. But is rather limited.</p>
  
  <p>Say, for instance - I had a specific type of graph that shows various priorities of data. So, the higher the value, the higher the priority. The priority would be visualized with red, orange, yellow and green colors for high, medium, low, none, respectively.</p>
  
  <p>It's fairly simple to do using a switch case or an if/else in the <code>.attr('fill', function(d){})</code>method, like so: </p>
  
  <pre><code>.attr('fill', function(d) {
    if (percentage(d) &lt;= 0.25) { // 0.25 is a percentage value representing the data
      return 'green';
    }
    else if (percentage(d) &lt;= 0.50) {
      return 'yellow';
    }
    else if (percentage(d) &lt;= 0.75) {
      return 'orange';
    }
    else if (percentage(d) &lt;= 1) {
      return 'red';
    }
  });
  </code></pre>
  
  <p>But what if I wanted to dynamically change the intensity/shade of the colour itself based on the intensity of the data? So not only would it have basic color values, but visualize the intensity of the data within that colour range by making the colour a different shade (or saturation) depending on the intensity of data before shifting to the next color.</p>
  
  <p>One way I've postulated is to combine both strategies for each percentage, but that would mean I'd have to have 25 if statements nested within each of the 5 if statements I already have for each 1% percentile representation of the data. Is there a better, more efficient way of doing it?</p>
  
  <p>I'm aware that I could limit the number of shades of each colour to a smaller number - but I would like to see if there is any other way to do it besides nesting conditionals.</p>