垂直定位文本

时间:2018-01-16 10:44:46

标签: javascript d3.js rotation

我想将标签的每个字符设置为垂直,如下所示:

G
R
A
P
H

L
A
B
L
E

感谢任何帮助,您可以找到以下代码:

var centerG = g.append('g')
    .attr('x', ((width / 2) - (barWidth / 2)))
    .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

centerG.append('text')
    .text('GRAPH LABEL')
    .attr("font-family", "Arimo")
    .attr("font-size", "18px")
    .attr("font-weight", "bold")
    .attr("fill", "#FFFFFF")
    .attr("letter-spacing", "1.5px")
    .attr("text-anchor", "middle")
    .attr("transform", "translate(" + ((width / 2) + barOffset) +
        ", " + yHeight + ") rotate(270)")
    .attr("class", "centerLabelText")
    .attr("y", 0)

1 个答案:

答案 0 :(得分:2)

您的问题标题("将文字的每个字符旋转到垂直" ,我编辑过)会产生误导:您不想旋转文本的每个字符。如果你停下来看,文本中的每个字符都在正确的位置!这里没有任何动静。

你想要的是垂直定位文本

您可以使用Dim strPassword As String Dim DBpath As String Dim DBname As String Dim tblengagement As String DBpath = "C:\Projects" DBname = "Data.accdb" strPassword = "1234" tblengagement = "tbl_engagement" 'CurrentDb.Execute " UPDATE [MS Access;pwd=" & strPassword & ";database=" & DBpath & "\" & DBname & "].[" & tblengagement & "] " _ ' & "(Close_Time) VALUES " _ ' & "(#" & Time & "#)" _ ' & "WHERE ID = '" & SessionID & "';" 传递选择的函数,并将文本拆分为其字符,然后重新定位它们。这是我刚创建的一个例子:

call

这是一个演示:



function vertical(elem) {
  var xPos = elem.attr("x")
  var letters = elem.text().split("");
  var tspan = elem.text(null);
  while (letters.length) {
    tspan.append("tspan")
      .text(letters.shift())
      .attr("dy", "1em")
      .attr("x", xPos)
  }
}

var svg = d3.select("svg")
var text = svg.append('text')
  .text('GRAPH LABEL')
  .attr("font-family", "Arimo")
  .attr("font-size", "18px")
  .attr("font-weight", "bold")
  .attr("fill", "#444")
  .attr("letter-spacing", "1.5px")
  .attr("text-anchor", "middle")
  .attr("x", 150)
  .attr("y", 20)
  .call(vertical);

function vertical(elem) {
  var xPos = elem.attr("x")
  var letters = elem.text().split("");
  var tspan = elem.text(null);
  while (letters.length) {
    tspan.append("tspan")
      .text(letters.shift())
      .attr("dy", "1em")
      .attr("x", xPos)
  }
}