2018-06-15 18:11:25 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-08-13 10:19:32 +00:00
|
|
|
import { isEqual } from 'lodash';
|
2018-08-08 11:00:45 +00:00
|
|
|
import { Component, createRef } from '@wordpress/element';
|
2018-06-15 18:11:25 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classNames from 'classnames';
|
2018-09-10 12:57:36 +00:00
|
|
|
import { timeFormat as d3TimeFormat, utcParse as d3UTCParse } from 'd3-time-format';
|
2018-07-25 14:58:32 +00:00
|
|
|
import { select as d3Select } from 'd3-selection';
|
2018-06-15 18:11:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2018-07-11 15:23:16 +00:00
|
|
|
import D3Base from './d3-base';
|
2018-07-05 12:18:02 +00:00
|
|
|
import {
|
|
|
|
drawAxis,
|
|
|
|
drawBars,
|
|
|
|
drawLines,
|
|
|
|
getDateSpaces,
|
|
|
|
getOrderedKeys,
|
|
|
|
getLine,
|
|
|
|
getLineData,
|
2018-09-14 17:43:53 +00:00
|
|
|
getXTicks,
|
2018-07-05 12:18:02 +00:00
|
|
|
getUniqueKeys,
|
|
|
|
getUniqueDates,
|
|
|
|
getXScale,
|
|
|
|
getXGroupScale,
|
|
|
|
getXLineScale,
|
|
|
|
getYMax,
|
|
|
|
getYScale,
|
|
|
|
getYTickOffset,
|
|
|
|
} from './utils';
|
2018-06-15 18:11:25 +00:00
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A simple D3 line and bar chart component for timeseries data in React.
|
|
|
|
*/
|
2018-07-11 15:23:16 +00:00
|
|
|
class D3Chart extends Component {
|
2018-08-08 11:00:45 +00:00
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
2018-08-31 17:27:21 +00:00
|
|
|
this.drawChart = this.drawChart.bind( this );
|
2018-08-08 11:00:45 +00:00
|
|
|
this.getAllData = this.getAllData.bind( this );
|
2018-08-31 17:27:21 +00:00
|
|
|
this.getParams = this.getParams.bind( this );
|
2018-08-08 11:00:45 +00:00
|
|
|
this.state = {
|
|
|
|
allData: this.getAllData( props ),
|
2018-09-11 11:04:26 +00:00
|
|
|
type: props.type,
|
2018-08-08 11:00:45 +00:00
|
|
|
width: props.width,
|
|
|
|
};
|
|
|
|
this.tooltipRef = createRef();
|
|
|
|
}
|
2018-07-11 15:23:16 +00:00
|
|
|
|
2018-08-08 11:00:45 +00:00
|
|
|
componentDidUpdate( prevProps, prevState ) {
|
2018-09-11 11:04:26 +00:00
|
|
|
const { type, width } = this.props;
|
2018-08-08 11:00:45 +00:00
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
if ( width !== prevProps.width ) {
|
|
|
|
this.setState( { width } );
|
2018-07-11 15:23:16 +00:00
|
|
|
}
|
2018-08-08 11:00:45 +00:00
|
|
|
const nextAllData = this.getAllData( this.props );
|
|
|
|
if ( ! isEqual( [ ...nextAllData ].sort(), [ ...prevState.allData ].sort() ) ) {
|
|
|
|
this.setState( { allData: nextAllData } );
|
|
|
|
}
|
2018-09-11 11:04:26 +00:00
|
|
|
if ( type !== prevProps.type ) {
|
|
|
|
this.setState( { type } );
|
|
|
|
}
|
2018-08-08 11:00:45 +00:00
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
|
|
|
}
|
2018-07-11 15:23:16 +00:00
|
|
|
|
2018-08-08 11:00:45 +00:00
|
|
|
getAllData( props ) {
|
|
|
|
const orderedKeys =
|
|
|
|
props.orderedKeys || getOrderedKeys( props.data, getUniqueKeys( props.data ) );
|
|
|
|
return [ ...props.data, ...orderedKeys ];
|
2018-07-11 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
drawChart( node, params ) {
|
2018-08-13 10:19:32 +00:00
|
|
|
const { data, margin, type } = this.props;
|
2018-07-03 14:44:10 +00:00
|
|
|
const g = node
|
|
|
|
.attr( 'id', 'chart' )
|
2018-06-15 18:11:25 +00:00
|
|
|
.append( 'g' )
|
|
|
|
.attr( 'transform', `translate(${ margin.left },${ margin.top })` );
|
|
|
|
|
|
|
|
const adjParams = Object.assign( {}, params, {
|
|
|
|
height: params.height - margin.top - margin.bottom,
|
|
|
|
width: params.width - margin.left - margin.right,
|
2018-07-25 14:58:32 +00:00
|
|
|
tooltip: d3Select( this.tooltipRef.current ),
|
2018-10-19 13:23:47 +00:00
|
|
|
valueType: params.valueType,
|
2018-06-15 18:11:25 +00:00
|
|
|
} );
|
2018-08-12 17:01:10 +00:00
|
|
|
drawAxis( g, adjParams );
|
2018-07-11 15:23:16 +00:00
|
|
|
type === 'line' && drawLines( g, data, adjParams );
|
|
|
|
type === 'bar' && drawBars( g, data, adjParams );
|
|
|
|
|
2018-07-03 14:44:10 +00:00
|
|
|
return node;
|
2018-08-31 17:27:21 +00:00
|
|
|
}
|
2018-06-15 18:11:25 +00:00
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
getParams( node ) {
|
2018-09-05 20:52:35 +00:00
|
|
|
const {
|
|
|
|
colorScheme,
|
|
|
|
data,
|
2018-09-10 12:57:36 +00:00
|
|
|
dateParser,
|
2018-09-05 20:52:35 +00:00
|
|
|
height,
|
2018-09-14 17:43:53 +00:00
|
|
|
layout,
|
2018-10-17 13:44:43 +00:00
|
|
|
interval,
|
2018-09-05 20:52:35 +00:00
|
|
|
margin,
|
2018-09-25 09:42:08 +00:00
|
|
|
mode,
|
2018-09-05 20:52:35 +00:00
|
|
|
orderedKeys,
|
2018-09-24 11:11:18 +00:00
|
|
|
pointLabelFormat,
|
2018-09-05 20:52:35 +00:00
|
|
|
tooltipFormat,
|
2018-09-20 14:28:22 +00:00
|
|
|
tooltipTitle,
|
2018-09-05 20:52:35 +00:00
|
|
|
type,
|
|
|
|
xFormat,
|
2018-09-07 10:28:02 +00:00
|
|
|
x2Format,
|
2018-09-05 20:52:35 +00:00
|
|
|
yFormat,
|
2018-10-19 13:23:47 +00:00
|
|
|
valueType,
|
2018-09-05 20:52:35 +00:00
|
|
|
} = this.props;
|
2018-08-08 11:00:45 +00:00
|
|
|
const { width } = this.state;
|
2018-06-15 18:11:25 +00:00
|
|
|
const calculatedWidth = width || node.offsetWidth;
|
|
|
|
const calculatedHeight = height || node.offsetHeight;
|
2018-07-05 12:18:02 +00:00
|
|
|
const scale = width / node.offsetWidth;
|
|
|
|
const adjHeight = calculatedHeight - margin.top - margin.bottom;
|
|
|
|
const adjWidth = calculatedWidth - margin.left - margin.right;
|
|
|
|
const uniqueKeys = getUniqueKeys( data );
|
2018-08-12 17:01:10 +00:00
|
|
|
const newOrderedKeys = orderedKeys || getOrderedKeys( data, uniqueKeys );
|
|
|
|
const lineData = getLineData( data, newOrderedKeys );
|
2018-07-05 12:18:02 +00:00
|
|
|
const yMax = getYMax( lineData );
|
|
|
|
const yScale = getYScale( adjHeight, yMax );
|
2018-09-10 12:57:36 +00:00
|
|
|
const parseDate = d3UTCParse( dateParser );
|
|
|
|
const uniqueDates = getUniqueDates( lineData, parseDate );
|
2018-07-05 12:18:02 +00:00
|
|
|
const xLineScale = getXLineScale( uniqueDates, adjWidth );
|
|
|
|
const xScale = getXScale( uniqueDates, adjWidth );
|
2018-10-17 13:44:43 +00:00
|
|
|
const xTicks = getXTicks( uniqueDates, adjWidth, layout, interval );
|
2018-06-15 18:11:25 +00:00
|
|
|
return {
|
2018-08-13 10:19:32 +00:00
|
|
|
colorScheme,
|
2018-10-17 18:08:34 +00:00
|
|
|
dateSpaces: getDateSpaces( data, uniqueDates, adjWidth, xLineScale ),
|
2018-06-15 18:11:25 +00:00
|
|
|
height: calculatedHeight,
|
2018-08-13 10:19:32 +00:00
|
|
|
line: getLine( xLineScale, yScale ),
|
2018-07-05 12:18:02 +00:00
|
|
|
lineData,
|
2018-06-15 18:11:25 +00:00
|
|
|
margin,
|
2018-09-25 09:42:08 +00:00
|
|
|
mode,
|
2018-07-11 15:23:16 +00:00
|
|
|
orderedKeys: newOrderedKeys,
|
2018-09-24 11:11:18 +00:00
|
|
|
pointLabelFormat,
|
2018-09-10 12:57:36 +00:00
|
|
|
parseDate,
|
2018-07-05 12:18:02 +00:00
|
|
|
scale,
|
2018-09-05 20:52:35 +00:00
|
|
|
tooltipFormat: d3TimeFormat( tooltipFormat ),
|
2018-09-20 14:28:22 +00:00
|
|
|
tooltipTitle,
|
2018-07-03 14:44:10 +00:00
|
|
|
type,
|
2018-07-05 12:18:02 +00:00
|
|
|
uniqueDates,
|
|
|
|
uniqueKeys,
|
|
|
|
width: calculatedWidth,
|
2018-07-10 12:46:57 +00:00
|
|
|
xFormat: d3TimeFormat( xFormat ),
|
2018-09-07 10:28:02 +00:00
|
|
|
x2Format: d3TimeFormat( x2Format ),
|
2018-07-05 12:18:02 +00:00
|
|
|
xGroupScale: getXGroupScale( orderedKeys, xScale ),
|
|
|
|
xLineScale,
|
2018-09-14 17:43:53 +00:00
|
|
|
xTicks,
|
2018-07-05 12:18:02 +00:00
|
|
|
xScale,
|
|
|
|
yMax,
|
|
|
|
yScale,
|
|
|
|
yTickOffset: getYTickOffset( adjHeight, scale, yMax ),
|
2018-09-05 20:52:35 +00:00
|
|
|
yFormat,
|
2018-10-19 13:23:47 +00:00
|
|
|
valueType,
|
2018-06-15 18:11:25 +00:00
|
|
|
};
|
2018-08-31 17:27:21 +00:00
|
|
|
}
|
2018-06-15 18:11:25 +00:00
|
|
|
|
2018-07-11 15:23:16 +00:00
|
|
|
render() {
|
|
|
|
if ( ! this.props.data ) {
|
|
|
|
return null; // TODO: improve messaging
|
|
|
|
}
|
|
|
|
return (
|
2018-07-31 08:34:02 +00:00
|
|
|
<div
|
|
|
|
className={ classNames( 'woocommerce-chart__container', this.props.className ) }
|
2018-08-08 11:00:45 +00:00
|
|
|
style={ { height: this.props.height } }
|
2018-07-31 08:34:02 +00:00
|
|
|
>
|
2018-07-25 14:58:32 +00:00
|
|
|
<D3Base
|
|
|
|
className={ classNames( this.props.className ) }
|
|
|
|
data={ this.state.allData }
|
|
|
|
drawChart={ this.drawChart }
|
|
|
|
getParams={ this.getParams }
|
2018-09-11 11:04:26 +00:00
|
|
|
type={ this.state.type }
|
2018-08-08 11:00:45 +00:00
|
|
|
width={ this.state.width }
|
2018-07-25 14:58:32 +00:00
|
|
|
/>
|
|
|
|
<div className="tooltip" ref={ this.tooltipRef } />
|
|
|
|
</div>
|
2018-07-11 15:23:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-06-15 18:11:25 +00:00
|
|
|
|
2018-08-08 11:00:45 +00:00
|
|
|
D3Chart.propTypes = {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Additional CSS classes.
|
|
|
|
*/
|
2018-08-08 11:00:45 +00:00
|
|
|
className: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A chromatic color function to be passed down to d3.
|
|
|
|
*/
|
|
|
|
colorScheme: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* An array of data.
|
|
|
|
*/
|
2018-08-08 11:00:45 +00:00
|
|
|
data: PropTypes.array.isRequired,
|
2018-09-10 12:57:36 +00:00
|
|
|
/**
|
|
|
|
* Format to parse dates into d3 time format
|
|
|
|
*/
|
|
|
|
dateParser: PropTypes.string.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Relative viewpoirt height of the `svg`.
|
|
|
|
*/
|
2018-08-08 11:00:45 +00:00
|
|
|
height: PropTypes.number,
|
2018-09-05 20:52:35 +00:00
|
|
|
/**
|
|
|
|
* Interval specification (hourly, daily, weekly etc.)
|
|
|
|
*/
|
2018-09-04 18:03:52 +00:00
|
|
|
interval: PropTypes.oneOf( [ 'hour', 'day', 'week', 'month', 'quarter', 'year' ] ),
|
2018-09-14 17:43:53 +00:00
|
|
|
/**
|
|
|
|
* `standard` (default) legend layout in the header or `comparison` moves legend layout
|
|
|
|
* to the left or 'compact' has the legend below
|
|
|
|
*/
|
|
|
|
layout: PropTypes.oneOf( [ 'standard', 'comparison', 'compact' ] ),
|
2018-09-24 11:11:18 +00:00
|
|
|
/**
|
|
|
|
* Date format of the point labels (might be used in tooltips and ARIA properties).
|
|
|
|
*/
|
|
|
|
pointLabelFormat: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Margins for axis and chart padding.
|
|
|
|
*/
|
2018-08-08 11:00:45 +00:00
|
|
|
margin: PropTypes.shape( {
|
|
|
|
bottom: PropTypes.number,
|
|
|
|
left: PropTypes.number,
|
|
|
|
right: PropTypes.number,
|
|
|
|
top: PropTypes.number,
|
|
|
|
} ),
|
2018-09-25 09:42:08 +00:00
|
|
|
/**
|
|
|
|
* `items-comparison` (default) or `time-comparison`, this is used to generate correct
|
|
|
|
* ARIA properties.
|
|
|
|
*/
|
|
|
|
mode: PropTypes.oneOf( [ 'item-comparison', 'time-comparison' ] ),
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* The list of labels for this chart.
|
|
|
|
*/
|
2018-08-08 11:00:45 +00:00
|
|
|
orderedKeys: PropTypes.array,
|
2018-09-05 20:52:35 +00:00
|
|
|
/**
|
2018-09-20 14:28:22 +00:00
|
|
|
* A datetime formatting string to format the date displayed as the title of the toolip
|
|
|
|
* if `tooltipTitle` is missing, passed to d3TimeFormat.
|
2018-09-05 20:52:35 +00:00
|
|
|
*/
|
|
|
|
tooltipFormat: PropTypes.string,
|
2018-09-20 14:28:22 +00:00
|
|
|
/**
|
|
|
|
* A string to use as a title for the tooltip. Takes preference over `tooltipFormat`.
|
|
|
|
*/
|
|
|
|
tooltipTitle: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Chart type of either `line` or `bar`.
|
|
|
|
*/
|
2018-08-08 11:00:45 +00:00
|
|
|
type: PropTypes.oneOf( [ 'bar', 'line' ] ),
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Relative viewport width of the `svg`.
|
|
|
|
*/
|
2018-08-08 11:00:45 +00:00
|
|
|
width: PropTypes.number,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A datetime formatting string, passed to d3TimeFormat.
|
|
|
|
*/
|
2018-08-08 11:00:45 +00:00
|
|
|
xFormat: PropTypes.string,
|
2018-09-07 10:28:02 +00:00
|
|
|
/**
|
|
|
|
* A datetime formatting string, passed to d3TimeFormat.
|
|
|
|
*/
|
|
|
|
x2Format: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A number formatting string, passed to d3Format.
|
|
|
|
*/
|
2018-08-08 11:00:45 +00:00
|
|
|
yFormat: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
D3Chart.defaultProps = {
|
2018-08-12 17:01:10 +00:00
|
|
|
data: [],
|
2018-09-10 12:57:36 +00:00
|
|
|
dateParser: '%Y-%m-%dT%H:%M:%S',
|
2018-08-08 11:00:45 +00:00
|
|
|
height: 200,
|
|
|
|
margin: {
|
|
|
|
bottom: 30,
|
|
|
|
left: 40,
|
|
|
|
right: 0,
|
|
|
|
top: 20,
|
|
|
|
},
|
2018-09-14 17:43:53 +00:00
|
|
|
layout: 'standard',
|
2018-09-25 09:42:08 +00:00
|
|
|
mode: 'item-comparison',
|
2018-09-20 14:21:03 +00:00
|
|
|
tooltipFormat: '%B %d, %Y',
|
2018-08-08 11:00:45 +00:00
|
|
|
type: 'line',
|
|
|
|
width: 600,
|
|
|
|
xFormat: '%Y-%m-%d',
|
2018-09-07 10:28:02 +00:00
|
|
|
x2Format: '',
|
2018-10-12 20:39:38 +00:00
|
|
|
yFormat: '.3s',
|
2018-08-08 11:00:45 +00:00
|
|
|
};
|
|
|
|
|
2018-06-15 18:11:25 +00:00
|
|
|
export default D3Chart;
|