SingleChart.js 3.1 KB
Newer Older
1
import React, { memo, useEffect, useMemo, useRef } from 'react';
2
import { BasicChart } from '@wisdom-components/basicchart';
3
import PandaEmpty from '@wisdom-components/empty';
4
import optionGenerator, { alarmMarkLine, minMaxMarkPoint } from './utils';
5
import { isArray, cloneDeep } from 'lodash';
6 7

const SimgleChart = memo((props) => {
8 9 10 11 12 13
  const {
    dataSource,
    contrast = false,
    contrastOption = 'day',
    smooth = true,
    curveCenter,
14
    showGridLine = false,
15
    deviceAlarmSchemes,
16
  } = props;
17
  const chartRef = useRef();
18 19 20 21

  const option = useMemo(() => {
    const config = {
      needUnit: true,
22
      curveCenter,
23
      showGridLine,
24
      deviceAlarmSchemes,
25 26
      showMarkLine: true,
      showPoint: true,
27 28
    };
    return optionGenerator(dataSource, null, contrast, contrastOption, smooth, config);
29
  }, [dataSource, smooth, curveCenter]);
30

31 32
  useEffect(() => {
    chartRef.current?.resize?.();
33
    const chart = chartRef.current?.getEchartsInstance?.();
34 35 36 37 38 39 40

    function hander(params) {
      const { selected } = params;
      const count = Object.values(selected || {}).filter((item) => item).length;
      const option = cloneDeep(chart.getOption());
      const needMarkLine = count === 1;
      option.series.forEach((item, index) => {
41 42 43 44 45 46 47 48 49 50 51 52
        if (needMarkLine && selected[item.name]) {
          item.markLine = alarmMarkLine(
            dataSource[index],
            index,
            [dataSource[index]],
            deviceAlarmSchemes,
          );
          item.markPoint = minMaxMarkPoint(dataSource[index], index, [dataSource[index]]);
        } else {
          item.markLine = {};
          item.markPoint = {};
        }
53 54 55 56 57 58 59 60 61
      });
      chart.setOption(option, true);
    }
    if (!chart) return;
    chart.on('legendselectchanged', hander);
    return () => {
      chart.off('legendselectchanged', hander);
    };
  }, [dataSource, deviceAlarmSchemes]);
62

63 64
  // 网格开关,不更新整个图表
  useEffect(() => {
65
    const chart = chartRef.current?.getEchartsInstance?.();
66
    if (!chart) return;
67 68 69 70 71 72 73
    const option = chart.getOption();
    // 交互指针
    const tooltip = {
      trigger: 'axis',
      axisPointer: {
        type: showGridLine ? 'cross' : 'line',
      },
74
    };
75 76 77 78 79 80 81
    // 网格线
    const axisConfig = {
      minorTick: {
        show: showGridLine,
      },
      minorSplitLine: {
        show: showGridLine,
82 83 84 85 86 87
        lineStyle: {
          type: 'dashed',
        },
      },
      splitLine: {
        show: showGridLine,
88 89 90 91 92 93
      },
    };
    let yAxis = axisConfig;
    if (isArray(option.yAxis)) {
      yAxis = option.yAxis.map((item) => ({ ...axisConfig }));
    }
94
    let xAxis = axisConfig;
95
    chart.setOption({
96
      xAxis,
97 98 99 100
      yAxis,
      tooltip,
    });
  }, [showGridLine]);
101

102 103 104 105 106 107 108 109 110 111 112 113
  // 数据都为空显示缺省页
  const isEmpty = useMemo(
    () =>
      !dataSource ||
      !dataSource.length ||
      !dataSource.find((e) => e.dataModel && e.dataModel.length > 0),
    [dataSource],
  );

  return isEmpty ? (
    <PandaEmpty />
  ) : (
114 115
    <BasicChart ref={chartRef} option={option} notMerge style={{ width: '100%', height: '100%' }} />
  );
116 117
});

118
export default SimgleChart;