D3图表,如何迭代通过Json数据NOT从文件? [英] D3 Charts, How to iterate through Json data NOT from file?
问题描述
我试图将JSON数据(从AJAX调用)传递到使用D3图表来迭代数据的函数。我没有传递的URL到Json文件,我传递的数据从AJAX调用返回的Json。然而,每次我这样做,回调中的数据是未定义的,不会让我迭代通过它。如何管理?
I'm trying to pass JSON data (from an AJAX call) into a function that uses D3 charts to iterate through the data. I am NOT passing the URL to a Json file, I am passing the DATA from a Json that is returned from an AJAX call. Every time I do this, however, the 'data' in the callback is undefined and won't let me iterate through it. How do I manage this? I just want to iterate through the JSON data and draw things according to it.
这里是示例代码:
drawLine(jsonData)
function drawLine(chartData) {
d3.json(chartData, function(data) {
data.forEach(function(d) {
d.date = parseDateD3(d.date);
d.close = +d.close;
});
// DO STUFF PAST HERE TO DATA
});
}
推荐答案
code> d3.json 如果你正在做一个AJAX调用。如果你已经有数据,代码应该是这样的。
You only need to use d3.json
if you're actually doing an AJAX call. If you have the data already, the code should look like this.
function drawLine(data) {
data.forEach(function(d) {
d.date = parseDateD3(d.date);
d.close = +d.close;
});
// DO STUFF PAST HERE TO DATA
}
这篇关于D3图表,如何迭代通过Json数据NOT从文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!