gpt4 book ai didi

d3.js - 使用 D3.js 解析时间序列数据

转载 作者:行者123 更新时间:2023-12-02 17:30:22 25 4
gpt4 key购买 nike

是时候寻求帮助了。我已经学习 D3.js 几个星期了,我才开始觉得我理解了其中的 10%(哈哈哈)。我正在尝试生成一个非常简单的线图。只要数据非常简单,我就可以做到这一点,但我的原始数据源具有 UTC 时间戳和实数/小数,这会导致任何超出简单范围的崩溃。

原始数据源如下所示:

{
"Links": {},
"Items": [
{
"Timestamp": "2016-07-12T22:21:10Z",
"Value": 1055.6793212890625,
"UnitsAbbreviation": "m3/h",
"Good": true,
"Questionable": false,
"Substituted": false
},
{
"Timestamp": "2016-07-12T22:39:10Z",
"Value": 989.00830078125,
"UnitsAbbreviation": "m3/h",
"Good": true,
"Questionable": false,
"Substituted": false
}
],
"UnitsAbbreviation": "m3/h"
}

使用 jQuery 和 JavaScript 时间格式化函数,我能够组装以下简化的数据集:

var dataset = [
{'theTime': '2016/07/12 15:58:40', 'theValue': 1123.07275390625},
{'theTime': '2016/07/12 16:21:10', 'theValue': 1055.6793212890625},
{'theTime': '2016/07/12 16:45:40', 'theValue': 962.4850463867188},
{'theTime': '2016/07/12 17:14:40', 'theValue': 831.2259521484375},
{'theTime': '2016/07/12 17:55:10', 'theValue': 625.3046875}
];

这是我的代码:

//~ Populate the 'dataset':
var dataset = [];
$.get(url, function(data){
var itemCount = data.Items.length;
var commaCount = itemCount - 1;
for(i=0; i < itemCount; i++){
if(i == commaCount){
dataset.push("{'theTime': '" + formattedDateTime(data.Items[i].Timestamp) + "', 'theValue': " + data.Items[i].Value + "}");
}
else {
dataset.push("{'theTime': '" + formattedDateTime(data.Items[i].Timestamp) + "', 'theValue': " + data.Items[i].Value + "},");
}
}

var margin = {top: 20, right: 20, bottom: 30, left: 50};
var width = 960 - margin.left - margin.right;
var height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
var x = d3.time.scale()
.range([0, width]);

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

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

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

var line = d3.svg.line()
.x(function(d) { return x(d.theTime); })
.y(function(d) { return y(d.theValue); });

var svg = d3.select("#myChart").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 + ")");

dataset.forEach(function(d) {
d.theTime = parseDate(d.theTime);
d.theValue = +d.theValue;
});

x.domain(d3.extent(data, function(d) { return d.theTime; }));
y.domain(d3.extent(data, function(d) { return d.theValue;}));

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("return time(ms)");

svg.append("path")
.datum(dataset)
.attr("class", "line")
.attr("d", line);
});

//~~~ Format The Date:
function formattedDateTime(dateAndTime) {
var d = new Date(dateAndTime);
var numDate = d.getDate();
var numMonth = d.getMonth() + 1;
var numYear = d.getFullYear();
var numHours = d.getHours();
var numMinutes = d.getMinutes();
var numSeconds = d.getSeconds();
numDate = (numDate < 10) ? "0" + numDate : numDate;
numMonth = (numMonth < 10) ? "0" + numMonth : numMonth;
numHours = (numHours < 10) ? "0" + numHours : numHours;
numMinutes = (numMinutes < 10) ? "0" + numMinutes : numMinutes;
numSeconds = (numSeconds < 10) ? "0" + numSeconds : numSeconds;

return numYear + "/" + numMonth + "/" + numDate + " " +
numHours + ":" + numMinutes + ":" + numSeconds;
};

第一个错误发生在“dataset.forEach()”函数中,即“Uncaught TypeError: Cannot read property 'length' of undefined ”。我解析该数据的努力源于代码中“svg.append(“path”)”点发生的另一个错误,即“Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNL…”。

最佳答案

问题是您传递的日期采用以下格式:

'2016/07/12 15:58:40'

您用来解析它的parsedate函数是(注意日期中没有连字符):

var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;

应该是

 var parseDate = d3.time.format("%Y/%m/%d %H:%M:%S").parse;

下一个错误是,您正在传递数据以获取范围,但它没有在任何地方定义:

 x.domain(d3.extent(data, function(d) { return d.theTime; }));
y.domain(d3.extent(data, function(d) { return d.theValue;}));

应该是:

x.domain(d3.extent(dataset, function(d) {
return d.theTime;
}));
y.domain(d3.extent(dataset, function(d) {
return d.theValue;
}));

工作代码here

关于d3.js - 使用 D3.js 解析时间序列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38341253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com