如何使用 d3.js 对图表标签进行自动换行 [英] How to do wordwrap for chart labels using d3.js
问题描述
我正在尝试使用 d3.js 实现水平条形图.一些图表标签太长.如何对y aixs上的图表标签进行自动换行?
I am trying to implement the horizontal bar chart using d3.js.Some of the chart labels are too long. How to do word wrap for the chart labels on y aixs?
源代码:
var data = [{"Name": "Label 1", "Count": "428275" }, { "Name": "Label 2", "Count": "365005" }, { "Name": "Label 3", "Count": "327619" }];
var m = [30, 10, 10, 310],
w = 1000 - m[1] - m[3],
h = 550 - m[0] - m[2];
var format = d3.format(",.0f");
var x = d3.scale.linear().range([0, w + 10]),
y = d3.scale.ordinal().rangeRoundBands([0, h], .4);
var xAxis = d3.svg.axis().scale(x).orient("bottom").tickSize(h),
yAxis = d3.svg.axis().scale(y).orient("left").tickSize(0);
$("#chartrendering").empty();
var svg = d3.select("#chartrendering").append("svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");
// Set the scale domain.
x.domain([0, d3.max(data, function (d) { return d.Count; })]);
y.domain(data.map(function (d) { return d.Name; }));
var bar = svg.selectAll("g.bar")
.data(data)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function (d) { return "translate(0," + y(d.Name) + ")"; });
bar.append("rect")
.attr("width", function (d) { return x(d.Count); })
.attr("height", y.rangeBand());
bar.append("text")
.attr("class", "value")
.attr("x", function (d) { return x(d.Count); })
.attr("y", y.rangeBand() / 2)
.attr("dx", +55)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.text(function (d) { return format(d.Count); });
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
推荐答案
这是我通过汇总各种位而编写的工作实现.正如另一个答案所暗示的那样,foreignObject 仍然是要走的路.先说函数:
Here is a working implementation I've written by pulling together various bits. As the other answer suggests, foreignObject is still the way to go. First the function:
var insertLinebreaks = function (t, d, width) {
var el = d3.select(t);
var p = d3.select(t.parentNode);
p.append("foreignObject")
.attr('x', -width/2)
.attr("width", width)
.attr("height", 200)
.append("xhtml:p")
.attr('style','word-wrap: break-word; text-align:center;')
.html(d);
el.remove();
};
这需要一个文本元素 (t)、文本内容 (d) 和要换行的宽度.然后它获取text
对象的parentNode,并将一个foreignObject
节点附加到它,其中添加了一个xhtml:p
.foreignObject
设置为所需的 width
和偏移 -width/2
到中心.最后删除原来的文本元素.
This takes in a text element (t), the text content (d), and the width to wrap to. It then gets the parentNode of the text
object, and attaches a foreignObject
node to it into which an xhtml:p
is added. The foreignObject
is set to the desired width
and offset -width/2
to center. Finally, the original text element is deleted.
这可以应用于您的轴元素,如下所示:
This can then be applied to your axis elements as follows:
d3.select('#xaxis')
.selectAll('text')
.each(function(d,i){ insertLinebreaks(this, d, x1.rangeBand()*2 ); });
这里我使用 rangeBand 来获取宽度(图表上的 2 个条形使用 *2).
Here I've used rangeBand to get the width (with *2 for 2 bars on the graph).
这篇关于如何使用 d3.js 对图表标签进行自动换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!