次のコードは、mousemoveとmouseclickで郡を強調表示するためのコードです。コードはほとんど正しいですが、私が問題を解決するのに唯一問題となるのは、マウスをクリックして移動するときです。最初の選択は強調表示されず、スキップされます(選択して戻ることができますが、最初から選択してください)。クリックが開始された最初の選択にドラッグ機能が適用されない
ありがとうございます。
<!DOCTYPE html>
<title>Heartland Remapping Tool</title>
<link rel="shortcut icon" type="image/png" href="/faviconpng.png"/>
<style>
svg{
width:100%;
height: auto;
}
.counties, .unhovered {
fill: white;
stroke: #7887AB;
stroke-width: .5px;
}
.counties .hovered, .counties :hover {
fill: #061539;
stroke-width: 0px;
}
.selected {
fill: #061539;
}
.deselected {
fill: white;
}
.deselected :hover {
fill: white;
}
.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 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();
var clickDown = true;
var numSelectedCounties = 0;
var selectedCounties = {};
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
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; })));
//Clicking stuff below.
let hoverEnabled = false;
svg.selectAll('.counties path')
.on('mousedown', x => hoverEnabled = true)
.on('mouseup', x => hoverEnabled = false)
.on('mouseover', function() {
if (hoverEnabled) {
if (!d3.select(this).classed('hovered')) {
d3.select(this).classed('hovered', true);
numSelectedCounties++;
}
}
})
.on('click', function() {
if (d3.select(this).classed('hovered')) {
d3.select(this).classed('hovered', false);
numSelectedCounties--;
}
else {
d3.select(this).classed('hovered', true);
numSelectedCounties++;
}
})
});
</script>
クリックしてのMouseMoveをドラッグ – mehulmpt