下载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>
复制