Vue3使用ECharts實(shí)現(xiàn)?;鶊D的代碼示例
1. 前言
?;鶊D是一種用于直觀顯示流向數(shù)據(jù)的可視化工具,特別適合展示復(fù)雜的網(wǎng)絡(luò)關(guān)系和資源流動(dòng)。在前端項(xiàng)目中,通過(guò)結(jié)合 Vue 3 和 ECharts,可以快速實(shí)現(xiàn)交互性強(qiáng)、樣式美觀的?;鶊D。本文將通過(guò)完整的代碼示例,帶你一步步完成一個(gè)?;鶊D的實(shí)現(xiàn)。
2. 項(xiàng)目準(zhǔn)備
2.1 安裝必要的依賴(lài)
在 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
,用于封裝桑基圖的實(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)目后,訪問(wèn)相應(yīng)頁(yè)面,即可看到一個(gè)動(dòng)態(tài)的桑基圖,展示了各省份之間的流向數(shù)據(jù)。
5. 功能擴(kuò)展
數(shù)據(jù)動(dòng)態(tài)更新
- 將圖表數(shù)據(jù)通過(guò) 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é)
本文通過(guò)完整的代碼示例,展示了如何使用 Vue 3 和 ECharts 實(shí)現(xiàn)桑基圖。從項(xiàng)目搭建到細(xì)節(jié)優(yōu)化,都進(jìn)行了詳細(xì)講解。希望這篇文章對(duì)你有所幫助!
到此這篇關(guān)于Vue3使用ECharts實(shí)現(xiàn)?;鶊D的代碼示例的文章就介紹到這了,更多相關(guān)Vue3 ECharts桑基圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+ElementUI前端添加展開(kāi)收起搜索框按鈕完整示例
最近一直在用element ui做后臺(tái)項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于Vue+ElementUI前端添加展開(kāi)收起搜索框按鈕的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05Vue props實(shí)現(xiàn)父組件給子組件傳遞數(shù)據(jù)的方式
Vue中的配置項(xiàng)Props能讓組件接收外部傳遞過(guò)來(lái)的數(shù)據(jù),本文給大家介紹了Vue props實(shí)現(xiàn)父組件給子組件傳遞數(shù)據(jù)的幾種方式,文中有詳細(xì)的實(shí)現(xiàn)方式,具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10vue element-ui表格自定義動(dòng)態(tài)列具體實(shí)現(xiàn)
這周工作中遇見(jiàn)了一個(gè)表格動(dòng)態(tài)列的需求,下面這篇文章主要給大家介紹了關(guān)于vue element-ui表格自定義動(dòng)態(tài)列具體實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06vue cli4中mockjs在dev環(huán)境和build環(huán)境的配置詳情
這篇文章主要介紹了vue cli4中mockjs在dev環(huán)境和build環(huán)境的配置詳情,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue3.0中使用websocket,封裝到公共方法的實(shí)現(xiàn)
這篇文章主要介紹了vue3.0中使用websocket,封裝到公共方法的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10基于vue框架手寫(xiě)一個(gè)notify插件實(shí)現(xiàn)通知功能的方法
這篇文章主要介紹了基于vue框架手寫(xiě)一個(gè)notify插件實(shí)現(xiàn)通知功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03vue3使用別名報(bào)錯(cuò)問(wèn)題的解決辦法(vetur插件報(bào)錯(cuò)問(wèn)題)
最近在寫(xiě)一個(gè)購(gòu)物網(wǎng)站使用vue,使用中遇到了問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于vue3使用別名報(bào)錯(cuò)問(wèn)題的解決辦法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07在vue項(xiàng)目中集成graphql(vue-ApolloClient)
這篇文章主要介紹了在vue項(xiàng)目中集成graphql(vue-ApolloClient),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09