riku
2025-03-20 3970cefa60ea7e5d899b7475345b65646c19c110
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { Legend } from './Legend';
 
const _hMap = {
  19: [0, 1000], //NO
  1: [0, 1000], //NO2
  2: [0, 2000], //CO
  3: [0, 1000], //H2S
  4: [0, 1000], //SO2
  5: [0, 1000], //O3
  6: [0, 1000], //PM2.5
  7: [0, 1000], //PM10
  8: [0, 100], //TEMPERATURE
  9: [0, 100], //HUMIDITY
  10: [0, 500], //VOC
  11: [0, 1000] //NOI
};
const _hRange = [0, 1000];
 
function getFactorHeight(type, data, _range) {
  var range = _range == undefined ? _hMap.get(type) : _range;
  var min = range[0];
  var max = range[1];
  var scale = max - min == 0 ? 0 : (_hRange[1] - _hRange[0]) / (max - min);
  var offset = min;
  // console.log("height:" + (data - offset) * scale * 10);
  if (data < range[0]) {
    return (range[0] - offset) * scale * 10;
  } else if (data > range[1]) {
    return (range[1] - offset) * scale * 10;
  } else {
    return (data - offset) * scale * 10;
  }
}
 
/**
 * 监测因子类
 * 存储某一类型的监测因子数据,提供3d地图绘制高度换算,绘图范围设定等功能
 */
function Factor(options) {
  /**
    * {factorData: 43.209
        factorId: "1"
        factorName: "NO2"
        physicalQuantity: 211.1
        sensorId: null
        statusList: null}
    */
  this.datas = []; // 原始数据
  // this.lnglats = [] //3d地图当前展示坐标点数组
  this.factorName;
  this.factorId;
  this.heights = []; //3d地图当前展示坐标点对应的高度数组
  this.colors = []; // 3d地图当前展示坐标点对应的颜色数组
  this.bottomColors = []; //最小值对应的图例色数组
  this.bottomColor; //最小值对应的图例色
  this.min = -1; // 当前显示的最小值
  this.max = -1; // 当前显示的最大值
  this.originMin = -1; // 原始数据中的最小值
  this.originMax = -1; // 原始数据中的最大值
  this.standardMin = -1; //监测因子类型对应的标准最小值
  this.standardMax = -1; //监测因子类型对应的标准最大值
 
  this.legendType = Legend.S_TYPE; //图例模式
  // this.legendType = Legend.D_TYPE; //图例模式
 
  if (typeof options === 'object') {
    for (const key in options) {
      if (Object.prototype.hasOwnProperty.call(options, key)) {
        const value = options[key];
        this[key] = value;
      }
    }
 
    // this.datas = options.datas;
    // this.heights = options.heights;
    // this.min = options.min;
    // this.max = options.max;
    // this.originMin = options.originMin;
    // this.originMax = options.originMax;
 
    // this.factorName = options.factorName;
    // this.factorId = options.factorId;
    // this.colors = options.colors;
    // this.bottomColors = options.bottomColors;
    // this.bottomColor = options.bottomColor;
    // this.standardMin = options.standardMin;
    // this.standardMax = options.standardMax;
 
    // this.legendType = options.legendType;
  }
}
 
