I have data in the form
var JSONData = [
{"id": "Line1", "x_data": ["2005-01-01 01:00", "2005-01-01 02:00", "2005-01-01 03:00", "2005-01-01 04:00", "2005-01-01 05:00"], "y_data": [1, 2, 3, 4, 5]},
{"id": "Line2", "x_data": ["2005-01-01 01:00", "2005-01-01 02:00", "2005-01-01 03:00", "2005-01-01 04:00", "2005-01-01 05:00"], "y_data": [6, 7, 8, 9, 10]},
{"id": "Line3", "x_data": ["2005-01-01 01:00", "2005-01-01 02:00", "2005-01-01 03:00", "2005-01-01 04:00", "2005-01-01 05:00"], "y_data": [11, 12, 13, 1, 15]}
]
and would like to zip x data with y data and apply a date parsing function to each x element of the returned array of array.
I tried the following but it is returning undefined array.
var parseDate = d3.time.format("%Y-%m-%d %H:%M").parse;
var nested = d3.nest()
.key(function(d) {
return d.name;
})
.map(JSONData)
console.log("nested ", nested);
var keyring = d3.keys(nested).filter(function(key) {
return (key !== "x_data" && key !== "y_data");
});
console.log("keyring ", keyring);
var transpose = keyring.map(function(name) {
console.log("name ", name);
return {
name: name,
values: nested[name].map(function(d) {
var zip_data = d3.zip(d.x_data, d.y_data).forEach(function(d, i) {
d[0] = parseDate(d[0]);
d[1] = +d[1];
});
console.log("zip data ", zip_data);
return zip_data;
})
};
});
Thanks for help.
Try this.
var parseDate = d3.time.format("%Y-%m-%d %H:%M").parse;
var requiredArray= JSONData.map(function(d) {
var parsedX = d.x_data.map(function(date){ return parseDate(date) });
return d3.zip(parsedX , d.y_data)
});
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.