可以的,ECharts 可以和 Vue 很好地结合使用,而且这是前端数据可视化项目中非常常见的组合。
下面给你几种常见、实用的结合方式:
npm install echarts
<template>
<div ref="chart" style="width: 600px; height: 400px;"></div>
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts'
const chart = ref(null)
let myChart = null
onMounted(() => {
myChart = echarts.init(chart.value)
myChart.setOption({
title: { text: '示例' },
tooltip: {},
xAxis: { data: ['A', 'B', 'C'] },
yAxis: {},
series: [{ type: 'bar', data: [10, 20, 30] }]
})
})
onBeforeUnmount(() => {
myChart && myChart.dispose()
})
</script>
✅ 优点:简单直接,适合大多数项目
✅ 兼容 Vue2 / Vue3
npm install vue-echarts echarts
Vue3 示例:
<template>
<v-chart :option="option" style="height: 400px;" />
</template>
<script setup>
import VChart from 'vue-echarts'
import { use } from 'echarts/core'
import { BarChart } from 'echarts/charts'
import { TitleComponent, TooltipComponent } from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'
use([
BarChart,
TitleComponent,
TooltipComponent,
CanvasRenderer
])
const option = {
title: { text: 'Vue + ECharts' },
xAxis: { data: ['A', 'B', 'C'] },
yAxis: {},
series: [{ type: 'bar', data: [5, 15, 25] }]
}
</script>
✅ 自动响应式
✅ 支持 option 动态更新
✅ 更适合中大型项目
chart.resize()dispose(),防止内存泄漏<script setup>如果你告诉我:
我可以给你更具体的示例或封装方案。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。