在d3 javascript中添加圆形对象内的图像?

DeB*_*aid 23 javascript css ruby-on-rails d3.js

我的目标是使用d3将图像添加到现有圆圈中.圆圈将呈现并与鼠标悬停方法交互,但仅当我使用"填充","颜色",而不是像.append("image")那样更复杂的东西.

      g.append("circle")
         .attr("class", "logo")
         .attr("cx", 700)
         .attr("cy", 300)
         .attr("r", 10)
         .attr("fill", "black")       // this code works OK
         .attr("stroke", "white")     // displays small black dot
         .attr("stroke-width", 0.25)
         .on("mouseover", function(){ // when I use .style("fill", "red") here, it works 
               d3.select(this)        
                   .append("svg:image")
                   .attr("xlink:href", "/assets/images/logo.jpeg")
                   .attr("cx", 700)
                   .attr("cy", 300)
                   .attr("height", 10)
                   .attr("width", 10);
         });
Run Code Online (Sandbox Code Playgroud)

鼠标悬停后图像不显示.使用Ruby on Rails应用程序,我的图像"logo.jpeg"存储在assets/images /目录中.有什么帮助让我的徽标显示在圈内?谢谢.

use*_*080 31

正如Lars所说,你需要使用模式,一旦你这样做,它变得非常简单.这是d3 google网上关于此内容的对话的链接.我在这里使用该对话中的品脱图像和上面的代码设置了一个小提琴.

要设置模式:

    <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>
Run Code Online (Sandbox Code Playgroud)

然后我们只改变填充的d3:

svg.append("circle")
         .attr("class", "logo")
         .attr("cx", 225)
         .attr("cy", 225)
         .attr("r", 20)
         .style("fill", "transparent")       
         .style("stroke", "black")     
         .style("stroke-width", 0.25)
         .on("mouseover", function(){ 
               d3.select(this)
                   .style("fill", "url(#image)");
         })
          .on("mouseout", function(){ 
               d3.select(this)
                   .style("fill", "transparent");
         });
Run Code Online (Sandbox Code Playgroud)