简体   繁体   中英

D3.js - Plotting time series data - Format x-axis from JSON

I plot very simple data from a JSON file: I need help format my x-date-axis. I don't know how to specify the date format from the JSON-File for d3.js. I tried the following:

var parseDate = d3.time.format("%Y%m%d").parse;

The JSON data looks like this:

var data = [
{"mytime": 20150801, "tt": 17.0}, 
{"mytime": 20150802, "tt": 17.6},
];

The result on the x-axis is not as expected. Find my fiddle here: https://jsfiddle.net/1m1qm6pv/1/

Problem i think is this:

data.forEach(function(d) {
  d.mytime = parseDate(d.mytime);
});

With these 3 lines of code it doesn't work.

Your "dates" are numbers and hence cannot be parsed into Date objects. To parse them, use strings instead of numbers:

var data = [
  {"mytime": "20150801", "tt": 17.0}, 
  {"mytime": "20150802", "tt": 17.6},
];

Complete demo here .

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