UNPKG

supergroup

Version:

Nested groups on arrays of objects where groups are Strings that know what you want them to know about themselves and their relatives.

149 lines (135 loc) 10.2 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl"> <title data-ice="title">API Document</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> </head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="./manual/index.html" data-ice="manualHeaderLink">Manual</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <a data-ice="repoURL" href="https://github.com/Sigfried/supergroup.git" class="repo-url-github">Repository</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> </header> <nav class="navigation" data-ice="nav"><div> <ul> </ul> </div> </nav> <div class="content" data-ice="content"><div data-ice="index" class="github-markdown"><h1 id="supergroup">Supergroup</h1> <p>Supergroup performs single- or multi-level grouping on collections of records. It provides a host of utitily and conveniece methods on the returned array of group values as well as on each of these specific group values. If a multi-level grouping is performed, each value&apos;s <code>children</code> array also acts as a Supergroup list.</p> <p>Supergroup is implemented as an Underscore or LoDash mixin, so just include one of those first:</p> <pre><code><code class="source-code prettyprint">&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js&quot;&gt;&lt;/script&gt; &lt;script src=&quot;https://rawgit.com/Sigfried/supergroup/master/supergroup.js&quot;&gt;&lt;/script&gt;</code> </code></pre><p>At first glance what Supergroup returns appears to be a list of String objects representing the top-level grouping. (Examples use a subset of <a href="https://github.com/Sigfried/supergroup/blob/master/examples/OlympicAthletes.csv">these Olympic athlete records</a>.)</p> <pre><code><code class="source-code prettyprint">sg = _.supergroup(data, [&apos;Country&apos;,&apos;Sport&apos;,&apos;Year&apos;]) // ==&gt; [&quot;United States&quot;,&quot;Russia&quot;,&quot;Australia&quot;] sg[0] // ==&gt; &quot;United States&quot;</code> </code></pre><h4 id="original-records-in-each-group-show-up-as-a-property-of-that-group-s-string-object-">Original records in each group show up as a property of that group&apos;s String object:</h4> <pre><code><code class="source-code prettyprint">sg[0].records.length // ==&gt; 210 sg[0].records.slice(0,2) // ==&gt; [ {&quot;Athlete&quot;:&quot;Michael Phelps&quot;,&quot;Age&quot;:&quot;23&quot;,&quot;Country&quot;:&quot;United States&quot;,&quot;Year&quot;:&quot;2008&quot;,&quot;Closing Ceremony Date&quot;:&quot;8/24/08&quot;,&quot;Sport&quot;:&quot;Swimming&quot;,&quot;Gold Medals&quot;:&quot;8&quot;,&quot;Silver Medals&quot;:&quot;0&quot;,&quot;Bronze Medals&quot;:&quot;0&quot;,&quot;Total Medals&quot;:&quot;8&quot;}, {&quot;Athlete&quot;:&quot;Michael Phelps&quot;,&quot;Age&quot;:&quot;19&quot;,&quot;Country&quot;:&quot;United States&quot;,&quot;Year&quot;:&quot;2004&quot;,&quot;Closing Ceremony Date&quot;:&quot;8/29/04&quot;,&quot;Sport&quot;:&quot;Swimming&quot;,&quot;Gold Medals&quot;:&quot;6&quot;,&quot;Silver Medals&quot;:&quot;0&quot;,&quot;Bronze Medals&quot;:&quot;2&quot;,&quot;Total Medals&quot;:&quot;8&quot;} ]</code> </code></pre><h4 id="and-subgroups-appear-in-a-children-property-">and subgroups appear in a children property:</h4> <pre><code><code class="source-code prettyprint">sg[0].children // ==&gt; [&quot;Swimming&quot;,&quot;Gymnastics&quot;,&quot;Diving&quot;,&quot;Wrestling&quot;,&quot;Weightlifting&quot;]</code> </code></pre><h4 id="aggregates">Aggregates</h4> <p>Unlike common data grouping/nesting utilities (D3.nest, Underscore.Nest) Supergroup gives you record subsets at every level, not just at the leaf level. No need to roll up subgroups for calculating aggregates at higher levels. Supergroup also provides convenience methods for aggregating:</p> <pre><code><code class="source-code prettyprint">sg[0].aggregate(d3.sum, &quot;Total Medals&quot;) // ==&gt; 352 sg[0].children[0].aggregate(d3.sum, &quot;Total Medals&quot;) // ==&gt; 267 sg.aggregates(d3.sum, &quot;Total Medals&quot;) // ==&gt; [352,157,180] sg.aggregates(d3.sum, &quot;Total Medals&quot;, &quot;dict&quot;) // ==&gt; {&quot;United States&quot;:352,&quot;Russia&quot;:157,&quot;Australia&quot;:180}</code> </code></pre><h4 id="metadata">Metadata</h4> <p>Supergroup remembers the dimension names used to produce groupings. And individual nodes contain a reference to the level they&#x2019;re on and to their parent values and lists:</p> <pre><code><code class="source-code prettyprint">sg[0].children[0].children[0] // ==&gt; 2000 sg[0].children[0].children[0].depth // (top level is 0) ==&gt; 2 sg[0].children[0].children[0].dim // ==&gt; &quot;Year&quot; sg[0].children[0].children[0].parent // ==&gt; &quot;Swimming&quot; sg[0].children[0].children[0].parentList // ==&gt; [2000,2004,2008,2012] sg[0].children[0].children[0].namePath() // ==&gt; &quot;United States/Swimming/2000&quot; sg[0].children[0].children[0].dimPath() // ==&gt; &quot;Country/Sport/Year&quot;</code> </code></pre><h4 id="lookup-descendants-leafnodes">lookup, descendants, leafNodes</h4> <p>Find nodes by looking up specific values. From any node, get all descendant or just leaf nodes:</p> <pre><code><code class="source-code prettyprint">sg.lookup([&quot;Russia&quot;,&quot;Swimming&quot;]) // ==&gt; &quot;Swimming&quot; sg.lookup(&quot;Russia&quot;).descendants() // ==&gt; [&quot;Gymnastics&quot;,2000,2004,2008,2012,&quot;Diving&quot;,2000,2004,2008,2012,&quot;Swimming&quot;,2000,2004,2008,2012,&quot;Wrestling&quot;,2000,2004,2008,2012,&quot;Weightlifting&quot;,2000,2004,2008,2012] sg.lookup(&quot;Russia&quot;).leafNodes() // ==&gt; [2000,2004,2008,2012,2000,2004,2008,2012,2000,2004,2008,2012,2000,2004,2008,2012,2000,2004,2008,2012]</code> </code></pre><h4 id="all-nodes-in-a-single-array-">All nodes in a single array:</h4> <pre><code><code class="source-code prettyprint">sg.flattenTree() // ==&gt; [&quot;United States&quot;,&quot;Swimming&quot;,2000,2004,2008,2012,&quot;Gymnastics&quot;,2000,2004,2008,2012,&quot;Diving&quot;,2000,2012,&quot;Wrestling&quot;,2000,2004,2008,2012,&quot;Weightlifting&quot;,2000,&quot;Russia&quot;,&quot;Gymnastics&quot;,2000,2004,2008,2012,&quot;Diving&quot;,2000,2004,2008,2012,&quot;Swimming&quot;,2000,2004,2008,2012,&quot;Wrestling&quot;,2000,2004,2008,2012,&quot;Weightlifting&quot;,2000,2004,2008,2012,&quot;Australia&quot;,&quot;Swimming&quot;,2000,2004,2008,2012,&quot;Diving&quot;,2000,2004,2008,2012] _.invoke(sg.flattenTree(), &quot;namePath&quot;) // ==&gt; [ &quot;United States&quot;, &quot;United States/Swimming&quot;, &quot;United States/Swimming/2000&quot;, &quot;United States/Swimming/2004&quot;, ...</code> </code></pre><h4 id="output-in-d3-nest-formats">Output in d3.nest formats</h4> <pre><code><code class="source-code prettyprint">sg.d3map() // ==&gt; { &quot;United States&quot;:{ &quot;Swimming&quot;:{ &quot;2000&quot;:[ {&quot;Athlete&quot;:&quot;Dara Torres&quot;,&quot;Age&quot;:&quot;33&quot;, ... {&quot;Athlete&quot;:&quot;Gary Hall Jr.&quot;,&quot;Age&quot;:&quot;25&quot;, ... ], &quot;2004&quot;:[ {&quot;Athlete&quot;:&quot;Michael Phelps&quot;,&quot;Age&quot;:&quot;19&quot;, ... sg.d3entries() // ==&gt; [ {&quot;key&quot;:&quot;United States&quot;,&quot;values&quot;:[ {&quot;key&quot;:&quot;Swimming&quot;,&quot;values&quot;:[ {&quot;key&quot;:&quot;2000&quot;,&quot;values&quot;:[ {&quot;Athlete&quot;:&quot;Dara Torres&quot;,&quot;Age&quot;:&quot;33&quot;, ...</code> </code></pre><h4 id="for-use-in-d3-hierarchy-layouts">For use in D3 hierarchy layouts</h4> <pre><code><code class="source-code prettyprint">// D3 hierarchies need a single root node root = sg.asRootVal(&quot;Olympics&quot;) // ==&gt; &quot;Olympics&quot; root.children // ==&gt; [&quot;United States&quot;,&quot;Russia&quot;,&quot;Australia&quot;] // normally leaf nodes are the bottom level grouping: _.invoke(root.leafNodes(),&apos;namePath&apos;) // ==&gt; [&quot;United States/Swimming/2000&quot;, &quot;United States/Swimming/2004&quot;, ... // but D3 hierachies need to have actual records as leaf nodes root.addRecordsAsChildrenToLeafNodes() // this adds a level to the grouping (changes sg also) _.invoke(root.leafNodes(),&apos;namePath&apos;) // ==&gt; [&quot;Olympics/United States/Swimming/2000/[object Object]&quot;, &quot;Olympics/United States/Swimming/2000/[object Object]&quot;] // it&apos;s now a 5-level hierarchy with a root node at top and original records at bottom</code> </code></pre><h4 id="multivalued-groups">Multivalued groups</h4> <pre><code><code class="source-code prettyprint">_.supergroup([{A:[1,2]}, {A:[2,3]}], &apos;A&apos;).d3map() // normal operation // ==&gt; { &quot;1,2&quot;: [{&quot;A&quot;:[1,2]}], &quot;2,3&quot;: [{&quot;A&quot;:[2,3]}] } _.supergroup([{A:[1,2]}, {A:[2,3]}], &apos;A&apos;,{multiValuedGroup:true}).d3map() // allow records to appear in more than one group // ==&gt; { &quot;1&quot;:[{&quot;A&quot;:[1,2]}], &quot;2&quot;:[{&quot;A&quot;:[1,2]},{&quot;A&quot;:[2,3]}], &quot;3&quot;:[{&quot;A&quot;:[2,3]}] }</code> </code></pre></div> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.4.3)</span></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>