vue3中使用Vchart的示例代碼
使用vue開發(fā)的web項(xiàng)目中使用圖表,可以使用v-charts,它基于 Vue 和 echarts 封裝的 v-charts 圖表組件,只需要統(tǒng)一提供一種對前后端都友好的數(shù)據(jù)格式設(shè)置簡單的配置項(xiàng),便可輕松生成常見的圖表,避免了做繁瑣的數(shù)據(jù)類型轉(zhuǎn)化、修改復(fù)雜的配置項(xiàng)。
步驟
1、npm 安裝
npm install echarts vue-echarts
2、在vue中使用
折線圖

代碼:
<template>
<div>
<h3>下載流量 <span style="font-weight: normal">7天內(nèi)趨勢</span></h3>
<v-chart
style="width: 100%; height: 210px"
:option="(chartOptions as EChartsOption)"
></v-chart>
</div>
</template>
<script setup lang="ts">
import { onMounted,ref } from 'vue'
import { use } from 'echarts/core'
import { LineChart } from 'echarts/charts'
import { GridComponent, LegendComponent, TooltipComponent } from 'echarts/components'
import VChart from 'vue-echarts'
import { CanvasRenderer } from 'echarts/renderers'
import type { EChartsOption } from 'echarts'
import dayjs from 'dayjs'
use([CanvasRenderer, LineChart, GridComponent, LegendComponent, TooltipComponent])
const sevenDays: string[] = []
const todayNow = dayjs()
for (let i = 6; i >= 0; i--) {
const day = todayNow.add(-i, 'days').format('YYYY/MM/DD')
sevenDays.push(day)
}
const chartOptions = ref({
//提示框
tooltip: {
trigger: 'axis',
formatter: function (data: any) {
let formatValue
if (data[0] && data[0].value) {
formatValue = `${Utils.getFileSize(data[0].value)}`
} else {
formatValue = 0
}
return data[0].axisValueLabel + '<br />' + data[0].marker + formatValue
}
},
//橫軸配置
xAxis: {
type: 'category',
data: sevenDays,
lineWidth: 0,
left: '-2%',
axisTick: {
show: false
}
},
//縱軸配置
yAxis: {
type: 'value',
axisLabel: {
formatter: (value: number) => {
if (value) return `${Utils.getFileSize(value)}`
return 0
}
}
},
//數(shù)據(jù)集配置
series: [
{
data: [0, 0, 0, 0, 0, 0, 0],
type: 'line'
}
],
grid: {
top: '20px',
bottom: '0px',
left: '0px',
right: '20px',
containLabel: true
}
})
onMounted(() => {
FirmReq.getFirmFlow(firmStore().profile?.id!).then((res) => {
const daysData = [...sevenDays]
//daysData = ['2024/01/10', '2024/01/11', '2024/01/12', '2024/01/13', '2024/01/14', '2024/01/15', '2024/01/16']
/**
* 接口返回?cái)?shù)據(jù)res.data.data
* [{dt: "2024/01/12",total: "13029932"},{dt: "2024/01/16",total: "18977"}]
*/
console.log('daysData', daysData)
chartOptions.value.series = [
{
type: 'line',
data: daysData.map((day) => {
const data = res.data.data.find(
(_item: { dt: string; total: string }) => _item.dt == day
)
if (data && data.total) return Number(data.total)
return 0
})
}
]
})
})
</script>
餅圖

