我在地图上,部门和城市绘制公寓和镇的不同颜色。这些部门由红线划定界限。我想当我把鼠标悬停在市政府的时候,除了画这个直辖市外,还画了另一种颜色的部门。d3.js
var width = 900,
height = 900;
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("https://cdn.rawgit.com/finiterank/mapa-colombia-js/9ae3e4e6/colombia-municipios.json", function(error, co) {
var subunits = topojson.feature(co, co.objects.mpios);
var projection = d3.geo.mercator()
.scale(2000)
.translate([width/2, height/2])
.center([-61,43])
.rotate([2,3,2]);
var path = d3.geo.path()
.projection(projection);
svg.append("path")
.datum(subunits)
.attr("d", path);
//departments
svg.selectAll(".dpto")
.data(topojson.feature(co, co.objects.depts).features)
.enter().append("path")
.on('mouseover', mouseoverDep)
.on('mouseout',mouseoutDep)
.attr("class", function(d) { return "depts " + "_" + d.id; })
.attr("d", path)
svg.append("path")
.datum(topojson.mesh(co, co.objects.depts, function(a, b) { return true; }))
.attr("d", path)
.attr("class", "depto-borde");
//municipalities
svg.selectAll(".mpio")
.data(topojson.feature(co, co.objects.mpios).features)
.enter().append("path")
.on('mouseover', mouseoverMun)
.on('mouseout',mouseoutMun)
.attr("class", function(d) { return "mpio " + "_" + d.id; })
.attr("d", path)
svg.append("path")
.datum(topojson.mesh(co, co.objects.mpios, function(a, b) { return a !== b; }))
.attr("d", path)
.attr("class", "mpio-borde")
})
function mouseoverMun(d){
d3.select(this).style("fill","orange");
div.style("opacity", .9)
.html(d.properties.name)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
document.getElementById("department").innerHTML=d.properties.dpt
}
function mouseoutMun() {
d3.select(this).style("fill","#777");
div.style("opacity",0);
document.getElementById("department").innerHTML='';
}
function mouseoverDep(d){
d3.select(this).style("fill","blue");
div.style("opacity", .9)
.html(d.properties.dpt)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
document.getElementById("department").innerHTML=d.properties.dpt
}
function mouseoutDep(d){
d3.select(this).style("fill","#777");
div.style("opacity",0);
document.getElementById("department").innerHTML='';
}
你的问题还不清楚。当你悬停部门时你想要什么?另外,我不了解公寓的作用。 –
@ ShaishavJogani请再读一遍。 – yavg
那么,当鼠标悬停在那里时,你想要将部门颜色线从红色变成蓝色? –