简体   繁体   English

d3包布局的地理排序功能

[英]geographic sort function for d3's pack layout

Using d3's pack layout, I made some bubbles associated with states. 使用d3的包布局,我制作了一些与状态相关的气泡。 Current test script: https://jsfiddle.net/80wjyxp4/4/ . 当前的测试脚本: https : //jsfiddle.net/80wjyxp4/4/ They're colored according to region. 它们是根据区域着色的。 You'll note Texas is in the "NW", California in the "SE", etc. 您会注意到,德克萨斯州在“ NW”中,加利福尼亚州在“ SE”中,依此类推。

Question: 题:

How would you geographically sort the circles in pack-layout? 您如何在地理上对以组装布局的圆圈进行排序?

One hack way might use the default d3.layout.pack.sort(null) . 一种破解方法可能使用默认的d3.layout.pack.sort(null) This sort starts with the first data point (in this case, AK) and then adds bubbles in a counterclockwise direction. 这种排序从第一个数据点(在这种情况下为AK)开始,然后沿逆时针方向添加气泡。 I could hand-sort data to be input in an order that approximates state positions, and add in blank circles to move edge circles around. 我可以按近似状态位置的顺序对要输入的数据进行手动排序,并添加空白圆圈以移动边缘圆圈。

I'm interested about better ideas. 我对更好的想法感兴趣。 Would it be better to use a modified force layout, like http://bl.ocks.org/mbostock/1073373 ? 使用修改后的部队布局(例如http://bl.ocks.org/mbostock/1073373)会更好吗? The d3.geom.voronoi() seems useful. d3.geom.voronoi()似乎很有用。

在此处输入图片说明

On this http://bl.ocks.org/mbostock/1073373 look at these lines: 在此http://bl.ocks.org/mbostock/1073373上,请看以下行:

  states.features.forEach(function(d, i) {
    if (d.id === 2 || d.id === 15 || d.id === 72) return; // lower 48
    var centroid = path.centroid(d);  //   <===== polygon center
    if (centroid.some(isNaN)) return;
    centroid.x = centroid[0];              <==== polygon lat
    centroid.y = centroid[1];              <==== polygon lng
    centroid.feature = d;                  
    nodes.push(centroid);              <== made node array of centroids
  });
----------------
force
      .gravity(0)
      .nodes(nodes)           <==== asign array nodes to nodes
      .links(links)
      .linkDistance(function(d) { return d.distance; })
      .start();

Each state acts like a multi-foci layout. 每个状态的行为都像一个多焦点布局。 Like this one: http://bl.ocks.org/mbostock/1021841 像这样的一个: http : //bl.ocks.org/mbostock/1021841

  1. Started with pack layout, to get circle radii 从包装布局开始,以获得圆半径
  2. used state centroids from this modified force layout http://bl.ocks.org/mbostock/1073373 此修改后的部队布局中使用的状态质心http://bl.ocks.org/mbostock/1073373
  3. Contrived to avoid overlaps using collisions https://bl.ocks.org/mbostock/7881887 试图避免使用碰撞的重叠https://bl.ocks.org/mbostock/7881887

The code is here: https://jsfiddle.net/xyn85de1/ . 代码在这里: https : //jsfiddle.net/xyn85de1/ It does NOT run because I can't get data for the topojson file, at http://bl.ocks.org/mbostock/raw/4090846/us.json , from the link, and the file is too large to copy-paste. 它无法运行,因为我无法通过链接从http://bl.ocks.org/mbostock/raw/4090846/us.json获取有关topojson文件的数据,并且该文件太大,无法复制-糊。 Download to your own server then run. 下载到您自己的服务器,然后运行。

It has a hover-title text and transitions in, but ends up looking like this: 它具有悬浮标题文本和过渡,但最终看起来像这样:

泡泡

Spacing and stuff is modifiable with different parameters. 间距和填充物可以使用不同的参数进行修改。 The circles are approximately sorted geographically. 这些圆在地理上大致排序。 MO is that big gold one in the middle; MO是那中间的大金币。 AK and HI are dark blue to the left; AK和HI在左侧为深蓝色; CA is lower left in pink; CA左下方为粉红色; TX is at the bottom in light blue. TX位于浅蓝色的底部。 And so on. 等等。 Code: After collecting all data (location data for init position of circles), along with the state name, value, area (for color coding) into the nodes variable: 代码:将所有数据(圆的初始位置的位置数据)以及状态名称,值,面积(用于颜色编码)收集到nodes变量后:

// circles
var circles = svg.selectAll("g") //g
    .data(nodes)
    .enter()
    .append("g")
    .attr("transform", function(d) {
        return "translate(" + -d.x + "," + -d.y + ")";
    })
    .append("circle")
    .attr("transform", function(d) {
        return "translate(" + d.x + "," + d.y + ")";
    })
    .attr("r", function(d) {return d.r;})
    .attr("fill", function(d) { return color(d.area); })
    .call(force.drag); // lets you change the orientation

// title text
circles.append("title")
    .text(function(d) { return d.state + ": " + format(d.value) + " GWh"; });

// // text // doesn't work :/ porque?
// circles.append("text")
//     .attr("dy", ".3em")
//     //.style("text-anchor", "middle")
//     .text(function(d) { return d.state.substring(0, d.r / 3); });

// circle tick function
function tick(e) {
    circles
        .each(collide(collision_alpha))
        .attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
}

// force
force.nodes(nodes)
    .on("tick", tick)
    .start();

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

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