vue3引入Echarts图表

说明:echarts是父组件,stack是子组件,将stack引入到echarts文件中

查看echarts.vue时可看到stack.vue中的图表

#

#

#

引入方式

第一步

Echarts官网:快速上手 - 使用手册 - Apache ECharts

第二步

在控制台安装Echarts模块,例如:

// 安装命令
npm install echarts
第三步 

在main.js文件中引入echarts

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
// 配置1
import * as echarts from "echarts";

const app = createApp(App)
app.use(router)
app.mount('#app')
// 配置2
app.config.globalProperties.$echarts = echarts;
第四步 

在components文件夹下创建一个文件为stack.vue的组件,复制代码,例如:

<template>
    <h1>我是stack</h1>
    <div id="stack" :style="{ width: '400px', height: '400px',marginTop:'10px' }"></div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import * as echarts from 'echarts'
const stackChart = ref(null)

const state = {
    option: {
        title: {
            text: 'Stacked Area Chart'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#6a7985'
                }
            }
        },
        legend: {
            data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                boundaryGap: false,
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                name: 'Email',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [120, 132, 101, 134, 90, 230, 210]
            },
            {
                name: 'Union Ads',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [220, 182, 191, 234, 290, 330, 310]
            },
            {
                name: 'Video Ads',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [150, 232, 201, 154, 190, 330, 410]
            },
            {
                name: 'Direct',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [320, 332, 301, 334, 390, 330, 320]
            },
            {
                name: 'Search Engine',
                type: 'line',
                stack: 'Total',
                label: {
                    show: true,
                    position: 'top'
                },
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [820, 932, 901, 934, 1290, 1330, 1320]
            }
        ]
    }
}

// vue3钩子函数
onMounted(() => {
    stackChart.value = echarts.init(document.getElementById('stack'))
    stackChart.value.setOption(state.option)
})


</script>

<style lang="less" scoped></style>
第五步

在components文件夹下创建一个文件为echarts.vue的组件,复制代码,例如:

<template>
    <h1>echarts</h1>
    <div id="rose" :style="{ width: '200px', height: '200px'}"></div>
    <!-- 引入的stack -->
    <Stack></Stack>
</template>

<script setup>
import { ref, onMounted } from 'vue'
// 引入的stack文件
import Stack from './stack.vue'
import * as echarts from 'echarts'
const roseChart = ref(null)

const state = {
    option: {
        series: [
            {
                type: 'pie',
                data: [
                    { value: 100, name: 'A' },
                    { value: 200, name: 'B' },
                    { value: 300, name: 'C' },
                    { value: 400, name: 'D' },
                    { value: 500, name: 'E' }
                ],
                roseType: 'area'
            }
        ]
    }
}

// 使用钩子函数
onMounted(() => {
    roseChart.value = echarts.init(document.getElementById('rose'))
    roseChart.value.setOption(state.option)
})
</script>

<style lang="less" scoped></style>

可进入Echarts官网自行配置自己所需要的图标:Examples - Apache ECharts

效果展示:

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值