拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 D3Jsv3:路径未显示在节点元素上

D3Jsv3:路径未显示在节点元素上

白鹭 - 2022-03-24 1960 0 0

使用 Javascript 生成带有 SVG 的 HTML 代码。然后我想在其中显示一个甜甜圈图。我能够在静态 HTML 元素上绘制图表。但是,当我尝试在我的 JavaScript 生成的节点元素中显示它时,路径没有显示出来,但我可以看到文本。我在这里想念什么?

https://jsfiddle.net/fuL5doja/46/

function createNodes(){
var parent = document.getElementById('chart');
var child = document.createElement('div');
child.classList.add('childContainer');
parent.appendChild(child);
var svg = document.createElement('svg');
svg.id = 'donut';
child.appendChild(svg);
}



  function donutChart(){
    // set the dimensions and margins of the graph
    var width = 30
        height = 30
        margin = 0

    // The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.
    var radius = 30

    // append the svg object to the div called 'my_dataviz'
    var svg = d3.select('#donut')
        .attr("width", width)
        .attr("height", height)
      .append("g")
        .attr("transform", "translate("   width / 2   ","   height / 2   ")");

    // Create dummy data
    var dataDummy = {a: 70, b:30}
    // set the color scale
    var color = d3.scale.ordinal()
      .domain(dataDummy)
      .range(["#bebfc2", "#8FB91C"])

    // Compute the position of each group on the pie:
    var pie = d3.layout.pie()
      .value(function(d) {return d.value; })
    var data_ready = pie(d3.entries(dataDummy))

    // Build the pie chart: Basically, each part of the pie is a path that we build using the arc function.
    svg.selectAll('whatever')
      .data(data_ready)
      .enter()
      .append('path')
      .attr('d', d3.svg.arc()
        .innerRadius(5)         // This is the size of the donut hole
        .outerRadius(radius)
      )
      .attr('fill', function(d){ return(color(d.data.key)) })
      .style("opacity", 0.7)

      svg.append("text")
        .attr("x", -12) // space legend
        .attr("y", 2)
        .attr("class", "donutText")
        .text('30%');
  }
  function donutChart2(){
    // set the dimensions and margins of the graph
    var width = 30
        height = 30
        margin = 0

    // The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.
    var radius = 30

    // append the svg object to the div called 'my_dataviz'
    var svg = d3.select('#test')
        .attr("width", width)
        .attr("height", height)
      .append("g")
        .attr("transform", "translate("   width / 2   ","   height / 2   ")");

    // Create dummy data
    var dataDummy = {a: 70, b:30}
    // set the color scale
    var color = d3.scale.ordinal()
      .domain(dataDummy)
      .range(["#bebfc2", "#8FB91C"])

    // Compute the position of each group on the pie:
    var pie = d3.layout.pie()
      .value(function(d) {return d.value; })
    var data_ready = pie(d3.entries(dataDummy))

    // Build the pie chart: Basically, each part of the pie is a path that we build using the arc function.
    svg.selectAll('whatever')
      .data(data_ready)
      .enter()
      .append('path')
      .attr('d', d3.svg.arc()
        .innerRadius(5)         // This is the size of the donut hole
        .outerRadius(radius)
      )
      .attr('fill', function(d){ return(color(d.data.key)) })
      .style("opacity", 0.7)

      svg.append("text")
        .attr("x", -12) // space legend
        .attr("y", 2)
        .attr("class", "donutText")
        .text('30%');
  }



createNodes();
donutChart();
donutChart2();
.childContainer {
  width: 200px;
  height: 100px;
  border: 1px solid black;
}
#mySvg {
  
}
<div id="chart"></div>

<svg id="test"></svg>

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8 M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

uj5u.com热心网友回复:

您需要svg使用 svg 命名空间 uri创建元素,以便path在直接使用 JavaScript 创建时支持

  var svg = document.createElementNS('http://www.w3.org/2000/svg','svg');

而不仅仅是典型的

var svg = document.createElement('svg');

或者,您可以使用 D3 附加svg,这将确保它的命名空间正确!

  d3.select(child).append('svg').attr('id', 'donut');
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *