D3包装文字传说

发布于 2025-02-07 10:31:01 字数 1355 浏览 1 评论 0原文

我正在为D3图表使用传奇。以下是代码 -

 var padding = { left:50, right:100, top:30, bottom:30 };


    var legend = svgColorCode.selectAll(".legend")
      .data(keyText) //.filter(x=>x!="Completed On Time")
      .enter().append("g")
      .attr("class", "legend")
      .attr("transform", function (d, i) { return "translate(" + (((keys.length - (i)) * -25) + (i == 0 ? 0 : (i == 1 ? 60 : (i == 2 ? 90 : (i == 3 ? 100 : 0))))) + "," + (height - 190) + ")"; })
      .attr("fill", function (d, i) { return colors[i]; });

    legend.append("rect")
      .attr("x", (x, i) => (padding.top * 2 + labHeight * (i)) + 40)
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", function (d, i) { return colors[operationalKeys[i]]; })

    legend.append("text")      
      .attr("x", (x, i) => (padding.top * 2 + labHeight * i) + 60)
      .attr("y", 9)
      .attr("font-size", "0.65rem")
      .attr("dy", ".35em")      
      .style("text-anchor", "start")
      .text(function (d) { console.log("d");console.log(d); return d.replace("&nbsp","\n"); })
      .call(wrap)
      ;

在这里,我有很长的文字,用于传说和小地方可以容纳它。我想包装传说的文字。

为此,我遵循 this 所以答案并使用wrap方法。但是,在这种情况下,包裹似乎没有起作用。

如何在D3 JS中包装文字以获取传奇?

I am using legends for d3 charts. Below is code -

 var padding = { left:50, right:100, top:30, bottom:30 };


    var legend = svgColorCode.selectAll(".legend")
      .data(keyText) //.filter(x=>x!="Completed On Time")
      .enter().append("g")
      .attr("class", "legend")
      .attr("transform", function (d, i) { return "translate(" + (((keys.length - (i)) * -25) + (i == 0 ? 0 : (i == 1 ? 60 : (i == 2 ? 90 : (i == 3 ? 100 : 0))))) + "," + (height - 190) + ")"; })
      .attr("fill", function (d, i) { return colors[i]; });

    legend.append("rect")
      .attr("x", (x, i) => (padding.top * 2 + labHeight * (i)) + 40)
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", function (d, i) { return colors[operationalKeys[i]]; })

    legend.append("text")      
      .attr("x", (x, i) => (padding.top * 2 + labHeight * i) + 60)
      .attr("y", 9)
      .attr("font-size", "0.65rem")
      .attr("dy", ".35em")      
      .style("text-anchor", "start")
      .text(function (d) { console.log("d");console.log(d); return d.replace(" ","\n"); })
      .call(wrap)
      ;

Here I have long texts for legends and small place to accomodate it. I want to wrap the text for legends.

For that I followed this SO answer and used wrap method. But seems wrap is not working in this case.

How can I wrap my text for legends in d3 js?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文