tools.js 2.16 KB
/*
 * @Description:
 * @Date: 2021-07-02 10:57:34
 */
import { base } from '../chart-type/common';

const dealStr = (str, unit = '') => {
  const length = unit.length;
  if (`${str}`.slice(0 - length) === unit) {
    return str;
  } else {
    return `${str}${unit}`;
  }
};

export const dealAxis = (arr = [], unit = '', lastUnit = '') => {
  return arr.map((item, index) => {
    const isLast = index === arr.length - 1;
    const xName = item.genderType || item.xTopicName || item.optionName;
    return {
      x: xName ? dealStr(xName, isLast ? lastUnit : unit) : '-',
      y: item.yTopicName,
      value: item.num || item.countNum || 0
    };
  });
};
export const dealLegends = (arr = [], unit = '', lastUnit = '') => {
  const total = arr.reduce((pre, cur) => {
    return pre + (cur.num || cur.countNum || 0);
  }, 0);
  return arr.reduce((pre, cur, index) => {
    pre.push(
      `${dealStr(
        cur.genderType || cur.xTopicName,
        index === arr.length - 1 ? lastUnit : unit
      )}${cur.num || cur.countNum || 0}人,占比${base.numberFormat(
        total ? (cur.num || cur.countNum || 0) / total : 0,
        0.01
      )}%`
    );
    return pre;
  }, []);
};
export const randomType = (arr = []) => {
  return arr[Math.ceil(Math.random() * arr.length)] || arr[0];
};

export const chartConfigToSetInfo = (chartConfigs, biInfoId) => {
  return chartConfigs.map((item, index) => {
    const { baseInfo, desc, analysisDesc, analysis, isDelete, chart } = item;
    const { id, type } = baseInfo;
    const { customText = '', setShow } = desc;
    return {
      biInfoId: biInfoId,
      id: id,
      groupDetailId: id,
      topicDetailId: id,
      fixedId: id,
      type: type,
      chartType: +chart.type,
      customTitle: item.title.name,
      customGroupTitle: item.title.customGroupTitle,
      colourGroup: Array.isArray(chart.colors)
        ? chart.colors.join(',')
        : chart.colors,
      showSort: +index + 1,
      isDelete: isDelete,
      resultUser: customText,
      znResultUser: analysisDesc.customText,
      resultIsShow: setShow ? 1 : 0,
      znResultIsShow: analysisDesc.setShow ? 1 : 0,
      znTableIsShow: analysis.setShow ? 1 : 0
    };
  });
};