307 lines
13 KiB
JavaScript
307 lines
13 KiB
JavaScript
import { createSeriesModel, createMarkLineModels, createMartPoint, createMartPointReverse, hexColor } from './util';
|
||
import store from '@/store/index';
|
||
|
||
const defaultConfig = {
|
||
/** 最小时间*/
|
||
minTime: 0,
|
||
/** 最大时间*/
|
||
maxTime: 3600 * 24 - 1,
|
||
/** 边缘高度*/
|
||
edge: 600,
|
||
/** 间隔高度*/
|
||
multiple: 1,
|
||
/** 偏移时间*/
|
||
translation: 0
|
||
};
|
||
|
||
class EqualRatioParser {
|
||
constructor() {
|
||
}
|
||
|
||
/** 加载配置*/
|
||
load(config = defaultConfig) {
|
||
this.config = config;
|
||
return this;
|
||
}
|
||
|
||
/** 解析excel数据转换为Json后台数据*/
|
||
importData(sheet, data) {
|
||
if (this.config &&
|
||
this.config.importData) {
|
||
return this.config.importData(sheet, data);
|
||
} else {
|
||
console.info('no import data function');
|
||
}
|
||
}
|
||
|
||
/** 将后台数据解析成图表*/
|
||
convertDataToModels(data, stations, kmRangeMap, lineStyle) {
|
||
var models = [];
|
||
if (data && data.serviceNumberDataList && data.serviceNumberDataList.length) {
|
||
/** 按服务遍历数据*/
|
||
data.serviceNumberDataList.forEach((service) => {
|
||
/** 按车次遍历数据*/
|
||
var opt = { name: '', markPointData: [], data: [] };
|
||
if (service.tripNumberDataList && service.tripNumberDataList.length) {
|
||
service.tripNumberDataList.forEach((train, j) => {
|
||
var pointdata = {};
|
||
var idx = 0;
|
||
var num = 0;
|
||
var lastPoint = null;
|
||
var nextPoint = null;
|
||
|
||
/** 如果车次号为空,不显示名称*/
|
||
if (train.tripNumber) {
|
||
/** 创建标记点名称和坐标*/
|
||
// pointdata.name = `${service.serviceNumber}${train.directionCode}${train.tripNumber}`;
|
||
pointdata.name = `${service.serviceNumber}${train.tripNumber}`;
|
||
pointdata.color = '#000' || lineStyle.color;
|
||
pointdata.directionCode = train.right ? '2' : '1';
|
||
if (!store.state.map.mapConfig.upRight) {
|
||
pointdata.directionCode = train.right ? '1' : '2';
|
||
}
|
||
pointdata.coord = [train.stationTimeList[1].secondTime, this.getCoordYByElem(stations, kmRangeMap, train.stationTimeList[1], pointdata.directionCode, false)];
|
||
|
||
/** 给服务对象添加服务名称和标记点*/
|
||
opt.markPointData.push(this.__createMartPoint(pointdata));
|
||
/** 创建服务号名称*/
|
||
opt.name = `${service.serviceNumber}`;
|
||
}
|
||
|
||
/** 计算非折返点车次点坐标集合*/
|
||
train.stationTimeList.forEach((elem, index) => {
|
||
idx = index;
|
||
// if (index == 0 && train.stationTimeList[index].stationCode != train.stationTimeList[index + 1].stationCode ||
|
||
// index == train.stationTimeList.length - 2 && train.stationTimeList[index].secondTime != train.stationTimeList[index + 1].secondTime ||
|
||
// index > 0 && index < train.stationTimeList.length - 1) {
|
||
const aa = `${train.tripNumber}`;
|
||
opt.data.push([elem.secondTime, this.getCoordYByElem(stations, kmRangeMap, elem, pointdata.directionCode, false), elem.stationCode, aa]);
|
||
// }
|
||
});
|
||
|
||
/** 计算折返点车次坐标点集合*/
|
||
if (!train.backup && train.reentry && service.tripNumberDataList[j + 1] && service.tripNumberDataList[j + 1].stationTimeList) {
|
||
const currentTimeList = service.tripNumberDataList[j].stationTimeList;
|
||
const nextTimeList = service.tripNumberDataList[j + 1].stationTimeList;
|
||
if (currentTimeList[currentTimeList.length - 1].secondTime != nextTimeList[0].secondTime) {
|
||
lastPoint = train.stationTimeList[train.stationTimeList.length - 1];
|
||
nextPoint = service.tripNumberDataList[j + 1].stationTimeList[0];
|
||
num = this.computedReentryNumber(train.tripNumber);
|
||
const aa = `${train.tripNumber}`;
|
||
opt.data.push([lastPoint.secondTime, this.getCoordYByElem(stations, kmRangeMap, lastPoint, pointdata.directionCode, true, num), lastPoint.stationCode, aa]);
|
||
opt.data.push([nextPoint.secondTime, this.getCoordYByElem(stations, kmRangeMap, nextPoint, pointdata.directionCode, true, num), nextPoint.stationCode, aa]);
|
||
}
|
||
}
|
||
|
||
/** 如果是备用车,按车次添加线*/
|
||
if (train.backup) {
|
||
/** 创建一条完成的服务数据*/
|
||
// opt.name += j;
|
||
const length = opt.name.length;
|
||
const optName = parseInt(opt.name) + j;
|
||
opt.name = optName.toString().padStart(length, '0');
|
||
var model = this.__createSeriesModel(opt, Object.assign({ color: hexColor.toCreate() }, lineStyle));
|
||
if (model) {
|
||
models.push(model);
|
||
opt = { name: '', markPointData: [], data: [] };
|
||
}
|
||
}
|
||
});
|
||
|
||
// 不是备用车,按服务添加线
|
||
if (!service.backup) {
|
||
/** 创建一条完成的服务数据*/
|
||
var model = this.__createSeriesModel(opt, Object.assign({ color: hexColor.toCreate() }, lineStyle));
|
||
if (model) {
|
||
models.push(model);
|
||
}
|
||
}
|
||
}
|
||
});
|
||
return models;
|
||
} else {
|
||
return [];
|
||
}
|
||
}
|
||
|
||
/** 更新数据并解析成图表*/
|
||
updateDataToModels(data, stations, kmRangeMap, runPlanData, series, lineStyle) {
|
||
if (data && data.length) {
|
||
data.forEach(elem => {
|
||
/** 判断此条记录的服务号是否存在*/
|
||
if (!runPlanData[elem.serviceNumber]) {
|
||
/** 创建一个新服务号标记*/
|
||
runPlanData[elem.serviceNumber] = {};
|
||
/** 不存在此服务号,则需要创建一条新的line*/
|
||
series.push(this.__createSeriesModel({
|
||
zlevel: 1,
|
||
name: `run${elem.serviceNumber}`,
|
||
data: [],
|
||
markPointData: []
|
||
}, Object.assign({ color: hexColor.toCreate() }, lineStyle)));
|
||
}
|
||
|
||
/** 添加数据*/
|
||
series.forEach(serie => {
|
||
/** 找到服务号所在图数据的位置*/
|
||
if (serie.name == `run${elem.serviceNumber}`) {
|
||
/** 添加车组号记录标记*/
|
||
if (!runPlanData[elem.serviceNumber][elem.tripNumber]) {
|
||
runPlanData[elem.serviceNumber][elem.tripNumber] = [];
|
||
}
|
||
|
||
runPlanData[elem.serviceNumber][elem.tripNumber].push(elem);
|
||
runPlanData[elem.serviceNumber][elem.tripNumber].sort((a, b) => {
|
||
return parseInt(a.secondTime) - parseInt(b.secondTime);
|
||
});
|
||
let directionCode = elem.right ? '2' : '1';
|
||
if (!store.state.map.mapConfig.upRight) {
|
||
directionCode = elem.right ? '1' : '2';
|
||
}
|
||
/** 如果此记录车组号的数据为第一条时,则打上标签*/
|
||
if (runPlanData[elem.serviceNumber][elem.tripNumber].length <= 1) {
|
||
const result = serie.markPoint.data.some(ele => ele.name == `(${elem.groupNumber})${elem.serviceNumber}${elem.tripNumber}`);
|
||
if (!result) {
|
||
serie.markPoint.data.push(this.__createMartPoint({
|
||
directionCode: directionCode,
|
||
coord: [parseInt(elem.secondTime), this.getCoordYByElem(stations, kmRangeMap, elem, false)],
|
||
name: `(${elem.groupNumber})${elem.serviceNumber}${elem.tripNumber}`,
|
||
color: lineStyle.color || '#000'
|
||
}));
|
||
}
|
||
}
|
||
|
||
/** 计算折返点*/
|
||
var nextPoint = [parseInt(elem.secondTime), this.getCoordYByElem(stations, kmRangeMap, elem, false), directionCode];
|
||
if (serie.data.length > 0) {
|
||
var lastPoint = serie.data[serie.data.length - 1];
|
||
if (lastPoint[2] !== nextPoint[2]) {
|
||
var num = this.computedReentryNumber(elem.tripNumber);
|
||
serie.data.push([lastPoint[0], this.getValueYByDirectionCode(lastPoint[1], lastPoint[2], num), lastPoint[2]]);
|
||
serie.data.push([nextPoint[0], this.getValueYByDirectionCode(nextPoint[1], lastPoint[2], num), lastPoint[2]]);
|
||
}
|
||
}
|
||
|
||
/** 添加车组号数据到对应的服务图数据中*/
|
||
serie.data.push(nextPoint);
|
||
|
||
/** 保证原始数据排序*/
|
||
serie.data.sort((a, b) => {
|
||
return parseInt(a[0]) - parseInt(b[0]);
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
return series;
|
||
}
|
||
|
||
/** 初始化Y轴*/
|
||
initializeAxisY(stations) {
|
||
return this.__createMarkLineModels(stations, (elem) => {
|
||
return this.getEdge() + elem.kmRange * this.getMultiple();
|
||
});
|
||
}
|
||
|
||
getAxisYValueByStation(station, index) {
|
||
return this.getEdge() + station.kmRange * this.getMultiple();
|
||
}
|
||
|
||
/** 将后台数据转换为试图序列模型*/
|
||
convertStationsToMap(stations) {
|
||
var map = {};
|
||
if (stations && stations.length) {
|
||
stations.forEach((elem, index) => {
|
||
map[`${elem.kmRange}`] = this.getAxisYValueByStation(elem, index);
|
||
});
|
||
}
|
||
|
||
return map;
|
||
}
|
||
|
||
/** 计算y轴最小值*/
|
||
computedAxisYMinValue(stations) {
|
||
return stations[0].kmRange * this.getMultiple();
|
||
}
|
||
|
||
/** 计算y轴最大值*/
|
||
computedAxisYMaxValue(stations) {
|
||
return stations[stations.length - 1].kmRange * this.getMultiple() + this.getEdge() * 2;
|
||
}
|
||
|
||
/** 格式化y轴数据*/
|
||
computedFormatAxisY(stations, params) {
|
||
var yText = '0m';
|
||
|
||
stations.forEach(elem => {
|
||
if (elem.kmRange < parseInt(params.value) / this.getMultiple() - this.getEdge()) {
|
||
yText = Math.floor(elem.kmRange) + 'm';
|
||
}
|
||
});
|
||
|
||
return yText;
|
||
}
|
||
|
||
/** 根据是否和上一个车次是否相交,计算下一个车次的折返的高度*/
|
||
computedReentryNumber(code) {
|
||
return 1;
|
||
}
|
||
|
||
/** 根据方向计算y折返偏移量*/
|
||
getValueYByDirectionCode(defaultValue, directionCode, num) {
|
||
const sign = this.getReverse() ? -1 : 1;
|
||
if (directionCode === '1') {
|
||
defaultValue -= sign * this.getEdge() / 2 * num;
|
||
} else if (directionCode === '2') {
|
||
defaultValue += sign * this.getEdge() / 2 * num;
|
||
}
|
||
|
||
return defaultValue;
|
||
}
|
||
|
||
/** 根据elem计算y值*/
|
||
getCoordYByElem(stations, kmRangeMap, elem, directionCode, isSpecial, num) {
|
||
var defaultValue = 0;
|
||
var station = stations.find(it => { return it.code == elem.stationCode; });
|
||
if (station) {
|
||
defaultValue = kmRangeMap[`${station.kmRange}`];
|
||
if (isSpecial) {
|
||
defaultValue = this.getValueYByDirectionCode(defaultValue, directionCode, num);
|
||
}
|
||
}
|
||
|
||
return defaultValue;
|
||
}
|
||
|
||
getMultiple() {
|
||
return this.config.multiple;
|
||
}
|
||
|
||
getTranslation() {
|
||
return this.config.translation;
|
||
}
|
||
|
||
getEdge() {
|
||
return this.config.edge;
|
||
}
|
||
|
||
getReverse() {
|
||
return this.config.reverse;
|
||
}
|
||
|
||
__createMartPoint(...args) {
|
||
return this.config.reverse ? createMartPointReverse(...args) : createMartPoint(...args);
|
||
}
|
||
|
||
__createSeriesModel(...args) {
|
||
return createSeriesModel(...args);
|
||
}
|
||
|
||
__createMarkLineModels(...args) {
|
||
return createMarkLineModels(...args);
|
||
}
|
||
}
|
||
|
||
export default EqualRatioParser;
|