formatEcharts.js 2.05 KB
Newer Older
wuhao's avatar
wuhao committed
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
import getMaxNum from "./getMaxNum";
import formatTwoNum from "./formatTwoNum";
export default function formatEcharts(chartRef, leftArr, rightArr) {
  return {
    legendselectchanged: (params) => {
      const echarts_instance = chartRef.getEchartsInstance(),
        option = echarts_instance.getOption(),
        { yAxis, series } = option,
        { selected } = params,
        selectArr = [];
      for (let i in selected) {
        if (selected[i]) {
          selectArr.push(i);
        }
      }
      let newSeries = series.filter((it) => selectArr.indexOf(it.name) > -1),
        leftSeries = newSeries.filter((it) => leftArr?.indexOf(it.name) > -1),
        rightSeries = newSeries.filter((it) => rightArr?.indexOf(it.name) > -1),
        leftData = [],
        rightData = [];
      leftSeries.forEach((it) => {
        leftData = [...leftData, ...it.data];
      });
      rightSeries.forEach((it) => {
        rightData = [...rightData, ...it.data];
      });
      let leftMax = getMaxNum(leftData, "left"),
        rightMax = getMaxNum(rightData, "right"),
        leftInterval = formatTwoNum(leftMax / 5),
        rightInterval = formatTwoNum(rightMax / 5);

      if (leftSeries.length) {
        yAxis[0].axisLabel = {
          ...yAxis[0]?.axisLabel,
          show: true,
        };
        yAxis[0].max = leftMax;
        yAxis[0].interval = leftInterval;
      } else {
        yAxis[0].axisLabel = {
          ...yAxis[0]?.axisLabel,
          show: false,
        };
        yAxis[0].max = 0;
        yAxis[0].interval = 0;
      }
      if (yAxis.length > 1) {
        if (rightSeries.length) {
          yAxis[1].axisLabel = {
            ...yAxis[1]?.axisLabel,
            show: true,
          };
          yAxis[1].max = rightMax;
          yAxis[1].interval = rightInterval;
        } else {
          yAxis[1].axisLabel = {
            ...yAxis[1]?.axisLabel,
            show: false,
          };
          yAxis[1].max = 0;
          yAxis[1].interval = 0;
        }
      }

      option.yAxis = [...yAxis];
      echarts_instance.setOption(option);
    },
  };
}