0
私は2つの異なる問題を抱えているが、それらが関連している可能性があり:私は2つのJSONファイルのいずれかをロードしている間、私はコールバック関数を呼び出す場合は動作しd3.queueを取得することはできませんD3のchoroplethキューと塗りつぶしの問題
- 同時。 d3.queueを使用せず、別のd3.jsonを別のd3.jsonの中にネストすると、マップが表示されますが、塗りつぶしの問題(後述)は解決されません。
- d3.queueの有無にかかわらず、米国の各州に独自の塗りつぶしの色が表示されるようにはできません。私はこれが、それぞれの州ごとに別々の「コンテナ」を作ることに失敗しているためだと考えています。ブラウザのインスペクタで "path class = states"のコンテナにカーソルを合わせると、マップ全体が強調表示され、想定されている個々の状態ではなくなります。したがって、すべての色がお互いの上に適用されていますが、最も暗い影が支配的であり、その色しか見えません。
これは、私が使用していますus.jsonファイルがhereとmedian_earnings hereある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>
とバージョンです。
そして、これはあなたのファイルのIDが一致しないので、それがある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>
あなたはそれについて詳しく説明できますか?私はあなたが意味することを理解していません。 – PythonGuy