woocommerce/plugins/woocommerce-admin/client/components/d3/base/index.js

97 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-05-22 13:00:06 +00:00
/** @format */
/**
* External dependencies
*/
import { Component } from '@wordpress/element';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { select as d3Select } from 'd3-selection';
import { isEqual } from 'lodash';
2018-05-22 13:00:06 +00:00
2018-06-15 18:11:25 +00:00
/**
* Internal dependencies
*/
import './style.scss';
2018-05-22 13:00:06 +00:00
class D3Base extends Component {
constructor() {
super( ...arguments );
this.updateParams = this.updateParams.bind( this );
this.setNodeRef = this.setNodeRef.bind( this );
this.state = {};
}
componentDidMount() {
window.addEventListener( 'resize', this.updateParams );
this.updateParams();
}
componentDidUpdate( prevProps ) {
if ( ! isEqual( prevProps, this.props ) ) {
this.updateParams( this.props );
this.draw();
}
2018-05-22 13:00:06 +00:00
}
componentWillUnmount() {
window.removeEventListener( 'resize', this.updateParams );
delete this.node;
}
updateParams( nextProps ) {
const getParams = ( nextProps && nextProps.getParams ) || this.props.getParams;
this.setState( getParams( this.node ), this.draw );
}
draw() {
this.props.drawChart( this.createNewContext(), this.state );
}
createNewContext() {
const { className } = this.props;
const { width, height } = this.state;
d3Select( this.node )
.selectAll( 'svg' )
.remove();
d3Select( this.node )
.selectAll( `.${ className }__tooltip` )
.remove();
const newNode = d3Select( this.node );
newNode
2018-05-22 13:00:06 +00:00
.append( 'svg' )
.attr( 'class', `${ className }__viewbox` )
.attr( 'viewBox', `0 0 ${ width } ${ height }` )
.attr( 'preserveAspectRatio', 'xMidYMid meet' )
.append( 'g' );
newNode
.append( 'div' )
.attr( 'class', `${ className }__tooltip tooltip` )
.style( 'display', 'none' );
2018-05-22 13:00:06 +00:00
return newNode;
}
setNodeRef( node ) {
this.node = node;
}
render() {
return (
<div className={ classNames( 'd3-base', this.props.className ) } ref={ this.setNodeRef } />
);
}
}
D3Base.propTypes = {
className: PropTypes.string,
drawChart: PropTypes.func.isRequired,
getParams: PropTypes.func.isRequired,
};
export default D3Base;