如何将文本附加到d3.js中的一行 [英] How to append text to a line in d3.js

查看:86
本文介绍了如何将文本附加到d3.js中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文本附加到条形图上创建的线条上。问题是我可以看到文本元素已创建。但它没有显示在svg上的任何地方。
我追加文字的部分代码

I am trying to append a text to a line created on bar chart. The problem is that I can see that the text element is created. But it does not show anywhere on the svg. part of code where I append text

 var line = svg.append("line")
              .attr("x1", function(){ return x(lineEnd)})
              .attr("x2", function(){ return x(lineEnd)})
              .attr("y1", 0)
              .attr("y2", height)
              .attr("stroke-width", 6)
              .attr("stroke", "black")
              .attr("stroke-dasharray", "8,8")



  line.append('text')
             .attr('class', 'barsEndlineText')
             .attr('text-anchor', 'middle')
              .attr("x", 0)
             .attr("y", ".35em")
             .text('I am label')

请参阅plunker获取完整代码

推荐答案

您无法将文本附加到。只需为文本创建另一个变量:

You can't append a text to a line. Just create another variable for the text:

var myText =  svg.append("text")
   .attr("y", height - 10)//magic number here
   .attr("x", function(){ return x(lineEnd)})
   .attr('text-anchor', 'middle')
   .attr("class", "myLabel")//easy to style with CSS
   .text("I'm a label");

这篇关于如何将文本附加到d3.js中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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