【发布时间】:2017-10-09 17:34:07
【问题描述】:
我正在使用 D3 创建基本条形图
对于我的 x 轴,我想将每个标签放在各自的条形上方。文字也应该旋转90度
要查看执行此操作的代码,请从第 51 行开始。https://codepen.io/Fallenstedt/pen/xdYooE
//This is where I attempt to create an x Axis label
//create a container to hold the text element
var textContainer = svg.append('g')
.selectAll('g')
.data(data).enter()
.append('g')
.attr('class', 'x-axis')
.attr('x', function(d, i) {
return i * (width/data.length)
})
.attr('y', function(d, i) {
return height - (d.value) + 15;
})
.attr("transform", function(d, i) {return "translate(" + (i * (width/data.length)) + ",330)";});
//now that a container is made, I can append a text element to it so I can rotate the text 90 degrees.
textContainer.append('text')
.text(function(d) {
return d.type
})
.attr('font-size', '34px')
.attr('fill', 'white')
.attr("text-anchor","end")
.attr("transform", function(d, i) {return "translate(40,0) rotate(-90,0,0)";});
标签出现并且它们旋转了 90 度,但是我不能将它们定位在它们各自的矩形上方。如何将每个 x 轴标签定位在其矩形的正上方?我觉得我的处理方法过于复杂。
【问题讨论】:
标签: javascript d3.js svg