vue2 使用echarts注意点 clear dispose resize 方法

<template>
    <div id="temperature-main">
    </div>
</template>

<script>
import * as echarts from 'echarts';
var myChart = null
export default {
    props: {
        echartsData: {
            type: Object,
            default: () => { }
        }
    },
    watch: {
        echartsData() {
            this.updateData()
        }
    },
    mounted() {
        // console.log(this.echartsData.electricAvg);
        this.init()
    },
    beforeDestroy() {
        if (myChart != null) {
            myChart.dispose()
        }
        window.removeEventListener("resize", function line() {
            myChart.resize();
        });
    },
    methods: {
        init() {
            var chartDom = document.getElementById('temperature-main');
            myChart = echarts.init(chartDom);
            window.addEventListener('resize', function line() {
                myChart.resize();
            })
            // this.updateData()

        },
        updateData() {
            if (myChart != null) {
                myChart.clear()
            }
            var option;

            option = {
                title: {
                    text: '线缆温度与电流温度对比图',
                    textStyle: {
                        color: '#3D3D3D'
                    },
                    top: '5%'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['平均三相电流', '平均三相温度']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    top: '20%',
                    containLabel: true
                },
                toolbox: {
                    feature: {
                        saveAsImage: {}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: this.echartsData.electricAvg.map(it => it.time)
                },
                yAxis: [{
                    name: 'A',
                    type: 'value',
                    // min: 'dataMin',
                    // max: 'dataMax',
                }, {
                    name: '°C',
                    type: 'value',
                    // min: 'dataMin',
                    // max: 'dataMax',
                }],
                dataZoom: [{ // 这部分是关键配置
                    type: 'inside', // 内置型数据区域缩放组件
                    start: 0, // 数据窗口范围的起始百分比
                    end: 100 // 数据窗口范围的结束百分比
                }],
                series: [
                    {
                        name: '平均三相电流',
                        type: 'line',
                        yAxisIndex: 0,
                        symbol: 'circle',
                        itemStyle: {
                            color: '#E7A900'
                        },
                        smooth: true,
                        lineStyle: {
                            color: '#E7A900'
                        },
                        data: this.echartsData.electricAvg.map(it => +it.value)
                    },
                    {
                        name: '平均三相温度',
                        type: 'line',
                        yAxisIndex: 1,
                        symbol: 'circle',
                        itemStyle: {
                            color: '#2D9CDB'
                        },
                        smooth: true,
                        lineStyle: {
                            color: '#2D9CDB',
                        },
                        data: this.echartsData.temperatureAvg.map(it => +it.value)
                    }
                ]
            };

            option && myChart.setOption(option);
        }
    }
}
</script>

<style lang="scss" scoped>
#temperature-main {
    width: 100%;
    height: 100%;
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值