default.js 5.97 KB
Newer Older
崔佳豪's avatar
崔佳豪 committed
1
import _ from 'lodash';
2 3 4 5 6 7 8
import {
  DEF_COLORS,
  DEF_GRAD_COLORS,
  colorOfSeries,
  toGRADColorForLine,
  buildDefaultColors,
} from './color';
9
import { isArray } from './types';
崔佳豪's avatar
崔佳豪 committed
10 11
import { isAxisChart, isLineType } from './chart';
import { buildDefaultTooltip } from './tooltip';
12 13
import { buildDefaultLegend } from './legend';
import { globalConfig } from './global';
崔佳豪's avatar
崔佳豪 committed
14 15 16

// 默认标题配置

17 18 19 20 21 22 23
// const titleFormater = text => `{prefix|}{t|${title}}{suffix|内容}`;

export const buildDefaultTitle = (option) => {
  const { title } = option;
  const { show, text } = title ?? {};
  if (show && text) {
    return {
24 25
      left: 12,
      top: 12,
26 27 28 29 30
      textStyle: {
        rich: {
          prefix: {
            width: 5,
            height: 16,
31
            backgroundColor: globalConfig.primaryColor,
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
          },
          t: {
            fontSize: 15,
            fontFamily: 'Microsoft YaHei',
            fontWeight: 'bold',
            color: '#333333',
            padding: [0, 0, 0, 10],
          },
          suffix: {
            fontSize: 12,
            color: '#999999',
            padding: [0, 0, 0, 5],
          },
        },
      },
    };
  } else {
    return { show: false };
  }
};

崔佳豪's avatar
崔佳豪 committed
53 54 55 56 57
export const defaultGrid = {
  right: 40,
  left: 60,
};

58 59
export const buildDefaultGrid = (option) => {
  const { title, yAxis, series } = option;
60 61
  const axisArr = isArray(yAxis) ? yAxis : _.isObject(yAxis) ? [yAxis] : [];
  const seriesArr = isArray(series) ? series : _.isObject(series) ? [series] : [];
62 63 64
  const hasName = axisArr.some((d) => !!d.name) || seriesArr.some((d) => !!d.unit);
  const hasTitle = !!title && title.show;
  const top = hasTitle && hasName ? 70 : 50;
65
  return { right: 40, left: 60, top, bottom: 40 };
66 67
};

崔佳豪's avatar
崔佳豪 committed
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
export const defaultDataZoom = [
  {
    type: 'inside',
  },
];

// 直角坐标系图表x轴默认配置(非直角坐标系图表不能有xAxis配置,会报错)
export const buildDefaultXAxis = (axis) => {
  const cfg = {
    show: true,
    axisLine: {
      show: true,
      lineStyle: {
        color: '#e2e2e2',
      },
    },
    axisTick: {
      show: true,
      inside: true,
      lineStyle: {
        color: '#e2e2e2',
      },
    },
    axisLabel: {
      show: true,
      color: '#5b5b5b',
      hideOverlap: true,
    },
    splitLine: {
      show: false,
      lineStyle: {
        color: '#e2e2e2',
      },
    },
  };
103
  if (axis && axis.type === 'time') {
崔佳豪's avatar
崔佳豪 committed
104 105 106 107 108 109 110 111 112 113 114 115 116
    cfg.axisLabel.formatter = {
      year: '{yyyy}',
      month: '{MM}月',
      day: '{MM}-{dd}',
      hour: '{HH}:{mm}',
      minute: '{HH}:{mm}',
      second: '{HH}:{mm}:{ss}',
      millisecond: '{hh}:{mm}:{ss} {SSS}',
      none: '{yyyy}-{MM}-{dd} {hh}:{mm}:{ss} {SSS}',
    };
  }
  return cfg;
};
117

崔佳豪's avatar
崔佳豪 committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
export const defaultXAxis = {
  show: true,
  axisLine: {
    show: true,
    lineStyle: {
      color: '#e2e2e2',
    },
  },
  axisTick: {
    show: true,
    inside: true,
    lineStyle: {
      color: '#e2e2e2',
    },
  },
  axisLabel: {
    show: true,
    color: '#5b5b5b',
    hideOverlap: true,
  },
  splitLine: {
    show: false,
    lineStyle: {
      color: '#e2e2e2',
    },
  },
};

