import clsx from 'clsx'; import PropTypes from 'prop-types'; import { PureComponent } from 'react'; import ChartBarTooltip from './bar-tooltip'; export default class ChartBar extends PureComponent { static propTypes = { className: PropTypes.string, clickHandler: PropTypes.func, count: PropTypes.number, data: PropTypes.object.isRequired, isTouch: PropTypes.bool, max: PropTypes.number, tooltipPosition: PropTypes.string, }; static defaultProps = { max: Infinity, }; clickHandler = () => { if ( typeof this.props.clickHandler === 'function' ) { this.props.clickHandler( this.props.data ); } }; computeTooltipPosition() { const { chartWidth, index, count } = this.props; const barWidth = chartWidth / count; const barOffset = barWidth * ( index + 1 ); const shouldFlip = barOffset > chartWidth - 230 && barOffset + barWidth > 230; return shouldFlip ? 'bottom left' : 'bottom right'; } mouseEnter = () => { if ( ! this.props.data.tooltipData || ! this.props.data.tooltipData.length || this.props.isTouch ) { return null; } this.props.setTooltip( this.bar, this.computeTooltipPosition(), this.getTooltipData() ); }; mouseLeave = () => { this.props.setTooltip( null ); }; getTooltipData() { return this.props.data.tooltipData.map( function ( options, i ) { return ; } ); } getScaleY() { const scaleY = this.props.data.value / this.props.max; // Hack: We use an invisible but non-zero value here, becaue zero scaleY-ed bars grows to max and then disappear when combined with container animation on initialization in Chrome. return scaleY < 1e-4 ? '0.0001' : scaleY.toFixed( 4 ); } getNestedPercentage() { const { data: { nestedValue, value }, } = this.props; return value && nestedValue ? Math.ceil( ( nestedValue / value ) * 10000 ) / 100 : 0; } setRef = ( ref ) => ( this.bar = ref ); renderNestedBar() { const { data: { nestedValue }, } = this.props; return ( nestedValue && (
) ); } renderBar() { return (
{ this.renderNestedBar() }
); } render() { return (