echarts折線圖月份數(shù)據(jù)不足自動補0和日期達到數(shù)據(jù)連續(xù)的效果(最新推薦)
需求:查詢一個月的數(shù)據(jù),但是有些數(shù)據(jù)為0,后端沒傳,所以要前端進行操作,把沒傳的數(shù)據(jù)進行補0填充達到月數(shù)據(jù)完整效果
1.錯誤展示
如果這個月為0的數(shù)據(jù)后端沒傳,那么圖片不能展示這個月所有的數(shù)據(jù),會導致折線圖不直觀
// 模擬后端數(shù)據(jù) let result = [ { date: '2023-11-01', value: 3200 }, { date: '2023-11-11', value: 6850 } ];
2.正確效果
3.代碼講解
3.1思路
默認請求后端接口肯定是當月的數(shù)據(jù),當月的格式應該為2024-03按照年和月去請求
var date = new Date(); const year = date.getFullYear(); const month = date.getMonth() + 1; this.monthFlow = `${year}-${month}`;
獲取到當月的天數(shù),如果想獲取其他月的天數(shù)可以通過var today = new Date('2023-12');
// 得到這個月的總天數(shù) var lastDay = new Date(year, month + 1, 0).getDate(); console.log(lastDay, '當月的天數(shù)');
之后通過linq插件實現(xiàn),下載并且在頁面導入
//下載 npm install linq //導入 import Linq from "linq";
通過for循環(huán)當月的天數(shù),之后判斷2024-01-23,中的23是不是等于當天,如果等于就把數(shù)據(jù)填充,如果不等于也就是這天沒有數(shù)據(jù),那么就自動補0,最后通過sendMonthOption變量接收數(shù)據(jù),這個日期當日可以是動態(tài)的,根據(jù)實際數(shù)據(jù)進行更改
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() { // 假如是當月的數(shù)據(jù)查詢 var today = new Date(); // 獲取到年月 var year = today.getFullYear(); var month = today.getMonth(); // 得到這個月的總天數(shù) var lastDay = new Date(year, month + 1, 0).getDate(); console.log(lastDay, '當月的天數(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ù)過多避免重疊 xAxis: [ { type: 'category', boundaryGap: true, axisLine: { //坐標軸軸線相關(guān)設(shè)置。數(shù)學上的x軸 show: true, lineStyle: { color: '#f9f9f9' } }, axisLabel: { //坐標軸刻度標簽的相關(guān)設(shè)置 color: '#d1e6eb' // margin: 15, }, axisTick: { show: false }, data: this.sendMonthOption.xAxis } ], dataZoom: [ { type: 'inside' // 使用滑動條型的 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 } ] }; // 使用剛指定的配置項和數(shù)據(jù)顯示圖表。true是為了實時更新數(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ù)不足自動補0和日期達到數(shù)據(jù)連續(xù)的效果的文章就介紹到這了,更多相關(guān)echarts折線圖月份內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文詳解Web Audio瀏覽器采集麥克風音頻數(shù)據(jù)
這篇文章主要為大家介紹Web Audio瀏覽器采集麥克風音頻數(shù)據(jù)實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03JavaScript使用二分查找算法在數(shù)組中查找數(shù)據(jù)的方法
這篇文章主要介紹了JavaScript使用二分查找算法在數(shù)組中查找數(shù)據(jù)的方法,較為詳細的分析了二分查找法的原理與javascript實現(xiàn)技巧,需要的朋友可以參考下2015-04-04javascript使用isNaN()函數(shù)判斷變量是否為數(shù)字
javascript中判斷變量是否為數(shù)字的方法,這里主要介紹javascript里的 isNaN() 函數(shù),具體使用如下,感興趣的朋友可以參考下2013-09-09uniapp返回上一頁并實現(xiàn)刷新界面數(shù)據(jù)的完整代碼
從一個列表界面點擊新增按鈕,進入新增元素的界面,然后新增之后返回列表界面,并刷新列表界面,下面小編給大家分享uniapp返回上一頁,并實現(xiàn)刷新界面數(shù)據(jù)的代碼,感興趣的朋友跟隨小編一起看看吧2024-04-04JavaScript常用工具函數(shù)匯總(瀏覽器環(huán)境)
這篇文章主要匯總了JavaScript常用的工具函數(shù),幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下2020-09-09JavaScript使用push方法添加一個元素到數(shù)組末尾用法實例
這篇文章主要介紹了JavaScript使用push方法添加一個元素到數(shù)組末尾,實例分析了javascript中push函數(shù)的使用技巧,需要的朋友可以參考下2015-04-04談談js中的prototype及prototype屬性解釋和常用方法
prototype是javascript中筆記難理解的一部分內(nèi)容,下面通過幾個關(guān)鍵知識點給大家講解js中的prototype,對js中的prototype相關(guān)知識感興趣的朋友一起學習吧2015-11-11