diff --git a/src/components/Charts/Bar/index.js b/src/components/Charts/Bar/index.js index 0f398e9f0e0c79f70247c5f03aae26819466bc43..f4d87b987e1e26a37eda2d44c1f31868540ae2ec 100644 --- a/src/components/Charts/Bar/index.js +++ b/src/components/Charts/Bar/index.js @@ -1,11 +1,18 @@ import React, { PureComponent } from 'react'; import G2 from 'g2'; +import Debounce from 'lodash-decorators/debounce'; import equal from '../equal'; import styles from '../index.less'; class Bar extends PureComponent { + state = { + autoHideXLabels: false, + } + componentDidMount() { this.renderChart(this.props.data); + + window.addEventListener('resize', this.resize); } componentWillReceiveProps(nextProps) { @@ -15,17 +22,53 @@ class Bar extends PureComponent { } componentWillUnmount() { + window.removeEventListener('resize', this.resize); if (this.chart) { this.chart.destroy(); } } + @Debounce(200) + 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, + }); + this.renderChart(data); + } + } else if (autoHideXLabels) { + this.setState({ + autoHideXLabels: false, + }); + this.renderChart(data); + } + } + handleRef = (n) => { this.node = n; } renderChart(data) { - const { height = 0, fit = true, color = 'rgba(24, 144, 255, 0.85)', margin = [32, 0, 32, 40] } = this.props; + const { autoHideXLabels } = this.state; + const { + height = 0, + fit = true, + color = 'rgba(24, 144, 255, 0.85)', + margin = [32, 0, (autoHideXLabels ? 8 : 32), 40], + } = this.props; + if (!data || (data && data.length < 1)) { return; @@ -47,9 +90,17 @@ class Bar extends PureComponent { }, }); - chart.axis('x', { - title: false, - }); + if (autoHideXLabels) { + chart.axis('x', { + title: false, + tickLine: false, + labels: false, + }); + } else { + chart.axis('x', { + title: false, + }); + } chart.axis('y', { title: false, line: false, diff --git a/src/components/Charts/ChartCard/index.js b/src/components/Charts/ChartCard/index.js index 20adbb5e9b6e18fe2e79a5ba85288fe31554d37e..5624f21f2cf91edac66df157df9dc69169f9da1e 100644 --- a/src/components/Charts/ChartCard/index.js +++ b/src/components/Charts/ChartCard/index.js @@ -1,13 +1,10 @@ import React from 'react'; -import { Card } from 'antd'; +import { Card, Spin } from 'antd'; import styles from './index.less'; -const ChartCard = ({ contentHeight, title, action, total, footer, children, ...rest }) => ( - +const ChartCard = ({ loading, contentHeight, title, action, total, footer, children, ...rest }) => { + const content = (
{title} @@ -30,7 +27,20 @@ const ChartCard = ({ contentHeight, title, action, total, footer, children, ...r ) }
- -); + ); + + return ( + + { + loading ? ( + {content} + ) : content + } + + ); +}; export default ChartCard; diff --git a/src/components/Charts/MiniArea/index.js b/src/components/Charts/MiniArea/index.js index 4995b6b8ff0bf4b424a2a2f707ecda1138700c3b..65f09697a073483101041cbeabed7246e26d2d73 100644 --- a/src/components/Charts/MiniArea/index.js +++ b/src/components/Charts/MiniArea/index.js @@ -114,7 +114,7 @@ class MiniArea extends PureComponent { return (
-
+
diff --git a/src/components/Charts/MiniBar/index.js b/src/components/Charts/MiniBar/index.js index 90d9ae7c41e619ae2d6870272141247893969e9c..991571b71a643a9c70e85e4d1b5200d4728e9690 100644 --- a/src/components/Charts/MiniBar/index.js +++ b/src/components/Charts/MiniBar/index.js @@ -76,7 +76,7 @@ class MiniBar extends PureComponent { return (
-
+
diff --git a/src/components/Charts/index.less b/src/components/Charts/index.less index 19e13908d494024bdd753d05cfb46a7ce6c12ecd..52f97c4c970fae0cdf61b7e81cbfeb437bfaec1c 100644 --- a/src/components/Charts/index.less +++ b/src/components/Charts/index.less @@ -1,7 +1,7 @@ .miniChart { position: relative; width: 100%; - & > div { + .chartContent { position: absolute; bottom: -34px; width: 100%; @@ -10,4 +10,10 @@ overflow: hidden; } } + .chartLoading { + position: absolute; + top: 16px; + left: 50%; + margin-left: -7px; + } } diff --git a/src/components/Charts/index.md b/src/components/Charts/index.md index 11dbc9e5a9f18fd503a88e512e312b00fa6e5263..aed2ef4e26075abf9dba41f448f6986480dbeb6c 100644 --- a/src/components/Charts/index.md +++ b/src/components/Charts/index.md @@ -60,6 +60,7 @@ Ant Design Pro 提供的业务中常用的图表类型,都是基于 [G2](https | margin | 图表内部间距 | array | \[32, 0, 32, 40\] | | height | 图表高度 | number | - | | data | 数据 | array<{x, y}> | - | +| autoLabel | 在宽度不足时,自动隐藏 x 轴的 label | boolean | `true` | ### Pie diff --git a/src/routes/Dashboard/Analysis.js b/src/routes/Dashboard/Analysis.js index a35cdf718490a2c8af1da80e7903bc59fe126d40..00e4b80f6dfff42da5e6063f74a7438604ef9f3a 100644 --- a/src/routes/Dashboard/Analysis.js +++ b/src/routes/Dashboard/Analysis.js @@ -27,6 +27,7 @@ for (let i = 0; i < 7; i += 1) { })) export default class Analysis extends Component { state = { + loading: true, salesType: 'all', currentTabKey: '', rangePickerValue: [], @@ -35,6 +36,10 @@ export default class Analysis extends Component { componentDidMount() { this.props.dispatch({ type: 'chart/fetch', + }).then(() => { + this.setState({ + loading: false, + }); }); } @@ -89,7 +94,7 @@ export default class Analysis extends Component { } render() { - const { rangePickerValue, salesType, currentTabKey } = this.state; + const { rangePickerValue, salesType, currentTabKey, loading } = this.state; const { chart } = this.props; const { visitData, @@ -221,6 +226,7 @@ export default class Analysis extends Component { } @@ -238,6 +244,7 @@ export default class Analysis extends Component { } @@ -254,6 +261,7 @@ export default class Analysis extends Component { } @@ -269,6 +277,7 @@ export default class Analysis extends Component { } @@ -291,6 +300,7 @@ export default class Analysis extends Component { @@ -359,8 +369,9 @@ export default class Analysis extends Component { - + - +