Git Product home page Git Product logo

d3blackbox's Introduction

d3blackbox

npm package

Take any D3 example you find in the wild and wrap it in a React component. Great for quick experiments and meeting deadlines. ๐Ÿ˜›

Meet your deadline in 2 minutes

Check out how it works in a live Codesandbox

  1. Install d3blackbox with npm
$ npm install -s d3blackbox
  1. Find a D3 example you like
  2. Copy its code, put it in a function
  3. Wrap function in D3blackbox
import React from "react";
import D3blackbox from "d3blackbox";
import * as d3 from "d3";

const MyDataviz = D3blackbox(function(anchor, props, state) {
    const svg = d3.select(anchor.current);

    // the rest of your D3 code
});

export default MyDataviz;
  1. Render inside an <svg></svg> element
  2. Enjoy your blackbox D3 component

D3blackbox renders an anchor element and delegates control to your render function. You get an anchor ref, the component's props, and state. Do what you want :)

Great for meeting deadlines and playing around with other people's code. Not recommended for large scale use due to performance constraints. Your render function runs on every component update and redraws the entire DOM subtree. React's rendering engine can't help you.

That's why it's called a sandbox.

What if I don't use D3?

That's okay. D3blackbox lets you delegate control to any rendering library you want. As long as you're okay rendering into a <g></g> element.

You can even use this approach to render Vue apps inside your React apps. ๐Ÿคจ

What if I don't want <g>?

That's okay you weirdo. Use the component argument to specify a different component.

render() {
    return <MyDataviz component="div" />
}

HOCs are so September, do you have hooks?

Yes. Hooks are alpha support and all that, but here's how you can use D3 blackbox as a React hook.

import { useD3 } from "d3blackbox";

function renderSomeD3(anchor) {
    d3.select(anchor);

    // ...
}

const MyD3Component = ({ x, y }) => {
    const refAnchor = useD3(anchor => renderSomeD3(anchor));

    return <g ref={refAnchor} transform={`translate(${x}, ${y})`} />;
};

With love โค๏ธ

Built with love by Swizec
Cheers

Only took me 2 years to get around to opensourcing this ๐Ÿ˜…

d3blackbox's People

Contributors

shawnhosea avatar swizec avatar

Watchers

 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.