GithubHelp home page GithubHelp logo

Comments (6)

timelyportfolio avatar timelyportfolio commented on June 11, 2024

@jstrin, thanks so much for pointing this out as I have been meaning to clarify, since this can be both confusing and misleading. Your solution is very nice, so I appreciate both taking the time to find a solution and sharing it here.

The problem arises when a tree is pre-summed (generally not the case in my experience, but definitely the case with treemap) and can be traced to this line. My proposed fix would be to only use size at the leaf level, so the sums would be recalculated at all non-leaf nodes.

var root = hierarchy(json)
        .sum(function(d) {
 		// only sum if no children (or is leaf) 
 		if(!(d.children && d.children.length > 0)) return d[x.options.valueField || "size"];
	});

This would be a potentially breaking change, so I'll let this sit for a while and seek input in case someone has an opinion. Please let me know if you see anything amiss.

For testing, we can use the following code.

library(sunburstR)
library(d3r)

df <- data.frame(
  index1 = c(rep("A",3),"B"),
  index2 = c(NA,"A.1","A.1",NA),
  index3 = c(NA, NA, "A.1.1", NA),
  size = c(5,5,5, 10),
  stringsAsFactors = FALSE
)

sunburst(
  d3_nest(df, value_cols="size"),
  count = TRUE
)

... and should expect

image

This would be much easier if we can assume all data passed to sunburst is pre-summed, but I think case this does not fit reality, so I prefer the above approach.

from sunburstr.

timelyportfolio avatar timelyportfolio commented on June 11, 2024

More for testing...

library(sunburstR)
library(d3r)
library(treemap)
library(dplyr)


df <- data.frame(
  index1 = c(rep("A",3),"B"),
  index2 = c(NA,"A.1","A.1",NA),
  index3 = c(NA, NA, "A.1.1", NA),
  size = c(5,5,5, 10),
  stringsAsFactors = FALSE
)

sunburst(
  d3_nest(df, value_cols="size"),
  count = TRUE
)


rhd <- random.hierarchical.data()

sunburst(
  d3_nest(rhd, value_cols="x"),
  valueField = "x"
)

tm <- treemap(
  rhd,
  index = paste0("index", 1:3),
  vSize = "x"
)$tm


sunburst(
  tm %>%
    select(index1, index2, index3, vSize) %>%
    d3_nest(value_cols = "vSize"),
  valueField = "vSize"
)

from sunburstr.

timelyportfolio avatar timelyportfolio commented on June 11, 2024

@jstrin b8a18d1 implements the proposed change. Would you mind testing with a couple trees on your side to see if it works as expected?

from sunburstr.

timelyportfolio avatar timelyportfolio commented on June 11, 2024

from @jstrin


@timelyportfolio sorry for the delayed response. I ran through a handful of tests including using data with truncated paths (e.g., NA values for one of the indices) and it looks good on my end. I'll re-run my test scripts after you push the updated d2b code.

from sunburstr.

timelyportfolio avatar timelyportfolio commented on June 11, 2024

Test code now

library(sunburstR)
library(d3r)
library(treemap)
library(dplyr)


df <- data.frame(
  index1 = c(rep("A",3),"B"),
  index2 = c(NA,"A.1","A.1",NA),
  index3 = c(NA, NA, "A.1.1", NA),
  size = c(5,5,5, 10),
  stringsAsFactors = FALSE
)

sunburst(
  d3_nest(df, value_cols="size"),
  count = TRUE,
  valueField = "size",
  sumNodes = FALSE
)


rhd <- random.hierarchical.data()

sunburst(
  d3_nest(rhd, value_cols="x"),
  valueField = "x"
)

tm <- treemap(
  rhd,
  index = paste0("index", 1:3),
  vSize = "x"
)$tm


sunburst(
  tm %>%
    select(index1, index2, index3, vSize) %>%
    d3_nest(value_cols = "vSize"),
  valueField = "vSize",
  sumNodes = FALSE
)

from sunburstr.

timelyportfolio avatar timelyportfolio commented on June 11, 2024

going to close, since I believe the new sumNodes argument introduced in 8bc0b1c will satisfy this need.

from sunburstr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.