1

I'm using sankeyNetwork from . When I plot my data, if I hover the mouse on the sankey links the link label appears, and the thousands separator in this label is a comma. I would like it to be a point.

Here is an example:

library(networkD3)

nodes <- data.frame(name = c('a','b'))
links <- data.frame(source = c(0), target = c(1), value = c(12000))

p <- sankeyNetwork(
  Links = links,
  Source = "source",
  Target = "target",
  Value = "value",
  Nodes = nodes,
  NodeID = "name",
  fontSize = 12,
  nodeWidth = 30,
  iterations = 0
)

p

output

thankss!!

Xevi

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Xevi
  • 379
  • 1
  • 2
  • 11
  • 1
    It is hardcoded into the JavaScript. You would either have to modify the underlying JavaScript or somehow override the custom format function [here](https://github.com/christophergandrud/networkD3/blob/master/inst/htmlwidgets/sankeyNetwork.js#L96-L100). – CJ Yetman Apr 10 '19 at 06:13
  • "You would either have to modify the underlying JavaScript": How can I do that? – Xevi Apr 10 '19 at 12:58
  • 1
    Fork the repo, change the “,” to a “.”, install your modified version of the package. – CJ Yetman Apr 10 '19 at 13:00

1 Answers1

0

You can achieve this by rewriting the link titles with htmlwidgets::onRender...

library(networkD3)
library(htmlwidgets)

nodes <- data.frame(name = c('a','b'))
links <- data.frame(source = c(0), target = c(1), value = c(12000))

p <- sankeyNetwork(
  Links = links,
  Source = "source",
  Target = "target",
  Value = "value",
  Nodes = nodes,
  NodeID = "name",
  fontSize = 12,
  nodeWidth = 30,
  iterations = 0
)

customJS <- '
function(el,x) { 
    var link = d3.selectAll(".link");

    var format = d3.formatLocale({"decimal": ",", "thousands": ".", "grouping": [3], "currency": ["", "\u00a0€"]}).format(",.0f");

    link.select("title").select("body")
        .html(function(d) { return "<pre>" + d.source.name + " \u2192 " + d.target.name +
            "\\n" + format(d.value) + "<pre>"; });
}
'
onRender(p, customJS)

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56