使用d3.js将图像插入到树状图的叶子中 [英] Insert images into dendogram's leaves using d3.js

查看:93
本文介绍了使用d3.js将图像插入到树状图的叶子中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用集群树状图. 我想根据我的聚类结果将图像插入到树状图的叶子中. 我该怎么办?

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

.node circle {
  fill: #fff;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.node {
  font: 10px sans-serif;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1.5px;
}

</style>
<body>
 <svg id="mySvg" width="80" height="80">
    <defs id="mdef">
      <pattern id="image" x="-100" y="-100" height="200" width="200" patternUnits="userSpaceOnUse" >
        <image x="50" y="5" width="10" height="10" xlink:href="http://www.e-pint.com/epint.jpg"></image>
      </pattern>

    </defs>
  </svg>

<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var width = 960,
    height = 2200;

var cluster = d3.layout.cluster()
    .size([height, width - 160]);

var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.y, d.x]; });

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(40,0)");



d3.json("https://rawgit.com/bansaghi/d3.chart.layout.hierarchy/master/example/data/flare.json", function(error, root) {
  var nodes = cluster.nodes(root),
      links = cluster.links(nodes);

  var link = svg.selectAll(".link")
      .data(links)
    .enter().append("path")
      .attr("class", "link")
      .attr("d", diagonal);

  var node = svg.selectAll(".node")
      .data(nodes)
    .enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })

  //var circle = svg.append("circle")
   //   .style("fill", "red")
   //   .style("fill", "url(#image)"); 

  node.append("circle")
      .attr("r", 4.5);
      //.style("fill", "black");e

  node.append("image")
      .attr("xlink:href", "http://www.e-pint.com/epint.jpg")


  node.append("text")
      .attr("dx", function(d) { return d.children ? -8 : 8; })
      .attr("dy", 3)
      .style("text-anchor", function(d) { return d.children ? "end" : "start"; })
      .text(function(d) { return d.name; });
});

d3.select(self.frameElement).style("height", height + "px");
console.log(d3);
</script>

在这段代码中,我添加了样式部分和node.append("image") .attr("xlink:href", "http://www.e-pint.com/epint.jpg").

问题是我看不到圆圈中充满了图像.有什么问题吗?

解决方案

选中此 JSFiddle

以下是向叶子添加图像的代码:

var leafnodes = svg.selectAll('g.leaf.node')
   .append("image")
   .attr("xlink:href", "http://www.e-pint.com/epint.jpg")
   .attr("width", 10)
   .attr("height", 10);

在此之前,叶节点被标记为leaf类,如下所示:

.attr("class", function(n) {
      return n.children ? "inner node" : "leaf node";
})

更新:

要将不同的图像添加到节点,您可以执行以下操作:

var imgs = ['http://***.jpg',
            'http://***.jpg',
            'http://***.jpg'];

var leafnodes = svg.selectAll('g.leaf.node')
    .append("image")
    .attr("xlink:href", function (d) { 
        // get random image from array
        return imgs[Math.floor(Math.random() * imgs.length)]; 
    })
    .attr("width", 10)
    .attr("height", 10);

在上面的JSFiddle中查看完整的示例.

I am using Cluster Dendogram . And I want to insert images in to the leaves of the dendogram according to my clustering results. How can I do that?

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

.node circle {
  fill: #fff;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.node {
  font: 10px sans-serif;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1.5px;
}

</style>
<body>
 <svg id="mySvg" width="80" height="80">
    <defs id="mdef">
      <pattern id="image" x="-100" y="-100" height="200" width="200" patternUnits="userSpaceOnUse" >
        <image x="50" y="5" width="10" height="10" xlink:href="http://www.e-pint.com/epint.jpg"></image>
      </pattern>

    </defs>
  </svg>

<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var width = 960,
    height = 2200;

var cluster = d3.layout.cluster()
    .size([height, width - 160]);

var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.y, d.x]; });

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(40,0)");



d3.json("https://rawgit.com/bansaghi/d3.chart.layout.hierarchy/master/example/data/flare.json", function(error, root) {
  var nodes = cluster.nodes(root),
      links = cluster.links(nodes);

  var link = svg.selectAll(".link")
      .data(links)
    .enter().append("path")
      .attr("class", "link")
      .attr("d", diagonal);

  var node = svg.selectAll(".node")
      .data(nodes)
    .enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })

  //var circle = svg.append("circle")
   //   .style("fill", "red")
   //   .style("fill", "url(#image)"); 

  node.append("circle")
      .attr("r", 4.5);
      //.style("fill", "black");e

  node.append("image")
      .attr("xlink:href", "http://www.e-pint.com/epint.jpg")


  node.append("text")
      .attr("dx", function(d) { return d.children ? -8 : 8; })
      .attr("dy", 3)
      .style("text-anchor", function(d) { return d.children ? "end" : "start"; })
      .text(function(d) { return d.name; });
});

d3.select(self.frameElement).style("height", height + "px");
console.log(d3);
</script>

In this code I added style part and node.append("image") .attr("xlink:href", "http://www.e-pint.com/epint.jpg").

The problem is I can not see the circles filled with image. What can be wrong?

解决方案

Check this JSFiddle

Here's the code that adds an image to leaves:

var leafnodes = svg.selectAll('g.leaf.node')
   .append("image")
   .attr("xlink:href", "http://www.e-pint.com/epint.jpg")
   .attr("width", 10)
   .attr("height", 10);

And prior to that leaf nodes were marked with the leaf class like so:

.attr("class", function(n) {
      return n.children ? "inner node" : "leaf node";
})

UPDATE:

To add different images to nodes you can do something like that:

var imgs = ['http://***.jpg',
            'http://***.jpg',
            'http://***.jpg'];

var leafnodes = svg.selectAll('g.leaf.node')
    .append("image")
    .attr("xlink:href", function (d) { 
        // get random image from array
        return imgs[Math.floor(Math.random() * imgs.length)]; 
    })
    .attr("width", 10)
    .attr("height", 10);

Check the JSFiddle above for the full example.

这篇关于使用d3.js将图像插入到树状图的叶子中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