I'm encountering an issue when attempting to convert CSV to JSON. The following is the snippet of code I am using for the conversion:
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;
});
});
to
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;
}
});
When I use the json code, I encounter the error
19:15:41.950 TypeError: _ is undefined1 d3.v2.js:1982:10
. Can someone please guide me on what mistake I might be making during the conversion process?