如何在d3.axis.tickFormat中换行

How to break a line in d3.axis.tickFormat?

本文关键字:tickFormat 换行 axis d3      更新时间:2023-09-26

我想写一个函数,它返回一个带有两行文本的刻度标签。正如我所看到的,svg文本标记用于文本标签。有没有办法在那里加tspan之类的?

您可以访问axis:Demo 创建的元素

d3.select('svg')
  .append('g')
  .attr('transform', 'translate(180, 10)')
  .call(xAxis)
  .selectAll('text') // `text` has already been created
  .selectAll('tspan')  
  .data(function (d) { return bytesToString(d); }) // Returns two vals
  .enter()
  .append('tspan')
  .attr('x', 0)
  .attr('dx', '-1em')
  .attr('dy', function (d, i) { return (2 * i - 1) + 'em'; })
  .text(String);

此外,您还必须在axis上将.tickFormat设置为''