[英]d3 hexbin scaling issues
I'm trying to learn how to use the d3.js hexbin plugin. 我正在尝试学习如何使用d3.js hexbin插件。
I started with the example: http://bl.ocks.org/mbostock/4248145 , and I'm adapting it. 我从示例开始: http : //bl.ocks.org/mbostock/4248145 ,我正在对其进行调整。
I have a data set of points between [0,0] and [600,600]. 我的数据集介于[0,0]和[600,600]之间。 I want to output them to a 300,300 graph. 我想将它们输出到300,300图形。
My graph doesn't look right. 我的图看起来不正确。 It looks like the data isn't being scaled properly and the graph is only showing 1/4 of the data. 看起来数据没有正确缩放,并且图形仅显示了数据的1/4。 Can someone tell me what's wrong? 有人可以告诉我怎么了吗? I've read a book about using d3, but I don't have very much experience using it. 我已经读过一本有关使用d3的书,但是我没有太多使用它的经验。
var graph_width = 300;
var graph_height = 300;
var data_width = 600;
var data_height = 600;
var randomX = d3.random.normal(data_width / 2, 80),
randomY = d3.random.normal(data_height / 2, 80),
points = d3.range(2000).map(function() { return [randomX(), randomY()]; });
var color = d3.scale.linear()
.domain([0, 20])
.range(["white", "steelblue"])
.interpolate(d3.interpolateLab);
var hexbin = d3.hexbin()
.size([graph_width, graph_height])
.radius(20);
var x = d3.scale.linear()
.domain([0, data_width])
.range([0, graph_width]);
var y = d3.scale.linear()
.domain([0, data_height])
.range([0, graph_height]);
var svg = d3.select("body").append("svg")
.attr("width", graph_width)
.attr("height", graph_height)
.append("g");
svg.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("class", "mesh")
.attr("width", graph_width)
.attr("height", graph_height);
svg.append("g")
.attr("clip-path", "url(#clip)")
.selectAll(".hexagon")
.data(hexbin(points))
.enter().append("path")
.attr("class", "hexagon")
.attr("d", hexbin.hexagon())
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.style("fill", function(d) { return color(d.length); });
I think I understand. 我想我明白。 You have data values in in the range of 0 to 600 but want those mapped to x/y positions in the range of 0 to 300. 您的数据值介于0到600之间,但希望将它们映射到0/300范围内的x / y位置。
If that's it then scale the points: 如果是这样,那么就扩大分数:
var x = d3.scale.linear()
.domain([0, data_width])
.range([0, graph_width]);
var y = d3.scale.linear()
.domain([0, data_height])
.range([0, graph_height]);
var randomX = d3.random.normal(data_width / 2, 80),
randomY = d3.random.normal(data_height / 2, 80),
points = d3.range(2000).map(function() { return [x(randomX()), y(randomY())]; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.