特定の郡IDに特定のクラスを割り当てる関数を作成しようとしています。私はonclick
を使って機能を書いたが、それはDOM要素を利用していた。以下は、コードのトリミングバージョンである出力は青のクラスを割り当てていない、それだけで、既存のクラスを削除し、郡の黒を回しています:特定のクラスを特定の郡に割り当てる - D3.js
<!DOCTYPE html>
<style>
.c550000 {
fill: blue;
stroke-width: .5px;
}
.counties .hovered,
.counties :hover {
fill: #ffffff;
stroke-width: .5px;
}
.county-borders {
fill: none;
stroke: #F0F8FF;
stroke-width: .2px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
.state-borders {
fill: none;
stroke: #162955;
opacity: .8;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
</style>
<svg class="map" width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
var svg = d3.select("svg");
var path = d3.geoPath();
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
var jsonCounties = us.objects.counties.geometries;
function loadCurrent() {
for (var i = 0; i < jsonCounties.length; i++) {
if (jsonCounties[i].id == 32007) { //Random county id for testing
var geoCounty = us.objects.counties.geometries.filter(function(d) {return d.id == 32007;});
us.objects.counties.geometries = geoCounty;
var county = topojson.feature(us, us.objects.counties);
svg.append("g")
.attr("class", "c550000") //apply this attribute to this county
.selectAll("path")
.data(county)
.enter()
.append("path")
.attr("d", path);
}
}
}
loadCurrent();
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter()
.append("path")
.attr("d", path);
svg.append("g")
.attr("class", "state-borders")
.selectAll("path")
.data(topojson.feature(us, us.objects.nation).features)
.enter()
.append("path")
.attr("d", path);
svg.append("path")
.attr("class", "state-borders")
.attr("d", path(topojson.mesh(us, us.objects.nation, function(a, b) {
return a !== b;
})));
svg.append("path")
.attr("class", "state-borders")
.attr("d", path(topojson.mesh(us, us.objects.states, function(a, b) {
return a !== b;
})));
svg.append("path")
.attr("class", "county-borders")
.attr("d", path(topojson.mesh(us, us.objects.counties, function(a, b) {
return a !== b;
})));
});
</script>
感謝レスポンスマーク!最終的な目標は、マップ上のすべての郡に10のクラスの1つを割り当てることです。申し訳ありませんが、指定してください。上記のコードは、forループのマクロスケールにも適用されますか? –
@MatthewSnell、上記の編集を参照してください。 – Mark