|
|
import clsx from 'clsx'; |
|
|
import { select as d3Select } from 'd3-selection'; |
|
|
import { isEmpty } from 'lodash'; |
|
|
import PropTypes from 'prop-types'; |
|
|
import { createRef, Component } from 'react'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default class D3Base extends Component { |
|
|
static propTypes = { |
|
|
className: PropTypes.string, |
|
|
data: PropTypes.any, |
|
|
drawChart: PropTypes.func.isRequired, |
|
|
getParams: PropTypes.func.isRequired, |
|
|
}; |
|
|
|
|
|
state = { |
|
|
data: null, |
|
|
params: null, |
|
|
drawChart: null, |
|
|
getParams: null, |
|
|
}; |
|
|
|
|
|
chartRef = createRef(); |
|
|
|
|
|
static getDerivedStateFromProps( nextProps, prevState ) { |
|
|
let state = {}; |
|
|
|
|
|
if ( nextProps.data !== prevState.data ) { |
|
|
state = { ...state, data: nextProps.data }; |
|
|
} |
|
|
|
|
|
if ( nextProps.drawChart !== prevState.drawChart ) { |
|
|
state = { ...state, drawChart: nextProps.drawChart }; |
|
|
} |
|
|
|
|
|
if ( nextProps.getParams !== prevState.getParams ) { |
|
|
state = { ...state, getParams: nextProps.getParams }; |
|
|
} |
|
|
|
|
|
if ( ! isEmpty( state ) ) { |
|
|
return { ...state, params: null }; |
|
|
} |
|
|
|
|
|
return null; |
|
|
} |
|
|
|
|
|
componentDidMount() { |
|
|
window.addEventListener( 'resize', this.updateParams ); |
|
|
|
|
|
this.drawChart(); |
|
|
} |
|
|
|
|
|
shouldComponentUpdate( nextProps, nextState ) { |
|
|
return ( |
|
|
( nextState.params !== null && this.state.params !== nextState.params ) || |
|
|
this.state.data !== nextState.data |
|
|
); |
|
|
} |
|
|
|
|
|
componentDidUpdate() { |
|
|
this.drawChart(); |
|
|
} |
|
|
|
|
|
componentWillUnmount() { |
|
|
window.removeEventListener( 'resize', this.updateParams ); |
|
|
|
|
|
this.deleteChart(); |
|
|
} |
|
|
|
|
|
deleteChart() { |
|
|
d3Select( this.chartRef.current ).selectAll( 'svg' ).remove(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
drawChart() { |
|
|
if ( ! this.state.params ) { |
|
|
this.updateParams(); |
|
|
return; |
|
|
} |
|
|
|
|
|
const svg = this.getContainer(); |
|
|
this.props.drawChart( svg, this.state.params ); |
|
|
} |
|
|
|
|
|
getContainer() { |
|
|
const { className } = this.props; |
|
|
const { width, height } = this.state.params; |
|
|
|
|
|
this.deleteChart(); |
|
|
|
|
|
const svg = d3Select( this.chartRef.current ) |
|
|
.append( 'svg' ) |
|
|
.attr( 'viewBox', `0 0 ${ width } ${ height }` ) |
|
|
.attr( 'preserveAspectRatio', 'xMidYMid meet' ); |
|
|
|
|
|
if ( className ) { |
|
|
svg.attr( 'class', `${ className }__viewbox` ); |
|
|
} |
|
|
|
|
|
return svg.append( 'g' ); |
|
|
} |
|
|
|
|
|
updateParams = () => { |
|
|
const params = this.state.getParams( this.chartRef.current ); |
|
|
this.setState( { params } ); |
|
|
}; |
|
|
|
|
|
render() { |
|
|
return <div className={ clsx( 'd3-base', this.props.className ) } ref={ this.chartRef } />; |
|
|
} |
|
|
} |
|
|
|