chart-theme.js 5.43 KB
/**
 * ECharts 明暗配色(暗黑下轴线/标签/图例/tooltip 用浅色)
 * @param {boolean} isDark
 */
export function getChartTheme(isDark) {
  if (isDark) {
    return {
      text: 'rgba(255,255,255,0.85)',
      textSecondary: 'rgba(255,255,255,0.65)',
      textMuted: 'rgba(255,255,255,0.45)',
      axisLine: 'rgba(255,255,255,0.15)',
      splitLine: 'rgba(255,255,255,0.08)',
      pieBorder: '#1f1f1f',
      tooltipBg: 'rgba(31,31,31,0.96)',
      tooltipBorder: 'rgba(255,255,255,0.12)',
      barBg: 'rgba(255,255,255,0.08)',
      radarSplitArea: ['rgba(255,255,255,0.04)', 'rgba(255,255,255,0.02)'],
      empty: 'rgba(255,255,255,0.45)'
    }
  }
  return {
    text: '#303133',
    textSecondary: '#606266',
    textMuted: '#909399',
    axisLine: '#E4E7ED',
    splitLine: '#E4E7ED',
    pieBorder: '#fff',
    tooltipBg: 'rgba(255,255,255,0.95)',
    tooltipBorder: '#E4E7ED',
    barBg: '#F2F6FC',
    radarSplitArea: ['#f5f7fa', '#fff'],
    empty: '#909399'
  }
}

/** Vuex slideClass 是否为 dark */
export function isSlideDark(vm) {
  return !!(vm && vm.$store && vm.$store.state &&
    vm.$store.state.settings && vm.$store.state.settings.slideClass === 'dark')
}

function mergeAxisTheme(axis, t, isDark) {
  if (!axis) return axis
  const applyOne = (a) => {
    if (!a || typeof a !== 'object') return a
    const next = { ...a }
    const axisLabelColor = isDark
      ? t.textSecondary
      : ((a.axisLabel && a.axisLabel.color) || t.textSecondary)
    next.axisLabel = { ...(a.axisLabel || {}), color: axisLabelColor }
    const axisLineColor = isDark
      ? t.axisLine
      : (((a.axisLine && a.axisLine.lineStyle && a.axisLine.lineStyle.color) || t.axisLine))
    next.axisLine = {
      ...(a.axisLine || {}),
      lineStyle: { ...((a.axisLine && a.axisLine.lineStyle) || {}), color: axisLineColor }
    }
    if (a.splitLine !== false && !(a.splitLine && a.splitLine.show === false)) {
      const splitLineColor = isDark
        ? t.splitLine
        : (((a.splitLine && a.splitLine.lineStyle && a.splitLine.lineStyle.color) || t.splitLine))
      next.splitLine = {
        ...(a.splitLine || {}),
        lineStyle: { ...((a.splitLine && a.splitLine.lineStyle) || {}), color: splitLineColor }
      }
    }
    if (a.name || a.nameTextStyle) {
      const nameColor = isDark
        ? t.textMuted
        : ((a.nameTextStyle && a.nameTextStyle.color) || t.textMuted)
      next.nameTextStyle = { ...(a.nameTextStyle || {}), color: nameColor }
    }
    return next
  }
  return Array.isArray(axis) ? axis.map(applyOne) : applyOne(axis)
}

/**
 * 把明暗主题注入到 ECharts option(轴/图例/tooltip/饼图边框与标签)
 * @param {object} option
 * @param {boolean} isDark
 */
export function applyChartTheme(option, isDark) {
  if (!option) return option
  const t = getChartTheme(isDark)
  const next = { ...option, backgroundColor: 'transparent' }
  next.textStyle = isDark
    ? { ...(option.textStyle || {}), color: t.text }
    : { color: t.text, ...(option.textStyle || {}) }
  const tooltipTextStyle = (option.tooltip && option.tooltip.textStyle) || {}
  next.tooltip = {
    ...(option.tooltip || {}),
    backgroundColor: t.tooltipBg,
    borderColor: t.tooltipBorder,
    textStyle: isDark
      ? { ...tooltipTextStyle, color: t.text }
      : { color: t.text, ...tooltipTextStyle }
  }
  if (option.legend) {
    const legendTextStyle = (option.legend && option.legend.textStyle) || {}
    next.legend = {
      ...option.legend,
      textStyle: isDark
        ? { ...legendTextStyle, color: t.textSecondary }
        : { color: t.textSecondary, ...legendTextStyle }
    }
  }
  if (option.title && typeof option.title === 'object') {
    const titleTextStyle = (option.title && option.title.textStyle) || {}
    next.title = {
      ...option.title,
      textStyle: isDark
        ? { ...titleTextStyle, color: t.empty, fontSize: titleTextStyle.fontSize || 14 }
        : { color: t.empty, fontSize: 14, ...titleTextStyle }
    }
  }
  if (option.xAxis) next.xAxis = mergeAxisTheme(option.xAxis, t, isDark)
  if (option.yAxis) next.yAxis = mergeAxisTheme(option.yAxis, t, isDark)
  if (option.visualMap) {
    const vm = option.visualMap
    const applyVm = (v) => ({
      ...v,
      textStyle: { color: t.textSecondary, ...(v.textStyle || {}) }
    })
    next.visualMap = Array.isArray(vm) ? vm.map(applyVm) : applyVm(vm)
  }
  if (Array.isArray(option.series)) {
    next.series = option.series.map(s => {
      if (!s || typeof s !== 'object') return s
      const series = { ...s }
      if (s.type === 'pie' || s.type === 'funnel') {
        const border = (s.itemStyle && s.itemStyle.borderColor === '#fff') ? t.pieBorder
          : ((s.itemStyle && s.itemStyle.borderColor) || t.pieBorder)
        series.itemStyle = { ...(s.itemStyle || {}), borderColor: border }
        if (s.label) {
          const labelColor = isDark
            ? (s.type === 'funnel' && s.label.position === 'inside' ? '#fff' : t.textSecondary)
            : (s.label.color || (s.type === 'funnel' && s.label.position === 'inside' ? '#fff' : t.textSecondary))
          series.label = { ...s.label, color: labelColor }
        }
        if (s.labelLine && s.labelLine.lineStyle) {
          series.labelLine = {
            ...s.labelLine,
            lineStyle: { ...s.labelLine.lineStyle, color: t.axisLine }
          }
        }
      }
      if (s.type === 'heatmap' && s.label) {
        series.label = { ...s.label, color: t.text }
      }
      return series
    })
  }
  return next
}