import React, { Component } from 'react'; import { Chart, Axis, Tooltip, Geom } from 'bizcharts'; import Debounce from 'lodash-decorators/debounce'; import Bind from 'lodash-decorators/bind'; import ResizeObserver from 'resize-observer-polyfill'; import styles from '../index.less'; class Bar extends Component { state = { width: 0, height: 0, autoHideXLabels: false, }; componentDidMount() { window.addEventListener('resize', this.resize, { passive: true }); } componentWillUnmount() { window.removeEventListener('resize', this.resize); } handleRoot = n => { this.root = n; }; handleRef = n => { this.node = n; }; resizeObserver() { const ro = new ResizeObserver(entries => { const { width, height } = entries[0].contentRect; this.setState((preState, { hasLegend }) => { if (preState.width !== width || preState.height !== height) { return { width: width - (hasLegend ? 240 : 0), height, }; } return null; }); }); if (this.root) { ro.observe(this.root); } } @Bind() @Debounce(400) resize() { if (!this.node) { return; } const canvasWidth = this.node.parentNode.clientWidth; const { data = [], autoLabel = true } = this.props; if (!autoLabel) { return; } const minWidth = data.length * 30; const { autoHideXLabels } = this.state; if (canvasWidth <= minWidth) { if (!autoHideXLabels) { this.setState({ autoHideXLabels: true, }); } } else if (autoHideXLabels) { this.setState({ autoHideXLabels: false, }); } } render() { const { height: propsHeight, title, forceFit = true, data, color = 'rgba(24, 144, 255, 0.85)', padding, } = this.props; const { autoHideXLabels } = this.state; const scale = { x: { type: 'cat', }, y: { min: 0, }, }; const tooltip = [ 'x*y', (x, y) => ({ name: x, value: y, }), ]; const { height: stateHeight, width } = this.state; const height = propsHeight || stateHeight; return (
{title &&

{title}

}
); } } export default Bar;