简体   繁体   English

在d3.js中绘制公寓和城镇的不同颜色

[英]Paint the apartment and the town of different colors in d3.js

I have a map, departments and municipalities. 我有地图,部门和市政当局。 The departments are delimited by the red lines. 各部门之间用红线分隔。 I want when I mouse over the municipality, in addition to painting this municipality, also painted the department of another color. 我想当我将鼠标悬停在市政上时,除了要绘制该市政外,还要绘制另一种颜色的部门。

  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='';
  }

http://jsfiddle.net/ctqhd851/ http://jsfiddle.net/ctqhd851/

You have set up two mouseover events for two different and entirely overlapping layers, but the mouseover (and mouseoff) events will only trigger for the topmost layer. 您已经为两个不同且完全重叠的层设置了两个mouseover事件,但是mouseover(和mouseoff)事件只会在最顶层触发。 So, you can instead achieve this effect with the mouseover only on the municipality layer. 因此,您可以仅在市政层上通过鼠标悬停来实现此效果。

As the municipality layer has a department property you want to: 由于市政层具有部门属性,因此您要:

  1. Color all municipalities that share a department a certain color 为共享部门的所有市政当局上色
  2. Color the specific municipality that the mouse is over a second color 为鼠标经过第二种颜色的特定市政部门上色

For number 1, you can achieve one easily if you give a class to each municipality that is its department's name (you could also apply a filter and update some of the features). 对于数字1,如果给每个市政当局上一堂以其部门名称命名的课程,则可以轻松实现(您也可以应用过滤器并更新某些功能)。 I changed where you set the class of your municipality features to this: 我将您在其中设置市政功能类别的位置更改为:

.attr("class", function(d) { return "mpio " + "_" + d.id + " " + d.properties.dpt})

Then I added some code in the mouseover event for municipalities: 然后,在市政当局的mouseover事件中添加了一些代码:

  d3.selectAll("."+d.properties.dpt)
     .style("fill","steelblue")

This will select every municipality in a given department, and set its color to blue. 这将选择给定部门中的每个市政当局,并将其颜色设置为蓝色。

For number 2: 对于数字2:

In relation to the individual municipality that the mouse is hovering on, nothing changes from what you have really, you just want to apply that style after you set the department's municipalities to show one color (so you don't overwrite the change when coloring each municipality in the department). 关于鼠标悬停的各个市政当局,您的实际状况没有任何变化,您只想在将部门的市政当局设置为显示一种颜色之后应用该样式(因此,在给每种颜色上色时您不会覆盖更改该部门的自治市)。

The mouseout event just needs to remove the styles we've added by giving a fill of none to all the municipalities. mouseout事件只需要删除所有市政当局都没有添加的样式,就可以删除我们添加的样式。 As we only modified municipalities, we can select every municipality and set the fill to none. 由于我们仅修改了市政当局,因此我们可以选择每个市政当局并将填充设置为无。

Here's a snippet showing mouseover effects that highlight department and municipality. 这是显示鼠标悬停效果的摘要,突出显示部门和市政当局。

Note that I took out the meshes that you were using, and instead applied the formatting of the edges to the polygons themselves 请注意,我取出了您正在使用的网格,而是将边缘的格式应用于多边形本身

 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") .attr("class", function(d) { return "depts " + "_" + d.id; }) .attr("d", path) //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 + " " + d.properties.dpt}) .attr("d", path) }) function mouseoverMun(d){ // Turn the department blue d3.selectAll("."+d.properties.dpt) .style("fill","steelblue") // Turn the municipality orange d3.select(this).style("fill","orange"); // Show a tooltip div.style("opacity", .9) .html(d.properties.name) .style("left", (d3.event.pageX) + "px") .style("top", (d3.event.pageY - 28) + "px"); } function mouseoutMun() { d3.selectAll(".mpio").style("fill","none"); div.style("opacity",0); } 
  path { fill: #777; } .mpio { fill: none; stroke: #fff; stroke-opacity: .25; stroke-width: .5px; pointer-events: all; } .depts { fill: none; stroke: #ff0000; stroke-linejoin: round; stroke-width: 1; opacity: 1; } div.tooltip { position: absolute; text-align: center; width: 100px; height: 28px; padding: 2px; font: 12px sans-serif; background: white; border-radius: 8px; pointer-events: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <script src="https://d3js.org/topojson.v1.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM