Q:

How to overlay color in d3 area chart

I am working on an area chart like http://bl.ocks.org/mbostock/3883195 and I want to have an additional feature on it. I want to have different colors depending on the height or some other variable. Is there a way that I can do this?
  var area = d3.svg.area().interpolate("step-before")
                          .x(function(d) { return x(d.decile/10);})
                          .y0(height)
                          .y1(function(d) { return y(d.count);});

  var svg = d3.select("body").append("svg")
              .attr("width", width + margin.left + margin.right)
              .attr("height", height + margin.top + margin.bottom)
              .append("g")
              .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.tsv("mark_vis.tsv", function(error, data){
        if(error) throw error;
        data = getCount(data);
        console.log(data);

      // data[0] = {decile : 1, count:0.34, other_variable:0.3}

      svg.append("path").datum(data).attr("class", "area").attr("d", area);
            //I wish I could do something like this       .style("fill", function(d){return d.other_variable};);

      svg.append("g").attr("class", "x axis").attr("transform", "translate(0," + height + ")")
                    .call(xAxis);

      svg.append("g").attr("class", "y axis").call(yAxis)
                      .append("text")
                      .attr("transform","rotate(-90)")
                      .attr("y", 6)
                      .attr("dy", "0.71em")
                      .style("text-anchor", "end")
                      .text("%");

     });

A:

First you need to make the chunks of data. 
Here the criteria to make the chunks of dataset is using field close (i.e. any data.close > 150)
  var bucket = [];
  var prev = undefined;
  var datanew = [];
  data.forEach(function(d){
     if (d.close > 150){
       if (prev && prev.close < 150){
         datanew.push(bucket);
         bucket = [];
       }
       bucket.push(d);
     } else {
       if (prev && prev.close > 150){
         datanew.push(bucket);
         bucket = [];
       }
       bucket.push(d);
     }
     prev = d;
  });
  datanew.push(bucket); //datanew will hold the chunk of data with close > 150.

In the current dataset it will make 8 chunks.

Chunk1 (dataset > 150)
Chunk2 (dataset < 150)
Chunk3 (dataset > 150)

..so on
Now while making the paths do:
 datanew.forEach(function(d){//iterate through the chunks
        console.log(d)
        if (d[0].close>150)
          c = "red";//less than 150 so make it red as this chunk is for less than 150
        else 
          c="blue";//greater than 150 so make it blue as this chunk is for greater than 150
        svg.append("path")
          .datum(d)
          .attr("class", "area")
          .attr("stroke", c)//color selected above
          .style("fill", c)
          .attr("d", area);
      });

Full working code here

var margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%d-%b-%y").parse;

var x = d3.time.scale()
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

var area = d3.svg.area()
    .x(function(d) { return x(d.date); })
    .y0(height)
    .y1(function(d) { return y(d.close); });

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.tsv("https://gist.githubusercontent.com/cyrilcherian/6baf8967e3fceaa4fec1/raw/02ab25f4b7956b86be41523932da434529377fdb/data.tsv", function(error, data) {
  if (error) throw error;

  data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.close = +d.close;
  });

  x.domain(d3.extent(data, function(d) { return d.date; }));
  y.domain([0, d3.max(data, function(d) { return d.close; })]);

  var bucket = [];
  var prev = undefined;
  var datanew = [];
  data.forEach(function(d){
     if (d.close > 150){
       if (prev && prev.close < 150){
         datanew.push(bucket);
         bucket = [];
       }
       bucket.push(d);
     } else {
       if (prev && prev.close > 150){
         datanew.push(bucket);
         bucket = [];
       }
       bucket.push(d);
     }
     prev = d;
  });
  datanew.push(bucket); //datanew will hold the chunk of data with close > 150.
  
  datanew.forEach(function(d){//iterate through the chunks
        console.log(d)
        if (d[0].close>150)
          c = "red";//less than 150 so make it red as this chunk is for less than 150
        else 
          c="blue";//greater than 150 so make it blue as this chunk is for greater than 150
        svg.append("path")
          .datum(d)
          .attr("class", "area")
          .attr("stroke", c)//color selected above
          .style("fill", c)
          .attr("d", area);
      });


  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Price ($)");
});
body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.area {
  fill: steelblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

Hope this helps!

