1
我現在面臨困難,同時轉換到CSV JSON.Below是我試圖轉換皈依從CSV到JSON在d3.js
d3.csv("http://localhost:8080/Sample/flight.csv", function(flights) {
//alert(flights);
var linksByOrigin = {},
countByAirport = {},
locationByAirport = {},
positions = [];
var arc = d3.geo.greatArc()
.source(function(d) { return locationByAirport[d.source]; })
.target(function(d) { return locationByAirport[d.target]; });
//reading from csv
flights.forEach(function(flight) {
var origin = flight.origin,
destination = flight.destination,
links = linksByOrigin[origin] || (linksByOrigin[origin] = []);
links.push({source: origin, target: destination});
countByAirport[origin] = (countByAirport[origin] || 0) + 1;
countByAirport[destination] = (countByAirport[destination] || 0) + 1;
});
代碼});
到
d3.json("http://localhost:8080/Sample/flight.json", function(flights) {
//alert(flights);
var linksByOrigin = {},
countByAirport = {},
locationByAirport = {},
positions = [];
var arc = d3.geo.greatArc()
.source(function(d) { return locationByAirport[d.source]; })
.target(function(d) { return locationByAirport[d.target]; });
var flights = flights.flights;
alert("flights"+flights.length);
for(var i = 0; i < flights.length; i++) {
alert("origin"+flights[i].origin+"dest"+flights[i].destination);
var origin = flights[i].origin;
var destination = flights[i].destination;
alert("origin"+origin+"dest"+destination)
var links = linksByOrigin[origin] || (linksByOrigin[origin] = []);
links.push({source: origin , target: destination});
countByAirport[origin] = (countByAirport[origin] || 0) + 1;
countByAirport[destination] = (countByAirport[destination] || 0) + 1;
}
});
這是給我19:15:41.950 TypeError: _ is undefined1 d3.v2.js:1982:10
磨片我使用JSON代碼。 請告訴我轉換時我做錯了什麼。
here my json - '{「flights」:[{「origin」:「6972475」,「destination」:「1411474」,「count」:「1853」}, {「origin」:「6972475」 「目標」: 「4683455」, 「算」: 「2853」} ]}'和我的航班以csv'始發地,目的地,算上 6972475,1411474,2853 6972475,4683455,1853' – user2420143
它現在的工作。在構建json時需要非常小心。我做的錯誤是我留下了一個空格。而不是「目的地」:「1411474」'它就像''目的地「:」1411474「'。那就是爲什麼拋出錯誤。 – user2420143
偉大你設法搞清楚!在沒有完整的json和代碼的情況下,無法找到。 –