【问题标题】:How to insert images into d3 hexbin如何将图像插入 d3 hexbin
【发布时间】:2014-10-01 07:24:19
【问题描述】:

我使用 D3 的 hexbin 创建了一个六边形网格。我想用图像填充六边形。我看了一下这个question,其中一个圆圈充满了图像。我试图做同样的事情并用图像填充每个垃圾箱,但它没有用。

这是我尝试过的:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.hexagon {
  fill: none;
  stroke: #000;
  stroke-width: 2px;
}

</style>

<body>
  <svg id="mySvg" width="80" height="80">
    <defs id="mdef">
      <pattern id="image" x="0" y="0" height="40" width="40">
        <image x="0" y="0" width="40" height="40" xlink:href="http://www.e-pint.com/epint.jpg"></image>
      </pattern>
    </defs>
  </svg>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.hexbin.v0.min.js?5c6e4f0"></script>
<script>

var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 900 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var color = d3.scale.linear()
    .domain([0, 20])
    .range(["orange", "orange"])
    .interpolate(d3.interpolateLab);

var hexbin = d3.hexbin()
    .size([width, height])
    .radius(100);

var points = hexbin.centers();

var x = d3.scale.identity()
    .domain([0, width]);

var y = d3.scale.linear()
    .domain([0, height])
    .range([height, 0]);

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)

svg.append("clipPath")
    .attr("id", "clip")
  .append("rect")
    .attr("class", "mesh")
    .attr("width", width)
    .attr("height", 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", 'url(#image)');

</script>

那么如何用图片填充六边形呢?

【问题讨论】:

  • 还没有解决,但有趣的是,将patternUnits="userSpaceOnUse" 属性添加到您的pattern 会导致图像以平铺方式显示。

标签: image svg d3.js path


【解决方案1】:

看起来您的模式只是没有以会出现在路径中的方式定义。这应该将图像移动到图案的右侧以显示在您的十六进制网格中。

<pattern id="image" x="-100" y="-100" height="200" width="200">
    <image x="50" y="50" width="100" height="100" xlink:href="http://www.e-pint.com/epint.jpg"></image>
</pattern>

【讨论】:

  • 这解决了它。我还必须添加 patternUnits="userSpaceOnUse"。就像上面提到的迈克。
猜你喜欢
  • 2011-01-14
  • 2010-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 2018-12-21
相关资源
最近更新 更多