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

d3-react

v1.0.4

Published

Render React elements with D3

Downloads

36

Readme

d3-react npm version Build Status js-standard-style

WARNING: Depreciated in favour of react-faux-dom, my other (better) approach to using D3 with React.

Render React elements with D3 declaratively and without state, as it should be.

Usage

This plugin essentially allows you to use D3 as your React render function without letting D3 mutate any existing DOM. You build the entire result from scratch on each render and then let React reconcile the DOM / SVG. Here's a simple chart built with the plugin.

var Graph = React.createClass({
  propTypes: {
    data: React.PropTypes.arrayOf(React.PropTypes.number)
  },
  render: function () {
    var chart = d3.select(document.createElement('div'))

    chart
      .selectAll('.bar')
      .data(this.props.data)
      .enter().append('div')
      .prop({
        className: 'bar',
        key: function (d, i) {
          return i
        },
        style: function (d, i) {
          return {
            width: d * 10
          }
        }
      })
      .text(function (d) {
        return d
      })

    return chart.toReact()
  }
})

var data = [4, 8, 15, 16, 23, 42]

React.render(
  React.createElement(Graph, {data: data}),
  document.getElementById('mount-chart')
)

As you can see, I'm using prop in place of attr and toReact at the end to build the React DOM. I'm also specifying a key so React knows which element is which. prop works just like attr so you can give it an object, key/value or key/function.

This script depends upon D3 and React, so make sure they're available within your application. It's wrapped in a UMD, so you should be able to use it with most module systems. You may need to configure your build tool to not include multiple versions of React and D3 in your final script bundle.

Why?

This was born from trying to use these two excellent tools together, but not liking how the existing bridges were executed (react-d3-wrap, for example). The wrap approach works, but I'd much rather declare my view and let React work out what to render.

This repository is a polished version of my experiment. The main drawback is that you can't use some normal D3 features, such as animations or anything else that mutates the DOM after it's rendered. You can however now write modular React components and animate the elements they produce. It's a trade off, you're sacrificing parts of D3 for simplicity and a more React-like approach.

The main driving factor was to make building great React / D3 things at Qubit easier. Qubit is awesome.

Development

# Fetch the dependencies
make bootstrap

# Test
make test

# Test continually
make test-watch

Author

Oliver Caldwell (@OliverCaldwell)

Unlicenced

Find the full unlicense in the UNLICENSE file, but here's a snippet.

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

Do what you want. Learn as much as you can. Unlicense more software.