Rachel
Rachel

Reputation: 359

d3 line chart not showing,can not read in date data

Here is my code:

    // Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 600 - margin.left - margin.right,
    height = 270 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%d-%m-%y").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(5);

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

// Define the line
var valueline = d3.svg.line()
    .x(function(d) { return x(+d.AtTime); })
    .y(function(d) { return y(d.Temperature); });

// Adds the svg canvas
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 + ")");

// Get the data
d3.csv("../datasets/test.csv", function(error, data) {
    data.forEach(function(d) {
        d.AtTime = parseDate(d.AtTime);
        d.Temperature = +d.Temperature;
    });
console.log(data);
    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.AtTime; }));
    y.domain([0, d3.max(data, function(d) { return d.Temperature; })]);

    // Add the valueline path.
    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data));

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

    // Add the Y Axis
    svg.append("g")
        .attr("class", "y axis")
        .call(yAxis);

});

And the line chat just can not show...Here is my simple csv dataset:

Category,Temperature,Pressure,AtTime A,76,400,1-1-2014 B,23,239,2-3-2013 C,38.6,378,2-6-2016 D,43.2,289,3-5-2015 E,49,501,3-5-2015

I used console.log(data) to check, and I found that AtTime has null value.... I do not why..Anyone could help me with this? Thanks!

Upvotes: 1

Views: 350

Answers (2)

torresomar
torresomar

Reputation: 2229

There is a mistake in your dates, I parsed them and ordered them so the line would render correctly.

  data.forEach(function(d) {
    var dateArr = d.AtTime.split('-');
    d.AtTime = new Date(+dateArr[2], +dateArr[1], +dateArr[0]);
    d.Temperature = +d.Temperature;
  })
  data.sort(function(a, b) {
    return new Date(b.AtTime) - new Date(a.AtTime);
  });

Remove the implicit number cast in your valueLine function:

var valueline = d3.svg.line()
  .x(function(d) {
    return x(d.AtTime);
  })
  .y(function(d) {
    return y(d.Temperature);
  });

And finally if you don't have any css add a stroke to your line so it renders:

  svg.append("path")
    .attr("class", "line")
    .attr('stroke', 'red')
    .attr("d", valueline(data));

Working plnkr: https://plnkr.co/edit/ujFDtHkdtC2EeBwJ1foU?p=preview

Upvotes: 0

mgraham
mgraham

Reputation: 6207

It's one character in your time format string.

//var parseDate = d3.time.format("%d-%m-%y").parse; // lower-case Y expects two digits only
var parseDate = d3.time.format("%d-%m-%Y").parse; // use capital Y for 4 digit years 

see https://github.com/d3/d3/wiki/Time-Formatting#format for all the different modifiers

Upvotes: 1

Related Questions