1

I'm using a geoJson to load a world map. I'm testing to see if it works but I'm not getting anything back. I know the file being loaded is correct. What I'm expected is to get the data returned, again to validate things are working. I don't get any errors.

import { Component, OnInit, AfterViewInit } from '@angular/core'; import * as d3 from 'd3';

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss']
})
export class MapComponent implements OnInit, AfterViewInit {

    private svg;
    private width;
    private height;
    private features;

  constructor() { }

  ngOnInit() {
  }

  ngAfterViewInit() {
      this.svg = d3.select("svg");
      this.height = this.svg.attr("height");
      this.width = this.svg.attr("width");

      this.features = this.svg.append("g")

      d3.json("/assets/files/views/maps/world.json", function(error, mapData) {
          console.log(mapData.features);
      });

  }

}
dcp3450
  • 10,959
  • 23
  • 58
  • 110
  • Which version of d3 are you using? – Andrew Reid May 29 '18 at 17:33
  • @AndrewReid d3: 5.4; angular: 6.0.1; – dcp3450 May 29 '18 at 18:00
  • 1
    d3 v5 has a few changes in this regard. The library now uses the d3-fetch module([link](https://github.com/d3/d3-fetch)) (built on top of Fetch), where `d3.json("file.json")` return a promise instead of invoking a callback function, so you are looking for `d3.json("file.json").then(function(data) { /* do stuff */ });` – Andrew Reid May 29 '18 at 18:04
  • @AndrewReid that was it! can you supply this as an answer so you get credit? – dcp3450 May 29 '18 at 18:20
  • I would have, but I figure that this has been asked a few times. Yesterday I answered a similar [question](https://stackoverflow.com/q/50572762/7106086) (also a duplicate), though looking a little more carefully now I came across this [answer](https://stackoverflow.com/q/49768165/7106086) by Altocumulus that addresses what is really the same issue as here. – Andrew Reid May 29 '18 at 18:36
  • 1
    Possible duplicate of [Code within d3.json() callback is not executed](https://stackoverflow.com/questions/49768165/code-within-d3-json-callback-is-not-executed) – Andrew Reid May 29 '18 at 21:13

0 Answers0