Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何将图像插入d3 hexbin_Image_Svg_D3.js_Path - Fatal编程技术网

Image 如何将图像插入d3 hexbin

Image 如何将图像插入d3 hexbin,image,svg,d3.js,path,Image,Svg,D3.js,Path,我用D3的hexbin创建了一个六边形网格。我想用图像填充六边形。我看了一个圆圈,里面有一个图像。我试着做完全相同的事情,用图像填充每个箱子,但没有成功 这就是我所尝试的: <!DOCTYPE html> <meta charset="utf-8"> <style> .hexagon { fill: none; stroke: #000; stroke-width: 2px; } </style> <body> &

我用D3的hexbin创建了一个六边形网格。我想用图像填充六边形。我看了一个圆圈,里面有一个图像。我试着做完全相同的事情,用图像填充每个箱子,但没有成功

这就是我所尝试的:

<!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>

.六边形{
填充:无;
行程:#000;
笔画宽度:2px;
}
var margin={顶部:20,右侧:20,底部:30,左侧:40},
宽度=900-边距。左侧-边距。右侧,
高度=500-margin.top-margin.bottom;
var color=d3.scale.linear()
.domain([0,20])
.范围([“橙色”、“橙色”])
.插值(d3.插值B);
var hexbin=d3.hexbin()
.尺寸([宽度、高度])
.半径(100);
var points=hexbin.centers();
var x=d3.scale.identity()
.域([0,宽度]);
变量y=d3.scale.linear()
.domain([0,高度])
.范围([高度,0]);
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
svg.append(“clipPath”)
.attr(“id”、“剪辑”)
.append(“rect”)
.attr(“类”、“网格”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
svg.append(“g”)
.attr(“剪辑路径”、“url(#剪辑)”)
.selectAll(“.hexagon”)
.数据(hexbin(点))
.enter().append(“路径”)
.attr(“类”、“六边形”)
.attr(“d”,hexbin.hexagon())
.attr(“transform”,函数(d){return“translate”(“+d.x+”,“+d.y+”)”);})
.样式(“填充”、“url(#图像”);

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

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

<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”
属性添加到
模式中会导致图像以平铺方式显示。这解决了此问题。我还必须添加patternUnits=“userSpaceOnUse”。就像上面提到的迈克。