
我使用的是d3.v4图形,它们工作得很好,但是我的条形图的标题太长了,我想把标题分成两行。
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end");
var state = svg.selectAll(".state")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function (d) {
return "translate(0," + y(d.State) + ")";
})d.State包含了标题,这是我迄今为止尝试过的:
return "translate(0," + y(d.State.text().split("+")) + ")";“+”符号在标题中,我插入了这个符号,将标题分成两半。
发布于 2018-04-13 13:04:13
在这里输入图像描述这段代码适用于我
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end");
var state = svg.selectAll(".state")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) {
return "translate(0," + y(d.State) + ")";
})
$('.y .tick text')
.each(function(d){
var self = d3.select(this);
var lines = self.text().split("+");
self.text("");
self.append("tspan")
.attr("x", -8)
.attr("dy", "0em")
.attr('font-weight', 'bold')
.text(lines[0]);
self.append('tspan')
.attr("x", -8)
.attr("dy", ".92em")
.style('fill', '2E3092')
.text(lines[1]);
})https://stackoverflow.com/questions/49816528
复制相似问题