score:2

Accepted answer

You're using a date object in the line generator...

var line = d3.line()
    .x(function(d) {
        return x(new Date(d.Date)); 
})

...but not in the scale:

x.domain(d3.extent(data, function(d) { 
    return d.Date; 
}));

Therefore, it should be:

x.domain(d3.extent(data, function(d) { 
    return new Date(d.Date); 
}));

Here is your code with that change:

var data = [{
  Date: "2017-01-01",
  "New York": 63.4,
  "San Francisco": 62.7,
  "Austin": 72.2
}, {
  Date: "2017-02-01",
  "New York": 58,
  "San Francisco": 59.9,
  "Austin": 67.7
}, {
  Date: "2017-03-01",
  "New York": 53.3,
  "San Francisco": 59.1,
  "Austin": 69.4
}, {
  Date: "2017-04-01",
  "New York": 55.7,
  "San Francisco": 58.8,
  "Austin": 68
}, {
  Date: "2017-05-01",
  "New York": 64.2,
  "San Francisco": 58.7,
  "Austin": 72.4
}, {
  Date: "2017-06-01",
  "New York": 58.8,
  "San Francisco": 57,
  "Austin": 77
}, {
  Date: "2017-07-01",
  "New York": 57.9,
  "San Francisco": 56.7,
  "Austin": 82.3
}, {
  Date: "2017-08-01",
  "New York": 61.8,
  "San Francisco": 56.8,
  "Austin": 78.9
}, {
  Date: "2017-09-01",
  "New York": 69.3,
  "San Francisco": 56.7,
  "Austin": 68.8
}, {
  Date: "2017-10-01",
  "New York": 71.2,
  "San Francisco": 60.1,
  "Austin": 68.7
}, {
  Date: "2017-11-01",
  "New York": 68.7,
  "San Francisco": 61.1,
  "Austin": 70.3
}, {
  Date: "2017-12-01",
  "New York": 61.8,
  "San Francisco": 61.5,
  "Austin": 75.3
}, ];
var columns = ["Date", "New York", "San Francisco", "Austin"]


var svg = d3.select("svg"),
  margin = {
    top: 20,
    right: 80,
    bottom: 30,
    left: 50
  },
  width = svg.attr("width") - margin.left - margin.right,
  height = svg.attr("height") - margin.top - margin.bottom,
  g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var parseTime = d3.timeParse("%Y-%m-%d");

var x = d3.scaleTime().range([0, width]),
  y = d3.scaleLinear().range([height, 0]),
  z = d3.scaleOrdinal(d3.schemeCategory10);
var convertToDate = function(mon) {
  return new Date(Date.parse(mon + " 1, 2012"))
}
var line = d3.line()
  .curve(d3.curveBasis)
  .x(function(d) {
    return x(new Date(d.Date));
  })
  .y(function(d) {
    return y(d.temperature);
  });

/* chetan*/
function make_x_axis() {
  return d3.axisBottom()
    .scale(x)
    .ticks(12)
}

function make_y_axis() {
  return d3.axisLeft()
    .scale(y)
    .ticks(12)
}
//d3.tsv("data.tsv", type, function(error, data) {
//if (error) throw error;

var cities = columns.slice(1).map(function(id) {
  return {
    id: id,
    values: data.map(function(d) {
      return {
        Date: d.Date,
        temperature: d[id]
      };
    })
  };
});

x.domain(d3.extent(data, function(d) {
  return new Date(d.Date);
}));
y.domain([
  d3.min(cities, function(c) {
    return d3.min(c.values, function(d) {
      return d.temperature;
    });
  }),
  d3.max(cities, function(c) {
    return d3.max(c.values, function(d) {
      return d.temperature;
    });
  })
]);

z.domain(cities.map(function(c) {
  return c.id;
}));

g.append("g")
  .attr("class", "axis axis--x")
  .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(x));

g.append("g")
  .attr("class", "axis axis--y")
  .call(d3.axisLeft(y))
var city = g.selectAll(".city")
  .data(cities)
  .enter().append("g")
  .attr("class", "city");

city.append("path")
  .attr("class", "line")
  .attr("d", function(d) {
    return line(d.values);
  })
  .style("stroke", function(d) {
    return z(d.id);
  })
  .style("fill", "none");

/* city.append("text")
      .datum(function(d) { return {id: d.id, value: d.values[d.values.length - 1]}; })
      .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.temperature) + ")"; })
      .attr("x", 3)
      .attr("dy", "0.35em")
      .style("font", "10px sans-serif")
      .text(function(d) { return d.id; });
	  */
// add grid lines
svg.append("g")
  .attr("class", "grid")
  .attr("transform", "translate(50," + (height + 20) + ")")
  .call(make_x_axis()
    .tickSize(-height, 0, 0)
    .tickFormat("")
  )

/* svg.append("g")         
     .attr("class", "grid")
     .call(make_y_axis()
         .tickSize(-width, 0, 0)
         .tickFormat("")
     ) */


//});

function type(d, _, columns) {
  d.date = parseTime(d.date);
  for (var i = 1, n = columns.length, c; i < n; ++i) d[c = columns[i]] = +d[c];
  return d;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg width="960" height="500"></svg>

PS: The best practice, however, is changing the data itself:

data.forEach(function(d){
    d.Date = parseTime(d.Date)
});

That way, you can just pass d.Date for the line generator, the scale, the attributes, whatever.

Also, you should follow the JavaScript convention for names: instead of Date, use date.


Related Query