更改networkD3图的背景颜色 [英] Change background color of networkD3 plot

查看:149
本文介绍了更改networkD3图的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我制作了一个networkD3图-使用软件包中的最小示例

Say I make a networkD3 plot - using the minimal example in the package

#
library(networkD3)

# Load data
data(MisLinks)
data(MisNodes)

# Plot
forceNetwork(Links = MisLinks, Nodes = MisNodes,
            Source = "source", Target = "target",
            Value = "value", NodeID = "name",
            Group = "group", opacity = 0.8)

如果我在浏览器中打开它,则可以使用开发人员工具将主体的背景色更改为例如background-color: #DAE3F9;"

If I open this in the browser, I can use developer tools to change the background color of body to e.g. background-color: #DAE3F9;"

有没有一种方法可以自动定义绘图的背景颜色(从默认的白色)到另一种颜色,而无需在浏览器中打开?基本上,我们可以像添加JS函数那样直接将CSS添加到代码中吗?

Is there a way to automatically define background color of a plot (From default white) to another color, without opening in the browser ? Basically, can we add CSS directly to the code like we can add JS functions?

推荐答案

这是我的技巧.这不是很漂亮,但是我在linkDistance参数中添加了一个函数来执行肮脏的工作.希望有人最终会提供一个不太复杂的解决方案:

Here's my hack at it. It's not pretty, but I added a function in the linkDistance argument to do the dirty work. Hopefully, someone will come along eventually with a less kludgy solution:

forceNetwork(Links = MisLinks, Nodes = MisNodes,
  Source = "source", Target = "target",
  Value = "value", NodeID = "name",
  Group = "group", opacity = 0.8,
  linkDistance = 
    JS('function(){d3.select("body").style("background-color", "#DAE3F9"); return 50;}'))

另一个不那么吸引人的选择是添加clickAction自变量(例如clickAction = 'd3.select("body").style("background-color", "#DAE3F9")'),但这只会在用户单击节点时更改背景.

Another, similarly unappealing option would be to add a clickAction argument (e.g. clickAction = 'd3.select("body").style("background-color", "#DAE3F9")'), but that would only change the background if a user clicks a node.

这篇关于更改networkD3图的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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