Created
February 12, 2016 11:07
-
-
Save abuseofnotation/1844c47ffbb3ea9a7bd4 to your computer and use it in GitHub Desktop.
Integrating D3 within React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A function for integrating D3 and React. | |
* | |
* Receives a reusable D3 chart as per https://bost.ocks.org/mike/chart/. | |
* | |
* Returns a React component which Renders the chart and updates it | |
* using the 'data' property from its 'props' object. | |
*/ | |
// IMPLEMENTATION: | |
import {createClass, createElement, createFactory} from 'react' | |
import d3 from 'd3' | |
export default (d3Component) => { | |
const svg = createFactory('svg') | |
const renderD3 = (data) => (component) => { | |
d3.select(component).datum(data).call(d3Component) | |
} | |
return createClass({ | |
render () { | |
return svg({className:'graphic', ref:renderD3(this.props.data)}) | |
} | |
}) | |
} | |
//USAGE | |
//Configure the D3 chart | |
const myChart = chart().width(720).height(80); | |
//Use it in a layout | |
export default (props) => CreateElement(toD3(myChart), {data: props.chartData}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment