简体   繁体   English

在 d3 中旋转 x 轴文本

[英]rotate x axis text in d3

I am new to d3 and svg coding and am looking for a way to rotate text on the xAxis of a chart.我是 d3 和 svg 编码的新手,正在寻找一种在图表的 xAxis 上旋转文本的方法。 My problem is that typically the xAxis titles are longer than the bars in the bar chart are wide.我的问题是 xAxis 标题通常比条形图中的条宽。 So I'm looking to rotate the text to run vertically (rather than horizontally) beneath the xAxis.所以我希望旋转文本以在 xAxis 下方垂直(而不是水平)运行。

I've tried adding the transform attribute: .attr("transform", "rotate(180)")我已经尝试添加转换属性: .attr("transform", "rotate(180)")

But when I do that, the text disappears altogether.但是当我这样做时,文本完全消失了。 I've tried increasing the height of the svg canvas, but still was unable to view the text.我试过增加 svg 画布的高度,但仍然无法查看文本。

Any thoughts on what I'm doing wrong would be great.关于我做错了什么的任何想法都会很棒。 Do I need to also adjust the x and y positions?我还需要调整 x 和 y 位置吗? And, if so, by how much (hard to troubleshoot when I can see it in Firebug).并且,如果是这样,多少(当我在 Firebug 中看到它时很难进行故障排除)。

If you set a transform of rotate(180), it rotates the element relative to the origin , not relative to the text anchor.如果您设置了 rotate(180) 的变换,它会相对于原点旋转元素,而不是相对于文本锚点。 So, if your text elements also have an x and y attribute set to position them, it's quite likely that you've rotated the text off-screen.因此,如果您的文本元素还设置了xy属性来定位它们,则很可能您已将文本旋转到屏幕外。 For example, if you tried,例如,如果您尝试过,

<text x="200" y="100" transform="rotate(180)">Hello!</text>

the text anchor would be at ⟨-200,100⟩.文本锚点将在⟨-200,100⟩。 If you want the text anchor to stay at ⟨200,100⟩, then you can use the transform to position the text before rotating it, thereby changing the origin.如果您希望文本锚点保持在⟨200,100⟩,那么您可以在旋转之前使用变换来定位文本,从而改变原点。

<text transform="translate(200,100)rotate(180)">Hello!</text>

Another option is to use the optional cx and cy arguments to SVG's rotate transform , so that you can specify the origin of rotation.另一种选择是将可选的cxcy参数用于 SVG 的旋转变换,以便您可以指定旋转的原点。 This ends up being a bit redundant, but for completeness, it looks like this:这最终有点多余,但为了完整性,它看起来像这样:

<text x="200" y="100" transform="rotate(180,200,100)">Hello World!</text>

Shamelessly plucked from elsewhere , all credit to author. 从别处无耻地采摘,所有功劳都归功于作者。

margin included only to show the bottom margin should be increased.包含的边距仅用于显示应增加的底部边距。

var margin = {top: 30, right: 40, bottom: 50, left: 50},

svg.append("g")
   .attr("class", "x axis")
   .attr("transform", "translate(0," + height + ")")
   .call(xAxis)
     .selectAll("text")  
     .style("text-anchor", "end")
     .attr("dx", "-.8em")
     .attr("dy", ".15em")
     .attr("transform", "rotate(-65)");

One problem with this rotating D3 axis labels is that you have to re-apply this logic each time you render the axis.这种旋转 D3 轴标签的一个问题是每次渲染轴时都必须重新应用此逻辑。 This is because you do not have access to the enter-update-exit selections that the axis uses to render the ticks and labels.这是因为您无权访问轴用于呈现刻度和标签的输入-更新-退出选项。

d3fc is a component library that has a decorate pattern allowing you to gain access to the underling data join used by components. d3fc 是一个组件库,它具有装饰模式,允许您访问组件使用的底层数据连接。

It has a drop-in replacement for the D3 axis, where axis label rotation is performed as follows:它具有 D3 轴的替代品,其中轴标签旋转执行如下:

var axis = fc.axisBottom()
  .scale(scaleBand)
  .decorate(function(s) {
    s.enter()
        .select('text')
        .style('text-anchor', 'start')
        .attr('transform', 'rotate(45 -10 10)');
  });

Notice that the rotation is only applied on the enter selection.请注意,旋转仅应用于输入选择。

在此处输入图片说明

You can see some other possible uses for this pattern on the axis documentation page .您可以在轴文档页面上看到此模式的其他一些可能用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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