export const defaultYAxis = {
  show: true,
  alignTicks: true,
  nameGap: 25,
  nameTextStyle: {
    align: 'right',
    padding: 0,
    color: '#8e8e8e',
  },
  axisLine: {
    show: false,
    lineStyle: {
      color: '#e2e2e2',
    },
  },
  axisTick: {
    show: false,
    inside: true,
    lineStyle: {
      color: '#e2e2e2',
    },
  },
  axisLabel: {
    show: true,
    color: '#8e8e8e',
  },
  splitLine: {
    show: true,
    lineStyle: {
      color: '#e2e2e2',
      type: 'dashed',
    },
  },
};

export const buildAxisPointer = (option) => {
  if (!option || !option.xAxis) return null;
  const { xAxis: x } = option;
184
  const xAxis = isArray(x) ? x : _.isObject(x) ? [x] : [];
崔佳豪's avatar
崔佳豪 committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

  const type = xAxis.some((item) => item.type === 'category') ? 'shadow' : 'line';
  const typeConfig =
    type === 'line'
      ? {
          lineStyle: {
            type: 'solid',
            color: 'rgba(22,133,255,0.3)',
          },
        }
      : {};
  return {
    z: 3,
    type,
    ...typeConfig,
  };
};

// 折线图: 圆形标记点
// 曲线图: 不加标记点
// 折线面积图: 圆形标记点、面积渐变色配置
// 曲线面积图: 不加标记点、面积渐变色配置
const seriesItem = (series, index) => {
  const cfg = {};
209

崔佳豪's avatar
崔佳豪 committed
210 211 212 213 214 215
  if (isLineType(series)) {
    // cfg.symbol = 'ciecle';
    cfg.symbolSize = 4;
    cfg.showSymbol = !series.smooth;
    cfg.areaStyle = series.areaStyle && {
      opacity: 1,
216
      color: toGRADColorForLine(colorOfSeries(index)),
崔佳豪's avatar
崔佳豪 committed
217 218 219 220 221 222 223
    };
  }
  return _.merge(cfg, series);
};

export const buildDefaultSeries = (option) => {
  const { series } = option;
224
  const serieArr = isArray(series) ? series : !!series ? [series] : [];
崔佳豪's avatar
崔佳豪 committed
225
  const defaultSeries = serieArr.map(seriesItem);
226
  return isArray(series) ? defaultSeries : !!series ? defaultSeries[0] : null;
崔佳豪's avatar
崔佳豪 committed
227 228 229 230
};

export const buildDefaultOption = (option) => {
  const exports = {};
231
  exports.color = buildDefaultColors();
232
  exports.title = buildDefaultTitle(option);
崔佳豪's avatar
崔佳豪 committed
233 234 235 236 237 238 239 240 241 242 243 244 245
  (option.dataZoom == undefined || option.dataZoom === null) &&
    (exports.dataZoom = defaultDataZoom);
  exports.tooltip = buildDefaultTooltip(option);
  return exports;
};

export const buildSpecificOption = (option) => {
  const { xAxis, yAxis, grid, legend } = option;
  // 直角坐标系类图标
  if (isAxisChart(option)) {
    return _.merge(
      {},
      {
246
        xAxis: isArray(xAxis)
崔佳豪's avatar
崔佳豪 committed
247 248
          ? xAxis.map((item) => ({ ...buildDefaultXAxis(item) }))
          : buildDefaultXAxis(xAxis),
249 250
        yAxis: isArray(yAxis) ? yAxis.map(() => ({ ...defaultYAxis })) : defaultYAxis,
        grid: isArray(grid)
251 252
          ? grid.map(() => ({ ...buildDefaultGrid(option) }))
          : buildDefaultGrid(option),
253 254 255
        legend: isArray(legend)
          ? legend.map(() => ({ ...buildDefaultLegend(option) }))
          : buildDefaultLegend(option),
崔佳豪's avatar
崔佳豪 committed
256 257 258 259 260 261 262 263
        series: buildDefaultSeries(option),
        tooltip: {
          axisPointer: buildAxisPointer(option),
        },
      },
    );
  }
};