以组件的形成加载echart图

前言

工作中经常要绘制echart图,为了防止一个.vue文件代码过多,echart都会单独抽离到一个.vue文件里,然后通过组件的形式加载。写了很多了,个人感觉下面的这种形式是比较好的

示例

父组件中加载

<send-money-back-line ref="lineRef"></send-money-back-line>

this.$refs.lineRef.init();

echart代码

<template>
    <div class="line" ref="line">
    </div>
</template>

<script>
import echarts from 'echarts';
export default {
    data() {
        return {
        };
    },
    mounted() {
        this.init();
    },
    methods: {
        // 初始化柱状图
        init() {
            let legend = ['21年应收账款', '20年应收账款', '19年应收账款'];
            let xAxisData = ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'];
            let seriesData = [
                [135, 147, 260,150,150, 230, 224, 218, 230, 224, 218, 135],
                [150, 230, 224, 218, 135, 147, 260,150, 230, 224, 218, 135],
                [260,150, 230, 224, 218, 135,150, 230, 224, 218, 135, 147]
            ];
            this.setOption(legend,xAxisData,seriesData);
        },
        setOption(legend,xAxisData,seriesData) {
            let myChart = echarts.init(this.$refs.line);
            let option = {
                tooltip: {
                    trigger: 'axis'
                },
                title: {
                    left: 'center',
                    textStyle: {
                        fontSize: 28,
                        color: '#755978'
                    }
                },
                legend: {
                    data: legend,
                    top: '5%'
                },
                xAxis: {
                    type: 'category',
                    data: xAxisData
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: seriesData[0],
                        type: 'line',
                        name: legend[0],
                        color: '#4F81BD'
                    },
                    {
                        data: seriesData[1],
                        type: 'line',
                        name: legend[1],
                        color: '#C0504D'
                    },
                    {
                        data: seriesData[2],
                        type: 'line',
                        name: legend[2],
                        color: '#9BBB59'
                    }
                ]
            };
            myChart.setOption(option);
            window.addEventListener('resize', function() {
                myChart.resize();
            });
        }
    }
};
</script>

<style scoped lang="scss">
.line{
    width: 100%;
    height: 100%;
}
</style>

效果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无知的小菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值