页面仅显示2个图表中的1个

在此codepen中,我尝试使用D3.js创建2个树图,但仅显示第二个树图。当我在控制台中检查元素时,我可以看到创建了2个不同的svg并将它们写入DOM,但是只有第二个“ g”标记填充了SVG rect元素。有2个具有不同ID的不同div标签。如何并排绘制2个D3.sj树图?谢谢。

D3.js代码

var margin = {top: 10,right: 10,bottom: 10,left: 10},width = 445 - margin.left - margin.right,height = 445 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz1")
.append("svg")
  .attr("width",width + margin.left + margin.right)
  .attr("height",height + margin.top + margin.bottom)
.append("g")
  .attr("transform","translate(" + margin.left + "," + margin.top + ")");

// read json data
d3.json("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/data_dendrogram_full.json",function(data) {

  // Give the data to this cluster layout:
  var root = d3.hierarchy(data).sum(function(d){ return d.value}) // Here the size of each leave is given in the 'value' field in input data

  // Then d3.treemap computes the position of each element of the hierarchy
  d3.treemap()
    .size([width,height])
    .paddingTop(28)
    .paddingRight(7)
    .paddingInner(3)      // Padding between each rectangle
    //.paddingOuter(6)
    //.padding(20)
    (root)

  // prepare a color scale
  var color = d3.scaleOrdinal()
    .domain(["source1","source2","source3"])
    .range([ "#402D54","#D18975","#8FD175"])

  // And a opacity scale
  var opacity = d3.scaleLinear()
    .domain([10,30])
    .range([.5,1])

  // use this information to add rectangles:
  svg
    .selectAll("rect")
    .data(root.leaves())
    .enter()
    .append("rect")
      .attr('x',function (d) { return d.x0; })
      .attr('y',function (d) { return d.y0; })
      .attr('width',function (d) { return d.x1 - d.x0; })
      .attr('height',function (d) { return d.y1 - d.y0; })
      .style("stroke","black")
      .style("fill",function(d){ return color(d.parent.data.name)} )
      .style("opacity",function(d){ return opacity(d.data.value)})

  // and to add the text labels
  svg
    .selectAll("text")
    .data(root.leaves())
    .enter()
    .append("text")
      .attr("x",function(d){ return d.x0+5})    // +10 to adjust position (more right)
      .attr("y",function(d){ return d.y0+20})    // +20 to adjust position (lower)
      .text(function(d){ return d.data.name.replace('mister_','') })
      .attr("font-size","19px")
      .attr("fill","white")

  // and to add the text labels
  svg
    .selectAll("vals")
    .data(root.leaves())
    .enter()
    .append("text")
      .attr("x",function(d){ return d.y0+35})    // +20 to adjust position (lower)
      .text(function(d){ return d.data.value })
      .attr("font-size","11px")
      .attr("fill","white")

  // Add title for the 3 groups
  svg
    .selectAll("titles")
    .data(root.descendants().filter(function(d){return d.depth==1}))
    .enter()
    .append("text")
      .attr("x",function(d){ return d.x0})
      .attr("y",function(d){ return d.y0+21})
      .text(function(d){ return d.data.name })
      .attr("font-size",function(d){ return color(d.data.name)} )

  // Add title for the 3 groups
  svg
    .append("text")
      .attr("x",0)
      .attr("y",14)    // +20 to adjust position (lower)
      .text("Three group sources")
      .attr("font-size","grey" )

})

var margin = {top: 10,height = 445 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz2")
.append("svg")
  .attr("width","grey" )

})
.dataviz {
  display: inline-block;
}
<script src="https://d3js.org/d3.v4.js"></script>
<div>
<div class="dataviz" id="my_dataviz1"></div>
<div class="dataviz" id="my_dataviz2"></div>
</div>

zhaoyu410186459 回答:页面仅显示2个图表中的1个

两个SVG选项名称相同:

var svg = d3.select("#my_dataviz1")//etc...
var svg = d3.select("#my_dataviz2")//etc..

解决方案非常简单,为他们提供不同的名称:

var svg = d3.select("#my_dataviz1")//etc...
var svg2 = d3.select("#my_dataviz2")//etc...

最后,考虑到两个绘图函数都执行相同的操作,请考虑创建一个函数,将SVG选择和数据传递给该函数。这样,您就可以大大减少代码的大小。

