【问题标题】:Text in the circle [duplicate]圆圈中的文字[重复]
【发布时间】:2019-12-15 05:36:45
【问题描述】:

我正在为每个圆圈编写一个文本元素(x 轴测量值),但即使在浏览器的检查中显示文本元素后,它也没有显示

我已经在给定相同 x 和 y 的圆圈下添加了文本,但它通过了

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

<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>

<script>

// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 40, left: 100},
    width = 460 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
  .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 + ")");

// Parse the Data
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/7_OneCatOneNum_header.csv", function(data) {

// sort data
data.sort(function(b, a) {
  return a.Value - b.Value;
});

// Add X axis
var x = d3.scaleLinear()
  .domain([0, 13000])
  .range([ 0, width]);
svg.append("g")
  .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(x))
  .selectAll("text")
    .attr("transform", "translate(-10,0)rotate(-45)")
    .style("text-anchor", "end");

// Y axis
var y = d3.scaleBand()
  .range([ 0, height ])
  .domain(data.map(function(d) { return d.Country; }))
  .padding(1);
svg.append("g")
  .call(d3.axisLeft(y))

// Lines
svg.selectAll("myline")
  .data(data)
  .enter()
  .append("line")
    .attr("x1", x(0))
    .attr("x2", x(0))
    .attr("y1", function(d) { return y(d.Country); })
    .attr("y2", function(d) { return y(d.Country); })
    .attr("stroke", "grey")

// Circles -> start at X=0
svg.selectAll("mycircle")
  .data(data)
  .enter()
  .append("circle")
    .attr("cx", x(0) )
    .attr("cy", function(d) { return y(d.Country); })
    .attr("r", "7")
    .style("fill", "#69b3a2")
    .attr("stroke", "black")

// Change the X coordinates of line and circle
svg.selectAll("circle")
  .transition()
  .duration(2000)
  .attr("cx", function(d) { return x(d.Value); })

svg.selectAll("line")
  .transition()
  .duration(2000)
  .attr("x1", function(d) { return x(d.Value); })

// this is the line i have added at my end and it showing as well while i do the inspect element.
svg.selectAll("circle")
                .append(Text)
                .attr("x", function (d) { return x(d.Value); })
                .attr("y", function (d) { return y(d.Country); })
                .text(function (d) { return d.Value})
                .attr("font-family", "sans-serif")
                .attr("font-size", "6px")
                .attr("fill", "black")
                .style("text-anchor", "middle")

})

</script>

希望在圆圈下显示测量值,这样用户就不必猜测 x 轴了。圆圈在 13000,所以它应该显示为圆圈中的 13 除以 1000

【问题讨论】:

    标签: d3.js svg graph


    【解决方案1】:

    据我所知,有几件事正在发生。

    首先,而不是:

    ...
    .append(Text)
    

    它试图将一个名为Text 的变量传递给append 函数,它应该是:

    ...
    .append('text')
    

    即附加一个 svg 文本元素。

    但是,这仍然是将文本元素附加到圆形元素。如果您通过 Chrome Devtools 查看元素,您可以看到每个圆形元素内部都会有一个文本元素,实际上并没有显示任何内容。

    相反,标签文本需要使用类似的东西与圆圈分开呈现。

    svg.selectAll("mytext")
      .data(data)
      .enter()
      .append('text')
        .attr("x", function (d) { return x(d.Value) + 10; })
        .attr("y", function (d) { return y(d.Country) + 4; })
        .text(function (d) { return d.Value})
        .attr("font-family", "sans-serif")
        .attr("font-size", "10px")
        .attr("fill", "black")
        .style("text-anchor", "start")
        .style('opacity', 0)
        .transition()
        .delay(1500)
        .duration(500)
        .style('opacity', 1);
    

    我将字体放大了一点,调整了 x 和 y 值并使用了text-anchor: start,这样现在文本就出现在圆圈的右侧。我还添加了基于不透明度的过渡效果,并带有延迟,以便文本仅出现在圆圈动画的末尾。

    【讨论】:

    • 非常感谢,太好了,点赞!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 2020-04-22
    • 1970-01-01
    相关资源
    最近更新 更多