d3js - 查找将`json`转换为日期值的问题

时间:2016-02-17 09:07:46

标签: d3.js

我从后端获取json个数据。使用后端数据,我试图绘制折线图。

但是图表非常难看,因为数字值并没有正确转换。此外,我没有将x轴值作为日期值。如何在这里正确转换日期编号?

我的尝试:



var datas = [
{"date":1404075600000,"ActualPercentage" : 63.4, "PlanPercentage" : 62.7},
{"date":1404680400000,"ActualPercentage" : 58.0, "PlanPercentage" : 59.9},
{"date":1405285200000,"ActualPercentage" : 53.3, "PlanPercentage" : 59.1},
{"date":1405890000000,"ActualPercentage" : 55.7, "PlanPercentage" : 58.8},
{"date":1406494800000,"ActualPercentage" : 64.2, "PlanPercentage" : 58.7},
{"date":1407099600000,"ActualPercentage" : 58.8, "PlanPercentage" : 57.0},
{"date":1407704400000,"ActualPercentage" : 57.9, "PlanPercentage" : 56.7},
{"date":1408309200000,"ActualPercentage" : 61.8, "PlanPercentage" : 56.8},
{"date":1408914000000,"ActualPercentage" : 69.3, "PlanPercentage" : 56.7},
{"date":1409518800000,"ActualPercentage" : 71.2, "PlanPercentage" : 60.1}

]


var margin = {top: 20, right: 80, bottom: 30, left: 50},
    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]);
var y = d3.scale.linear().range([height, 0]);

var color = d3.scale.ordinal()
      .domain(["ActualPercentage", "PlanPercentage"])
      .range(["#FF0000", "#009933"]);

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

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

var line = d3.svg.line()
    .interpolate("basis")
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.temperature); }); 


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


  color.domain(d3.keys(datas[0]).filter(function(key) { return key !== "date"; }));

  datas.forEach(function(d) {
    d.date = parseDate( String(d.date).slice(0, 8 ) );
  });

  var cities = color.domain().map(function(name) {
    return {
      name: name,
      values: datas.map(function(d) {
        return {date: d.date, temperature: +d[name]};
      })
    };
  });
  

  x.domain(d3.extent(datas, function(d) { return d.date; }));

  y.domain([
    d3.min(cities, function(c) { return d3.min(c.values, function(v) { return v.temperature; }); }),
    d3.max(cities, function(c) { return d3.max(c.values, function(v) { return v.temperature; }); })
  ]);

  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("Temperature (ºF)");

  var city = svg.selectAll(".city")
      .data(cities)
      .enter().append("g")
      .attr("class", "city");

  var path = city.append("path")
      .attr("class", "line")
      .attr("d", function(d) { return line(d.values); })
      .style("stroke", function(d) { return color(d.name); });
      
  var totalLength = [path[0][0].getTotalLength()];
  
  path
      .attr("stroke-dasharray", totalLength + " " + totalLength)
      .attr("stroke-dashoffset", totalLength)
      .transition()
      .duration(2000)
      .ease("linear")
      .attr("stroke-dashoffset", 0);

body {
  font: 10px sans-serif;
}

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

.x.axis path {
  display: none;
}

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您只需使用Date函数将时间戳字符串转换为日期即可。

datas.forEach(function(d) {
  d.date = new Date(d.date);
});

&#13;
&#13;
var datas = [{
    "date": 1404075600000,
    "ActualPercentage": 63.4,
    "PlanPercentage": 62.7
  }, {
    "date": 1404680400000,
    "ActualPercentage": 58.0,
    "PlanPercentage": 59.9
  }, {
    "date": 1405285200000,
    "ActualPercentage": 53.3,
    "PlanPercentage": 59.1
  }, {
    "date": 1405890000000,
    "ActualPercentage": 55.7,
    "PlanPercentage": 58.8
  }, {
    "date": 1406494800000,
    "ActualPercentage": 64.2,
    "PlanPercentage": 58.7
  }, {
    "date": 1407099600000,
    "ActualPercentage": 58.8,
    "PlanPercentage": 57.0
  }, {
    "date": 1407704400000,
    "ActualPercentage": 57.9,
    "PlanPercentage": 56.7
  }, {
    "date": 1408309200000,
    "ActualPercentage": 61.8,
    "PlanPercentage": 56.8
  }, {
    "date": 1408914000000,
    "ActualPercentage": 69.3,
    "PlanPercentage": 56.7
  }, {
    "date": 1409518800000,
    "ActualPercentage": 71.2,
    "PlanPercentage": 60.1
  }

]


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


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

var color = d3.scale.ordinal()
  .domain(["ActualPercentage", "PlanPercentage"])
  .range(["#FF0000", "#009933"]);

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

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

var line = d3.svg.line()
  .interpolate("basis")
  .x(function(d) {
    return x(d.date);
  })
  .y(function(d) {
    return y(d.temperature);
  });


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


color.domain(d3.keys(datas[0]).filter(function(key) {
  return key !== "date";
}));

datas.forEach(function(d) {
  d.date = new Date(d.date);
});

var cities = color.domain().map(function(name) {
  return {
    name: name,
    values: datas.map(function(d) {
      return {
        date: d.date,
        temperature: +d[name]
      };
    })
  };
});


x.domain(d3.extent(datas, function(d) {
  return d.date;
}));

y.domain([
  d3.min(cities, function(c) {
    return d3.min(c.values, function(v) {
      return v.temperature;
    });
  }),
  d3.max(cities, function(c) {
    return d3.max(c.values, function(v) {
      return v.temperature;
    });
  })
]);

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("Temperature (ºF)");

var city = svg.selectAll(".city")
  .data(cities)
  .enter().append("g")
  .attr("class", "city");

var path = city.append("path")
  .attr("class", "line")
  .attr("d", function(d) {
    return line(d.values);
  })
  .style("stroke", function(d) {
    return color(d.name);
  });

var totalLength = [path[0][0].getTotalLength()];

path
  .attr("stroke-dasharray", totalLength + " " + totalLength)
  .attr("stroke-dashoffset", totalLength)
  .transition()
  .duration(2000)
  .ease("linear")
  .attr("stroke-dashoffset", 0);
&#13;
body {
  font: 10px sans-serif;
}
.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}
.x.axis path {
  display: none;
}
.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;
&#13;
&#13;