├── .gitignore ├── .tern-project ├── .travis.yml ├── Makefile ├── README.md ├── UNLICENSE ├── package.json ├── src └── d3-react.js └── test ├── basic.js └── utils └── render.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /.tern-project: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": { 3 | "node": {} 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "stable" 4 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: default bootstrap test test-watch 2 | 3 | default: bootstrap test 4 | 5 | bootstrap: 6 | @npm install 7 | 8 | test: 9 | @npm test 10 | 11 | test-watch: 12 | @./node_modules/.bin/nodemon --exec "npm test" 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # d3-react [![npm version](https://badge.fury.io/js/d3-react.svg)](http://badge.fury.io/js/d3-react) [![Build Status](https://travis-ci.org/Olical/d3-react.svg?branch=master)](https://travis-ci.org/Olical/d3-react) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) 2 | 3 | # WARNING: Deprecated in favour of [react-faux-dom][], my other (better) approach to using D3 with React. 4 | 5 | Render [React][] elements with [D3][] declaratively and without state, as it should be. 6 | 7 | ## Usage 8 | 9 | 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. 10 | 11 | ```javascript 12 | var Graph = React.createClass({ 13 | propTypes: { 14 | data: React.PropTypes.arrayOf(React.PropTypes.number) 15 | }, 16 | render: function () { 17 | var chart = d3.select(document.createElement('div')) 18 | 19 | chart 20 | .selectAll('.bar') 21 | .data(this.props.data) 22 | .enter().append('div') 23 | .prop({ 24 | className: 'bar', 25 | key: function (d, i) { 26 | return i 27 | }, 28 | style: function (d, i) { 29 | return { 30 | width: d * 10 31 | } 32 | } 33 | }) 34 | .text(function (d) { 35 | return d 36 | }) 37 | 38 | return chart.toReact() 39 | } 40 | }) 41 | 42 | var data = [4, 8, 15, 16, 23, 42] 43 | 44 | React.render( 45 | React.createElement(Graph, {data: data}), 46 | document.getElementById('mount-chart') 47 | ) 48 | ``` 49 | 50 | 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. 51 | 52 | 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. 53 | 54 | ## Why? 55 | 56 | 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. 57 | 58 | This repository is a polished version of my [experiment][d3-lab]. 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. 59 | 60 | The main driving factor was to make building great React / D3 things at [Qubit][] easier. Qubit is awesome. 61 | 62 | ## Development 63 | 64 | ```bash 65 | # Fetch the dependencies 66 | make bootstrap 67 | 68 | # Test 69 | make test 70 | 71 | # Test continually 72 | make test-watch 73 | ``` 74 | 75 | ## Author 76 | 77 | [Oliver Caldwell][author-site] ([@OliverCaldwell][author-twitter]) 78 | 79 | ## Unlicenced 80 | 81 | Find the full [unlicense][] in the `UNLICENSE` file, but here's a snippet. 82 | 83 | >This is free and unencumbered software released into the public domain. 84 | > 85 | >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. 86 | 87 | Do what you want. Learn as much as you can. Unlicense more software. 88 | 89 | [unlicense]: http://unlicense.org/ 90 | [author-site]: http://oli.me.uk/ 91 | [author-twitter]: https://twitter.com/OliverCaldwell 92 | [d3]: http://d3js.org/ 93 | [react]: http://facebook.github.io/react/ 94 | [d3-lab]: http://lab.oli.me.uk/d3-to-react/ 95 | [react-d3-wrap]: https://www.npmjs.com/package/react-d3-wrap 96 | [qubit]: http://www.qubit.com/ 97 | [umd]: https://github.com/umdjs/umd 98 | [react-faux-dom]: https://github.com/Olical/react-faux-dom 99 | -------------------------------------------------------------------------------- /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "d3-react", 3 | "version": "1.0.4", 4 | "description": "Render React elements with D3", 5 | "main": "src/d3-react.js", 6 | "scripts": { 7 | "test": "standard && tape './test/**/*.js' | faucet" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/Olical/d3-react.git" 12 | }, 13 | "keywords": [ 14 | "d3", 15 | "react", 16 | "render", 17 | "graph", 18 | "chart" 19 | ], 20 | "author": { 21 | "name": "Oliver Caldwell", 22 | "email": "olliec87@gmail.com", 23 | "url": "http://oli.me.uk/" 24 | }, 25 | "license": "Unlicense", 26 | "bugs": { 27 | "url": "https://github.com/Olical/d3-react/issues" 28 | }, 29 | "homepage": "https://github.com/Olical/d3-react#readme", 30 | "devDependencies": { 31 | "d3": "^3.5.6", 32 | "faucet": "0.0.1", 33 | "jsdom": "^3.1.2", 34 | "nodemon": "^1.4.1", 35 | "react": "^0.13.3", 36 | "standard": "^5.1.0", 37 | "tape": "^4.2.0" 38 | }, 39 | "standard": { 40 | "globals": [ 41 | "define" 42 | ] 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/d3-react.js: -------------------------------------------------------------------------------- 1 | (function (root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | // AMD. Register as an anonymous module. 4 | define(['exports', 'd3', 'react'], factory) 5 | } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') { 6 | // CommonJS 7 | factory(exports, require('d3'), require('react')) 8 | } else { 9 | // Browser globals 10 | factory(null, root.d3, root.React) 11 | } 12 | }(this, function (exports, d3, React) { 13 | function getProps (el) { 14 | if (typeof el._reactProps === 'undefined') { 15 | el._reactProps = {} 16 | } 17 | 18 | return el._reactProps 19 | } 20 | 21 | function d3_selection_prop (name, value) { 22 | // For prop(string, null), remove the prop with the specified name. 23 | function removeProp () { 24 | delete getProps(this)[name] 25 | } 26 | 27 | // For prop(string, string), set the prop with the specified name. 28 | function setPropValue () { 29 | getProps(this)[name] = value 30 | } 31 | 32 | // For prop(string, function), evaluate the function for each element, and set 33 | // or remove the prop as appropriate. 34 | function setPropFunction () { 35 | var x = value.apply(this, arguments) 36 | if (x == null) removeProp() 37 | else getProps(this)[name] = x 38 | } 39 | 40 | return value == null 41 | ? removeProp : (typeof value === 'function' 42 | ? setPropFunction 43 | : setPropValue) 44 | } 45 | 46 | function d3_selection_toReact (node) { 47 | var elName = node.nodeName.toLowerCase() 48 | var children = Array.prototype.slice.call(node.childNodes) 49 | var props = getProps(node) 50 | 51 | if (node.nodeType === 3) { 52 | return node.textContent 53 | } else if (children.length === 0) { 54 | return React.createElement(elName, props) 55 | } else { 56 | return React.createElement(elName, props, children.map(d3_selection_toReact)) 57 | } 58 | } 59 | 60 | // Modified version of d3.selection.prototype.attr 61 | d3.selection.prototype.prop = function (name, value) { 62 | var node = this.node() 63 | 64 | if (arguments.length < 2) { 65 | // For prop(string), return the prop value for the first node. 66 | if (typeof name === 'string') { 67 | return getProps(node)[name] 68 | } 69 | 70 | // For prop(object), the object specifies the names and values of the getProps 71 | // to set or remove. The values may be functions that are evaluated for 72 | // each element. 73 | for (value in name) this.each(d3_selection_prop(value, name[value])) 74 | return this 75 | } 76 | 77 | return this.each(d3_selection_prop(name, value)) 78 | } 79 | 80 | d3.selection.prototype.toReact = function () { 81 | return d3_selection_toReact(this.node()) 82 | } 83 | })) 84 | -------------------------------------------------------------------------------- /test/basic.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var render = require('./utils/render') 3 | 4 | test('empty', function (t) { 5 | t.plan(1) 6 | render(function () {}, function (result) { 7 | t.equal(result.type, 'div', 'it built a div') 8 | }) 9 | }) 10 | 11 | test('with children', function (t) { 12 | t.plan(3) 13 | render(function (el) { 14 | el.selectAll('p') 15 | .data([1, 2, 3]) 16 | .enter() 17 | .append('p') 18 | .prop('key', function (d) { return d }) 19 | }, function (result) { 20 | t.equal(result.type, 'div', 'it built a div') 21 | 22 | var children = result._store.props.children 23 | t.equal(children[1].type, 'p', 'correct tags were added') 24 | t.equal(children[1].key, '2', 'tags have keys') 25 | }) 26 | }) 27 | 28 | test('with text', function (t) { 29 | t.plan(3) 30 | render(function (el) { 31 | el.append('p') 32 | .text('Hello, World!') 33 | .prop('key', 'some-text') 34 | }, function (result) { 35 | var tag = result._store.props.children[0] 36 | t.equal(tag.type, 'p', 'correct tags were added') 37 | t.equal(tag.key, 'some-text', 'tags has a key') 38 | t.equal(tag._store.props.children[0], 'Hello, World!', 'text is correct') 39 | }) 40 | }) 41 | 42 | test('depth of two', function (t) { 43 | t.plan(2) 44 | render(function (el) { 45 | el.append('p') 46 | .prop('key', 'one') 47 | .append('p') 48 | .prop('key', 'two') 49 | }, function (result) { 50 | var one = result._store.props.children[0] 51 | var two = one._store.props.children[0] 52 | 53 | t.equal(one.key, 'one', 'the first exists') 54 | t.equal(two.key, 'two', 'the second exists') 55 | }) 56 | }) 57 | -------------------------------------------------------------------------------- /test/utils/render.js: -------------------------------------------------------------------------------- 1 | var d3 = require('d3') 2 | var jsdom = require('jsdom') 3 | require('../../') 4 | 5 | function render (fn, cb) { 6 | jsdom.env('', function (errors, window) { 7 | var el = d3.select(window.document.createElement('div')) 8 | fn(el) 9 | cb(el.toReact()) 10 | }) 11 | } 12 | 13 | module.exports = render 14 | --------------------------------------------------------------------------------