nextstopsun
nextstopsun

Reputation: 473

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

I want to write a function, that returns a tick label with two lines of text. As I can see, an svg text tag is used for text labels. Is there a way to add tspan there or something?

Upvotes: 4

Views: 3929

Answers (1)

musically_ut
musically_ut

Reputation: 34288

You can access the elements created by the 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);

Also, you'll have to set .tickFormat to '' on the axis.

Upvotes: 5

Related Questions