我有两个不同的问题,但它们可能有关:
这是d3.queue的版本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Choropleth Map</title>
<style>
.counties {
fill: none;
}
.states {
fill: none;
stroke: black;
stroke-linejoin: round;
}
</style>
</head>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://d3js.org/d3-queue.v3.min.js"></script>
<script>
var margin = {top: 10, right: 200, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var earnings = d3.map();
var path = d3.geo.path();
var x = d3.scale.linear()
.domain([1, 10])
.rangeRound([600, 860]);
var color = d3.scale.threshold()
.domain(d3.range(2, 10))
.range(['#f7fcfd','#e5f5f9','#ccece6','#99d8c9','#66c2a4','#41ae76','#238b45','#006d2c','#00441b']);
// Load data
d3.queue()
.defer(d3.json, "us.json")
.defer(d3.json, "median_earnings.json", loadjson) //removing loadjson from here brings up the map of U.S. states without any fill. Keeping it shows a blank page
.await(ready);
function loadjson (error, earnings) {
if (error) throw error;
earnings.forEach(function(d) {
d.id = +d.id;
d.median = +d.median_earnings;
});
}
function ready(error, us) {
if (error) throw error;
svg.append("path")
.attr("class", "states")
.datum(topojson.feature(us, us.objects.states), function(a, b) { return a !== b; })
.attr("d", path);
svg.selectAll("path")
.attr("class", "states")
.datum(earnings)
.attr("fill", function(d,i) {
return color(d[i].median);
});
}
</script>
</body>
</html>
&#13;
我使用的us.json文件是here和median_earnings here。
这是d3.queue的版本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Choropleth Map of College Data</title>
<style>
.counties {
fill: none;
}
.states {
stroke: #fff;
stroke-linejoin: round;
}
</style>
</head>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://d3js.org/d3-queue.v3.min.js"></script>
<script>
var margin = {top: 10, right: 200, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
var earnings = d3.map();
var path = d3.geo.path();
var x = d3.scale.linear()
.domain([1, 10])
.rangeRound([600, 860]);
var color = d3.scale.threshold()
.domain([15000, 18000, 21000, 24000, 27000, 30000, 33000]) .range(['#f7fcfd','#e5f5f9','#ccece6','#99d8c9','#66c2a4','#41ae76','#238b45','#005824']);
d3.json("median_earnings.json", function loadjson(error, earnings) {
if (error) throw error;
earnings.forEach(function(d) {
d.id = +d.id;
d.median = +d.median_earnings;
d3.json("us.json", function ready(error, us) {
if (error) throw error;
svg.append("path")
.attr("class", "states")
.datum(topojson.feature(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("d", path)
.style("stroke", "grey");
svg.selectAll("path")
.attr("class", "states")
.datum(earnings)
.attr("fill", function(d,i) {
return color(d[i].median);
});
});
})
});
</script>
</body>
</html>
&#13;
答案 0 :(得分:0)
这是因为您文件上的ID不匹配。