当需要完全控制文本分割逻辑时,可手动处理换行点:
const text = "这是一段需要换行的长文本示例,用于演示D3.js的手动换行方法。"; const maxCharsPerLine = 15; // 每行最多字符数 svg.selectAll("text") .data([text.split(/(?=S{15})/)] ) // 按字符数分割 .enter() .append("text") .attr("x", 20) .attr("y", (d, i) => 30 + i * 20) // 行高20像素 .text(d => d);
优化点:
/(?=S{15})/
避免在单词中间断开 i * emToPxRatio
适配不同字体大小对复杂换行需求(如自动适应容器宽度),推荐使用社区验证的第三方库:
import { wrap } from 'https://cdn.jsdelivr.net/npm/d3-text-wrap@2.0'; svg.append("text") .attr("x", 50) .attr("y", 50) .style("font-size", "14px") .call(wrap() .maxWidth(200) // 最大宽度200px .padding(5) // 内边距 .breakWord(true) // 启用单词断行 ) .text("This is an English example demonstrating automatic line wrapping based on container width.");
优势:
结合SVG视图框变化动态调整:
function dynamicWrap(textElement, maxWidth) { textElement.each(function() { const text = d3.select(this); const words = text.text().split(/s+/); let line = []; let tspan = text.text(null) .append("tspan") .attr("x", 0) .attr("dy", "1.2em"); words.forEach(word => { line.push(word); tspan.text(line.join(" ")); if (tspan.node().getComputedTextLength() > maxWidth) { line.pop(); tspan.text(line.join(" ")); line = [word]; tspan = text.append("tspan") .attr("x", 0) .attr("dy", "1.2em") .text(word); } }); }); } // 调用示例 d3.select("#chart").append("text") .attr("transform", "translate(100,100)") .text("动态响应式文本换行示例") .call(dynamicWrap, 150); // 容器宽度150px
技术细节:
getComputedTextLength()
获取实时文本宽度 dy
属性控制行间距(1.2em为推荐值) window.addEventListener("resize", () => { const newWidth = document.getElementById("chart").offsetWidth; d3.selectAll(".wrapped-text").call(dynamicWrap, newWidth * 0.8); });
当需要CSS文本控制时,可通过ForeignObject嵌入HTML:
const foreignObject = svg.append("foreignObject") .attr("x", 50) .attr("y", 100) .attr("width", 200) .attr("height", 150); const div = foreignObject.append("xhtml:div") .style("width", "100%") .style("font-family", "Arial") .style("word-break", "break-all") .html(`<div class="html-text" style=" line-height: 1.5; text-align: justify; hyphens: auto;"> 混合HTML/CSS的文本排版示例,支持连字符、两端对齐等高级特性。 </div>`);
适用场景:
hyphens
) 性能优化
无障碍访问
textElement.attr("aria-label", "完整文本内容");
多语言支持
direction: rtl
视觉一致性
text-anchor
控制对齐方式 引用说明
文中技术方案参考自: