import store from '@/store'; import { timeFormat } from '@/utils/date'; /** 创建一个车次数据点*/ export function createMartPoint(opt) { const rotate = opt.directionCode === '2' ? 45 : (opt.directionCode === '1' ? -45 : 0); const position = opt.type ? 'insideBottomLeft' : 'insideTopLeft'; return { coord: opt.coord, name: opt.name, label: { normal: { rotate: rotate, formatter: '{b}', backgroundColor: 'rgb(242,242,242,0.1)', color: 'black', position: position } } }; } /** 创建一个车次数据序列*/ export function createSeriesModel(opt, lineStyle) { if (opt) { return { z: opt.z || 5, zlevel: opt.zlevel || 0, type: 'line', name: opt.name, data: opt.data, sampling: 'average', lineStyle: lineStyle || {}, markPoint: { symbol: 'roundRect', symbolSize: 1, data: opt.markPointData } }; } } /** 创建标记横线*/ export function createMarkLineModels(stations, computedYaxis) { const markLineModel = {}; if (stations && stations.length) { markLineModel.type = 'line'; markLineModel.name = 'markline'; markLineModel.markLine = {}; markLineModel.markLine.silent = true; markLineModel.markLine.data = []; markLineModel.markLine.lineStyle = { color: '#B0C4DE', width: 0.5 }; markLineModel.markLine.symbol = 'none'; stations.forEach((elem, index) => { markLineModel.markLine.data.push( { label: { show: true, position: 'start', formatter: elem.name, color: 'black' }, lineStyle: { type: 'solid', width: 0.5, opacity: 0.5 }, yAxis: computedYaxis(elem, index) } ); }); } return markLineModel; } /** 创建不会重复颜色的内部对象*/ export const hexColor = { colorIndex: 0, difValue: 0.25, // 一般为0.25 oddColor: null, eveColor: null, oldColor: null, newColor: null, colorList: [ '#000000', '#0000FF', '#8A2BE2', '#A52A2A', '#DEB887', '#5F9EA0', '#7FFF00', '#FF7F50', '#6495ED', '#DC143C', '#00FFFF', '#008B8B', '#B8860B', '#BDB76B', '#8B008B', '#FF8C00', '#9932CC', '#8FBC8F', '#FF1493', '#00BFFF', '#FF00FF', '#FFD700', '#FF69B4', '#FF4500', '#DB7093', '#4169E1', '#6A5ACD', '#00FF7F', '#EE82EE', '#40E0D0' ], colors: [ '#B9C671', '#6C9040', '#79C671', '#71A5C6', '#C6A071', '#71C689', '#' ], randomHsl: function () { const h = Math.random(); const s = Math.random(); const l = Math.random(); return [h, s, l]; }, hslToRgb: function (h, s, l) { let r, g, b; if (s === 0) { r = g = b = l; // achromatic } else { const hue2rgb = function hue2rgb(p, q, t) { if (t < 0) t += 1; if (t > 1) t -= 1; if (t < 1 / 6) return p + (q - p) * 6 * t; if (t < 1 / 2) return q; if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; return p; }; const q = l < 0.5 ? l * (1 + s) : l + s - l * s; const p = 2 * l - q; r = hue2rgb(p, q, h + 1 / 3); g = hue2rgb(p, q, h); b = hue2rgb(p, q, h - 1 / 3); } return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]; }, rgbToHsl: function (r, g, b) { // eslint-disable-next-line no-sequences r /= 255, g /= 255, b /= 255; const max = Math.max(r, g, b); const min = Math.min(r, g, b); let h; let s; const l = (max + min) / 2; if (max === min) { h = s = 0; // achromatic } else { const d = max - min; s = l > 0.5 ? d / (2 - max - min) : d / (max + min); switch (max) { case r: h = (g - b) / d + (g < b ? 6 : 0); break; case g: h = (b - r) / d + 2; break; case b: h = (r - g) / d + 4; break; } h /= 6; } return [h, s, l]; }, // 固定颜色 colorFixed() { var color = this.colorList[this.colorIndex++ % this.colorList.length]; return color; }, // 随机颜色 colorRandom() { return '#' + (Math.random() * 0xffffff << 0).toString(16); }, // 生成和前一个不同的随机颜色 colorContrast() { this.newColor = this.randomHsl(); // 获取随机的hsl,并且给一个默认的hsl this.newColor[1] = 0.7 + this.newColor[1] * 0.2; // [0.7 - 0.9] 排除过灰颜色 this.newColor[2] = 0.4 + this.newColor[2] * 0.2; // [0.4 - 0.8] 排除过亮过暗色 /** 如果oldColor不为空时,要根据车次号保证两次生成的颜色差值为difValue*/ this.oldColor = Number(this.colorIndex) % 2 ? this.oddColor : this.eveColor; if (this.oldColor) { /** 保证本次的颜色和上次的不一致*/ for (let i = 0; i < this.newColor.length && i < this.oldColor.length; i++) { if (i === 0 && Math.abs(this.newColor[i].toFixed(2) - this.oldColor[i].toFixed(2)) < this.difValue) { this.colorRandom(); break; } } } /** 保存之前的颜色状态*/ if (Number(this.colorIndex) % 2) { this.oddColor = this.newColor; } else { this.eveColor = this.newColor; } this.colorIndex += 1; return `#${this.hslToRgb(...this.newColor).map(e => { return Number(e).toString(16); }).join('')}`; }, // 渐进颜色 colorProgressiveColor() { }, toCreate: function () { return this.colorRandom(); } }; /** 对list数据进行排序, 相同元素保持原有顺序*/ export function sortListByCallBack(list, callback) { list.map((elem, index) => { elem[`oldIndex`] = index; }); list.sort((a, b) => { return callback(a, b) || a.oldIndex - b.oldIndex; }); return list; } /** 将数字转换成asc码*/ export function numToAsc(num) { const nmA = 'A'.charCodeAt(0); const nmZ = 'Z'.charCodeAt(0); const len = nmZ - nmA + 1; let str = ''; while (num >= 0) { str = String.fromCharCode(num % len + nmA) + str; num = Math.floor(num / len) - 1; } return str; } /** 将asc码转换成数字*/ export function ascToNum(asc) { const base = 'A'.charCodeAt() - 1; let idx = asc.length - 1; let num = 0; let mulFactor = 1; while (idx >= 0) { num += (asc[idx].charCodeAt() - base) * mulFactor; mulFactor *= 26; idx -= 1; } return num; } /** 根据索引获取单元格的数据*/ export function getCellValue(Sheet, index) { let value; const cell = Sheet[index]; if (cell) { value = cell.w || cell.v; } return value; } /** 转换sheet数据为json数据*/ export function convertSheetToList(Sheet, isReverse) { const dataList = []; if (Sheet) { const refarea = Sheet['!ref']; const regular = /([a-zA-Z]+)([0-9]+):([a-zA-Z]+)([0-9]+)/i; if (refarea == null) return []; // "A1:M698" if (regular.test(refarea)) { /** 正则转换解析行列数据*/ const CoordList = regular.exec(refarea); /** 转换数据为二维数组*/ const colBeg = ascToNum(CoordList[1]); const colEnd = ascToNum(CoordList[3]); const rowBeg = Number(CoordList[2]); const rowEnd = Number(CoordList[4]); if (isReverse) { for (let i = colBeg - 1; i < colEnd; i++) { dataList.push([]); for (let j = rowBeg; j <= rowEnd; j++) { dataList[dataList.length - 1].push(getCellValue(Sheet, numToAsc(i) + j)); } } } else { for (let i = rowBeg; i <= rowEnd; i++) { dataList.push([]); for (let j = colBeg - 1; j < colEnd; j++) { dataList[dataList.length - 1].push(getCellValue(Sheet, numToAsc(j) + i)); } } } } } return dataList; } /** 通过time将时间格式化*/ export function formatTime(time) { if (Number.isInteger(time)) { return timeFormat(time); } else { return ''; } } /** 通过code将名称格式化*/ export function formatName(code) { let name = ''; const device = store.getters['map/getDeviceByCode'](code); if (device) { name = device.name; } return name; } /** 将时间格式化前补零*/ export function prefixTime(time) { let str = `${time}` || ''; if (str) { const list = str.split(':'); str = list.map(elem => { return `00000${elem}`.substr(-2); }).join(':'); } return str; }