Vue3繪制多系列柱狀圖與曲線圖的示例代碼
一、引言
在數(shù)據(jù)可視化領(lǐng)域,圖表是展示復(fù)雜數(shù)據(jù)的重要工具。在Vue3項目中,結(jié)合ECharts庫,我們可以輕松實現(xiàn)各種類型的圖表展示,幫助用戶更直觀地理解數(shù)據(jù)背后的規(guī)律。本文將詳細介紹如何在Vue3中實現(xiàn)兩種常見的圖表類型:多系列柱狀圖和堆疊曲線圖,并結(jié)合實際場景進行數(shù)據(jù)展示。
二、項目背景
假設(shè)我們正在開發(fā)一個智能農(nóng)業(yè)監(jiān)控系統(tǒng),需要展示不同農(nóng)業(yè)設(shè)備的能耗數(shù)據(jù)以及不同大棚的溫度變化數(shù)據(jù)。系統(tǒng)需要展示兩種類型的圖表:
- 多系列柱狀圖:展示不同月份下,兩個不同年份的灌溉系統(tǒng)能耗對比。
- 堆疊曲線圖:展示不同農(nóng)業(yè)設(shè)備在一天中的能耗變化趨勢。
三、實現(xiàn)多系列柱狀圖
1) 場景描述
我們監(jiān)控某農(nóng)業(yè)基地的月度灌溉系統(tǒng)能耗數(shù)據(jù),對比2023年和2024年各月份的能耗情況。通過柱狀圖,可以直觀地看到不同年份同一月份的能耗差異,幫助分析能耗變化趨勢。
2) 數(shù)據(jù)準(zhǔn)備
const barChartData = {
categories: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
values1: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120], // 2023年數(shù)據(jù)
values2: [5, 15, 25, 35, 45, 55, 65, 75, 85, 95, 105, 115] // 2024年數(shù)據(jù)
}
3) 圖表配置與初始化
let barChartInstance = null;
const initBarChart = () => {
const chartDom = document.getElementById('barChart');
if (!chartDom) return;
barChartInstance = echarts.init(chartDom);
const option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
legend: {
data: ['2023年灌溉能耗', '2024年灌溉能耗'],
top: 10,
textStyle: {
color: '#333'
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
data: barChartData.categories,
axisLine: {
lineStyle: {
color: '#d1d8e0'
}
}
},
yAxis: {
type: 'value',
splitLine: {
lineStyle: {
color: '#eee'
}
},
axisLine: {
lineStyle: {
color: '#d1d8e0'
}
},
name: '能耗(kWh)'
},
series: [
{
name: '2023年灌溉能耗',
type: 'bar',
data: barChartData.values1,
itemStyle: {
color: '#5470C6'
},
barWidth: '40%'
},
{
name: '2024年灌溉能耗',
type: 'bar',
data: barChartData.values2,
itemStyle: {
color: '#91CC75'
},
barWidth: '40%'
}
]
};
barChartInstance.setOption(option);
};
4) 效果說明
- 圖表展示了2023年和2024年各月份的灌溉系統(tǒng)能耗對比。
- 藍色柱子代表2023年數(shù)據(jù),綠色柱子代表2024年數(shù)據(jù)。
- 鼠標(biāo)懸停在柱子上時,會顯示具體數(shù)值,方便對比分析。
四、實現(xiàn)堆疊曲線圖
1) 場景描述
我們監(jiān)控農(nóng)業(yè)基地內(nèi)不同設(shè)備(如灌溉泵、溫室通風(fēng)機、補光燈等)在一天中的能耗變化。通過堆疊曲線圖,可以清晰地看到各設(shè)備在不同時間段的能耗占比,以及整體能耗的變化趨勢。
2) 數(shù)據(jù)準(zhǔn)備
const chartDataStacked = {
categories: ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00',
'12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'],
series: [
{ name: '灌溉泵', data: [10, 20, 15, 25, 30, 22, 10, 20, 15, 25, 30, 22, 10, 20, 15, 25, 30, 22, 10, 20, 15, 25, 30, 22] },
{ name: '溫室通風(fēng)機', data: [5, 15, 10, 20, 25, 17, 5, 15, 10, 20, 25, 17, 5, 15, 10, 20, 25, 17, 5, 15, 10, 20, 25, 17] },
{ name: '補光燈', data: [8, 18, 13, 23, 28, 20, 8, 18, 13, 23, 28, 20, 8, 18, 13, 23, 28, 20, 8, 18, 13, 23, 28, 20] },
{ name: '溫控系統(tǒng)', data: [12, 22, 17, 27, 32, 24, 12, 22, 17, 27, 32, 24, 12, 22, 17, 27, 32, 24, 12, 22, 17, 27, 32, 24] }
]
};
3) 圖表配置與初始化
let stackedChartInstance = null;
const initStackedChart = () => {
const chartDom = document.getElementById('stackedLineChart');
if (!chartDom) return;
stackedChartInstance = echarts.init(chartDom);
const option = {
tooltip: {
trigger: 'axis',
backgroundColor: 'rgba(0, 0, 0, 0.7)',
borderColor: '#333',
textStyle: { color: '#fff' }
},
legend: {
data: chartDataStacked.series.map(item => item.name),
top: 30,
textStyle: { color: '#333' }
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap: false,
data: chartDataStacked.categories,
axisLine: {
lineStyle: {
color: '#d1d8e0'
}
}
},
yAxis: {
type: 'value',
name: '能耗(kWh)',
axisLine: {
lineStyle: {
color: '#d1d8e0'
}
}
},
series: chartDataStacked.series.map(item => ({
name: item.name,
type: 'line',
stack: '總量',
smooth: true,
lineStyle: {
width: 2
},
data: item.data
}))
};
stackedChartInstance.setOption(option);
};
4) 效果說明
- 圖表展示了灌溉泵、溫室通風(fēng)機、補光燈和溫控系統(tǒng)在一天中的能耗變化。
- 曲線采用堆疊方式,可以清晰地看到各設(shè)備在不同時間段的能耗占比。
- 鼠標(biāo)懸停在曲線上時,會顯示具體數(shù)值和設(shè)備名稱,方便分析。
五、完整代碼
<template>
<div class="chart-container">
<div id="barChart" class="chart"></div>
<div id="stackedLineChart" class="chart"></div>
</div>
</template>
<script setup>
import { onMounted, onBeforeUnmount } from 'vue';
import * as echarts from 'echarts';
// 柱狀圖數(shù)據(jù) - 灌溉系統(tǒng)月度能耗對比
const barChartData = {
categories: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
values1: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120], // 2023年數(shù)據(jù)
values2: [5, 15, 25, 35, 45, 55, 65, 75, 85, 95, 105, 115] // 2024年數(shù)據(jù)
};
// 堆疊曲線圖數(shù)據(jù) - 農(nóng)業(yè)設(shè)備24小時能耗變化
const chartDataStacked = {
categories: [
'00:00', '01:00', '02:00', '03:00', '04:00', '05:00',
'06:00', '07:00', '08:00', '09:00', '10:00', '11:00',
'12:00', '13:00', '14:00', '15:00', '16:00', '17:00',
'18:00', '19:00', '20:00', '21:00', '22:00', '23:00'
],
series: [
{ name: '灌溉泵', data: [10, 20, 15, 25, 30, 22, 10, 20, 15, 25, 30, 22, 10, 20, 15, 25, 30, 22, 10, 20, 15, 25, 30, 22] },
{ name: '溫室通風(fēng)機', data: [5, 15, 10, 20, 25, 17, 5, 15, 10, 20, 25, 17, 5, 15, 10, 20, 25, 17, 5, 15, 10, 20, 25, 17] },
{ name: '補光燈', data: [8, 18, 13, 23, 28, 20, 8, 18, 13, 23, 28, 20, 8, 18, 13, 23, 28, 20, 8, 18, 13, 23, 28, 20] },
{ name: '溫控系統(tǒng)', data: [12, 22, 17, 27, 32, 24, 12, 22, 17, 27, 32, 24, 12, 22, 17, 27, 32, 24, 12, 22, 17, 27, 32, 24] }
]
};
let barChartInstance = null;
let stackedChartInstance = null;
// 初始化柱狀圖
const initBarChart = () => {
const chartDom = document.getElementById('barChart');
if (!chartDom) return;
barChartInstance = echarts.init(chartDom);
const option = {
title: {
text: '灌溉系統(tǒng)月度能耗對比',
left: 'center',
textStyle: {
color: '#333'
}
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
},
formatter: params => {
const data = params[0];
return `${data.axisValue}
${data.seriesName}: ${data.value}kWh`;
}
},
legend: {
data: ['2023年灌溉能耗', '2024年灌溉能耗'],
top: 30,
textStyle: {
color: '#333'
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
data: barChartData.categories,
axisLine: {
lineStyle: {
color: '#d1d8e0'
}
},
axisLabel: {
color: '#666'
}
},
yAxis: {
type: 'value',
name: '能耗(kWh)',
nameTextStyle: {
color: '#333'
},
splitLine: {
lineStyle: {
color: '#eee'
}
},
axisLine: {
lineStyle: {
color: '#d1d8e0'
}
},
axisLabel: {
color: '#666'
}
},
series: [
{
name: '2023年灌溉能耗',
type: 'bar',
data: barChartData.values1,
itemStyle: {
color: '#5470C6'
},
barWidth: '40%',
label: {
show: true,
position: 'top',
color: '#333'
}
},
{
name: '2024年灌溉能耗',
type: 'bar',
data: barChartData.values2,
itemStyle: {
color: '#91CC75'
},
barWidth: '40%',
label: {
show: true,
position: 'top',
color: '#333'
}
}
]
};
barChartInstance.setOption(option);
};
// 初始化堆疊曲線圖
const initStackedChart = () => {
const chartDom = document.getElementById('stackedLineChart');
if (!chartDom) return;
stackedChartInstance = echarts.init(chartDom);
const option = {
title: {
text: '農(nóng)業(yè)設(shè)備24小時能耗變化',
left: 'center',
textStyle: {
color: '#333'
}
},
tooltip: {
trigger: 'axis',
backgroundColor: 'rgba(0, 0, 0, 0.7)',
borderColor: '#333',
textStyle: { color: '#fff' },
formatter: params => {
let result = `${params[0].axisValue}
`;
params.forEach(item => {
result += `${item.marker} ${item.seriesName}: ${item.value}kWh
`;
});
return result;
}
},
legend: {
data: chartDataStacked.series.map(item => item.name),
top: 30,
textStyle: { color: '#333' }
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap: false,
data: chartDataStacked.categories,
axis
總結(jié)
通過本文的介紹,我們學(xué)習(xí)了如何在Vue3中結(jié)合ECharts實現(xiàn)多系列柱狀圖和堆疊曲線圖。柱狀圖適用于對比不同類別的數(shù)據(jù),而堆疊曲線圖則適合展示時間序列數(shù)據(jù)的變化趨勢和占比。在實際項目中,可以根據(jù)具體需求調(diào)整圖表配置,如顏色、標(biāo)簽、提示框等,以達到最佳的展示效果。
到此這篇關(guān)于Vue3繪制多系列柱狀圖與曲線圖的示例代碼的文章就介紹到這了,更多相關(guān)Vue3繪圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vuejs在v-for中,利用index來對第一項添加class的方法
下面小編就為大家分享一篇Vuejs在v-for中,利用index來對第一項添加class的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
vue中.env文件配置環(huán)境變量的實現(xiàn)
本文主要介紹了vue中.env文件配置環(huán)境變量的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
vue3.0中使用Element-Plus中Select下的filter-method屬性代碼示例
這篇文章主要給大家介紹了關(guān)于vue3.0中使用Element-Plus中Select下的filter-method屬性的相關(guān)資料,Filter-method用法是指從一組數(shù)據(jù)中選擇滿足條件的項,文中通過圖文以及代碼介紹的非常詳細,需要的朋友可以參考下2023-12-12
Vuex實現(xiàn)數(shù)據(jù)增加和刪除功能
今天小編就為大家分享一篇Vuex實現(xiàn)數(shù)據(jù)增加和刪除功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Vue+Video.js實現(xiàn)視頻抽幀并返回抽幀圖片Base64
這篇文章主要為大家詳細介紹了Vue如何利用Video.js實現(xiàn)視頻抽幀并返回抽幀圖片Base64,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-01-01
element-ui 中使用upload多文件上傳只請求一次接口
這篇文章主要介紹了element-ui 中使用upload多文件上傳只請求一次接口,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
vue中使用elementui實現(xiàn)樹組件tree右鍵增刪改功能
這篇文章主要介紹了vue中使用elementui實現(xiàn)對樹組件tree右鍵增刪改功能,右擊節(jié)點可進行增刪改,對節(jié)點數(shù)據(jù)進行模糊查詢功能,本文給大家分享了完整代碼,需要的朋友可以參考下2022-08-08
一文詳解如何用Three.js和Vue?3實現(xiàn)3D商品展示
Three.js是一個基于JavaScript的開源庫,用于在網(wǎng)頁上創(chuàng)建和顯示3D圖形,這篇文章主要介紹了如何用Three.js和Vue?3實現(xiàn)3D商品展示的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-07-07
詳解vuex中mutations方法的使用與實現(xiàn)
這篇文章主要為大家詳細介紹了vuex中mutations方法的使用與實現(xiàn)的相關(guān)知識,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起了解一下2023-11-11

