d3美国地图未加载

时间:2019-04-09 08:30:38

标签: angular typescript d3.js

我正在使用 http://bl.ocks.org/mbostock/4699541 制作我的d3美国地图。

当我编译代码时,它只会显示一个黑框。在检查时,知道svg已取代它,但是'g'元素什么也没显示(g(0x0))。

callMap() {
    const albersUsa: d3.GeoProjection = d3.geoAlbersUsa();
    const geoPathCanvas: d3.GeoPath = d3.geoPath();
    var width = 960,
    height = 500,
    active = d3.select(null);

    var projection = albersUsa
        .scale(1000)
        .translate([width / 2, height / 2]);

    var path = geoPathCanvas
        .projection(projection);

    var svg = d3.select("body").append("svg")
        .attr("width", width)
        .attr("height", height);

    svg.append("rect")
        .attr("class", "background")
        .attr("width", width)
        .attr("height", height)
        .on("click", reset);

    var g = svg.append("g")
        .style("stroke-width", "1.5px");

    d3.json("assets/us.json", function(error, us) {
      if (error) throw error;

      g.selectAll("path")
          .data(topo.feature(us, us.objects.states).features)
        .enter().append("path")
          .attr("d", path)
          .attr("class", "feature")
          .on("click", clicked);

      g.append("path")
          .datum(topo.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
          .attr("class", "mesh")
          .attr("d", path);
    });
...
...

我已将“ /mbostock/raw/4090846/us.json”数据保存在本地文件中,并从中获取数据。但是作为输出,什么也没有显示。

谢谢。

0 个答案:

没有答案