Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Change rectangles to circles for sankeyNetwork in networkd3

I'm having issues changing the rectangles in sankeyNetwork into circles in the networkd3 package for R. So far my onrender js code only removes the rectangles, not sure why the circles aren't being added instead.

Here's my code:

htmlwidgets::onRender(
  p,
  '
  function(el,x){

  //node variable
  var node=d3.select(el).select("svg").selectAll(".node");
  
  //remove node
  node.selectAll("rect")
    .remove();
    
  // add circles instead
  node
    .append("circle")
    .attr("cx",sankey.nodeWidth()/2)
    .attr("cy",function(d){
    return d.dy/2;
    })
    .attr("r",function(d){
      return Math.sqrt(d.dy);})
    .style("fill", function(d) {
      return d.color = color_node(d); })
    .style("stroke", function(d) { return d3.rgb(d.color).darker(2); })
    .style("opacity", 0.9)
    .style("cursor", "move")
    .append("title")
    .append("foreignObject")
    .append("xhtml:body")
    .html(function(d) { return "<pre>" + d.name + "<br>" + format(d.value) + 
        " " + options.units + "</pre>"; })
    ;
    

  }
  '
)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You were referencing a few variables and functions that were not within scope of your function...

library(jsonlite)
library(networkD3)
library(htmlwidgets)

URL <- paste0('https://cdn.rawgit.com/christophergandrud/networkD3/',
              'master/JSONdata/energy.json')
energy <- jsonlite::fromJSON(URL)

p <- sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = 'source',
                   Target = 'target', Value = 'value', NodeID = 'name',
                   units = 'TWh', fontSize = 12, nodeWidth = 30)


htmlwidgets::onRender(
  p,
  '
  function(el,x){
  
  // map the variables you are trying to use below
  var options = x.options;
  var sankey = this.sankey;
  
  // replicate the private functions from the renderValue function that you call below
  var color = eval(options.colourScale);
  var color_node = function color_node(d){
          if (d.group){
            return color(d.group.replace(/ .*/, ""));
          } else {
            return "#cccccc";
          }
  }
  
  var formatNumber = d3.format(",.0f"),
        format = function(d) { 
            if (typeof d === "string") return d;
            return formatNumber(d); 
        }

  //node variable
  var node=d3.select(el).select("svg").selectAll(".node");
  
  //remove node
  node.selectAll("rect")
    .remove();
    
  // add circles instead
  node
    .append("circle")
    .attr("cx",sankey.nodeWidth()/2)
    .attr("cy",function(d){
    return d.dy/2;
    })
    .attr("r",function(d){
      return Math.sqrt(d.dy);})
    .style("fill", function(d) {
      return d.color = color_node(d); })
    .style("stroke", function(d) { return d3.rgb(d.color).darker(2); })
    .style("opacity", 0.9)
    .style("cursor", "move")
    .append("title")
    .append("foreignObject")
    .append("xhtml:body")
    .html(function(d) { return "<pre>" + d.name + "<br>" + format(d.value) + 
        " " + options.units + "</pre>"; })
    ;
    

  }
  '
)

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda