简体   繁体   中英

D3/Javascript code conundrum?

I have the following code:

d3.csv("flights-airport_3.csv", function(flights) {

var linksByOrigin = {},
  countByAirport = {},
  cancelledByAirport = {},
  locationByAirport = {},
  positions = [];

var arc = d3.geo.greatArc()
  .source(function(d) { return locationByAirport[d.source]; })
  .target(function(d) { return locationByAirport[d.target]; });

flights.forEach(function(flight) {
var origin = flight.origin,
    destination = flight.destination,
    count_airport = flight.count,
    cancelledByAirport = flight.count_cancelled,
    links = linksByOrigin[origin] || (linksByOrigin[origin] = []);
links.push({source: origin, target: destination});

My flights data array looks like this:

0: Object "": "" count: "9" count_cancelled: "0" destination: "IAD" origin: "ALB"

For some reason my cancelledByAirport is empty? I am not sure why since I am treating it exactly the same as my countByAirport variable. Can anyone help?

I think your problem is that you have two variables called "cancelledByAirport". One is declared just below d3.csv(...), another within foreach().

The solution would be giving different names to those variables, or perhaps getting rid of one of them.

Please review your code with respect to those two variables, and tell us what you find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM