2018-07-11 15:23:16 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-08-08 11:00:45 +00:00
|
|
|
import { Component, createRef } from '@wordpress/element';
|
2018-11-30 01:01:00 +00:00
|
|
|
import { isEqual } from 'lodash';
|
2018-07-11 15:23:16 +00:00
|
|
|
import { select as d3Select } from 'd3-selection';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides foundation to use D3 within React.
|
|
|
|
*
|
|
|
|
* React is responsible for determining when a chart should be updated (e.g. whenever data changes or the browser is
|
|
|
|
* resized), while D3 is responsible for the actual rendering of the chart (which is performed via DOM operations that
|
|
|
|
* happen outside of React's control).
|
|
|
|
*
|
|
|
|
* This component makes use of new lifecycle methods that come with React 16.3. Thus, while this component (i.e. the
|
|
|
|
* container of the chart) is rendered during the 'render phase' the chart itself is only rendered during the 'commit
|
|
|
|
* phase' (i.e. in 'componentDidMount' and 'componentDidUpdate' methods).
|
|
|
|
*/
|
|
|
|
export default class D3Base extends Component {
|
2018-11-30 01:01:00 +00:00
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
2018-09-11 11:04:26 +00:00
|
|
|
|
2018-11-30 01:01:00 +00:00
|
|
|
this.chartRef = createRef();
|
2018-07-11 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-11-30 01:01:00 +00:00
|
|
|
this.drawUpdatedChart();
|
2018-07-11 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 01:01:00 +00:00
|
|
|
shouldComponentUpdate( nextProps ) {
|
2018-07-11 15:23:16 +00:00
|
|
|
return (
|
2018-11-30 01:01:00 +00:00
|
|
|
this.props.className !== nextProps.className ||
|
|
|
|
! isEqual( this.props.data, nextProps.data ) ||
|
|
|
|
this.props.drawChart !== nextProps.drawChart ||
|
|
|
|
this.props.height !== nextProps.height ||
|
|
|
|
this.props.type !== nextProps.type ||
|
|
|
|
this.props.width !== nextProps.width
|
2018-07-11 15:23:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2018-11-30 01:01:00 +00:00
|
|
|
this.drawUpdatedChart();
|
2018-07-11 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.deleteChart();
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteChart() {
|
|
|
|
d3Select( this.chartRef.current )
|
|
|
|
.selectAll( 'svg' )
|
|
|
|
.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the chart, or triggers a rendering by updating the list of params.
|
|
|
|
*/
|
2018-11-30 01:01:00 +00:00
|
|
|
drawUpdatedChart() {
|
|
|
|
const { drawChart } = this.props;
|
2018-07-11 15:23:16 +00:00
|
|
|
const svg = this.getContainer();
|
2018-11-30 01:01:00 +00:00
|
|
|
drawChart( svg );
|
2018-07-11 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getContainer() {
|
2018-11-30 01:01:00 +00:00
|
|
|
const { className, height, width } = this.props;
|
2018-07-11 15:23:16 +00:00
|
|
|
|
|
|
|
this.deleteChart();
|
|
|
|
|
|
|
|
const svg = d3Select( this.chartRef.current )
|
|
|
|
.append( 'svg' )
|
|
|
|
.attr( 'viewBox', `0 0 ${ width } ${ height }` )
|
2018-11-15 15:27:55 +00:00
|
|
|
.attr( 'height', height )
|
|
|
|
.attr( 'width', width )
|
2018-07-11 15:23:16 +00:00
|
|
|
.attr( 'preserveAspectRatio', 'xMidYMid meet' );
|
|
|
|
|
|
|
|
if ( className ) {
|
|
|
|
svg.attr( 'class', `${ className }__viewbox` );
|
|
|
|
}
|
|
|
|
|
|
|
|
return svg.append( 'g' );
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className={ classNames( 'd3-base', this.props.className ) } ref={ this.chartRef } />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 01:01:00 +00:00
|
|
|
|
|
|
|
D3Base.propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
data: PropTypes.any, // required to detect changes in data
|
|
|
|
drawChart: PropTypes.func.isRequired,
|
|
|
|
type: PropTypes.string,
|
|
|
|
};
|