chart-utils.ts 2.6 KB
Newer Older
Z
zengqiao 已提交
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
import { ISeriesOption } from 'types/base-type';

export const isArrElementAllNull = (arr: any[]) => {
  let isAllNull = true;
  arr.forEach(item => {
    if (item !== null) {
      isAllNull = false;
    }
  });
  return isAllNull;
};

export const dealFlowData = (metricList: string[], data: any[]) => {
  let name = '';
  const metricData = [] as Array<{
    metric: string,
    data: number[],
  }>;
  metricList.map(metric => {
    metricData.push({
      metric,
      data: data.map(item => item[metric]),
    });
  });

  if (metricData.map(i => isMB(i.data)).some(i => i === true)) {
    name = 'MB/s';
    metricList.map(metric => {
      data.map(item => {
        item[metric] = item[metric] !== null ? Number((item[metric] / (1024 * 1024)).toFixed(2)) : null;
       });
    });
  } else if (metricData.map(i => isKB(i.data)).some(i => i === true)) {
    name = 'KB/s';
    metricList.map(metric => {
      data.map(item => {
        item[metric] = item[metric] !== null ? Number((item[metric] / (1024)).toFixed(2)) : null;
       });
    });
  } else {
    name = 'B/s';
    metricList.map(metric => {
      data.map(item => {
        item[metric] = item[metric] !== null ? Number(item[metric].toFixed(2)) : null;
       });
    });
  }
  return { name, data };
};

export function isMB(arr: number[]) {
  const filterData = arr.filter(i => i !== 0);
  if (filterData.length) return filterData.reduce((cur, pre) => cur + pre) / filterData.length >= 100000;
  return false;
}

export function isKB(arr: number[]) {
  const filterData = arr.filter(i => i !== 0);
  if (filterData.length) return filterData.reduce((cur, pre) => cur + pre) / filterData.length >= 1000;
  return false;
}

export const getFilterSeries = (series: ISeriesOption[]) => {
  const filterSeries = [].concat(...series);
  const nullIndex: string[] = [];
  for (const row of series) {
    if (isArrElementAllNull(row.data)) {
      nullIndex.push(row.name);
    }
  }

  nullIndex.map(line => {
    const index = filterSeries.findIndex(row => row.name === line);
    if (index > -1) {
      filterSeries.splice(index, 1);
    }
  });
  for (const item of filterSeries) {
    delete item.data;
  }
  return filterSeries;
};

export const baseLineConfig = {
  tooltip: {
    trigger: 'axis',
    padding: 10,
    backgroundColor: 'rgba(0,0,0,0.7)',
    borderColor: '#333',
    textStyle: {
      color: '#f3f3f3',
      fontSize: 10,
    },
  },
  yAxis: {
    type: 'value',
    nameLocation: 'end',
    nameGap: 10,
  },
  legend: {
    right: '1%',
    top: '10px',
  },
  grid: {
    left: '1%',
    right: '1%',
    bottom: '3%',
    top: '40px',
    containLabel: true,
  },
};