echarts-series.vue 3.92 KB
<template>
    <div :class="className" :style="{
        height: height, width: width, paddingRight: padding
    }" v-loading="loading" />
</template>

<script>

export default {
    name: '',
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '350px'
        },
        chartData: {
            type: Object,
            required: true
        },
        loading: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            chart: null,
            padding: "10px"
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    watch: {
        chartData: {
            deep: true,
            handler(val) {
                this.setOptions(val)
            }
        }
    },
    methods: {
        initChart() {
            this.chart = this.$echarts.init(this.$el)
            this.setOptions(this.chartData)
        },
        setOptions({ names, data1, data2, data3 } = {}) {
            this.chart.setOption({
                tooltip: {
                    trigger: 'axis',
                    axisPointer: { // 坐标轴指示器,坐标轴触发有效
                        type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
                    }
                },
                legend: {
                    show: true
                },
                dataZoom: [
                    {
                        type: 'inside',
                        xAxisIndex: [0],
                    },
                    {
                        type: 'slider',
                        show: true,
                        start: 0,
                        end: 85,
                        handleSize: 8
                    },
                ],
                grid: {
                    top: '15%',
                    left: '2%',
                    right: '2%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    data: names,
                    axisTick: {
                        alignWithLabel: true
                    }
                }],
                yAxis: [{
                    type: 'value',
                    axisTick: {
                        show: false
                    }
                }],
                series: [{
                    name: '件数',
                    type: 'bar',
                    data: data1,
                    barWidth: '10%',
                    barGap: '0%',
                    label: {
                        show: true,
                        position: 'top',
                        fontSize: 14
                    },
                    animationDuration: 1000
                },
                {
                    name: '票数',
                    type: 'bar',
                    data: data2,
                    barWidth: '10%',
                    barGap: '0%',
                    label: {
                        show: true,
                        position: 'top',
                        fontSize: 14
                    },
                    animationDuration: 1000
                },
                {
                    name: '重量',
                    type: 'bar',
                    data: data3,
                    barWidth: '10%',
                    barGap: '0%',
                    label: {
                        show: true,
                        position: 'top',
                        fontSize: 14
                    },
                    animationDuration: 1000
                }]
            })
        }
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },

}
</script>

<style>

</style>