Created
September 23, 2018 12:33
-
-
Save max8hine/5c7f433cc474779a3432e8fba39f5b96 to your computer and use it in GitHub Desktop.
D3 + 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
// Barchart | |
// approach 1 - full feature integration | |
// D3 handles props and state, | |
// React handles rednering logic | |
class Barchart extends Component { | |
xScale = d3.scaleBand().paddingInner(0.1) | |
yScale = d3.scaleLinear() | |
// keep it synced | |
componentWillMount() { | |
this.updateD3(this.props) | |
} | |
componentWillUpdate(nextProps) { | |
this.updateD3(nextProps) | |
} | |
} | |
// Chart Axis | |
// approach 2 - blackbox approach (easier) | |
class Axis extends Component { | |
componentDidMount() { | |
this.renderAxis() | |
} | |
componentDidUpdate() { | |
this.renderAxis() | |
} | |
renderAxis() { | |
const scale = d3 | |
.scalelinear() | |
.domain([0, 10]) | |
.range([0, 200]) | |
const axis = d3.axisBottom(scale) | |
d3.select(this.refs.g).call(axis) | |
} | |
render() { | |
return <g transform='translate(10, 30)' ref='g'/> | |
} | |
} | |
// IN HOC | |
const d3Ele = function() { | |
const scale = d3 | |
.scaleLinear() | |
.domain([0, 10]) | |
.range([0, 200]) | |
const axis = d3.axisBottom(scale); | |
} | |
const Axis = D3Blackbox(d3Ele) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment