| by Arround The Web | No comments

D3.js Nests – Collection API

Nesting in D3.js allows the elements in an array (basically array of objects) to be grouped into a hierarchical tree structure. The levels of this tree can be specified using the key functions. It can also be possible to sort the leaf nodes by the value. This returns the nested array.

In this guide, we will discuss all the functions related to the D3.js nests collection with examples. As a bonus, we will see how to run the code snippets in the observable with some functions. We use the following HTML template for all the examples that are discussed in this guide. Only this script is provided for all the examples; you can include the script in the following template:

<html>

<head>

  <script src='https://d3js.org/d3.v4.min.js'></script>

</head>

<body>

<p> Inspect the page and see the result in the console...</p>

  // Include your Script here

</body>

</html>

Nests – Collection API

Nest.entries(Array)

In D3 nests, the nest.entries() function applies the nest to a specific array (every level of hierarchy) which returns the array of key-value entries (similar to map). Its parameter is an array of objects.

Return Format:

Create an array of five “cases” objects, pass the case_source as the key, and generate the nests by passing “cases” to the entries() function.

<script>

    // Create array of 5 objects
    let cases =[
      {case_source:"Phone", case_priority:"Medium", subject:"Radio issue"},
      {case_source:"Email", case_priority:"High", subject:"Mobile issue"},
      {case_source:"Web", case_priority:"Medium", subject:"Power Outage"},
      {case_source:"Phone", case_priority:"High", subject:"Battery drained"},
      {case_source:"Email", case_priority:"High", subject:"TV not working"}
    ]
   
    // nest.entries(cases)
    let res = d3.nest().key(function(i) { return i.case_source; }).entries(cases)
    console.log(res);
</script>

Output:

Inspect the HTML page and view it in the console.

Key1: “Phone” with two objects in the array, Key2: “Email” with two objects in the array, and Key3: “Web” with one object in the array.

Nest.key(Key)

If you want to register a new key function that is invoked for each element in the array, use the nest.key() function. Pass the key as the parameter in the form of accessor.

Use the previous array of “cases” objects and pass the case_priority as the first key and case_source as the second key.

<script>

    // Create array of 5 objects
    let cases =[
      {case_source:"Phone", case_priority:"Medium", subject:"Radio issue"},
      {case_source:"Email", case_priority:"High", subject:"Mobile issue"},
      {case_source:"Web", case_priority:"Medium", subject:"Power Outage"},
      {case_source:"Phone", case_priority:"High", subject:"Battery drained"},
      {case_source:"Email", case_priority:"High", subject:"TV not working"}
    ]
 

     // nest.key(case_priority).key(case_source)
  let res = d3.nest().key((i)=>{return i.case_priority}).key((i)=>{return i.case_source}).entries(cases)

  console.log(res);

</script>

Output:

  • The first nest holds two arrays with the “Medium” and “High” keys.

  • Let’s expand the first key. The following nest is displayed. For the “Medium” (case priority) key, there are two nested keys – “Phone” (this holds one object) and “Web” (this holds one object).

  • Let’s expand the second key. The following nest is displayed. For the “High” key, there are two nested keys – “Email” (this holds two objects) and “Phone” (this holds one object).

Nest.map(Array)

It is possible to generate a map from the nested pairs. Here, all the entries in the map (returned) correspond to a distinct key value that is returned by the first key function.

Use the previous array of the “cases” objects and create a map using nest.map() by passing “cases” as the input parameter to it.

<script>

    // Create array of 5 objects
    let cases =[
      {case_source:"Phone", case_priority:"Medium", subject:"Radio issue"},
      {case_source:"Email", case_priority:"High", subject:"Mobile issue"},
      {case_source:"Web", case_priority:"Medium", subject:"Power Outage"},
      {case_source:"Phone", case_priority:"High", subject:"Battery drained"},
      {case_source:"Email", case_priority:"High", subject:"TV not working"}
    ]
   
    // nest.map(cases)
    let res = d3.nest().key((i)=>{return i.case_source}).map(cases)
    console.log(res);

Output:

Here, the map is generated based on the case_source key. The first key which is “Email” holds an array with two objects and the second key which is “Phone” holds an array with two objects. The last key which is “Web” holds an array with one object.

Nest.object(Array)

It is possible to generate an object from the nested pairs. Here, all the entries in the associative array (returned) correspond to a distinct key value that is returned by the first key function.