Factor.prototype = {
  // drawMode: 绘制模式,0:自动模式,自动计算当前数据的范围,绘制合适的比例;1:手动模式,根据页面设置的绘图范围进行绘制
  pushData: function (data, drawMode) {
    if (this.factorName == undefined) {
      this.factorName = data.factorName;
      this.factorId = data.factorId;
    } else {
      if (this.factorName != data.factorName) {
        console.log(
          '错误: Factor中插入的数据前后名称不一致,原因子:' +
            this.factorName +
            ',新因子:' +
            data.factorName
        );
      }
    }
    this.datas.push(data);
    this.getRange(data, drawMode);
 
    if (this.standardMin == -1) {
      var range = Legend.getStandardRange(this.factorName);
      this.standardMin = range[0];
      this.standardMax = range[1];
    }
  },
  getRange: function (data, drawMode) {
    if (this.min == -1) {
      this.min = data.factorData;
      this.max = data.factorData;
    }
    if (this.originMin == -1) {
      this.originMin = data.factorData;
      this.originMax = data.factorData;
    }
    if (drawMode == 0) {
      this.min = Math.min(this.min, data.factorData);
      this.max = Math.max(this.max, data.factorData);
      // this.min = this.standardMin
      // this.max = this.standardMax
    }
    this.originMin = Math.min(this.originMin, data.factorData);
    this.originMax = Math.max(this.originMax, data.factorData);
  },
  getHeight: function () {
    this.heights = [];
    this.colors = [];
    this.datas.forEach((d) => {
      let h = getFactorHeight(d.factorId, d.factorData, [this.min, this.max]);
      if (d.factorData == -1) {
        h = -1;
      }
      this.heights.push(h);
      const c = Legend.getColor(
        this.factorName,
        this.legendType,
        d.factorData,
        this.min,
        this.max
      );
      this.colors.push(c);
      const b = Legend.getPreviousColor(this.factorName, this.legendType, c);
      this.bottomColors.push(b);
      // this.heights.push(d.factorData)
    });
    this.bottomColor = Legend.getColor(
      this.factorName,
      this.legendType,
      this.standardMin,
      this.min,
      this.max
    );
    // console.log(this.factorName + ':' + this.bottomColor);
  },
  setRange: function (range) {
    this.min = range[0];
    this.max = range[1];
    this.legendType = Legend.C_TYPE;
    this.getHeight();
  },
  clearRange: function () {
    this.min = this.originMin;
    this.max = this.originMax;
    this.legendType = Legend.D_TYPE;
    this.getHeight();
  },
  standardRange: function () {
    this.min = this.originMin;
    this.max = this.originMax;
    // this.min = this.standardMin
    // this.max = this.standardMax
    this.legendType = Legend.S_TYPE;
    this.getHeight();
  },
  // 根据开始和结束下标获取对应位置数据集
  getByIndex: function (s, e) {
    var d = this.datas.slice(s, e);
    var h = this.heights.slice(s, e);
    return new Factor({
      datas: d,
      heights: h,
      min: this.min,
      max: this.max,
      originMin: this.originMin,
      originMax: this.originMax,
      factorName: this.factorName,
      colors: this.colors,
      bottomColors: this.bottomColors,
      bottomColor: this.bottomColor,
      standardMin: this.standardMin,
      standardMax: this.standardMax
    });
  },
  // 新增数据同时插帧
  insertFrame: function (factor, count, isDraw) {
    var d1 = this.datas[this.datas.length - 1];
    var d2 = factor.datas[0];
    if (d1 == undefined || d2 == undefined) {
      return;
    }
    let diffValue = d2.factorData - d1.factorData;
    // 对于风向矢量来说,动画的变化应该从两个风向夹角较小的那一侧进行变化
    if (this.factorName == 'WIND_DIRECTION') {
      // 风向角度差
      if (diffValue > 180) {
        diffValue -= 360;
      } else if (diffValue < -180) {
        diffValue += 360;
      }
    }
    // 单帧数据值的差值
    const dValue = {
      factorData: diffValue / count
      // physicalQuantity: (d2.physicalQuantity - d2.physicalQuantity) / count
    };
 
    // 风向矢量修正
    const correct = (v) => {
      if (this.factorName == 'WIND_DIRECTION') {
        if (v < 0) {
          return 360 + v;
        } else if (v > 360) {
          return v - 360;
        } else {
          return v;
        }
      } else {
        return v;
      }
    };
    for (let i = 0; i < count - 1; i++) {
      var _data = {
        factorData: correct(d1.factorData + dValue.factorData * (i + 1)),
        factorId: d1.factorId,
        factorName: d1.factorName,
        // physicalQuantity:
        //   d1.physicalQuantity + dValue.physicalQuantity * (i + 1),
        sensorId: d1.sensorId,
        statusList: d1.statusList
      };
      if (!isDraw) {
        _data.factorData = -1;
        // _data.physicalQuantity = -1;
      }
      this.datas.push(_data);
    }
    // this.datas.push(d2)
  }
};
 
export { Factor };