Parallel Sets - Sum Value by Initial Key

Hello, first time using Observable and love the notebook env! I am working on a Parallel Sets visualization here and I’m trying to capture the sum of values but grouped by the initial key (where the link started). For example, each node’s value is calculated in total but I would also like to be able to reference the total split by White and Black for every node. I attempted to do this when building the links where I check if a particular Race is in the “names” array of a link, and if so, I add its’ value to the “_value” property of the link.

if (link) {  link.value += value;
                   // SUM WHITE VALUES
                   if (link["names"].includes("White")) {
                     link.white_value += value;                   
                   else {
                      link.black_value += value;

This does not work so I feel I’m not understanding how the code is aggregating the value field of my data. Would anyone be able to offer a hint on how to do this? I would really like to be able to display additional text by each node that shows the Black individual total and the White Individual total. Thank you for any help!

Apologize for replying to my own post but I was able to figure this out by realizing I needed to add the property to the nodes object itself. For each node, I traversed their targetLinks array and aggregated their value depending on if the key (Black or White) was in their ‘names’ array. I then add that value as a property to the node.

>  // Calculate total values by race and add to node
>   nodes.forEach(function(item, index) {
>     if (item["targetLinks"]) {
>       var target = item["targetLinks"];
>       var black_value = 0;
>       var white_value = 0;
>       target.forEach(function(tlink, index) {
>         if (tlink["names"].includes("White")) {
>           white_value += tlink.value;
>         } else {
>           black_value += tlink.value;
>         }
>       });
>     }
>     item.w_value = white_value;
>     item.b_value = black_value;
>   });
1 Like

No apology needed, and thanks for sharing your solution!

By the way, instead of using blockquotes I’d recommend to wrap your code snippets in code blocks (marked by three backticks or three tildes). E.g., writing

foo = "bar"


foo = "bar"


foo = "bar"