title: Simple d3.js graph with a json feed input
body: <p>I'm trying to draw a simple graph which gets its data from a json feed. The json feed looks like this:</p>
  
  <blockquote>
    <p>[{"date":"2013-09-06","count":158},{"date":"2013-09-07","count":107},{"date":"2013-09-08","count":124},{"date":"2013-09-05","count":9},{"date":"2013-09-04","count":4},{"date":"2013-09-03","count":6},{"date":"2013-09-02","count":6},{"date":"2013-09-01","count":1}]</p>
  </blockquote>
  
  <p>This is an example of the type of graph that I expect with the code below:</p>
  
  <p><img src="https://i.imgur.com/lIkRTo1.png" alt="d3 graph"></p>
  
  <p>The JavaScript looks like this:</p>
  
  <pre><code>var margin = {top: 10, right: 10, bottom: 100, left: 400},
      width  = 960 - margin.left - margin.right,
      height = 500 - margin.top - margin.bottom;
  
  var parseDate = d3.time.format("%Y-%m-%d").parse;
  
  var x = d3.time.scale().range([0, width]),
      y = d3.scale.linear().range([height, 0]);
  
  var xAxis = d3.svg.axis().scale(x).orient("bottom"),
      yAxis = d3.svg.axis().scale(y).orient("left");
  
  var brush = d3.svg.brush().on("brush", brushed);
  
  var area = d3.svg.area()
      .interpolate("monotone")
      .x(function(d) { return x(d.date); })
      .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);
  
  svg.append("defs").append("clipPath")
      .attr("id", "clip")
      .append("rect")
      .attr("width", width)
      .attr("height", height);
  
  var focus = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  
  d3.json('http://path/to/feed.json', function (data) {
      data.forEach(function(d) {
        d.date = parseDate(d.date);
      });
  
    x.domain(d3.extent(data.map(function(d) { return d.date; })));
    y.domain([0, d3.max(data.map(function(d) { return d.count; }))]);
  
    focus.append("path")
         .datum(data)
         .attr("clip-path", "url(#clip)")
         .attr("d", area);
  
    focus.append("g")
         .attr("class", "x axis")
         .attr("transform", "translate(0," + height + ")")
         .call(xAxis);
  
    focus.append("g")
         .attr("class", "y axis")
         .call(yAxis);
    });
  
    function brushed() {
      x.domain(brush.empty() ? x2.domain() : brush.extent());
      focus.select("path").attr("d", area);
      focus.select(".x.axis").call(xAxis);
    }
  </code></pre>
  
  <p><strong>Update</strong>: </p>
  
  <p>Got the graph to draw after fixing the bug Scott Cameron pointed out but it looks like this:</p>
  
  <p><img src="https://i.imgur.com/eZ9MKbP.png" alt="broken graph"></p>
  
  <p>Looks like the date parsing is acting up.</p>