Vue3使用ECharts實(shí)現(xiàn)桑基圖的代碼示例
1. 前言
?;鶊D是一種用于直觀顯示流向數(shù)據(jù)的可視化工具,特別適合展示復(fù)雜的網(wǎng)絡(luò)關(guān)系和資源流動(dòng)。在前端項(xiàng)目中,通過結(jié)合 Vue 3 和 ECharts,可以快速實(shí)現(xiàn)交互性強(qiáng)、樣式美觀的?;鶊D。本文將通過完整的代碼示例,帶你一步步完成一個(gè)?;鶊D的實(shí)現(xiàn)。
2. 項(xiàng)目準(zhǔn)備
2.1 安裝必要的依賴
在 Vue 3 項(xiàng)目中使用 ECharts,首先需要安裝 ECharts 包:
npm install echarts
2.2 創(chuàng)建項(xiàng)目結(jié)構(gòu)
在項(xiàng)目中創(chuàng)建一個(gè)組件文件,例如 SankeyChart.vue,用于封裝?;鶊D的實(shí)現(xiàn)邏輯和展示。
3. 代碼實(shí)現(xiàn)
以下是完整的代碼實(shí)現(xiàn),包括模板、腳本和樣式。
<!--
* @Author: 彭麒
* @Date: 2025/1/20
* @Email: 1062470959@qq.com
* @Description: 此源碼版權(quán)歸吉檀迦俐所有,可供學(xué)習(xí)和借鑒或商用。
-->
<template>
<div class="w-full justify-start flex h-[180px] items-center pl-10">
<BackButton @click="goBack"/>
</div>
<div class="font-bold text-[24px]">在Vue3中使用Echarts實(shí)現(xiàn)?;鶊D</div>
<div class="chart-container">
<div ref="chartRef" class="sankey-chart"></div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'
import BackButton from "@/views/components/BackButton.vue";
import router from "@/router";
const goBack = () => {
setTimeout(() => {
router.push('/Echarts')
}, 1000)
}
const chartRef = ref<HTMLElement | null>(null)
let chart: echarts.ECharts | null = null
const initChart = () => {
if (!chartRef.value) return
chart = echarts.init(chartRef.value)
const option = {
color: ['#7BC074', '#709EF1', '#F59363'],
series: [{
type: 'sankey',
draggable: false,
left: '8%',
right: '8%',
data: [
// 左點(diǎn)
{ name: '安徽', label: { position: 'left' } },
{ name: '廣西', label: { position: 'left' } },
{ name: '江西', label: { position: 'left' } },
{ name: '青海', label: { position: 'left' } },
{ name: '湖南', label: { position: 'left' } },
{ name: '四川', label: { position: 'left' } },
{ name: '湖北', label: { position: 'left' } },
// 右點(diǎn)
{ name: '江蘇 ', label: { position: 'right' } },
{ name: '廣東 ', label: { position: 'right' } },
{ name: '浙江 ', label: { position: 'right' } },
{ name: '重慶', label: { position: 'right' } }
],
links: [
{ source: '安徽', target: '江蘇 ', value: 18.68 },
{ source: '安徽', target: '浙江 ', value: 12.38 },
{ source: '廣西', target: '廣東 ', value: 30.36 },
{ source: '江西', target: '廣東 ', value: 12.48 },
{ source: '江西', target: '浙江 ', value: 12.67 },
{ source: '青海', target: '廣東 ', value: 13.47 },
{ source: '青海', target: '浙江 ', value: 11.03 },
{ source: '湖南', target: '廣東 ', value: 19.11 },
{ source: '四川', target: '重慶 ', value: 15.02 },
{ source: '湖北', target: '廣東 ', value: 11.66 }
],
label: {
normal: {
color: 'rgba(9, 27, 61, 0.6)',
fontSize: 14,
fontWeight: '400'
}
},
itemStyle: {
normal: {
borderWidth: 1,
borderColor: '#aaa'
}
},
lineStyle: {
normal: {
color: 'gradient',
borderColor: 'black',
borderWidth: 0,
opacity: 0.3,
curveness: 0.6
}
}
}]
}
chart.setOption(option)
}
const handleResize = () => {
chart?.resize()
}
onMounted(() => {
initChart()
window.addEventListener('resize', handleResize)
})
onUnmounted(() => {
chart?.dispose()
window.removeEventListener('resize', handleResize)
})
</script>
<style scoped>
.chart-container {
width: 100%;
height: 70%;
min-height: 600px;
background-color: #fff;
}
.sankey-chart {
width: 100%;
height: 100%;
}
@media screen and (max-width: 768px) {
.chart-container {
min-height: 400px;
}
}
@media screen and (max-width: 480px) {
.chart-container {
min-height: 300px;
}
}
</style>4. 運(yùn)行效果
運(yùn)行項(xiàng)目后,訪問相應(yīng)頁面,即可看到一個(gè)動(dòng)態(tài)的桑基圖,展示了各省份之間的流向數(shù)據(jù)。

5. 功能擴(kuò)展
數(shù)據(jù)動(dòng)態(tài)更新
- 將圖表數(shù)據(jù)通過 API 獲取,并動(dòng)態(tài)更新圖表內(nèi)容。
- 示例:
chart.setOption({ series: [{ data: newData, links: newLinks }] });交互功能
- 添加鼠標(biāo)懸停事件,展示詳細(xì)信息。
- 使用
tooltip配置實(shí)現(xiàn):
tooltip: { trigger: 'item', formatter: ': {c}', }6. 總結(jié)
本文通過完整的代碼示例,展示了如何使用 Vue 3 和 ECharts 實(shí)現(xiàn)?;鶊D。從項(xiàng)目搭建到細(xì)節(jié)優(yōu)化,都進(jìn)行了詳細(xì)講解。希望這篇文章對(duì)你有所幫助!
到此這篇關(guān)于Vue3使用ECharts實(shí)現(xiàn)?;鶊D的代碼示例的文章就介紹到這了,更多相關(guān)Vue3 ECharts?;鶊D內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)vuex中store和$store的區(qū)別說明
這篇文章主要介紹了對(duì)vuex中store和$store的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
基于uniapp+vue3?實(shí)現(xiàn)72小時(shí)倒計(jì)時(shí)功能
數(shù)組項(xiàng)有一個(gè)下單時(shí)間?,比如今天下單在72小時(shí)內(nèi)可以繼續(xù)支付,超過則默認(rèn)取消訂單,下面通過實(shí)例代碼介紹uniapp+vue3?實(shí)現(xiàn)72小時(shí)倒計(jì)時(shí)功能,感興趣的朋友一起看看吧2025-05-05
vue中如何實(shí)時(shí)監(jiān)聽本地存儲(chǔ)
這篇文章主要介紹了vue中如何實(shí)時(shí)監(jiān)聽本地存儲(chǔ),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue項(xiàng)目markdown內(nèi)容預(yù)覽顯示優(yōu)化方式
在Vue項(xiàng)目中使用markdown-it組件預(yù)覽Markdown內(nèi)容時(shí),通過安裝pnpm和markdown-it-github-markdown-css依賴,并重寫markdown部分樣式,實(shí)現(xiàn)了Markdown內(nèi)容的美化,在MarkdownPreview.vue組件中,通過變量renderedMarkdown接收并渲染Markdown內(nèi)容2025-10-10

