javascript - Uncaught TypeError: Cannot read property 'type' of undefined with d3.js - Stack Overflow

admin2025-04-19  0

I've been stuck on this problem for hours at this point and I can't figure out the problem.

I've been following / with my own data set.

The error: Uncaught TypeError: Cannot read property 'type' of undefined - topojson.v0.min.js:1

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<style>

path {
  fill: #ccc;
  stroke: #fff;
  stroke-width: .5px;
}

path:hover {
  fill: red;
}

</style>
</head>
<body>
<script src="../static/js/d3.min.js"></script>
<script src="../static/js/topojson.v0.min.js"></script>
<script>


var width = 900,
    height = 900;

var path = d3.geo.path();

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


d3.json("../static/json/newjerseymun.json", function(nj) {
  svg.append("path")
      .datum(topojson.object(nj, nj.objects.subunits))
      .attr("d", d3.geo.path().projection(d3.geo.mercator()));
});

</script>

</body>

Here is a sample of the beginning of my json file:

I've been stuck on this problem for hours at this point and I can't figure out the problem.

I've been following http://bost.ocks/mike/map/ with my own data set.

The error: Uncaught TypeError: Cannot read property 'type' of undefined - topojson.v0.min.js:1

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<style>

path {
  fill: #ccc;
  stroke: #fff;
  stroke-width: .5px;
}

path:hover {
  fill: red;
}

</style>
</head>
<body>
<script src="../static/js/d3.min.js"></script>
<script src="../static/js/topojson.v0.min.js"></script>
<script>


var width = 900,
    height = 900;

var path = d3.geo.path();

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


d3.json("../static/json/newjerseymun.json", function(nj) {
  svg.append("path")
      .datum(topojson.object(nj, nj.objects.subunits))
      .attr("d", d3.geo.path().projection(d3.geo.mercator()));
});

</script>

</body>

Here is a sample of the beginning of my json file: http://pastebin./g0Lut36V

Share asked Jan 27, 2013 at 7:03 PFlansPFlans 4202 gold badges6 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The problem seems to be in the file ../static/json/newjerseymun.json.

The segment of the newjerseymin.json you have posted does have the field objects which contains a JSON object but there does not seem to be a subunits field in that object, not at least in the segment you have posted. Hence, ns.objects.subunits would be undefined which causes the error.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745014069a279982.html

最新回复(0)