woocommerce/plugins/woocommerce-admin/client/analytics/components/report-chart/index.js

223 lines
6.0 KiB
JavaScript
Raw Normal View History

/** @format */
/**
* External dependencies
*/
import { Component } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { format as formatDate } from '@wordpress/date';
import PropTypes from 'prop-types';
/**
* WooCommerce dependencies
*/
import {
getAllowedIntervalsForQuery,
getCurrentDates,
getDateFormatsForInterval,
getIntervalForQuery,
2018-11-14 01:45:05 +00:00
getChartTypeForQuery,
getPreviousDate,
} from '@woocommerce/date';
import { Chart } from '@woocommerce/components';
/**
* Internal dependencies
*/
import { getReportChartData, getTooltipValueFormat } from 'wc-api/reports/utils';
import ReportError from 'analytics/components/report-error';
2018-12-04 15:33:30 +00:00
import withSelect from 'wc-api/with-select';
import { getChartMode } from './utils';
/**
* Component that renders the chart in reports.
*/
export class ReportChart extends Component {
getItemChartData() {
const { primaryData, selectedChart } = this.props;
const chartData = primaryData.data.intervals.map( function( interval ) {
const intervalData = {};
interval.subtotals.segments.forEach( function( segment ) {
if ( segment.segment_label ) {
const label = intervalData[ segment.segment_label ]
? segment.segment_label + ' (#' + segment.segment_id + ')'
: segment.segment_label;
intervalData[ label ] = {
value: segment.subtotals[ selectedChart.key ] || 0,
};
}
} );
return {
date: formatDate( 'Y-m-d\\TH:i:s', interval.date_start ),
...intervalData,
};
} );
return chartData;
}
getTimeChartData() {
const { query, primaryData, secondaryData, selectedChart } = this.props;
const currentInterval = getIntervalForQuery( query );
const { primary, secondary } = getCurrentDates( query );
const primaryKey = `${ primary.label } (${ primary.range })`;
const secondaryKey = `${ secondary.label } (${ secondary.range })`;
const chartData = primaryData.data.intervals.map( function( interval, index ) {
const secondaryDate = getPreviousDate(
interval.date_start,
primary.after,
secondary.after,
query.compare,
currentInterval
);
const secondaryInterval = secondaryData.data.intervals[ index ];
return {
date: formatDate( 'Y-m-d\\TH:i:s', interval.date_start ),
[ primaryKey ]: {
labelDate: interval.date_start,
value: interval.subtotals[ selectedChart.key ] || 0,
},
[ secondaryKey ]: {
labelDate: secondaryDate.format( 'YYYY-MM-DD HH:mm:ss' ),
value: ( secondaryInterval && secondaryInterval.subtotals[ selectedChart.key ] ) || 0,
},
};
} );
return chartData;
}
renderChart( mode, isRequesting, chartData ) {
const {
interactiveLegend,
itemsLabel,
legendPosition,
path,
query,
selectedChart,
showHeaderControls,
primaryData,
} = this.props;
const currentInterval = getIntervalForQuery( query );
const allowedIntervals = getAllowedIntervalsForQuery( query );
const formats = getDateFormatsForInterval( currentInterval, primaryData.data.intervals.length );
return (
<Chart
allowedIntervals={ allowedIntervals }
data={ chartData }
dateParser={ '%Y-%m-%dT%H:%M:%S' }
interactiveLegend={ interactiveLegend }
interval={ currentInterval }
isRequesting={ isRequesting }
itemsLabel={ itemsLabel }
legendPosition={ legendPosition }
mode={ mode }
path={ path }
query={ query }
showHeaderControls={ showHeaderControls }
title={ selectedChart.label }
tooltipLabelFormat={ formats.tooltipLabelFormat }
tooltipTitle={ ( 'time-comparison' === mode && selectedChart.label ) || null }
tooltipValueFormat={ getTooltipValueFormat( selectedChart.type ) }
type={ getChartTypeForQuery( query ) }
valueType={ selectedChart.type }
xFormat={ formats.xFormat }
x2Format={ formats.x2Format }
/>
);
}
renderItemComparison() {
const { primaryData } = this.props;
if ( primaryData.isError ) {
return <ReportError isError />;
}
const isRequesting = primaryData.isRequesting;
const chartData = this.getItemChartData();
return this.renderChart( 'item-comparison', isRequesting, chartData );
}
renderTimeComparison() {
const { primaryData, secondaryData } = this.props;
if ( ! primaryData || primaryData.isError || secondaryData.isError ) {
return <ReportError isError />;
}
const isRequesting = primaryData.isRequesting || secondaryData.isRequesting;
const chartData = this.getTimeChartData();
return this.renderChart( 'time-comparison', isRequesting, chartData );
}
render() {
const { mode } = this.props;
if ( 'item-comparison' === mode ) {
return this.renderItemComparison();
}
return this.renderTimeComparison();
}
}
ReportChart.propTypes = {
/**
* Filters available for that report.
*/
filters: PropTypes.array,
/**
* Label describing the legend items.
*/
itemsLabel: PropTypes.string,
/**
* `items-comparison` (default) or `time-comparison`, this is used to generate correct
* ARIA properties.
*/
mode: PropTypes.string,
/**
* Current path
*/
path: PropTypes.string.isRequired,
/**
* Primary data to display in the chart.
*/
primaryData: PropTypes.object.isRequired,
/**
* The query string represented in object form.
*/
query: PropTypes.object.isRequired,
/**
* Secondary data to display in the chart.
*/
secondaryData: PropTypes.object,
/**
* Properties of the selected chart.
*/
selectedChart: PropTypes.object.isRequired,
};
export default compose(
withSelect( ( select, props ) => {
const { query, endpoint, filters } = props;
const chartMode = props.mode || getChartMode( filters, query ) || 'time-comparison';
if ( 'item-comparison' === chartMode ) {
const primaryData = getReportChartData( endpoint, 'primary', query, select );
return {
mode: chartMode,
primaryData,
};
}
const primaryData = getReportChartData( endpoint, 'primary', query, select );
const secondaryData = getReportChartData( endpoint, 'secondary', query, select );
return {
mode: chartMode,
primaryData,
secondaryData,
};
} )
)( ReportChart );