为什么我的文字标签不显示? [英] Why aren't my text labels appearing?

查看:140
本文介绍了为什么我的文字标签不显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚为什么我的文字标签不显示。我没有在控制台中看到任何错误,只是...没有标签。

I'm trying to figure out why my text labels aren't showing up. I don't see any sort of error in the console, just ... no labels.

  svg.selectAll("text")
           .data(data)
           .enter().append("text")
           .text(function(d) {
             return (d.money);
             })
           .attr("text-anchor", "middle")
           .attr("x", function(d) { return x(d.year); })
           .attr("y", function(d) { return y(d.money); })
           .attr("class", "axis");

完整示例在 https://jsfiddle.net/s3jo8gkL/5/

推荐答案

右现在,您正在选择所有文本元素,并且您的SVG中已经有文本。只需更改你的第一行,选择任何给定的类(或任何给定的ID,任何假元素或甚至任何SVG元素,在那一点不存在):

Right now, you are selecting all the text elements, and you already have texts in your SVG. Just change your first line, selecting any given class (or any given ID, any fake element or even any SVG element that doesn't exist at that point):

svg.selectAll(".thistext")
       .data(data)
       .enter().append("text")
       .text(function(d) {
         return (d.money);
         })
       .attr("text-anchor", "middle")
       .attr("x", function(d) { return x(d.year); })
       .attr("y", function(d) { return y(d.money); })
       .attr("class", "axis");

要理解为什么,看看这个例子在S.O.文档:占位符在enter中的作用选择

To understand why, have a look at this example in the S.O. documentation: The role of placeholders in "enter" selections

这篇关于为什么我的文字标签不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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