vue3中使用Vchart的示例代码
作者:智慧女孩要秃头~
使用vue开发的web项目中使用图表,可以使用v-charts,本文主要介绍了vue3中使用Vchart的示例代码,具有一定的参考价值,感兴趣的可以了解一下
使用vue开发的web项目中使用图表,可以使用v-charts,它基于 Vue 和 echarts 封装的 v-charts 图表组件,只需要统一提供一种对前后端都友好的数据格式设置简单的配置项,便可轻松生成常见的图表,避免了做繁琐的数据类型转化、修改复杂的配置项。
步骤
1、npm 安装
npm install echarts vue-echarts
2、在vue中使用
折线图

代码:
<template>
<div>
<h3>下载流量 <span style="font-weight: normal">7天内趋势</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
}
}
},
//数据集配置
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']
/**
* 接口返回数据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)}台)`
const deviceOptionLabel1 = `旧版本 (${Number(low)}台)`
//更新饼图数据
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、添加图例点击事件
// 组件添加legendselectchanged事件
<v-chart :option="option2" @legendselectchanged="legendselectchanged"></v-chart>
// script标签中代码
const legendselectchanged = (res) => {
// 点击图例默认会改变系列的显示状态
// 自定义图例点击事件需要先阻止这个默认事件
// legend: {selectedMode: false},这个属性可以使点击事件不起效,但同样自定义legendselectchanged 事件也会失效,因此不能通过 selectedMode: false 来控制
// option2.value.legend.selected = {[res.name] : true};这句代码可以使点击的图例系列重新显示
option2.value.legend.selected = {[res.name] : true};
//下面就可以做一些你想做的功能
// .....
}
到此这篇关于vue3中使用Vchart的示例代码的文章就介绍到这了,更多相关vue3使用Vchart内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- vue3+echarts实现好看的圆角环形图
- echarts设置tootip轮播切换展示(vue3搭配vue-echarts粘贴即用)
- 手把手教你Vue3 按需引入 Echarts的过程(收藏)
- vue导出excel和echart图形分别在不同工作表的实现方法
- vue使用echarts实现柱状图动态排序效果
- vue2.0如何实现echarts饼图(pie)效果展示
- vue中如何使用echarts动态渲染数据
- vue使用echarts实现动态数据的示例详解
- Vuex进行Echarts数据页面初始化后如何更新dom
- vue+echarts图表的基本使用步骤总结
- 在Vue中使用Echarts+封装
- 使用vue3+ts打开echarts的正确方式
