d3.parseDate错误无法读取属性“forEach”未定义 [英] d3.parseDate Error cannot read property 'forEach' of undefined

查看:317
本文介绍了d3.parseDate错误无法读取属性“forEach”未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你能帮助我。我被卡住试图使用D3和一个JSON文件制作一个时间序列的条形图。我的JSON文件的结构如下:

I hope y'all can help me. I am stuck trying to make a bar chart with a time series using D3 and a JSON file. My JSON file is structured like so:

{
            "ProjectCode": "PRJ2.1",
            "numberOfSchools": 1188,
            "newSchools": 0,
            "date": 2011-12
        }, 

y轴应显示numberOfSchools,而x轴应显示已经过去的年数。我对x轴使用时间尺度和序数尺度的组合,并解析日期如下:

The y axis is supposed to show the numberOfSchools whilst the x axis is supposed to show the years passed. I used a combination of a time scale and ordinal scale for the x axis and parsed the date as so:

data.forEach(function(d) {
                    d.date = parseDate(d.date.toString());
                    d.numberOfSchools = +d.numberOfSchools;
                });

我猜JSON文件没有被正确解析,并尝试了解决方案在这里在这里没有运气。我使用D3.v3。以下是我的JSFiddle

I'm guessing the JSON file is not being parsed properly and have tried the solutions here and here with no luck. I'm using D3.v3. and below is my JSFiddle

http:// jsfiddle。 net / siyafrica / ESYE3 /

推荐答案

我已经看过上面的小提琴,给出文件所在的确切位置。 Json文件的URL可能是错误的,这就是为什么问题出现。
阅读下面的定义

I've seen above fiddle, and understood, from that I can say give the exact location where the file is. Url of the Json File might be wrong that's why problem is raising. Read below Definition


d3.json(url [,callback])

d3.json(url[, callback])

使用mime
类型application / json在指定的URL创建JSON文件的请求。如果指定了回调,则请求是立即发出的GET方法的
,当文件加载或请求失败时,回调将是
异步调用;
使用两个参数调用回调:错误(如果有)和
解析JSON。如果发生错误,则解析的JSON未定义。如果没有指定
回调,返回的请求可以使用
xhr.get或类似的方法发出,并使用xhr.on处理。

Creates a request for the JSON file at the specified url with the mime type "application/json". If a callback is specified, the request is immediately issued with the GET method, and the callback will be invoked asynchronously when the file is loaded or the request fails; the callback is invoked with two arguments: the error, if any, and the parsed JSON. The parsed JSON is undefined if an error occurs. If no callback is specified, the returned request can be issued using xhr.get or similar, and handled using xhr.on.

以上是来自GitHub - > https://github.com / mbostock / d3 / wiki / Requests#d3_json

Above is from GitHub--> https://github.com/mbostock/d3/wiki/Requests#d3_json

这篇关于d3.parseDate错误无法读取属性“forEach”未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