requesting (continued) help to re-structure mapped data

Hi Community.

I continue to have a bit of trouble figuring out data re-structuring and am reaching out for help.

I would like to make a Zoomable Sunburst chat showing the distribution of households across some different administrative areas. The challenge that I am having is how to access the value for household_total so that it is counted appropriately.

Previously, the community helped show me how to create a nesting function that returns an array from ‘rolled up’ data, and re-labels the attribute names in a manner expected of the Zoomable Sunburst:

function nest(rollup) {
  return Array.from(rollup, ([key, value]) =>
    value instanceof Map
      ? { name: key, children: nest(value) }
      : { name: key, value: value.length}
  );
}

However, in the case of my data, I still need to re-work the final value so that it’s interpreted as a number, rather than a name. Without this, the final count field is interpreted just a single leaf:

value-as-name

Try as I might, I haven’t figured out this final transformation. Here’s a notebook:

Any help or insights?

Hi Aaron,

I’m not entirely sure what the end product is supposed to look like, but you might try d3.rollup instead of d3.group, e.g.,

households_per_settlement_grouped = d3.rollup(
  household_data,
  (v) => d3.sum(v, (e) => e.household_total),
  (d) => d.municipality,
  (d) => d.ward,
  (d) => d.settlement_name
)

and change the nest function to just have value instead of value.length. I am likely completely off-base on what you are trying to achieve, but I shared a notebook with you, in case that helps.

Evan

1 Like

Thank you Evan! This does help… I dropped the ward tier for simplicity for now (but intend to add it). Your final mapping looks right:

image

… unfortunately I still have an error when I try to supply the output of this rollup into the sunburst chart RuntimeError: Cannot read properties of undefined (reading 'length').

Essentially, I would like the final leaf in the sunburst to be settlement_name and its size within it’s group to be proportional according to households_total. (If that’s clearly stated? Maybe not :confused: )

… looks like, to use your rolled data, I simply need to find a way to re-assign the attribute names to the mapped values. I’ll keep trying!

Ok, I got it - AND I remembered that I had issues on this before, and getting around it required a different sunburst chart, which Mike had graciously volunteered.

Thanks @Evan - you took me over the finish line! Based on your code, here’s the final bit:

households_per_settlement_rolled = {
  const rollup = d3.rollup(
  household_data,
  (v) => d3.sum(v, (e) => e.household_total),
  (d) => d.municipality,
  (d) => d.settlement_name);
  return d3.hierarchy(rollup).sum(([, value]) => value);
}
1 Like