bizcharts 折线图缩放案例
https://www.bizcharts.net/product/bizcharts/demo/22
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { Empty } from 'antd';
import { Axis, Chart, Geom, Tooltip, Legend } from 'bizcharts';
import Brush from '@antv/g2-brush';
LineChartZoom.propTypes = {
height: PropTypes.number,
padding: PropTypes.array,
custom: PropTypes.bool,
loading: PropTypes.bool,
dataSource: PropTypes.array.isRequired,
};
LineChartZoom.defaultProps = {
height: 460,
padding: [20, 56, 50, 56],
custom: false,
loading: false,
};
let chart;
function LineChartZoom(props) {
const { height, padding, custom, dataSource } = props;
if (!dataSource.length) return <Empty />;
useEffect(() => {
new Brush({
canvas: chart.get('canvas'),
style: {
fill: '#ccc',
fillOpacity: 0.4,
},
chart,
});
// 双击清除过滤,恢复初始状态
chart.on('plotdblclick', () => {
chart.get('options').filters = {};
chart.repaint();
});
}, []);
const scale = {
value: {
alias: '值',
tickCount: 5,
min: 0,
},
date: {
type: 'timeCat',
mask: 'YYYY-MM-DD HH:mm',
tickCount: custom ? 8 : 12,
nice: false,
},
};
const label = {
formatter(text) {
return custom ? text : text.slice(-5);
},
textStyle: {
textAlign: 'center',
fontSize: '12',
},
};
const attr = {
data: dataSource,
scale,
height,
padding,
forceFit: true,
onGetG2Instance: g2Chart => {
g2Chart.animate(false);
chart = g2Chart;
},
};
return (
<Chart {...attr}>
<Axis name='date' label={label} />
<Axis name='value' />
<Tooltip crosshairs={false}/>
<Legend />
<Geom type='line' position='date*value' size={2} shape='smooth' />
</Chart>
);
}
export default LineChartZoom;
<LineChartZoom dataSource={chartData} />