简体   繁体   中英

How to rotate text labels in a D3 x-axis?

I am trying to rotate only the <g class="tick"> 's labels of this D3 bar-chart but the whole axis rotates and ends-up looking like this:

在此处输入图片说明

I have tried:

  • A .attr("transform", "rotate(...)") : The whole axis rotates...
  • The same inside the line .selectAll(".tick") , which causes the labels to rotate along themselves but they get relocated to the corner of the graph, like this:

在此处输入图片说明

How can I rotate each element relative to its own position?

您只想为文本元素而不是整个刻度设置.attr("transform", "rotate(...)")

I figured it out with this D3 block . I have updated the chart so the old one is no longer visible. The lines added that fix the problem have been commented with "Line added to fix rotation" just in case someone comes across the same issue.

Thanks @Daniel for pointing me out in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM