riku
2025-09-03 b2392458ebf42594b9fc5390fda40d7a0a12f923
src/utils/map/grid.js
@@ -4,6 +4,7 @@
import { map } from './index_old';
import calculate from './calculate';
import { Legend } from '@/model/Legend';
import { getGridDataDetailFactorValue } from '@/model/GridDataDetail';
import { getHexColor, getColorBetweenTwoColors } from '../color';
/**
@@ -80,11 +81,7 @@
  });
}
/**
 * 文本标记
 * 可修改position
 */
function textMaker({ position, text, anchor, type, color }) {
function textMakerStyle({ type, color }) {
  let style = {};
  if (type == 'data') {
    style = {
@@ -111,6 +108,15 @@
      'margin-bottom': '4px'
    };
  }
  return style;
}
/**
 * 文本标记
 * 可修改position
 */
function textMaker({ position, text, anchor, type, color }) {
  let style = textMakerStyle({ type, color });
  // eslint-disable-next-line no-undef
  return new AMap.Text({
    text: text,
@@ -264,7 +270,8 @@
        strokeWeight: 1, //线条宽度,默认为 2
        strokeColor: 'white', //线条颜色
        fillOpacity: 0,
        extData: p.extData
        extData: p.extData,
        zIndex: 11
      });
      if (typeof event === 'function') {
@@ -279,11 +286,19 @@
    return gridViews;
  },
  drawGridText({ points, textViews, anchor, type, isCustomColor, useColor }) {
  drawGridText({
    points,
    textViews,
    anchor,
    type,
    isCustomColor,
    useColor,
    factorName = 'PM25'
  }) {
    let colorList = [];
    if (useColor) {
      colorList = calGridColor({
        factorName: 'PM25',
        factorName,
        data: points.map((p) => p.data),
        isCustomColor: isCustomColor
      });
@@ -304,6 +319,33 @@
    });
    // map.add(_textViews);
    return { textViews: _textViews };
  },
  changeGridText({
    points,
    textViews,
    type,
    isCustomColor,
    useColor,
    factorName = 'PM25'
  }) {
    let colorList = [];
    if (useColor) {
      colorList = calGridColor({
        factorName,
        data: points.map((p) => p.data),
        isCustomColor: isCustomColor
      });
    }
    if (textViews) {
      textViews.forEach((t, i) => {
        t.setText(points[i].data);
        t.setStyle(
          textMakerStyle({ type, color: useColor ? colorList[i] : 'white' })
        );
      });
    }
    return { textViews };
  },
  drawGridTextLabel(points, textViews, labelsLayer, direction) {
@@ -349,15 +391,20 @@
   * @param {number} opacity 透明度
   */
  drawGridColor(gridViews, gridDataDetail, factorName, style) {
    let { strokeWeight, strokeColor, opacity, zIndex } = style;
    let {
      strokeWeight = 1,
      strokeColor = 'white',
      opacity = 1,
      zIndex = 11
    } = style;
    const res = [];
    // 遍历卫星遥测数据数组
    gridDataDetail.forEach((d, i) => {
      if (d.pm25) {
      const data = getGridDataDetailFactorValue(d, factorName);
      if (data) {
        const grid = gridViews[i];
        // 根据遥测数据计算网格颜色
        const data = d.pm25;
        const { color, nextColor, range, nextRange } =
          Legend.getStandardColorAndNext(factorName, data);
        const ratio = (data - range) / (nextRange - range);
@@ -369,9 +416,11 @@
        const _extData = grid.getExtData();
        grid.setOptions({
          ...style,
          strokeWeight,
          strokeColor,
          zIndex,
          fillColor: _color, //多边形填充颜色
          fillOpacity: style.opacity ? style.opacity : color[3] == 0 ? 0 : 1,
          fillOpacity: opacity,
          extData: {
            ..._extData,
            gridData: d
@@ -393,27 +442,26 @@
    return res;
  },
  drawGridColorCustom(gridViews, gridDataDetail, style) {
    const { strokeWeight, strokeColor, opacity, zIndex } = style;
  drawGridColorCustom(gridViews, gridDataDetail, factorName) {
    var max, min;
    gridDataDetail.forEach((t) => {
      if (!t.pm25) return;
      if (!max || t.pm25 > max) {
        max = t.pm25;
      const data = getGridDataDetailFactorValue(t, factorName);
      if (!data) return;
      if (!max || data > max) {
        max = data;
      }
      if (!min || t.pm25 < min) {
        min = t.pm25;
      if (!min || data < min) {
        min = data;
      }
    });
    const res = [];
    // 遍历卫星遥测数据数组
    gridDataDetail.forEach((d, i) => {
      if (d.pm25) {
      const data = getGridDataDetailFactorValue(d, factorName);
      if (data) {
        const grid = gridViews[i];
        // 根据遥测数据计算网格颜色
        const data = d.pm25;
        const { color, nextColor, range, nextRange } =
          Legend.getCustomColorAndNext(data, min, max);
        const ratio = (data - range) / (nextRange - range);
@@ -424,9 +472,8 @@
          ratio
        );
        grid.setOptions({
          ...style,
          fillColor: _color, //多边形填充颜色
          fillOpacity: style.opacity ? style.opacity : color[3] == 0 ? 0 : 1
          fillColor: _color //多边形填充颜色
          // fillOpacity: style.opacity ? style.opacity : color[3] == 0 ? 0 : 1
        });
        res.push(grid);