首页 前端知识 vue3 typescript(Echarts正确使用姿势)

vue3 typescript(Echarts正确使用姿势)

2024-03-13 00:03:13 前端知识 前端哥 705 905 我要收藏

下载echarts依赖

npm install echarts -S

在main.ts中引入

import * as ECharts from 'echarts'
app.config.globalProperties.$ECharts = ECharts

示例图:

 

下面是全部代码

<template>
    <div id="myChart" :style="{ width: '1000px', height: '500px' }"></div>
</template>
 
<script lang="ts">
import { defineComponent, onMounted, getCurrentInstance } from 'vue'
export default defineComponent({
    setup() {
        const { proxy } = getCurrentInstance() as any
        const option = {
            title: {
                text: 'Stacked Line'
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name: 'Email',
                    type: 'line',
                    stack: 'Total',
                    data: [120, 132, 101, 134, 90, 230, 210]
                },
                {
                    name: 'Union Ads',
                    type: 'line',
                    stack: 'Total',
                    data: [220, 182, 191, 234, 290, 330, 310]
                },
                {
                    name: 'Video Ads',
                    type: 'line',
                    stack: 'Total',
                    data: [150, 232, 201, 154, 190, 330, 410]
                },
                {
                    name: 'Direct',
                    type: 'line',
                    stack: 'Total',
                    data: [320, 332, 301, 334, 390, 330, 320]
                },
                {
                    name: 'Search Engine',
                    type: 'line',
                    stack: 'Total',
                    data: [820, 932, 901, 934, 1290, 1330, 1320]
                }
            ]
        }
        onMounted(() => {
            // 获取挂载的组件实例
            const echarts = proxy.$ECharts
            //初始化挂载
            const echarts1 = echarts.init(document.getElementById('myChart'))
            //添加配置
            echarts1.setOption(option)
            // 自适应
            window.onresize = function () {
                echarts1.resize()
            }
        })
        return {}
    }
})
</script>

转载请注明出处或者链接地址:https://www.qianduange.cn//article/3745.html
标签
typescript
评论
发布的文章

JavaScript基础库

2024-04-09 00:04:11

解决vue npm 下载echart出错

2024-04-09 00:04:05

Echarts中option属性设置

2024-04-08 23:04:58

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!