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 = { height: 0, autoHideXLabels: false, }; handleRoot = n => { this.root = n; }; handleRef = n => { this.node = n; }; resizeObserver() { const ro = new ResizeObserver(entries => { const { width, height } = entries[0].contentRect; this.resize(); this.setState(preState => { if (preState.width !== width || preState.height !== height) { return { 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 } = this.state; const height = propsHeight || stateHeight; return (