简体   繁体   中英

D3 v4 line chart with JSON dates

I've worked on this for a few days now, and I cant really get my line drawn and got some date formating problems I can't crack.

Using this D3 v3 fiddle as inspiration: http://jsfiddle.net/vmvp0zja/ I tried to convert it to D3 v4, but I can't really get my data drawn properly.

I am trying to draw several lines, but I cant even draw one..

Could you take a look and see what I am missing here? Thanks! :)

// JSON data:
var data = [
{"Date":"\/Date(1475272800000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1475359200000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1475445600000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1475532000000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1475618400000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1475704800000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1475791200000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1475877600000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1475964000000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1476050400000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1476136800000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1476223200000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1476309600000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1476396000000)\/","Open":0,"Closed":0},
{"Date":"\/Date(1475445600000)\/","Open":1,"Closed":0},
{"Date":"\/Date(1475532000000)\/","Open":1,"Closed":0},
{"Date":"\/Date(1475618400000)\/","Open":2,"Closed":0},
{"Date":"\/Date(1475791200000)\/","Open":9,"Closed":0},
{"Date":"\/Date(1475964000000)\/","Open":1,"Closed":0},
{"Date":"\/Date(1475445600000)\/","Open":0,"Closed":1},
{"Date":"\/Date(1475532000000)\/","Open":0,"Closed":1},
{"Date":"\/Date(1475618400000)\/","Open":0,"Closed":1},
{"Date":"\/Date(1475964000000)\/","Open":0,"Closed":1}]

This is my D3 mess:

// linechart.js
var formatTime = d3.timeFormat("%Y-%m-%d");

    data.forEach(function (d) {
        var unixToISO = new Date(d.Date.match(/\d+/)[0]*1);
        d.Date = formatTime(unixToISO);
        d.Open = +d.Open;
        d.Closed = +d.Closed;
                console.log(d.Date);
        return d;
    });

    var margin = {top: 30, right: 40, bottom: 30, left: 50  },
       width = 600 - margin.left - margin.right,
       height = 270 - margin.top - margin.bottom;

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

    var y0 = d3.scaleLinear()
        .range([height, 0]);

    // Scale the range of the data
    x.domain(d3.extent(data, function (d) { return d.Date; }));
    y0.domain([
        d3.min(data, function (d) { return Math.min(d.Open); }),
        d3.max(data, function (d) { return Math.max(d.Open); })]);


    var valueline1 = d3.line()
      .x(function (d) { console.log(x(d.Date)); return x(d.Date); })
      .y(function (d) { return y(d.Open); });

    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 + ")");

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

    svg.append("g")
      .attr("class", "y axis")
      .style("fill", "steelblue")
      .call(d3.axisLeft(y0));

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

Help much appreciated in advance. I am probably overlooking something obvious. Thank you!

Changes:

data.forEach(function(d) {
  var unixToISO = new Date(d.Date.match(/\d+/)[0] * 1);
  d.Date = unixToISO; // here
  d.Open = +d.Open;

// (...)
var valueline1 = d3.line()
  .x(function(d) {
    console.log(x(d.Date));
    return x(d.Date);
  })
  .y(function(d) {
    return y0(d.Open); // here
  });

// (...)
svg.append("path")
  .data([data]) // here
  .attr("class", "line")
  .attr("d", valueline1);

and added x-axis labels with

svg.append("text")
  .attr("transform", "translate(" + (width / 2) + " ," +
    (height + margin.top + 20) + ")")
  .style("text-anchor", "middle")
  .text("Date");

BTW, your data is unsorted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM