首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rCharts sankey图中的单元

rCharts sankey图中的单元
EN

Stack Overflow用户
提问于 2016-06-15 15:14:17
回答 2查看 244关注 0票数 0

我使用以下代码生成rCharts Sankey图(桑基):

代码语言:javascript
复制
if(!require(rCharts)){
  library(devtools)
  install_github('ramnathv/rCharts')
}
library(rCharts)

sankeyPlot <- rCharts$new()
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot$set(
  data = data.frame(source=c('Cold','Warm','Total'),target=c('Total','Total','End'),value=c(20,80,100)),
  nodeWidth = 15,
  nodePadding = 10,
  layout = 32,
  width = 500,
  height = 300,
  units = "TWh",
  labelFormat = ".1%"
)
sankeyPlot$setTemplate(
  afterScript = "
  <script>
  // to be specific in case you have more than one chart
  d3.selectAll('#{{ chartId }} svg path.link')
  .style('stroke', function(d){
  //here we will use the source color
  //if you want target then sub target for source
  //or if you want something other than gray
  //supply a constant
  //or use a categorical scale or gradient
  return d.source.color;
  })
  //note no changes were made to opacity
  //to do uncomment below but will affect mouseover
  //so will need to define mouseover and mouseout
  //happy to show how to do this also
  // .style('stroke-opacity', .7)
  </script>
  ")

sankeyPlot

Sankeyplot$set中,我为单元设置了一个值。然而,我既没有看到单位,也没有看到价值。单元示例来自官方的github文档(example_hirst_f1.R)。如何显示图表中的值和单位?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-15 21:15:20

JeroenDM的答案也可以插入后面的脚本中。在这种情况下,无需修改就可以使用Github存储库。

代码语言:javascript
复制
sankeyPlot$setTemplate(
  afterScript = "
  <script>
  // to be specific in case you have more than one chart
  d3.selectAll('#{{ chartId }} svg path.link')
  .style('stroke', function(d){
  //here we will use the source color
  //if you want target then sub target for source
  //or if you want something other than gray
  //supply a constant
  //or use a categorical scale or gradient
  return d.source.color;
  })
  //note no changes were made to opacity
  //to do uncomment below but will affect mouseover
  //so will need to define mouseover and mouseout
  //happy to show how to do this also
  // .style('stroke-opacity', .7)

  units = ' TWh'

  var formatNumber = d3.format('0,.0f'),    // zero decimal places
  format = function(d) { return formatNumber(d) + units; }

  d3.selectAll('#{{ chartId }} svg .node text')
  .text(function (d) { return d.name + ' (' + format(d.value) + ')'; })
  </script>
  ")

票数 0
EN

Stack Overflow用户

发布于 2016-06-15 18:00:51

在sankeyPlot输出中,使用class=“节点”创建一个svg元素。在这个元素中,值及其单元被添加到代表节点的rect元素中的 title element 中。此title元素不是可见的元素。另一方面,名称被添加到文本元素中(在本例中是“温暖的”),并且是可见的。

您可以通过右键单击Rstudio中的“视图”窗口,然后“检查”来查看此结构。

Web检查器中节点结构的截图

快速修复方法是将值及其单位添加到此文本元素中。通过将布局/charts.html中的第105行替换为

代码语言:javascript
复制
  .text(function (d) { return d.name; })

使用

代码语言:javascript
复制
.text(function (d) { return d.name + " " + format(d.value); })

然后用这个作为模板。

当然,可能有更好的解决办法。我认为title元素的存在是出于某种原因(可能在mouseover事件中使用它)。但至少这是个开始。希望能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37839387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档