Giter Site home page Giter Site logo

d3-parent's Introduction

d3-parent

An inversion of d3.select which searches up through the DOM hierarchy instead of down, like a D3 equivalent of the .parents() method from jQuery. Provides a more stable and robust API around parentNode, simplifies traversal of deeply nested hierarchical data joins, and allows inverted manipulation of data binds.

demonstration

Setup

Load this script after D3.js but before your project code.

<html>
  <head>
    <script type="text/javascript" charset="utf-8" src="./d3.v3.js"></script>
    <script type="text/javascript" charset="utf-8" src="./d3-parent.js"></script>
    <script type="text/javascript" charset="utf-8" src="./project.js"></script>
  </head>

Usage

Call the .parent() method on any selection to return a selection of the parent nodes for each item:

  // probably returns a list node such as <ul> or <ol>
  // depending on the markup
  var list_items = d3.select('li');
  var list = list_items.parent();

Optionally provide a selector string to iterate upward through the DOM until a match is found:

  var list_items_links = d3.selectAll('a');
  var list = list_items.parent('ul');

Method calls can be chained together to traverse upward in steps:

  var list_items_links = d3.selectAll('a');
  var list = list_items.parent().parent();

Redundant node references are allowed in the results, and will be returned if you request them:

  var divs = d3.selectAll('div');
  // multiple references to the root node
  var parents = divs.parent('html');

To remove redundant node references, use selection.filter():

  var divs = d3.selectAll('div');
  // multiple references to the root node
  var parents = divs.parent('html');
  var nodes = [];
  // single reference to the root node
  var unique = parents.filter(function(d, i) {
    if (nodes.indexOf(this) === -1) {
      nodes.push(this);
      return true;
    } else {
      return false;
    }
  });

Notes

  • d3-parent can be thought of as a more robust API for parentNode, which is usually necessary for traversing upward in the DOM hierarchy, but is extremely fragile since it must be precisely matched to the document structure.
  • The group index argument is available in nested data joins to traverse upward in the bound data models, but it only moves up by one level. For deeply nested data join hierarchies, d3.parent will usually be a much simpler way to jump to a different level of data binding.
  • The parent() method provides an inverted equivalent to d3.select, but there is no equivalent method for d3.selectAll() which matches multiple parent nodes, because that would break grouping.
  • Existing group and item indices remain intact after DOM traversal, which allows you to programmatically operate on parent nodes using data bound to child nodes.

d3-parent's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

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.