Utilize the cases and generate an object using the nest.object() function by passing “cases” as the input parameter to it. Include the key as “case_priority”.

<script>

    // Create array of 5 objects
    let cases =[
      {case_source:"Phone", case_priority:"Medium", subject:"Radio issue"},
      {case_source:"Email", case_priority:"High", subject:"Mobile issue"},
      {case_source:"Web", case_priority:"Medium", subject:"Power Outage"},
      {case_source:"Phone", case_priority:"High", subject:"Battery drained"},
      {case_source:"Email", case_priority:"High", subject:"TV not working"}
    ]
   
    // nest.object(cases)
    let res = d3.nest().key((i)=>{return i.case_priority}).object(cases)
    console.log(res);

</script>

Output:

Here, the object is generated based on the case_priority key. The first key which is “Medium” holds an array with three objects and the second key which is “High” holds an array with two objects.

Nest.sortKeys(Comparator)

We can sort the resultant array of the key-value entries using the sortKeys() function. The “comparator” parameter takes the function to sort the keys in the nest. We can use the “d3.ascending” to sort in the ascending order. Similarly, the “d3.descending” is used to sort the array of the key-value entries in descending order.

Utilize the “cases” data, pass the case_source as a key, and generate the nests by passing “cases” to the entries() function. Sort the results based on keys in ascending and descending order separately by passing the “d3.ascending” and “d3.descending” comparators.

<script>

    // Create array of 5 objects
    let cases =[
      {case_source:"Phone", case_priority:"Medium", subject:"Radio issue"},
      {case_source:"Email", case_priority:"High", subject:"Mobile issue"},
      {case_source:"Web", case_priority:"Medium", subject:"Power Outage"},
      {case_source:"Phone", case_priority:"High", subject:"Battery drained"},
      {case_source:"Email", case_priority:"High", subject:"TV not working"}
    ]
   
    // nest.sortKeys(d3.ascending)
    let res1 = d3.nest().key(function(i) { return i.case_source; }).sortKeys(d3.ascending).entries(cases);
    console.log(res1);
   
    // nest.sortKeys(d3.descending)
    let res2 = d3.nest().key(function(i) { return i.case_source; }).sortKeys(d3.descending).entries(cases);
    console.log(res2);

</script>

Output:

Bonus: Implementation in Observable Notebook

Until now, we implement the examples in the HTML web browser. The Observable Notebook can be used to implement it directly and it is a free and user-interactive tool.

Let’s create an array of the “cases” objects in the first cell.

cases =[
      {case_source:"Phone", case_priority:"Medium", subject:"Radio issue"},
      {case_source:"Email", case_priority:"High", subject:"Mobile issue"},
      {case_source:"Web", case_priority:"Medium", subject:"Power Outage"},
      {case_source:"Phone", case_priority:"High", subject:"Battery drained"},
      {case_source:"Email", case_priority:"High", subject:"TV not working"}
    ]

Output:

nest.entries() : Pass the “case_priority” key accessor to the key and entries() takes “cases” as a parameter.

d3.nest().key(i => i.case_priority).entries(cases)

Output:

Return the array with two objects. The first object holds three arrays again as the values with the “Medium” key and the second object holds two arrays again as the values with the “High” key.

  1. nest.map() : Pass the “case_priority” key accessor to the key and map() takes “cases” as a parameter. This creates a map.
d3.nest().key(i => i.case_priority).map(cases)

Output:

Map is returned with two keys. The first key which is “Medium” holds an array of two objects and the second key which is “High” holds an array of three objects.

  1. nest.object() : Pass the “case_source” key accessor to the key and object() takes “cases” as a parameter. This creates a map.
d3.nest().key(i => i.case_source).object(cases)

Output:

Object is returned with three keys. The first key which is “Phone” holds an array of two objects, the second key which is “Email” holds an array of two objects, and the last key holds “Web” with an array of one object.

  1. nest.sortKeys() : Sort the keys of the returning array of values in descending order. Here, the key is “case_source”.
d3.nest().key(i => i.case_source).sortKeys(d3.descending).entries(cases)

Output:

You can see that an array of objects is sorted in descending order based on keys.

Conclusion

In this guide, we learned how to create nests in D3.js and the different functions related to it with examples. Nesting in D3.js allows the elements in an array of objects to be grouped into a hierarchical tree structure. Then, we implemented the same examples in the Observable Notebook. Refer to this Notebook related to nests.

Share Button

Source: linuxhint.com

Leave a Reply