import * as React from 'react'; import { DatePicker, notification, Spin } from 'component/antd'; import moment, { Moment } from 'moment'; import { timeStampStr } from 'constants/strategy'; import { disabledDate } from 'lib/utils'; import * as echarts from 'echarts/core'; // 引入柱状图 import { BarChart, LineChart } from 'echarts/charts'; // 引入提示框和标题组件 import { TitleComponent, TooltipComponent, LegendComponent, GridComponent, MarkLineComponent, DatasetComponent, } from 'echarts/components'; import { CanvasRenderer } from 'echarts/renderers'; // 注册必须的组件 echarts.use([ TitleComponent, LegendComponent, TooltipComponent, GridComponent, BarChart, LineChart, CanvasRenderer, DatasetComponent, MarkLineComponent, ]); import './index.less'; const { RangePicker } = DatePicker; interface IChartProps { getChartData: (startTime: moment.Moment, endTime: moment.Moment) => any; customerNode?: React.ReactNode; } export class ChartWithDatePicker extends React.Component { public state = { startTime: moment().subtract(1, 'hour'), endTime: moment(), loading: false, noData: false, }; public id: HTMLDivElement = null; public chart: echarts.ECharts; public getData = () => { const { startTime, endTime } = this.state; const { getChartData } = this.props; this.setState({ loading: true }); getChartData(startTime, endTime).then((data: any) => { this.setState({ loading: false }); this.changeChartOptions(data); }); } public componentDidMount() { this.chart = echarts.init(this.id); this.getData(); window.addEventListener('resize', this.resize); } public componentWillUnmount() { window.removeEventListener('resize', this.resize); } public resize = () => { this.chart.resize(); } public changeChartOptions(options: any) { const noData = options.series.length ? false : true; this.setState({ noData }); options.tooltip.formatter = (params: any) => { let res = '

' + params[0].data.time + '

'; // tslint:disable-next-line:prefer-for-of for (let i = 0; i < params.length; i++) { res += `
${params[i].seriesName} ${params[i].data[params[i].seriesName]}
`; } return res; }; this.chart.setOption(options, true); } public handleTimeChange = (dates: Moment[]) => { this.setState({ startTime: dates[0], endTime: dates[1], }); const { getChartData } = this.props; this.setState({ loading: true }); getChartData(dates[0], dates[1]).then((data: any) => { this.setState({ loading: false }); this.changeChartOptions(data); }); } public render() { const { customerNode } = this.props; return (
{customerNode}
{this.state.noData ?
暂无数据
: null}
this.id = id} />
); } }