echarts折線圖月份數(shù)據(jù)不足自動(dòng)補(bǔ)0和日期達(dá)到數(shù)據(jù)連續(xù)的效果(最新推薦)
需求:查詢一個(gè)月的數(shù)據(jù),但是有些數(shù)據(jù)為0,后端沒(méi)傳,所以要前端進(jìn)行操作,把沒(méi)傳的數(shù)據(jù)進(jìn)行補(bǔ)0填充達(dá)到月數(shù)據(jù)完整效果
1.錯(cuò)誤展示
如果這個(gè)月為0的數(shù)據(jù)后端沒(méi)傳,那么圖片不能展示這個(gè)月所有的數(shù)據(jù),會(huì)導(dǎo)致折線圖不直觀
// 模擬后端數(shù)據(jù) let result = [ { date: '2023-11-01', value: 3200 }, { date: '2023-11-11', value: 6850 } ];
2.正確效果
3.代碼講解
3.1思路
默認(rèn)請(qǐng)求后端接口肯定是當(dāng)月的數(shù)據(jù),當(dāng)月的格式應(yīng)該為2024-03按照年和月去請(qǐng)求
var date = new Date(); const year = date.getFullYear(); const month = date.getMonth() + 1; this.monthFlow = `${year}-${month}`;
獲取到當(dāng)月的天數(shù),如果想獲取其他月的天數(shù)可以通過(guò)var today = new Date('2023-12');
// 得到這個(gè)月的總天數(shù) var lastDay = new Date(year, month + 1, 0).getDate(); console.log(lastDay, '當(dāng)月的天數(shù)');
之后通過(guò)linq插件實(shí)現(xiàn),下載并且在頁(yè)面導(dǎo)入
//下載 npm install linq //導(dǎo)入 import Linq from "linq";
通過(guò)for循環(huán)當(dāng)月的天數(shù),之后判斷2024-01-23,中的23是不是等于當(dāng)天,如果等于就把數(shù)據(jù)填充,如果不等于也就是這天沒(méi)有數(shù)據(jù),那么就自動(dòng)補(bǔ)0,最后通過(guò)sendMonthOption變量接收數(shù)據(jù),這個(gè)日期當(dāng)日可以是動(dòng)態(tài)的,根據(jù)實(shí)際數(shù)據(jù)進(jìn)行更改
for (var i = 1; i <= lastDay; i++) { // new Date(x.date).getDate()獲取到數(shù)據(jù)的日期,如果為01,則為1,并且是數(shù)字類型 let count = Linq.from(result) .where((x) => new Date(x.date).getDate() == i) .toArray(); if (count.length > 0) { this.sendMonthOption.xAxis.push(count[0].date); this.sendMonthOption.seriesData.push(count[0].value); } else { this.sendMonthOption.xAxis.push(`2023-11-${i}`); this.sendMonthOption.seriesData.push(0); } }
4.完整代碼
<template> <div id="month" ref="bin" style="width: 500px; height: 300px; margin-top: 0.125rem"></div> </template> <script> import * as echarts from 'echarts'; import Linq from 'linq'; export default { data() { return { sendMonthOption: { xAxis: [], seriesData: [] } }; }, mounted() { this.play_echarts(); }, methods: { play_echarts() { // 假如是當(dāng)月的數(shù)據(jù)查詢 var today = new Date(); // 獲取到年月 var year = today.getFullYear(); var month = today.getMonth(); // 得到這個(gè)月的總天數(shù) var lastDay = new Date(year, month + 1, 0).getDate(); console.log(lastDay, '當(dāng)月的天數(shù)'); // 模擬后端數(shù)據(jù) let result = [ { date: '2023-11-01', value: 299 }, { date: '2023-11-11', value: 35 } ]; for (var i = 1; i <= lastDay; i++) { // new Date(x.date).getDate()獲取到數(shù)據(jù)的日期,如果為01,則為1,并且是數(shù)字類型 let count = Linq.from(result) .where((x) => new Date(x.date).getDate() == i) .toArray(); if (count.length > 0) { this.sendMonthOption.xAxis.push(count[0].date); this.sendMonthOption.seriesData.push(count[0].value); } else { this.sendMonthOption.xAxis.push(`2023-11-${i}`); this.sendMonthOption.seriesData.push(0); } } var pieChart = echarts.init(this.$refs.bin); var option = { backgroundColor: '#05224d', tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } }, grid: { top: '15%', left: '3%', right: '5%', bottom: '8%', containLabel: true }, // 內(nèi)置區(qū)域縮放 //數(shù)據(jù)過(guò)多避免重疊 xAxis: [ { type: 'category', boundaryGap: true, axisLine: { //坐標(biāo)軸軸線相關(guān)設(shè)置。數(shù)學(xué)上的x軸 show: true, lineStyle: { color: '#f9f9f9' } }, axisLabel: { //坐標(biāo)軸刻度標(biāo)簽的相關(guān)設(shè)置 color: '#d1e6eb' // margin: 15, }, axisTick: { show: false }, data: this.sendMonthOption.xAxis } ], dataZoom: [ { type: 'inside' // 使用滑動(dòng)條型的 dataZoom } ], yAxis: [ { type: 'value', min: 0, // max: 140, splitNumber: 7, splitLine: { show: true, lineStyle: { color: '#0a3256' } }, axisLine: { show: false }, axisLabel: { margin: 20, color: '#d1e6eb' }, axisTick: { show: false } } ], series: [ { type: 'line', itemStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: '#0ec1ff' }, { offset: 1, color: '#1cfffb' } ]) }, data: this.sendMonthOption.seriesData } ] }; // 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。true是為了實(shí)時(shí)更新數(shù)據(jù) pieChart.setOption(option, true); window.addEventListener('resize', function () { pieChart.resize(); }); // pieChart.on( // "touchmove", // function (e) { // e.preventDefault(); // }, // { passive: false } // ); } } }; </script> <style lang="scss" scoped></style>
到此這篇關(guān)于echarts折線圖月份數(shù)據(jù)不足自動(dòng)補(bǔ)0和日期達(dá)到數(shù)據(jù)連續(xù)的效果的文章就介紹到這了,更多相關(guān)echarts折線圖月份內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
15個(gè)簡(jiǎn)單的JS編碼標(biāo)準(zhǔn)讓你的代碼更整潔(小結(jié))
這篇文章主要介紹了15個(gè)簡(jiǎn)單的JS編碼標(biāo)準(zhǔn)讓你的代碼更整潔(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07一文詳解Web Audio瀏覽器采集麥克風(fēng)音頻數(shù)據(jù)
這篇文章主要為大家介紹Web Audio瀏覽器采集麥克風(fēng)音頻數(shù)據(jù)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03JavaScript使用二分查找算法在數(shù)組中查找數(shù)據(jù)的方法
這篇文章主要介紹了JavaScript使用二分查找算法在數(shù)組中查找數(shù)據(jù)的方法,較為詳細(xì)的分析了二分查找法的原理與javascript實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-04-04javascript使用isNaN()函數(shù)判斷變量是否為數(shù)字
javascript中判斷變量是否為數(shù)字的方法,這里主要介紹javascript里的 isNaN() 函數(shù),具體使用如下,感興趣的朋友可以參考下2013-09-09uniapp返回上一頁(yè)并實(shí)現(xiàn)刷新界面數(shù)據(jù)的完整代碼
從一個(gè)列表界面點(diǎn)擊新增按鈕,進(jìn)入新增元素的界面,然后新增之后返回列表界面,并刷新列表界面,下面小編給大家分享uniapp返回上一頁(yè),并實(shí)現(xiàn)刷新界面數(shù)據(jù)的代碼,感興趣的朋友跟隨小編一起看看吧2024-04-04JavaScript常用工具函數(shù)匯總(瀏覽器環(huán)境)
這篇文章主要匯總了JavaScript常用的工具函數(shù),幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下2020-09-09JavaScript使用push方法添加一個(gè)元素到數(shù)組末尾用法實(shí)例
這篇文章主要介紹了JavaScript使用push方法添加一個(gè)元素到數(shù)組末尾,實(shí)例分析了javascript中push函數(shù)的使用技巧,需要的朋友可以參考下2015-04-04談?wù)刯s中的prototype及prototype屬性解釋和常用方法
prototype是javascript中筆記難理解的一部分內(nèi)容,下面通過(guò)幾個(gè)關(guān)鍵知識(shí)點(diǎn)給大家講解js中的prototype,對(duì)js中的prototype相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-11-11