2018-07-11 15:23:16 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
import { formatCurrency } from 'lib/currency';
|
2018-08-13 10:19:32 +00:00
|
|
|
import { getColor } from './utils';
|
2018-07-11 15:23:16 +00:00
|
|
|
|
2018-10-19 13:23:47 +00:00
|
|
|
function getFormatedTotal( total, valueType ) {
|
|
|
|
let rowTotal = total;
|
|
|
|
switch ( valueType ) {
|
|
|
|
case 'average':
|
|
|
|
rowTotal = Math.round( total );
|
|
|
|
break;
|
|
|
|
case 'currency':
|
|
|
|
rowTotal = formatCurrency( total );
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rowTotal;
|
|
|
|
}
|
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A legend specifically designed for the WooCommerce admin charts.
|
|
|
|
*/
|
2018-07-11 15:23:16 +00:00
|
|
|
class Legend extends Component {
|
|
|
|
render() {
|
2018-08-13 10:19:32 +00:00
|
|
|
const {
|
|
|
|
colorScheme,
|
|
|
|
data,
|
|
|
|
handleLegendHover,
|
|
|
|
handleLegendToggle,
|
|
|
|
legendDirection,
|
2018-10-19 13:23:47 +00:00
|
|
|
valueType,
|
2018-08-13 10:19:32 +00:00
|
|
|
} = this.props;
|
|
|
|
const colorParams = {
|
|
|
|
orderedKeys: data,
|
|
|
|
colorScheme,
|
|
|
|
};
|
2018-09-18 14:53:30 +00:00
|
|
|
const numberOfRowsVisible = data.filter( row => row.visible ).length;
|
|
|
|
|
2018-07-11 15:23:16 +00:00
|
|
|
return (
|
|
|
|
<ul
|
|
|
|
className={ classNames(
|
|
|
|
'woocommerce-legend',
|
|
|
|
`woocommerce-legend__direction-${ legendDirection }`,
|
|
|
|
this.props.className
|
|
|
|
) }
|
|
|
|
>
|
2018-08-13 10:19:32 +00:00
|
|
|
{ data.map( row => (
|
2018-07-11 15:23:16 +00:00
|
|
|
<li
|
2018-09-04 09:56:25 +00:00
|
|
|
className={ classNames( 'woocommerce-legend__item', {
|
|
|
|
'woocommerce-legend__item-checked': row.visible,
|
|
|
|
} ) }
|
2018-07-25 14:58:32 +00:00
|
|
|
key={ row.key }
|
2018-07-11 15:23:16 +00:00
|
|
|
id={ row.key }
|
|
|
|
onMouseEnter={ handleLegendHover }
|
|
|
|
onMouseLeave={ handleLegendHover }
|
|
|
|
onBlur={ handleLegendHover }
|
|
|
|
onFocus={ handleLegendHover }
|
|
|
|
>
|
2018-09-18 14:53:30 +00:00
|
|
|
<button
|
|
|
|
onClick={ handleLegendToggle }
|
|
|
|
id={ row.key }
|
|
|
|
disabled={ row.visible && numberOfRowsVisible <= 1 }
|
|
|
|
>
|
2018-07-11 15:23:16 +00:00
|
|
|
<div className="woocommerce-legend__item-container" id={ row.key }>
|
|
|
|
<span
|
|
|
|
className={ classNames( 'woocommerce-legend__item-checkmark', {
|
|
|
|
'woocommerce-legend__item-checkmark-checked': row.visible,
|
|
|
|
} ) }
|
|
|
|
id={ row.key }
|
2018-09-19 14:41:13 +00:00
|
|
|
style={ { color: getColor( row.key, colorParams ) } }
|
2018-07-11 15:23:16 +00:00
|
|
|
/>
|
|
|
|
<span className="woocommerce-legend__item-title" id={ row.key }>
|
|
|
|
{ row.key }
|
|
|
|
</span>
|
|
|
|
<span className="woocommerce-legend__item-total" id={ row.key }>
|
2018-10-19 13:23:47 +00:00
|
|
|
{ getFormatedTotal( row.total, valueType ) }
|
2018-07-11 15:23:16 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
) ) }
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Legend.propTypes = {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Additional CSS classes.
|
|
|
|
*/
|
2018-07-11 15:23:16 +00:00
|
|
|
className: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A chromatic color function to be passed down to d3.
|
|
|
|
*/
|
2018-08-13 10:19:32 +00:00
|
|
|
colorScheme: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An array of `orderedKeys`.
|
|
|
|
*/
|
2018-07-11 15:23:16 +00:00
|
|
|
data: PropTypes.array.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Handles `onClick` event.
|
|
|
|
*/
|
2018-07-25 14:58:32 +00:00
|
|
|
handleLegendToggle: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Handles `onMouseEnter`/`onMouseLeave` events.
|
|
|
|
*/
|
2018-07-25 14:58:32 +00:00
|
|
|
handleLegendHover: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Display legend items as a `row` or `column` inside a flex-box.
|
|
|
|
*/
|
2018-07-11 15:23:16 +00:00
|
|
|
legendDirection: PropTypes.oneOf( [ 'row', 'column' ] ),
|
2018-10-19 13:23:47 +00:00
|
|
|
/**
|
|
|
|
* What type of data is to be displayed? Number, Average, String?
|
|
|
|
*/
|
|
|
|
valueType: PropTypes.string,
|
2018-07-11 15:23:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Legend.defaultProps = {
|
|
|
|
legendDirection: 'row',
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Legend;
|