utils.js 10.3 KB
Newer Older
1 2 3
import moment from 'moment';
import _ from 'lodash';

4
/** 轴宽度, 用于计算多轴显示时, 轴线偏移和绘图区域尺寸 */
5 6 7 8
const axisWidth = 40;

/**
 * 图表系列名称格式化
9 10
 *
 * @param {any} data
11
 * @param {boolean} contrast 是否为同期对比
12 13
 * @param {any} contrastOption 同期对比周期配置, day|month
 * @returns
14
 */
15
const nameFormatter = (data, contrast, contrastOption, nameWithSensor) => {
16
  const { equipmentName, sensorName, unit, dataModel, dateFrom, dateTo } = data;
17
  let name = nameWithSensor ? `${equipmentName}-${sensorName}` : equipmentName;
18 19 20 21 22 23 24 25 26
  if (contrast) {
    const time = dateFrom.slice(0, contrastOption === 'day' ? 10 : 7).replace(/-/g, '');
    name = `${name}-${time}`;
  }
  return name;
};

/**
 * 图表系列数据格式化
27 28
 *
 * @param {any} data
29
 * @param {boolean} contrast 是否为同期对比
30
 * @param {any} contrastOption 同期对比周期配置, day|month
31 32 33 34 35
 * @returns 图表系列数据, [[DateTime, value]]
 */
const dataAccessor = (data, contrast, contrastOption) => {
  const { dataModel } = data;
  const formatStr = contrastOption === 'day' ? '2020-01-01 HH:mm:00' : '2020-01-DD HH:mm:00';
36
  return dataModel.map((item) => {
37 38 39 40 41 42 43
    const time = contrast ? moment(item.pt).format(formatStr) : item.pt;
    return [moment(time).valueOf(), item.pv];
  });
};

/**
 * 面积图配置(目前默认曲线图)
44 45 46
 *
 * @param {any} data 数据项
 * @returns Null/areaStyle, 为null显示曲线图, 为areaStyle对象显示为面积图.
47 48 49 50 51 52
 */
const areaStyleFormatter = (data) => {
  const { sensorName } = data;
  return sensorName && sensorName.indexOf('流量') > -1 ? {} : null;
};

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/**
 * 数据项中指标值最小最大值
 *
 * @param {any} data 数据项
 * @returns
 */
const minMax = (data) => {
  const { dataModel } = data;
  let min = Number.MAX_SAFE_INTEGER;
  let max = Number.MIN_SAFE_INTEGER;
  dataModel.forEach((item) => {
    min = Math.min(min, item.pv ?? 0);
    max = Math.max(max, item.pv ?? 0);
  });
  return [min, max];
};
69

70 71 72 73 74 75 76 77 78 79 80
const markLineItem = (name, value, color) => {
  return {
    name: name,
    yAxis: value,
    value: value,
    lineStyle: {
      color: color || '#000',
    },
    label: {
      position: 'insideEndTop',
      color: color || '#000',
81 82 83
      formatter: function () {
        return `${name}:${value}`;
      },
84 85 86 87 88 89 90
    },
  };
};

export const alarmMarkLine = (dataItem, index, dataSource, schemes) => {
  // 只有一个数据曲线时显示markline
  if (!dataItem || !schemes || dataSource.length !== 1) return {};
91
  const { deviceType, stationCode, sensorName, decimalPoint } = dataItem;
92 93 94 95 96 97 98 99 100
  const curSchemes = schemes.filter(
    (item) =>
      item.deviceCode === stationCode &&
      item.sensorName === sensorName &&
      item.valueType === '直接取值',
  );
  const data = [];
  curSchemes.forEach((scheme) => {
    const { hLimit, hhLimit, lLimit, llLimit } = scheme;
101
    lLimit !== null && lLimit !== void 0 && data.push(markLineItem('低限', lLimit, '#fa8c16'));
102
    hLimit !== null && hLimit !== void 0 && data.push(markLineItem('高限', hLimit, '#fa8c16'));
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    llLimit !== null && llLimit !== void 0 && data.push(markLineItem('低低限', llLimit, '#FF0000'));
    hhLimit !== null && hhLimit !== void 0 && data.push(markLineItem('高高限', hhLimit, '#FF0000'));
  });
  data.push({
    name: '平均线',
    type: 'average',
    lineStyle: {
      color: '#00b8b1',
      type: 'solid',
    },
    label: {
      position: 'insideEndTop',
      color: '#00b8b1',
      formatter: function (param) {
        return `平均值:${param.value}`;
      },
    },
120 121 122 123 124 125 126
  });
  return {
    symbol: ['none', 'none'],
    data,
  };
};

127 128 129 130
export const minMaxMarkPoint = (dataItem, index, dataSource) => {
  // 只有一个数据曲线时显示markline
  if (!dataItem || dataSource.length !== 1) return {};
  const data = [];
张瑶's avatar
张瑶 committed
131 132
  data.push({ type: 'min', name: '最小: ' });
  data.push({ type: 'max', name: '最大: ' });
133
  return {
张瑶's avatar
张瑶 committed
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
    symbolSize: 1,
    symbolOffset: [0, '50%'],
    label: {
      formatter: '{b|{b} }{c|{c}}',
      backgroundColor:
        window.globalConfig &&
        window.globalConfig &&
        window.globalConfig.variableTheme?.primaryColor
          ? window.globalConfig.variableTheme.primaryColor
          : '#0087F7',
      borderColor: '#ccc',
      borderWidth: 1,
      borderRadius: 4,
      padding: [2, 10],
      lineHeight: 22,
      position: 'top',
      distance: 10,
      rich: {
        b: {
          color: '#fff',
        },
        c: {
          color: '#fff',
          fontSize: 16,
          fontWeight: 700,
        },
      },
    },
162 163 164 165
    data,
  };
};

166 167 168 169 170 171 172 173 174 175 176 177 178
/**
 * 坐标轴添加网格线配置
 *
 * @param {any} axis
 */
export const decorateAxisGridLine = (axis, showGrid) => {
  if (!axis) return;
  axis.minorTick = {
    lineStyle: {
      color: '#e2e2e2',
    },
    ...(axis.minorTick || {}),
    show: showGrid,
张瑶's avatar
张瑶 committed
179
    splitNumber: 2,
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  };
  axis.minorSplitLine = {
    lineStyle: {
      color: '#e2e2e2',
      type: 'dashed',
    },
    ...(axis.minorSplitLine || {}),
    show: showGrid,
  };
  axis.splitLine = {
    ...(axis.splitLine || {}),
    show: showGrid,
  };
};

张瑶's avatar
张瑶 committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/**
 * 坐标轴添加离线区间
 *
 * @param {any} dataItem
 */
export const offlineArea = (dataItem) => {
  if (!dataItem) return {};
  const { dataModel } = dataItem;
  let datas = new Array();
  let offlineData = [];
  let hasOffline = false;
  dataModel.forEach((item) => {
    if (!item.pv && !hasOffline) {
      offlineData = [
        {
          name: '离线',
张瑶's avatar
张瑶 committed
211
          xAxis: new Date(item.pt),
张瑶's avatar
张瑶 committed
212 213 214 215 216
        },
      ];
      hasOffline = true;
    } else if (item.pv && hasOffline) {
      offlineData.push({
张瑶's avatar
张瑶 committed
217
        xAxis: new Date(item.pt),
张瑶's avatar
张瑶 committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231
      });
      datas.push(offlineData);
      offlineData = [];
      hasOffline = false;
    }
  });
  return {
    itemStyle: {
      color: '#eee',
    },
    data: datas,
  };
};

232 233
/**
 * 图表配置项生成
234 235 236 237 238 239 240
 *
 * @param {any} dataSource 数据源
 * @param {any} cusOption 自定义属性
 * @param {any} contrast 是否为同期对比
 * @param {any} contrastOption 同期对比周期配置, day|month
 * @param {any} smooth Ture/false, 曲线/折线
 * @param {any} config 额外配置信息
241 242 243
 */
const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth, config) => {
  const needUnit = _.get(config, 'needUnit', false);
244
  const curveCenter = _.get(config, 'curveCenter', false);
245
  const nameWithSensor = _.get(config, 'nameWithSensor', true);
246
  const showGridLine = _.get(config, 'showGridLine', false);
247 248
  const showMarkLine = _.get(config, 'showMarkLine', false);
  const showPoint = _.get(config, 'showPoint', false);
249 250
  const deviceAlarmSchemes = _.get(config, 'deviceAlarmSchemes', []);

251
  // 自定义属性
252
  const restOption = _.pick(cusOption, ['title', 'legend']);
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

  // 一种指标一个y轴
  const yAxisMap = new Map();
  dataSource.forEach((item, index) => {
    const { sensorName, unit } = item;
    const key = sensorName;

    if (!yAxisMap.has(key)) {
      const i = yAxisMap.size;
      const axis = {
        type: 'value',
        name: needUnit ? unit : null,
        position: i % 2 === 0 ? 'left' : 'right',
        offset: Math.floor(i / 2) * axisWidth,
        axisLabel: {
268
          formatter: (value) => (value > 100000 ? `${value / 1000}k` : value),
269 270 271 272 273
        },
        axisLine: {
          show: true,
        },
        nameTextStyle: {
274
          align: i % 2 === 0 ? 'right' : 'left',
275
        },
276 277 278 279 280 281 282 283 284 285 286
        minorTick: {
          lineStyle: {
            color: '#e2e2e2',
          },
        },
        minorSplitLine: {
          lineStyle: {
            color: '#e2e2e2',
            type: 'dashed',
          },
        },
287
      };
288 289
      yAxisMap.set(key, axis);
    }
290 291 292 293 294 295 296 297

    // 曲线居中
    if (curveCenter && item.dataModel && item.dataModel.length > 0) {
      const [min, max] = minMax(item);
      const axis = yAxisMap.get(key);
      axis.min = axis.min === void 0 ? min : Math.min(min, axis.min);
      axis.max = axis.max === void 0 ? max : Math.max(max, axis.max);
    }
298 299

    // 网格显示
300 301
    const axis = yAxisMap.get(key);
    decorateAxisGridLine(axis, showGridLine);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
  });
  const yAxis = yAxisMap.size > 0 ? [...yAxisMap.values()] : { type: 'value' };

  // 根据y轴个数调整边距
  const leftNum = Math.ceil(yAxisMap.size / 2);
  const rightNum = Math.floor(yAxisMap.size / 2);
  const grid = {
    top: needUnit ? 80 : 60,
    left: 10 + leftNum * axisWidth,
    right: rightNum === 0 ? 20 : rightNum * axisWidth,
  };

  // 根据"指标名称"分类yAxis
  const yAxisInterator = (() => {
    const map = new Map();
    let current = -1;
318 319
    const get = (name) => (map.has(name) ? map.get(name) : map.set(name, ++current).get(name));
    return { get };
320 321
  })();

322
  const series = dataSource.map((item, index) => {
323
    const { sensorName, unit } = item;
324
    const name = nameFormatter(item, contrast, contrastOption, nameWithSensor);
325 326 327 328
    const data = dataAccessor(item, contrast, contrastOption);
    const type = 'line';
    const areaStyle = areaStyleFormatter(item);
    const yAxisIndex = yAxisInterator.get(sensorName);
329 330
    const markLine = showMarkLine ? alarmMarkLine(item, index, dataSource, deviceAlarmSchemes) : {};
    const markPoint = showPoint ? minMaxMarkPoint(item, index, dataSource) : {};
张瑶's avatar
张瑶 committed
331 332 333 334 335 336
    let markArea = null;
    let offlineAreas = offlineArea(item);
    if (offlineAreas.data?.length) {
      restOption.markArea = null;
      markArea = offlineAreas;
    }
337 338 339 340 341 342 343 344 345
    return {
      notMerge: true,
      name,
      type,
      data,
      areaStyle,
      yAxisIndex,
      smooth,
      unit,
346
      markLine,
347
      markPoint,
张瑶's avatar
张瑶 committed
348
      markArea,
349
    };
350 351 352
  });

  // 由于series更新后,没有的数据曲线仍然停留在图表区上,导致图表可视区范围有问题
353 354 355 356 357 358 359 360
  const min = Math.min(
    ...series.map((item) => item.data?.[0]?.[0]).filter((item) => item !== undefined),
  );
  const max = Math.max(
    ...series
      .map((item) => item.data?.[item.data.length - 1]?.[0])
      .filter((item) => item !== undefined),
  );
361
  const xAxis = { type: 'time', min, max };
362
  decorateAxisGridLine(xAxis, showGridLine);
363

364 365 366 367 368
  const tooltipTimeFormat = !contrast
    ? 'YYYY-MM-DD HH:mm:ss'
    : contrastOption === 'day'
    ? 'HH:mm'
    : 'DD HH:mm';
369 370 371 372 373 374 375
  const tooltip = {
    timeFormat: tooltipTimeFormat,
    // trigger: 'axis',
    // axisPointer: {
    //   type: 'cross'
    // }
  };
376 377 378 379 380 381 382 383

  return {
    yAxis,
    grid,
    xAxis,
    series,
    tooltip,
    ...restOption,
384
  };
385 386 387
};

export default optionGenerator;