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

标签 javascript css ruby-on-rails d3.js

我的目标是使用 d3 将图像添加到现有的圈子中。圆圈将呈现并与鼠标悬停方法交互,但仅当我使用 "fill""color" 时,而不是像 .append( “图片”)

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);
     });

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

最佳答案

正如 Lars 所说,您需要使用模式,一旦您这样做,它就会变得非常简单。这是 conversation 的链接在 d3 谷歌群组中关于这个。我设置了一个 fiddle此处使用该对话中的一品脱图像和上面的代码。

设置模式:

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

然后我们只改变填充的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");
         });

关于javascript - 在 d3 javascript 中的圆形对象中添加图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19202450/

相关文章:

css - 试图对齐 simple_form 框和按钮

ruby-on-rails - Rails 4 和 Devise : Allow email blank on certain conditions

javascript - 使用 Reactjs 在 Canvas 上绘制图像后无法将 Canvas 转换为图像

html - 使用 CSS 的样式表之外的选择器

html - 如何使用 flexbox 垂直定位 2x div

jquery - 单击 Bootstrap Accordion 时显示不同的颜色

ruby-on-rails - Phusion 乘客诉 WEBrick

javascript - 如何将对象的 for 循环输出分配给变量

javascript - DOM/Vanilla Javascript 相当于 jquery 的 .is (':empty' )

javascript - JSTree - 添加 Action 重定向