简体   繁体   English

D3 Hexbin网格对齐

[英]D3 Hexbin Mesh Alignment

I'm having problem getting the hexbin's mesh to overlap properly with the hexagons created to display data. 我在使hexbin的网格与创建以显示数据的六边形正确重叠方面遇到问题。 Here is the fiddle 这是小提琴

This was forked from Lars Karthoff, and it's just a vanilla graph with a single data point in the middle of the graph. 这是由Lars Karthoff派生的,它只是一个普通图形,在图形的中间有一个数据点。 In my fork, the only customization I made was to add the mesh. 在我的叉子中,我所做的唯一定制就是添加网格。

 svg.append("svg:path") .attr("clip-path", "url(#clip)") .attr("d", hexbin.mesh()) .style("stroke-width", .5) .style("stroke", "black") .style("fill", "none"); 

If the mesh and my custom hexagons are using the same hexbin function, why aren't they aligned? 如果网格和我的自定义六边形使用的是相同的hexbin函数,为什么它们不对齐?

The problem is that you're applying your scales to the result of the binning, but not the hex grid. 问题是您将比例尺应用到合并结果,而不是十六进制网格。 To align them properly, tell the hexbin layout to use the scales: 要正确对齐它们,请告诉hexbin布局使用比例尺:

var hexbin = d3.hexbin()
  .size([width, height])
  .x(function(d) { return x(d[0]); })
  .y(function(d) { return y(d[1]); })
  .radius(8);

Then you also don't need to apply the scales again when plotting the bins -- the coordinates can be taken directly. 然后,您在绘制垃圾箱时也无需再次应用比例尺-可以直接获取坐标。 Complete demo here . 在此处完成演示。

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

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