/** * 将监测数据格式化为e-charts图表数据 */ import { factorName } from '@/constant/factor-name'; function refreshX(factorDatas) { const newTimes = factorDatas.times.map((v) => { return v.split(' ')[1]; }); return newTimes; } function refreshY(factorDatas) { const allSeries = new Map(); for (const key in factorDatas.factor) { if (Object.hasOwnProperty.call(factorDatas.factor, key)) { const e = factorDatas.factor[key]; if (!allSeries.has(key)) { allSeries.set(key, { key: key, name: factorName[e.factorName], label: e.factorName, type: 'line', data: [], showAllSymbol: true, animationDelay: function (idx) { return idx * 10; } }); } if (e.datas.length == 0) { continue; } const series = allSeries.get(key); // 插入新数据 const newSeries = e.datas.map((v) => v.factorData); series.data = series.data.concat(newSeries); // 计算数据范围 const { min, max } = dataRange(series.data); series.min = min; series.max = max; // 记录最新数据 series.currentData = Math.round(series.data[series.data.length - 1] * 10) / 10; } } return allSeries; } function dataRange(dataList) { let min, max; dataList.forEach((e) => { if (!min || min > e) { min = e; } if (!max || max < e) { max = e; } }); return { min, max }; } function toList(xAxis, allSeries, selectedFactors) { const list = []; selectedFactors.forEach((t) => { if (allSeries.has(t.value)) { list.push({ xAxis: xAxis, series: allSeries.get(t.value) }); } }); return list; } export default { parseData(factorDatas, selectedFactors) { const xAxis = refreshX(factorDatas); const allSeries = refreshY(factorDatas); return toList(xAxis, allSeries, selectedFactors); } };