double-axes.js 3.89 KB
import React from 'react';
import { Button } from 'antd';

const formatter = o => {
    o = `${o}`.split('.');
    o[0] = o[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
    return o.join('.');
};

const formatLabel = (val, isShow) => {
    return isShow ? formatter(val) : '';
}

class DoubleAxes extends React.Component {
    constructor() {
        super();

        this.state = {
            clientRender: false
        };
    }
    componentDidMount() {
        import('bizcharts').then(({ Chart, Geom, Guide, Axis, Tooltip, Legend, Label }) => {
            this.Chart = Chart;
            this.Geom = Geom;
            this.Guide = Guide;
            this.Axis = Axis;
            this.Tooltip = Tooltip;
            this.Legend = Legend;
            this.Label = Label;

            this.setState({
                clientRender: true
            });
        });
    }
    downloadChart() {
        console.log(this);
        this.chartIns && this.chartIns.downloadImage();
    }
    render() {
        if (!this.state.clientRender) {
            return <div></div>;
        }

        const { Chart, Geom, Guide, Axis, Tooltip, Legend, Label } = this;
        const { Text } = Guide;
        const { height, padding, scale, axisX, axisY, axisY2 } = this.props.options || {};

        const position = axisX + '*' + axisY;
        const position2 = axisX + '*' + axisY2;

        return (
            <div>
                <div className="chart-option">
                    <Button shape="round" icon="download" size="small" onClick={this.downloadChart.bind(this)}>下载</Button>
                </div>
                <Chart
                    height={height}
                    scale={scale}
                    forceFit
                    data={this.props.data}
                    padding={padding}
                    onGetG2Instance={chartIns => {
                        this.chartIns = chartIns;
                    }}
                    background={{
                        fill: '#fff'
                    }}
                >
                    <Guide>
                        <Text
                            top={true}
                            position={['50%', '-12%']}
                            content={this.props.title || ''}
                            style={{
                                fill: '#444',
                                fontSize: '14',
                                textAlign: 'center'
                            }}
                        />
                    </Guide>
                    <Legend />
                    <Axis
                        name={axisY}
                        label={{ formatter }}
                    />
                    <Axis
                        name={axisY2}
                        position="right"
                        grid={null}
                        label={{ formatter }}
                    />
                    <Tooltip />

                    <Geom type="path" position={position} size={2} color="#1890ff">
                        <Label
                            content={[
                                axisY + '*label',
                                formatLabel
                            ]}
                            position="bottom"
                        />
                    </Geom>
                    <Geom type="point" position={position} color="#1890ff" size={3} shape="circle" />
                    <Geom type="line" position={position2} color="#2fc25b" size={2}>
                        <Label
                            content={[
                                axisY2 + '*label',
                                formatLabel
                            ]}
                            position="top"
                />
                    </Geom>
                    <Geom type="point" position={position2} color="#2fc25b" size={3} shape="circle"/>
                </Chart>
            </div>
        );
    }
}

export default DoubleAxes;