简体   繁体   English

Tremap 中的 D3 适当的文本()

[英]D3 proper text() in tremap

i'm new to d3.我是 d3 的新手。 My problem is unreadable text.我的问题是不可读的文本。 I suppose it's cuz i added text not to rect but to svg. How do i recode or fix this thing?我想这是因为我添加的文本不是为了 rect 而是为了 svg。我该如何重新编码或修复这个东西? https://codepen.io/DeanWinchester88/pen/xxrjLrM https://codepen.io/DeanWinchester88/pen/xxrjLrM

       svg
      .selectAll("text")
       .data(root.leaves())
       .enter()
          .append("text")
          .attr("x", (d)  =>  d.x0 +10)
          .attr("y",  (d) =>  d.y0  + 20)
          .text(  (d) => d.data.name)
          .attr("font-size", "12px")
          .attr("fill","white")
          .attr("overflow", "hidden")
          .attr("max-width",  (d) =>  d.x1 -  d.x0)
  tspan = text.text(null)
                        .append("tspan")
                        .attr("x", x)
                        .attr("y", y)
                        .attr("dy", dy + "em")

Here's your revised code based on my comment above:这是您根据我上面的评论修改后的代码:

https://codepen.io/mattsrinc/pen/MWozBWQ https://codepen.io/mattsrinc/pen/MWozBWQ

 svg
  .selectAll("text")
  .data(root.leaves())
  .enter()
    .append("text")
    .attr('transform', d => 'translate(' + d.x0 + ',' + d.y0 + ')')
    .selectAll('tspan')
    .data(d => d.data.name.split(/(?=[A-Z][^A-Z])/g))
    .enter()
      .append('tspan')
      .attr('font-size', '0.8em')
      .attr('fill', 'white')
      .attr('x', function(d) { console.log(d); return '0.5em' })
      .attr('y', (d, i) => 12 * (i + 1))
      .text(d => d);

I've left the console.log command so that you can see how the (game) name is split into parts.我已经离开了 console.log 命令,这样你就可以看到(游戏)名称是如何分成几个部分的。 And that Pac-Man is right but it's the only one game for the console category (2600) so it translates to thin black rectangle on top left (something you should consider how to solve it visually).那个吃豆人是对的,但它是控制台类别 (2600) 中唯一的一款游戏,因此它转换为左上角的细黑色矩形(您应该考虑如何在视觉上解决它)。

Then also SVG CSS attribute "overflow: hidden" won't work for your data cells with TSPAN.然后 SVG CSS 属性“溢出:隐藏”也不适用于使用 TSPAN 的数据单元格。 You would have to handle that with calculating this cell width and width of the current word(s) in a line (of TSPAN tag to add).您将不得不通过计算一行中当前单词的单元格宽度和宽度(要添加的 TSPAN 标记)来处理这个问题。 It would be better to follow and expand your other referenced codepen to work with cells that would ease this task.最好遵循并扩展您其他引用的代码笔以使用可以简化此任务的单元格。

Other than that I've just changed the color palette used and you can see color ranges here:除此之外,我刚刚更改了使用的调色板,您可以在此处查看颜色范围:

https://github.com/d3/d3-scale-chromatic https://github.com/d3/d3-scale-chromatic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM