在networkD3 sankey图中的节点标签中放入换行符 [英] Put line break in node labels in networkD3 sankey diagram

查看:209
本文介绍了在networkD3 sankey图中的节点标签中放入换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

++++++++++++++++++

++++++++++++++++

更新: 我认为我的问题的答案是您不能插入换行符.一位同事向我指出,节点标签是SVG块,不支持换行符.

Update: I think the answer to my question is that you can't put line breaks in. A colleague pointed out to me the node labels are SVG blocks, which don't support line breaks.

++++++++++++++++++

++++++++++++++++

如何在使用networkD3 R软件包生成的sankey图的节点标签中插入换行符?

How do I put a line break into the node labels for a sankey diagram produced using the networkD3 R package?

在Sankey图的右侧放置文本值来借阅示例我可以为标签添加值:

Borrowing the example from Place text values to right of sankey diagram I can add values to the lables:

library(networkD3)
library(data.table)
set.seed(1999)
links <- data.table(
  src = rep(0:4, times=c(1,1,2,3,5)),
  target = sample(1:11, 12, TRUE),
  value = sample(100, 12)
)[src < target, ]  # no loops
nodes <- data.table(name=LETTERS[1:12])

#### Need to hover to get counts
##sankeyNetwork(Links=links, Nodes=nodes, Source='src', Target='target',
##  Value='value', NodeID='name', fontSize=16)

## Add text to label
txt <- links[, .(total = sum(value)), by=c('target')]
nodes[txt$target+1L, name := paste0(name, ' (', txt$total, ')')]

## Displays the counts as part of the labels
sankeyNetwork(Links=links, Nodes=nodes, Source='src', Target='target',
  Value='value', NodeID='name', fontSize=16, width=600, height=300)

我希望可以天真地调整paste0使其包含换行符,例如:

I hoped that I could naively adjust the paste0 to include a line break character, such as:

 name := paste0(name, "\n ", txt$total)

name := paste0(name, "<br/> ", txt$total)

但是我无法使任何东西正常工作,而且我的JavaScript太生锈了,无法在产生后尝试对其进行修复.

But I haven't been able to get anything to work, and my JavaScript is too rusty to try and fix it once it is produced.

推荐答案

您可以用<foreignObject> text/html块替换SVG文本元素.此示例将需要大量其他格式/位置才能使用,但它表明有可能...

You could replace the SVG text elements with <foreignObject> blocks of text/html. This example would need a lot of additional formatting/positioning to be useful, but it demonstrates that it is possible...

library(networkD3)
library(htmlwidgets)
library(data.table)

set.seed(1999)
links <- data.table(
  src = rep(0:4, times=c(1,1,2,3,5)),
  target = sample(1:11, 12, TRUE),
  value = sample(100, 12)
)[src < target, ]  # no loops
nodes <- data.table(name=LETTERS[1:12])

## Add text to label
txt <- links[, .(total = sum(value)), by=c('target')]
nodes[txt$target+1L, name := paste0(name, '<br>(', txt$total, ')')]

## Displays the counts as part of the labels
sn <- sankeyNetwork(Links=links, Nodes=nodes, Source='src', Target='target',
              Value='value', NodeID='name', fontSize=16, width=600, height=300)

onRender(sn,
         '
  function(el,x) {
    d3.selectAll(".node text").remove()
    d3.selectAll(".node")
      .append("foreignObject")
      .attr("width", 100)
      .attr("height", 50)
      .html(function(d) { return d.name; })
  }
  '
)

这篇关于在networkD3 sankey图中的节点标签中放入换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