Skip to content

Instantly share code, notes, and snippets.

@max8hine
Created September 23, 2018 12:33
Show Gist options
  • Save max8hine/5c7f433cc474779a3432e8fba39f5b96 to your computer and use it in GitHub Desktop.
Save max8hine/5c7f433cc474779a3432e8fba39f5b96 to your computer and use it in GitHub Desktop.
D3 + React
// 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