riku
2025-09-11 307b17ef15c73a071912a262834f2a5f68e1fa87
src/model/Legend.js
@@ -10,7 +10,7 @@
  _legend_r: {
    NO: [0, 100, 200, 700, 1200, 2340],
    NO2: [0, 100, 200, 700, 1200, 2340],
    CO: [0, 5, 10, 35, 60, 90],
    CO: [0, 5000, 10000, 35000, 60000, 90000],
    H2S: [0, 150, 500, 650, 800, 1600],
    SO2: [0, 150, 500, 650, 800, 1600],
    O3: [0, 160, 200, 300, 400, 800],
@@ -148,7 +148,7 @@
    [0.87, 0.92, 0.03, 0.75],
    [0.8, 0.67, 0.04, 0.75],
    [0.92, 0.28, 0.07, 0.75],
    [0.6, 0.05, 0.05, 0.75]
    [0.96, 0.05, 0.05, 0.75]
  ],
  getStandardRange: function (name) {
@@ -164,15 +164,14 @@
    //   }
    //   max = parseInt(key)
    // }
    if (name == 'CO') {
      min *= 1000;
      max *= 1000;
    }
    return [min, max];
  },
  getColor: function (name, type, data, min, max) {
    if (!data) {
      return [0, 0, 0, 0];
    }
    if (type == this.S_TYPE) {
      return this.getStandardColor(name, data);
    } else {
@@ -220,9 +219,6 @@
    for (let i = 0; i < range.length; i++) {
      const d = range[i];
      let d1 = d;
      if (name == 'CO') {
        d1 *= 1000;
      }
      if (data >= d1) {
        selected = i;
      } else {
@@ -239,13 +235,87 @@
    return colors[selected];
  },
  getStandardColorAndNext: function (name, data) {
    if (!data) {
      return {
        color: [0, 0, 0, 0],
        nextColor: [0, 0, 0, 0],
        range: 0,
        nextRange: 0
      };
    }
    let range = this._legend_r[name];
    let colors = this._legend_c[name];
    if (range == undefined) {
      range = this._legend_r['PM25'];
      colors = this._legend_c['PM25'];
    }
    let selected = undefined;
    for (let i = 0; i < range.length; i++) {
      const d = range[i];
      let d1 = d;
      if (data >= d1) {
        selected = i;
      } else {
        break;
      }
    }
    // 避免下标越界
    if (selected >= colors.length) {
      selected = colors.length - 1;
    }
    let nextIndex = selected + 1;
    if (nextIndex >= colors.length) {
      nextIndex = colors.length - 1;
    }
    return {
      color: colors[selected],
      nextColor: colors[nextIndex],
      range: range[selected],
      nextRange: range[nextIndex]
    };
  },
  getCustomColor: function (data, min, max) {
    var per = (max - min) / this._custom.length;
    var i = parseInt(data / per);
    var i = parseInt((data - min) / per);
    if (i >= this._custom.length) {
      i = this._custom.length - 1;
    }
    return this._custom[i];
  },
  getCustomColorAndNext: function (data, min, max) {
    if (!data) {
      return {
        color: [0, 0, 0, 0],
        nextColor: [0, 0, 0, 0],
        range: 0,
        nextRange: 0
      };
    }
    // 将数据按照颜色数量分隔,求出每一段的数据偏移量
    var per = (max - min) / (this._custom.length - 1);
    // 计算当前数据所在的分段范围
    var i = parseInt((data - min) / per);
    // 如果是最大值,同样分割到最后一段
    if (i == this._custom.length - 1) i--;
    var range = min + i * per;
    let nextIndex = i + 1;
    let nextRange = min + nextIndex * per;
    return {
      color: this._custom[i],
      nextColor: this._custom[nextIndex],
      range,
      nextRange
    };
  }
};