vue中實現(xiàn)當前時間echarts圖表時間軸動態(tài)的數(shù)據(jù)(實例代碼)
更新時間:2022年10月25日 10:10:45 作者:三月長安
這篇文章主要介紹了vue中實現(xiàn)當前時間echarts圖表時間軸動態(tài)的數(shù)據(jù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
代碼如下所示:
<!-- ! 廢話不多說,直接看代碼吧 ! -->
<template>
<div class="">
<div class="chart" ref="ref_chart" style="width:370px;height:250px;"> </div>
</div>
</template>
<script lang="js">
export default {
data() {
return {
syca_myChart: null, // 圖表
interval: null, //定時器
x_tm: null, //獲取時間x軸的顯示時間
inTime: '', //當前的時間
A_data: [3, 5, 2, 3, 4,], // 電流數(shù)據(jù)
V_data: [200, 201, 204, 202, 201, 334], // 電壓
};
},
computed: {},
components: {},
mounted() {
this.x_time(); //先獲取x軸的一組時間
this.initChart(); //初始化dom元素
this.updateChart(); //設置配置進行渲染為圖表
this.getNewTime(); //更新時間
},
methods: {
// 獲取 x軸 一組時間值
x_time() {
let now = new Date();
let res = [];
for (let i = 0; i < 7; i++) {
res.unshift(now.toLocaleTimeString().replace(/^\D*/, ''));
now = new Date(+now - 3000); // 時間間隔
}
this.x_tm = res;
},
//初始化對象
initChart() {
this.syca_myChart = this.$echarts.init(this.$refs.ref_chart, "macarons");
},
//請求數(shù)據(jù)
get_data() {
// 在此處進行通過websoket進行數(shù)據(jù)交互代碼 略...
},
//更新數(shù)據(jù)
updateChart() {
let option = {
title: {
show: true,
text: "電流/電壓", //標題
top: 2,
left: 2,
textStyle: {
},
// subtext: ' - 折線圖', //二級標題
subtextStyle: {
// lineHeghit: 6
},
},
legend: {
data: ['電流', "電壓"],
top: 4,
},
toolbox: {
show: true, // 是否顯示工具
itemSize: 11,
itemGap: 6, //間隔大小
// right: 25,
feature: {
saveAsImage: { //保存為圖片
type: "jpg",
backgroundColor: "#00274b"
},
dataView: {
// 數(shù)據(jù)視圖
show: true,
readOnly: true, // 是否可以讀寫
// backgroundColor: "#00274b"
},
restore: {
// 還原
},
}
},
xAxis: {
type: 'category',
data: this.x_tm,
// name: "時間",
// nameLocation: "end"
// boundaryGap: false // 緊挨邊緣
axisLabel: {
fontSize: 11,
formatter: '{value}',
// y軸的顯示數(shù)據(jù)單位
rotate: -20,//刻度偏移
},
},
yAxis: [
{
type: 'value',
scale: true, //是否是脫離 0 值比例
// name: " 單位V",
axisLabel: {
fontSize: 11,
formatter: '{value} V',
// y軸的顯示數(shù)據(jù)單位
rotate: 20,//刻度偏移
},
minInterval: 1
},
],
grid: {
top: '20%',
right: '8%',
left: '12%',
bottom: '14%',
},
tooltip: { //圖標劃過顯示
show: true,
trigger: 'axis',
axisPointer: {
// type: 'cross', //十字提示指示線
// type: 'line', //
lineStyle: {
type: 'dashed', //線的類型 虛線
// snap: true, // 劃過吸附指示線
}
},
//懸浮窗的內(nèi)容
// a: 系列名,b: (x軸)類目值, c: 數(shù)據(jù)值
// formatter: `<br>{a}: {c} PM `,
// background: "red",//懸浮窗的背景色
// borderColor: '',//邊框色
borderWidth: 3,//邊框?qū)?
// padding: '', //內(nèi)邊距
alwaysShowContent: false, //懸浮窗是否一直顯示
hideDelay: 1000, //劃入時懸浮多少秒
enterable: true, //劃入正常顯示
textStyle: { //懸浮框的樣式
color: '#fff',
fontSize: 14,
}
},
series: [
{
name: '電流',
data: this.A_data,
type: 'line',
smooth: true, // 折線圖的線條是否平滑
areaStyle: {}, // 背景填充
// stack: "all", // 多組數(shù)據(jù)堆疊
label: {
show: true, //數(shù)據(jù)標簽顯示
position: 'top', //數(shù)據(jù)顯示位置
distance: 8, // 距離
offset: [-2, -2], //文字偏移
formatter: "{c}", //標簽內(nèi)容
},
},
{
name: '電壓',
data: this.V_data,
type: 'line', // line 折線 bar 柱狀
smooth: true, // 折線圖的線條是否平滑
areaStyle: {}, // 背景填充
// stack: "all", // 多組數(shù)據(jù)堆疊
label: {
show: true, //數(shù)據(jù)標簽顯示
position: 'top'
},
}
]
}
//進行渲染圖表
this.syca_myChart.setOption(option);
},
// 更新時間
getNewTime() {
clearInterval(this.interval); // 開啟定時器之前先清上次的
this.interval = setInterval(() => {
this.inTime = new Date().toLocaleTimeString();
this.x_tm.push(this.inTime);
if (this.x_tm.length > 5) {
this.x_tm.shift();
}
this.updateChart();
}, 3000)
},
},
watch: {},
destroyed() {
clearInterval(this.interval);
},
};
</script>
<style scoped lang='less'>
</style>效果圖展示:


到此這篇關于vue中實現(xiàn)當前時間echarts圖表時間軸動態(tài)的數(shù)據(jù)的文章就介紹到這了,更多相關vue echarts圖表時間軸動態(tài)數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- vue如何動態(tài)獲取當前時間
- vue怎樣獲取當前時間,并且傳遞給后端(不用注解)
- vue 使用moment獲取當前時間及一月前的時間
- Vue 中如何利用 new Date() 獲取當前時間
- 基于vue實現(xiàn)8小時帶刻度的時間軸根據(jù)當前時間實時定位功能
- Vue 按照創(chuàng)建時間和當前時間顯示操作(剛剛,幾小時前,幾天前)
- Vue 中獲取當前時間并實時刷新的實現(xiàn)代碼
- Vue filter 過濾當前時間 實現(xiàn)實時更新效果
- vue 2.1.3 實時顯示當前時間,每秒更新的方法
- element-ui vue input輸入框自動獲取焦點聚焦方式
- Vue3新增時自動獲取當前時間的操作方法
相關文章
vue+uniapp瀑布流布局多種實現(xiàn)方式示例代碼
由于使用uniapp開發(fā)的微信小程序不需要考慮響應式,因此瀑布流的實現(xiàn)相對于pc端更為簡單,下面這篇文章主要給大家介紹了關于vue+uniapp瀑布流布局多種實現(xiàn)方式的相關資料,需要的朋友可以參考下2023-03-03
解決element-ui el-input賦值后不能編輯的問題
這篇文章主要介紹了解決element-ui el-input賦值后不能編輯的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
vue3+elementplus 樹節(jié)點過濾功能實現(xiàn)
樹節(jié)點所展示的街道是讀取的成都市金牛區(qū)的范圍邊界線的json文件,街道下對應內(nèi)容市通過fetch調(diào)用接口獲取的內(nèi)容,通過mapTreeData函數(shù)循環(huán)遍歷,對數(shù)據(jù)進行處理,這篇文章主要介紹了vue3+elementplus 樹節(jié)點過濾功能實現(xiàn),需要的朋友可以參考下2024-05-05

