index.js 7.06 KB
/*
 * @Description:
 * @Date: 2021-06-19 17:49:06
 */

import colors from '../config/colors';

export const chartConfig = () => {
  let index = 0;
  const createData = (xLength = 0, yLength = 0) => {
    const createArr = length => {
      const data = [];
      for (let dataIndex = 0; dataIndex < length; dataIndex++) {
        data.push(parseInt(Math.random() * 100));
      }
      return data;
    };
    const info = {
      x: [],
      y: [],
      data: yLength == 0 ? [createArr(xLength)] : []
    };
    for (let x = 0; x < xLength; x++) {
      info.x.push(`${+x + 1}-x-选项`);
    }
    for (let y = 0; y < yLength; y++) {
      info.y.push(`${+y + 1}-y-选项`);
      info.data.push(createArr(xLength));
    }
    return info;
  };

  const dealAxis = (arr, isGroup) => {
    if (isGroup) {
      const info = {
        x: [],
        y: [],
        data: []
      };
      const a = arr.reduce((pre, cur) => {
        if (cur.y) {
          if (!pre[cur.y]) {
            pre[cur.y] = [cur];
          } else {
            pre[cur.y].push(cur);
          }
        }
        return pre;
      }, {});
      for (let y in a) {
        const s = a[y].sort((itema, itemb) => {
          if (itema.x < itemb.x) {
            return -1;
          }
        });
        info.y.push(y);
        info.data.push(s.map(item => item.value));
        info.x = s.map(item => item.x);
      }
      return info;
    }
    return {
      x: arr.map(item => item.x),
      y: [],
      data: [arr.map(item => item.value)]
    };
  };
  return {
    createConfig: (arr, options = {}) => {
      const {
        showInCatalogue,
        blockTitle,
        hideTitle,
        hideColor,
        hideTypes,
        hideChart,
        hideDesc,
        hideLegend,
        hideDelete,
        width = 1140,
        height = 450,
        colourGroup,
        extendTypes = [],
        disableTypes = [],
        isDelete,
        showSort,
        xAxisName,
        yAxisName,
        legendName = '',
        legends,
        chartType,
        isGroup,

        hideBtn,
        resultIsShow,
        resultSystem,
        resultUser,

        znTableIsShow,
        znResultSystem,
        znResultUser,
        znResultIsShow,
        summary,
        resultX2,
        resultP,

        customGroupTitle,
        dontContenteditable,
        toggleAxisContenteditable,

        xFactor,
        yFactor,

        hideToggleAxis
      } = options;
      const title = blockTitle;
      const { x, y, data } = dealAxis(arr, isGroup);
      const analysisData = data.map(item => {
        return item.concat(
          item.reduce((pre, cur) => {
            return pre + cur;
          }, 0)
        );
      });
      return {
        showInCatalogue,
        isDelete: isDelete || 0,
        sort: showSort || 0,
        chart: {
          show: !hideChart,
          type: chartType,
          colors:
            (typeof colourGroup === 'string' && colourGroup.split(',')) ||
            colors[0],
          data: data,
          mapScatterData: arr,
          axis: [
            { factorName: xFactor, name: xAxisName, names: x },
            {
              factorName: yFactor,
              name: yAxisName,
              names: y
            }
          ],
          width: width,
          height: height,
          extendTypes,
          disableTypes
        },
        title: {
          show: !hideTitle,
          name: title,
          hideColor: hideColor,
          hideDelete: hideDelete,
          hideChangeChart: hideTypes,
          contenteditable: !dontContenteditable,
          toggleAxisContenteditable: toggleAxisContenteditable,
          customGroupTitle: customGroupTitle,
          hideToggleAxis: hideToggleAxis
        },
        legend: {
          show: !hideLegend,
          title:
            legendName &&
            legendName.replace(
              '${num}',
              data[0].reduce((pre, cur) => {
                pre += cur;
                return pre;
              }, 0)
            ),
          data: legends || (isGroup ? y : x)
        },
        desc: {
          hideBtn: hideBtn,
          show: !hideDesc,
          setShow: !!resultIsShow,
          text: resultSystem,
          customText: resultUser || ''
        },
        analysis: {
          show: isGroup,
          setShow: !!znTableIsShow,
          data: {
            title: [
              { factorName: xFactor, name: xAxisName, axis: x },
              { factorName: yFactor, name: yAxisName, axis: y }
            ],
            arr: [
              ...analysisData,
              analysisData.reduce((pre, cur) => {
                cur.forEach((item, index) => {
                  pre[index] = (pre[index] || 0) + item;
                });
                return pre;
              }, [])
            ],
            res: {
              x: resultX2,
              p: resultP
            }
          },
          title: '卡方分析结果'
        },
        analysisDesc: {
          show: isGroup,
          setShow: !!znResultIsShow,
          title: '智能分析',
          text: znResultSystem,
          customText: znResultUser,
          resText: `总结可知:${summary}`
        },
        baseInfo: { ...options }
      };
    },
    create: (xLength, yLength, type = 1, options = {}) => {
      index += 1;
      const {
        blockTitle,
        hideTitle,
        hideChart,
        hideDesc,
        hideLegend,
        hideDelete,
        width = 1140,
        height = 450,
        extendTypes = [],
        disableTypes = []
      } = options;
      const title = blockTitle || `${index}题目题目`;
      const { x, y, data } = createData(xLength, yLength);
      return {
        isDelete: 0,
        sort: 0,
        chart: {
          show: !hideChart,
          type: type,
          colors: colors[0],
          data: data,
          axis: [
            { name: `原始x轴-${index}`, names: x },
            { name: `原始y轴-${index}`, names: y }
          ],
          width: width,
          height: height,
          extendTypes,
          disableTypes
        },
        title: {
          show: !hideTitle,
          name: title,
          hideColor: false,
          hideDelete: hideDelete,
          hideTypes: false
        },
        legend: {
          show: !hideLegend,
          name: '',
          data: yLength > 0 ? y : x
        },
        desc: {
          show: !hideDesc,
          setShow: true,
          text: '本报告通过定性(用户调研)和定量(调',
          customText: ''
        },
        analysis: {
          show: yLength > 0,
          setShow: true,
          title: '卡方分析结果'
        },
        analysisDesc: {
          show: yLength > 0,
          setShow: true,
          title: '智能分析',
          text:
            '本期样本n=200  ,通过卡方检验 进行不同患者年龄与患者发病状态的相关分析,计算得出X²= 0.833 p=0.316。因此,不同患者年龄与患者发病状态无统计学意义(  p>0.5)',
          customText: '',
          resText: '总结可知:不同年龄患者的发病状态无显著性差异。'
        }
      };
    }
  };
};