npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-d3-unmanaged-wrapper

v0.2.2

Published

React component that wraps d3 code inside a div or svg container.

Downloads

9

Readme

react-d3-unmanaged-wrapper travis npm

React component that uses Mike Bostock's reusable charts pattern to render d3 code inside a <svg> or <div> container. The DOM elements created with d3 are unmanaged: the React component is unaware of the contents of the container and will not touch those elements.

Example

Create your d3 drawing code following the reusable charts pattern. For example, create a file ./d3fn.js:

/**
 * Follow Mike Bostock's "reusable charts" pattern returning a closure
 * to (re-)draw the d3 content.
 *
 * The outer function sets up private variables like width, height, etc.
 * and returns the inner function (closure) to draw and update the chart.
 *
 * @return {Function}   drawing function that needs to handle both initial
 *                      drawing and updating of the chart.
 */
const d3fn = function() {
  // --- define private variables
  let width;  
  let height;

  // --- set up one-time elements like scales here...

  /**
   * The inner function generates and updates the chart.
   *
   * @param selection {d3.selection}   selection containing a chart
   */
  function inner(selection) {
    selection.each(function(data) {
      // generate chart here: `data` is the data and `this` is the container element
    });
  };

  // --- getter-setter functions here

  inner.width = function(value) {
    if (!arguments.length) return width;
    width = value;
    return inner;
  };

  inner.height = function(value) {
    if (!arguments.length) return height;
    height = value;
    return inner;
  };

  return inner;
}

module.exports = d3fn;

Now set up and render the D3 component, e.g. in ./index.js:


const ReactDOM = require('react-dom');
const D3Component = require('react-d3-unmanaged-wrapper');
const d3fn = require('./d3fn');

const options = {
  width: 800,
  height: 200,
  renderMode: 'svg',
  d3fn: d3fn,
  data: [14, 19, 25, 6, 9, 24, 31]
};

// instantiate component and pass in width, height, data and d3 function
const chart = <D3Component {...options} />;

// render in DOM (or embed in other components)
ReactDOM.render(chart, document.body);

React and D3

There are several different ways to combine d3 and React code. The issue is that they both manipulate the DOM in different ways. React uses a virtual DOM, and an efficient diff-ing algorithm, and updates only the DOM nodes that actually need to change. d3 accesses the DOM directly, and through data binding updates the DOM nodes that have been modified.

The approach taken here is quite simple. React sets up (and "owns") the container but considers the elements inside the container unmanaged and leaves their handling to d3. This is achieved by calling the d3 drawing function on componentDidMount (for initial setup) and componentDidUpdate (for redrawing).

License

Apache 2.0