d3.parseDate Error cannot read property 'forEach' of undefined

依然范特西╮ 提交于 2020-01-16 05:35:32

问题


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
        }, 

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;
                });

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/


回答1:


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])

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.

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



来源:https://stackoverflow.com/questions/24425252/d3-parsedate-error-cannot-read-property-foreach-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!