* Add different props to chart component that were inferred from 'mode'

* Add new chart props to packages CHANGELOG
This commit is contained in:
Albert Juhé Lluveras 2019-01-08 10:21:47 +01:00 committed by GitHub
parent ef6d7ec4cb
commit 03e17d372b
4 changed files with 119 additions and 68 deletions

View File

@ -68,7 +68,18 @@ export class ReportChart extends Component {
} }
render() { render() {
const { query, itemsLabel, mode, path, primaryData, secondaryData, selectedChart } = this.props; const {
interactiveLegend,
itemsLabel,
legendPosition,
mode,
path,
primaryData,
query,
secondaryData,
selectedChart,
showHeaderControls,
} = this.props;
if ( primaryData.isError || secondaryData.isError ) { if ( primaryData.isError || secondaryData.isError ) {
return <ReportError isError />; return <ReportError isError />;
@ -106,23 +117,26 @@ export class ReportChart extends Component {
return ( return (
<Chart <Chart
allowedIntervals={ allowedIntervals }
data={ chartData }
dateParser={ '%Y-%m-%dT%H:%M:%S' }
interactiveLegend={ interactiveLegend }
interval={ currentInterval }
isRequesting={ primaryData.isRequesting || secondaryData.isRequesting }
itemsLabel={ itemsLabel }
legendPosition={ legendPosition }
mode={ mode || this.getChartMode() }
path={ path } path={ path }
query={ query } query={ query }
data={ chartData } showHeaderControls={ showHeaderControls }
title={ selectedChart.label } title={ selectedChart.label }
interval={ currentInterval }
type={ getChartTypeForQuery( query ) }
allowedIntervals={ allowedIntervals }
itemsLabel={ itemsLabel }
mode={ mode || this.getChartMode() }
tooltipLabelFormat={ formats.tooltipLabelFormat } tooltipLabelFormat={ formats.tooltipLabelFormat }
tooltipValueFormat={ getTooltipValueFormat( selectedChart.type ) }
tooltipTitle={ selectedChart.label } tooltipTitle={ selectedChart.label }
tooltipValueFormat={ getTooltipValueFormat( selectedChart.type ) }
type={ getChartTypeForQuery( query ) }
valueType={ selectedChart.type }
xFormat={ formats.xFormat } xFormat={ formats.xFormat }
x2Format={ formats.x2Format } x2Format={ formats.x2Format }
dateParser={ '%Y-%m-%dT%H:%M:%S' }
valueType={ selectedChart.type }
isRequesting={ primaryData.isRequesting || secondaryData.isRequesting }
/> />
); );
} }

View File

@ -58,10 +58,12 @@ class ChartBlock extends Component {
<ReportChart <ReportChart
charts={ charts } charts={ charts }
endpoint={ endpoint } endpoint={ endpoint }
mode="block"
path={ path }
query={ query } query={ query }
interactiveLegend={ false }
legendPosition="bottom"
path={ path }
selectedChart={ charts[ 0 ] } selectedChart={ charts[ 0 ] }
showHeaderControls={ false }
/> />
</Card> </Card>
</div> </div>

View File

@ -3,7 +3,11 @@
- Add order number autocompleter to search component - Add order number autocompleter to search component
- Add order number, username, and IP address filters to the downloads report. - Add order number, username, and IP address filters to the downloads report.
- Added `interactive` prop for `d3chart/legend` to signal if legend items are clickable or not. - Added `interactive` prop for `d3chart/legend` to signal if legend items are clickable or not.
- Fix for undefined ref in `d3chart/legend` - Fix for undefined ref in `d3chart/legend`.
- Added three news props to `<Chart>`:
- `interactiveLegend`: whether legend items are clickable or not. Defaults to true.
- `legendPosition`: can be `top`, `side` or `bottom`. If not specified, it's calculated based on `mode` and viewport width.
- `showHeaderControls`: whether the header controls must be visible. Defaults to true.
# 1.3.0 # 1.3.0

View File

@ -113,8 +113,8 @@ class Chart extends Component {
} }
handleLegendToggle( event ) { handleLegendToggle( event ) {
const { data, mode } = this.props; const { data, interactiveLegend } = this.props;
if ( mode === 'block' ) { if ( ! interactiveLegend ) {
return; return;
} }
const orderedKeys = this.state.orderedKeys.map( d => ( { const orderedKeys = this.state.orderedKeys.map( d => ( {
@ -211,28 +211,44 @@ class Chart extends Component {
return 220; return 220;
} }
getLegendPosition() {
const { legendPosition, mode, isViewportWide } = this.props;
if ( legendPosition ) {
return legendPosition;
}
if ( isViewportWide && mode === 'time-comparison' ) {
return 'top';
}
if ( isViewportWide && mode === 'item-comparison' ) {
return 'side';
}
return 'bottom';
}
render() { render() {
const { orderedKeys, visibleData, width } = this.state; const { interactiveLegend, orderedKeys, visibleData, width } = this.state;
const { const {
dateParser, dateParser,
interval,
isRequesting,
isViewportLarge,
itemsLabel, itemsLabel,
mode, mode,
isViewportLarge, showHeaderControls,
isViewportWide,
title, title,
tooltipLabelFormat, tooltipLabelFormat,
tooltipValueFormat, tooltipValueFormat,
tooltipTitle, tooltipTitle,
type,
valueType,
xFormat, xFormat,
x2Format, x2Format,
interval,
valueType,
type,
isRequesting,
} = this.props; } = this.props;
let { yFormat } = this.props; let { yFormat } = this.props;
const legendDirection = mode === 'time-comparison' && isViewportWide ? 'row' : 'column';
const chartDirection = mode === 'item-comparison' && isViewportWide ? 'row' : 'column'; const legendPosition = this.getLegendPosition();
const legendDirection = legendPosition === 'top' ? 'row' : 'column';
const chartDirection = legendPosition === 'side' ? 'row' : 'column';
const chartHeight = this.getChartHeight(); const chartHeight = this.getChartHeight();
const legend = ( const legend = (
@ -241,7 +257,7 @@ class Chart extends Component {
data={ orderedKeys } data={ orderedKeys }
handleLegendHover={ this.handleLegendHover } handleLegendHover={ this.handleLegendHover }
handleLegendToggle={ this.handleLegendToggle } handleLegendToggle={ this.handleLegendToggle }
interactive={ mode !== 'block' } interactive={ interactiveLegend }
legendDirection={ legendDirection } legendDirection={ legendDirection }
legendValueFormat={ tooltipValueFormat } legendValueFormat={ tooltipValueFormat }
totalLabel={ sprintf( itemsLabel, orderedKeys.length ) } totalLabel={ sprintf( itemsLabel, orderedKeys.length ) }
@ -267,10 +283,10 @@ class Chart extends Component {
} }
return ( return (
<div className="woocommerce-chart"> <div className="woocommerce-chart">
{ mode !== 'block' && ( { showHeaderControls && (
<div className="woocommerce-chart__header"> <div className="woocommerce-chart__header">
<H className="woocommerce-chart__title">{ title }</H> <H className="woocommerce-chart__title">{ title }</H>
{ isViewportWide && legendDirection === 'row' && legend } { legendPosition === 'top' && legend }
{ this.renderIntervalSelector() } { this.renderIntervalSelector() }
<NavigableMenu <NavigableMenu
className="woocommerce-chart__types" className="woocommerce-chart__types"
@ -310,7 +326,7 @@ class Chart extends Component {
) } ) }
ref={ this.chartBodyRef } ref={ this.chartBodyRef }
> >
{ isViewportWide && legendDirection === 'column' && mode !== 'block' && legend } { legendPosition === 'side' && legend }
{ isRequesting && ( { isRequesting && (
<Fragment> <Fragment>
<span className="screen-reader-text"> <span className="screen-reader-text">
@ -328,7 +344,7 @@ class Chart extends Component {
height={ chartHeight } height={ chartHeight }
interval={ interval } interval={ interval }
margin={ margin } margin={ margin }
mode={ mode === 'block' ? 'item-comparison' : mode } mode={ mode }
orderedKeys={ orderedKeys } orderedKeys={ orderedKeys }
tooltipLabelFormat={ tooltipLabelFormat } tooltipLabelFormat={ tooltipLabelFormat }
tooltipValueFormat={ tooltipValueFormat } tooltipValueFormat={ tooltipValueFormat }
@ -343,7 +359,7 @@ class Chart extends Component {
/> />
) } ) }
</div> </div>
{ ( ! isViewportWide || mode === 'block' ) && ( { ( legendPosition === 'bottom' ) && (
<div className="woocommerce-chart__footer">{ legend }</div> <div className="woocommerce-chart__footer">{ legend }</div>
) } ) }
</Section> </Section>
@ -353,6 +369,10 @@ class Chart extends Component {
} }
Chart.propTypes = { Chart.propTypes = {
/**
* Allowed intervals to show in a dropdown.
*/
allowedIntervals: PropTypes.array,
/** /**
* An array of data. * An array of data.
*/ */
@ -365,6 +385,11 @@ Chart.propTypes = {
* Label describing the legend items. * Label describing the legend items.
*/ */
itemsLabel: PropTypes.string, itemsLabel: PropTypes.string,
/**
* `item-comparison` (default) or `time-comparison`, this is used to generate correct
* ARIA properties.
*/
mode: PropTypes.oneOf( [ 'item-comparison', 'time-comparison' ] ),
/** /**
* Current path * Current path
*/ */
@ -373,6 +398,35 @@ Chart.propTypes = {
* The query string represented in object form * The query string represented in object form
*/ */
query: PropTypes.object, query: PropTypes.object,
/**
* Whether the legend items can be activated/deactivated.
*/
interactiveLegend: PropTypes.bool,
/**
* Interval specification (hourly, daily, weekly etc).
*/
interval: PropTypes.oneOf( [ 'hour', 'day', 'week', 'month', 'quarter', 'year' ] ),
/**
* Information about the currently selected interval, and set of allowed intervals for the chart. See `getIntervalsForQuery`.
*/
intervalData: PropTypes.object,
/**
* Render a chart placeholder to signify an in-flight data request.
*/
isRequesting: PropTypes.bool,
/**
* Position the legend must be displayed in. If it's not defined, it's calculated
* depending on the viewport width and the mode.
*/
legendPosition: PropTypes.oneOf( [ 'bottom', 'side', 'top' ] ),
/**
* Wether header UI controls must be displayed.
*/
showHeaderControls: PropTypes.bool,
/**
* A title describing this chart.
*/
title: PropTypes.string,
/** /**
* A datetime formatting string or overriding function to format the tooltip label. * A datetime formatting string or overriding function to format the tooltip label.
*/ */
@ -385,6 +439,14 @@ Chart.propTypes = {
* A string to use as a title for the tooltip. Takes preference over `tooltipLabelFormat`. * A string to use as a title for the tooltip. Takes preference over `tooltipLabelFormat`.
*/ */
tooltipTitle: PropTypes.string, tooltipTitle: PropTypes.string,
/**
* Chart type of either `line` or `bar`.
*/
type: PropTypes.oneOf( [ 'bar', 'line' ] ),
/**
* What type of data is to be displayed? Number, Average, String?
*/
valueType: PropTypes.string,
/** /**
* A datetime formatting string, passed to d3TimeFormat. * A datetime formatting string, passed to d3TimeFormat.
*/ */
@ -397,53 +459,22 @@ Chart.propTypes = {
* A number formatting string, passed to d3Format. * A number formatting string, passed to d3Format.
*/ */
yFormat: PropTypes.string, yFormat: PropTypes.string,
/**
* `item-comparison` (default) or `time-comparison`, this is used to generate correct
* ARIA properties.
*/
mode: PropTypes.oneOf( [ 'block', 'item-comparison', 'time-comparison' ] ),
/**
* A title describing this chart.
*/
title: PropTypes.string,
/**
* Chart type of either `line` or `bar`.
*/
type: PropTypes.oneOf( [ 'bar', 'line' ] ),
/**
* Information about the currently selected interval, and set of allowed intervals for the chart. See `getIntervalsForQuery`.
*/
intervalData: PropTypes.object,
/**
* Interval specification (hourly, daily, weekly etc).
*/
interval: PropTypes.oneOf( [ 'hour', 'day', 'week', 'month', 'quarter', 'year' ] ),
/**
* Allowed intervals to show in a dropdown.
*/
allowedIntervals: PropTypes.array,
/**
* What type of data is to be displayed? Number, Average, String?
*/
valueType: PropTypes.string,
/**
* Render a chart placeholder to signify an in-flight data request.
*/
isRequesting: PropTypes.bool,
}; };
Chart.defaultProps = { Chart.defaultProps = {
data: [], data: [],
dateParser: '%Y-%m-%dT%H:%M:%S', dateParser: '%Y-%m-%dT%H:%M:%S',
interactiveLegend: true,
interval: 'day',
isRequesting: false,
mode: 'time-comparison',
showHeaderControls: true,
tooltipLabelFormat: '%B %d, %Y', tooltipLabelFormat: '%B %d, %Y',
tooltipValueFormat: ',', tooltipValueFormat: ',',
type: 'line',
xFormat: '%d', xFormat: '%d',
x2Format: '%b %Y', x2Format: '%b %Y',
yFormat: '$.3s', yFormat: '$.3s',
mode: 'time-comparison',
type: 'line',
interval: 'day',
isRequesting: false,
}; };
export default withViewportMatch( { export default withViewportMatch( {