这是您所做的更改的代码:

var margin = {top: 10,right: 10,bottom: 10,left: 10},width = 445 - margin.left - margin.right,height = 445 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz1")
.append("svg")
  .attr("width",width + margin.left + margin.right)
  .attr("height",height + margin.top + margin.bottom)
.append("g")
  .attr("transform","translate(" + margin.left + "," + margin.top + ")");

// read json data
d3.json("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/data_dendrogram_full.json",function(data) {

  // Give the data to this cluster layout:
  var root = d3.hierarchy(data).sum(function(d){ return d.value}) // Here the size of each leave is given in the 'value' field in input data

  // Then d3.treemap computes the position of each element of the hierarchy
  d3.treemap()
    .size([width,height])
    .paddingTop(28)
    .paddingRight(7)
    .paddingInner(3)      // Padding between each rectangle
    //.paddingOuter(6)
    //.padding(20)
    (root)

  // prepare a color scale
  var color = d3.scaleOrdinal()
    .domain(["source1","source2","source3"])
    .range([ "#402D54","#D18975","#8FD175"])

  // And a opacity scale
  var opacity = d3.scaleLinear()
    .domain([10,30])
    .range([.5,1])

  // use this information to add rectangles:
  svg
    .selectAll("rect")
    .data(root.leaves())
    .enter()
    .append("rect")
      .attr('x',function (d) { return d.x0; })
      .attr('y',function (d) { return d.y0; })
      .attr('width',function (d) { return d.x1 - d.x0; })
      .attr('height',function (d) { return d.y1 - d.y0; })
      .style("stroke","black")
      .style("fill",function(d){ return color(d.parent.data.name)} )
      .style("opacity",function(d){ return opacity(d.data.value)})

  // and to add the text labels
  svg
    .selectAll("text")
    .data(root.leaves())
    .enter()
    .append("text")
      .attr("x",function(d){ return d.x0+5})    // +10 to adjust position (more right)
      .attr("y",function(d){ return d.y0+20})    // +20 to adjust position (lower)
      .text(function(d){ return d.data.name.replace('mister_','') })
      .attr("font-size","19px")
      .attr("fill","white")

  // and to add the text labels
  svg
    .selectAll("vals")
    .data(root.leaves())
    .enter()
    .append("text")
      .attr("x",function(d){ return d.y0+35})    // +20 to adjust position (lower)
      .text(function(d){ return d.data.value })
      .attr("font-size","11px")
      .attr("fill","white")

  // Add title for the 3 groups
  svg
    .selectAll("titles")
    .data(root.descendants().filter(function(d){return d.depth==1}))
    .enter()
    .append("text")
      .attr("x",function(d){ return d.x0})
      .attr("y",function(d){ return d.y0+21})
      .text(function(d){ return d.data.name })
      .attr("font-size",function(d){ return color(d.data.name)} )

  // Add title for the 3 groups
  svg
    .append("text")
      .attr("x",0)
      .attr("y",14)    // +20 to adjust position (lower)
      .text("Three group sources")
      .attr("font-size","grey" )

})

var margin = {top: 10,height = 445 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg2 = d3.select("#my_dataviz2")
.append("svg")
  .attr("width",1])

  // use this information to add rectangles:
  svg2
    .selectAll("rect")
    .data(root.leaves())
    .enter()
    .append("rect")
      .attr('x',function(d){ return opacity(d.data.value)})

  // and to add the text labels
  svg2
    .selectAll("text")
    .data(root.leaves())
    .enter()
    .append("text")
      .attr("x","white")

  // and to add the text labels
  svg2
    .selectAll("vals")
    .data(root.leaves())
    .enter()
    .append("text")
      .attr("x","white")

  // Add title for the 3 groups
  svg2
    .selectAll("titles")
    .data(root.descendants().filter(function(d){return d.depth==1}))
    .enter()
    .append("text")
      .attr("x",function(d){ return color(d.data.name)} )

  // Add title for the 3 groups
  svg2
    .append("text")
      .attr("x","grey" )

})
.dataviz {
  display: inline-block;
}
<script src="https://d3js.org/d3.v4.js"></script>
<div>
<div class="dataviz" id="my_dataviz1"></div>
<div class="dataviz" id="my_dataviz2"></div>
</div>

本文链接:https://www.f2er.com/3154747.html

大家都在问