【问题标题】:Text not appearing in circle D3文字未出现在圆圈 D3 中
【发布时间】:2021-09-24 15:25:51
【问题描述】:

我正在尝试将由弧连接的圆圈内的文本居中。我看过其他 StackOverflow 答案,但没有一个能解决我的问题。我知道将每个圆形元素包装在一个 g 元素中,然后附加我在下面做的文本。如果有人可以帮助我,将不胜感激。

let svg = d3.select("body").append('svg').attr('viewBox', '0 0 ' + width + ' ' + boxHeight);
    let nodes: any = [[width / 2, boxHeight / 1.5], [width / 4, boxHeight / 3], [width / 1.5, boxHeight / 3]];
    let g = svg.append('g')
    for (let i = 0; i < nodes.length; i++) {
      g
        .append('circle')
        .attr('cx', nodes[i][0])
        .attr('cy', nodes[i][1])
        .attr('r', 40).style('stroke', 'green')
        .style('fill', 'none')
      g.append('text').attr('fill', 'black').text('Hello')
    }

    let links: any = [];
    // Link from the first node to the second
    links.push(
      d3.linkHorizontal()({
        source: nodes[0],
        target: nodes[1]
      })
    );

    // Link from the first node to the third
    links.push(
      d3.linkHorizontal()({
        source: nodes[0],
        target: nodes[2]
      })
    );

    // Append the links to the svg element
    for (let i = 0; i < links.length; i++) {
      svg
        .append('path')
        .attr('d', links[i])
        .attr('stroke', 'black')
        .attr('fill', 'none');
    }

【问题讨论】:

    标签: javascript typescript d3.js


    【解决方案1】:

    你定位圆圈,

    g.append('circle')
      .attr('cx', nodes[i][0])
      .attr('cy', nodes[i][1])
    

    但不是文字,

    g.append('text').attr('fill', 'black').text('Hello')
    

    所以它可能出现在屏幕的左上角(文本基线为 0,因此您可能会看到部分字母低于基线。

    相反,您可以定位 g。然后附加子文本,圆圈:

     let node = g.selectAll(null)
      .data(nodes)
      .enter()
      .append("g")
      .attr("transform", d=>"translate("+d+")");
    
     node.append("circle") 
        .attr('r', 40).style('stroke', 'green')
        .style('fill', 'none')
    
     node.append("text") 
        .text("Hello")
        .style("text-anchor","middle")
        .style("dominant-baseline","middle");
    

    例如:

    let g = d3.select("body").append("svg").append("g");
     
    let nodes = [[100,100],[200,100]]
     
     
    let node = g.selectAll(null)
          .data(nodes)
          .enter()
          .append("g")
          .attr("transform", d=>"translate("+d+")");
    
    node.append("circle") 
            .attr('r', 40).style('stroke', 'green')
            .style('fill', 'none')
    
    node.append("text") 
            .text("Hello")
            .style("text-anchor","middle")
            .style("dominant-baseline","middle");
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"&gt;&lt;/script&gt;

    否则,您可以使用与圆的 cx/cy 值相同的 x,y 值定位文本,并使用 text-anchor 和占主导地位的基线属性将文本置于该值的中心:

    let g = d3.select("body").append("svg").append("g");
     
    let nodes = [[100,100],[200,100]]
     
     
    let node = g.selectAll(null)
          .data(nodes)
          .enter()
          .append("g")
    
    node.append("circle") 
            .attr('r', 40).style('stroke', 'green')
            .style('fill', 'none')
            .attr("cx", d=>d[0])
            .attr("cy", d=>d[1]);
    
    node.append("text") 
            .text("Hello")
            .style("text-anchor","middle")
            .style("dominant-baseline","middle")
            .attr("x", d=>d[0])
            .attr("y", d=>d[1]);
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"&gt;&lt;/script&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多