代碼
<template>
<div style="width: 100%; height: 80%; margin-top: 10px">
<v-chart class="chart" :option="deviceOption"></v-chart>
</div>
</template>
<script setup lang="ts">
import { onMounted, ref} from 'vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { PieChart } from 'echarts/charts'
import type { EChartsOption } from 'echarts'
import VChart from 'vue-echarts'
use([CanvasRenderer, PieChart, TitleComponent, TooltipComponent, LegendComponent, GraphicComponent])
const deviceOption =ref({
tooltip: {
trigger: 'item'
},
legend: {
orient: 'vertical',
top: '10%',
left: '55%',
data: ['新版本', '舊版本']
},
series: [
{
name:'固件Agent分布',
type: 'pie',
radius: ['60%', '90%'],
center: ['25%', '50%'],
avoidLabelOverlap: false,
label: {
show: false,
position: 'center'
},
labelLine: {
show: false
},
data: [
{ value: 0, name: '新版本' },
{ value: 0, name: '舊版本' }
]
}
],
color: ['#6095CA', '#BAC8DB']
})
onMounted(() => {
StatisReq.teamDesktopVersion().then((res) => {
const { latest, low } = res.data
const deviceOptionLabel = `新版本 (${Number(latest)}臺(tái))`
const deviceOptionLabel1 = `舊版本 (${Number(low)}臺(tái))`
//更新餅圖數(shù)據(jù)
deviceOption.value.series[0].data[0] = { value: Number(latest), name: deviceOptionLabel }
deviceOption.value.series[0].data[1] = { value: Number(low), name: deviceOptionLabel1 }
deviceOption.value.legend.data[0] = deviceOptionLabel
deviceOption.value.legend.data[1] = deviceOptionLabel1
})
})
</script> 3、添加圖例點(diǎn)擊事件
// 組件添加legendselectchanged事件
<v-chart :option="option2" @legendselectchanged="legendselectchanged"></v-chart>
// script標(biāo)簽中代碼
const legendselectchanged = (res) => {
// 點(diǎn)擊圖例默認(rèn)會(huì)改變系列的顯示狀態(tài)
// 自定義圖例點(diǎn)擊事件需要先阻止這個(gè)默認(rèn)事件
// legend: {selectedMode: false},這個(gè)屬性可以使點(diǎn)擊事件不起效,但同樣自定義legendselectchanged 事件也會(huì)失效,因此不能通過 selectedMode: false 來控制
// option2.value.legend.selected = {[res.name] : true};這句代碼可以使點(diǎn)擊的圖例系列重新顯示
option2.value.legend.selected = {[res.name] : true};
//下面就可以做一些你想做的功能
// .....
}
到此這篇關(guān)于vue3中使用Vchart的示例代碼的文章就介紹到這了,更多相關(guān)vue3使用Vchart內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3+echarts實(shí)現(xiàn)好看的圓角環(huán)形圖
- echarts設(shè)置tootip輪播切換展示(vue3搭配vue-echarts粘貼即用)
- 手把手教你Vue3?按需引入?Echarts的過程(收藏)
- vue導(dǎo)出excel和echart圖形分別在不同工作表的實(shí)現(xiàn)方法
- vue使用echarts實(shí)現(xiàn)柱狀圖動(dòng)態(tài)排序效果
- vue2.0如何實(shí)現(xiàn)echarts餅圖(pie)效果展示
- vue中如何使用echarts動(dòng)態(tài)渲染數(shù)據(jù)
- vue使用echarts實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)的示例詳解
- Vuex進(jìn)行Echarts數(shù)據(jù)頁面初始化后如何更新dom
- vue+echarts圖表的基本使用步驟總結(jié)
- 在Vue中使用Echarts+封裝
- 使用vue3+ts打開echarts的正確方式
相關(guān)文章
vue使用Google地圖的實(shí)現(xiàn)示例代碼
這篇文章主要介紹了vue使用Google地圖的實(shí)現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Vue使用sign-canvas實(shí)現(xiàn)在線手寫簽名的實(shí)例
sign-canvas?一個(gè)基于?canvas?開發(fā),封裝于?Vue?組件的通用手寫簽名板(電子簽名板),支持?pc?端和移動(dòng)端,本文給大家分享Vue使用sign-canvas實(shí)現(xiàn)在線手寫簽名,感興趣的朋友一起看看吧2022-05-05
Nginx部署前端Vue項(xiàng)目的步驟、常見問題與解決方案
在現(xiàn)代Web開發(fā)中,Vue.js成為前端開發(fā)者構(gòu)建單頁應(yīng)用的熱門框架,Nginx以其高性能和穩(wěn)定性,成為部署Vue項(xiàng)目的理想選擇,這篇文章主要介紹了Nginx部署前端Vue項(xiàng)目的步驟、常見問題與解決方案,需要的朋友可以參考下2024-09-09
vue & vue Cli 版本及對應(yīng)關(guān)系解讀
這篇文章主要介紹了vue & vue Cli 版本及對應(yīng)關(guān)系,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
.netcore+vue 實(shí)現(xiàn)壓縮文件下載功能
這篇文章主要介紹了.netcore+vue 實(shí)現(xiàn)壓縮文件下載功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
詳解Electron中如何使用SQLite存儲(chǔ)筆記
這篇文章主要為大家介紹了Electron中如何使用SQLite存儲(chǔ)筆記示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
VUE多個(gè)下拉框?qū)崿F(xiàn)雙向聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了VUE多個(gè)下拉框?qū)崿F(xiàn)雙向聯(lián)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
VUE前端工程報(bào)錯(cuò)監(jiān)控問題及解決
文章介紹了在Vue3項(xiàng)目中實(shí)現(xiàn)錯(cuò)誤捕獲和郵件報(bào)錯(cuò)上報(bào)的方法,首先使用了`errorHandler`全局錯(cuò)誤捕獲,接著通過Nodemailer發(fā)送郵件實(shí)現(xiàn)錯(cuò)誤上報(bào)功能,過程中遇到一些坑,如需本地調(diào)試需用node啟動(dòng)服務(wù),同時(shí)總結(jié)了兩種方案的,包括直接存在的問題和限制2026-04-04
vue實(shí)現(xiàn)動(dòng)態(tài)給data函數(shù)中的屬性賦值
這篇文章主要介紹了vue實(shí)現(xiàn)動(dòng)態(tài)給data函數(shù)中的屬性賦值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Ubuntu系統(tǒng)下部署Vue/Vite應(yīng)用到Nginx完整步驟
這篇文章主要介紹了Ubuntu系統(tǒng)下部署Vue/Vite應(yīng)用到Nginx的相關(guān)資料,包括配置加載優(yōu)先級(jí)、部署前準(zhǔn)備工作、Nginx配置方案、文件部署與權(quán)限設(shè)置、訪問方式、故障排查、快速命令匯總和部署流程圖,需要的朋友可以參考下2026-02-02

