BarOptions.js 2.32 KB
/*
 * @Description: 柱状图配置
 * @Author: huyue
 * @Date: 2023-07-07 14:04:52
 * @LastEditors: huyue
 * @LastEditTime: 2023-08-03 16:42:12
 */
import * as echarts from 'echarts'
function getBarOptions(obj, colorList, isMultiple, barwidth, formatYAxis) {
  const barWidth = barwidth || '14'
  const multiple = isMultiple || false
  const { tooltipFormatter } = obj
  var barLineOption = {
    tooltip: {
      trigger: 'axis',
      borderWidth: 0,
      padding: 0,
      formatter: tooltipFormatter || '',
      confine: true
    },
    xAxis: {
      data: []
    },
    barWidth: barWidth,
    yAxis: {
      name: '',
      type: 'value',
      // min: 0,
      // max: 100,
      splitLine: {
        lineStyle: {
          color: '#1d2a38'
        }
      },
      splitNumber: 4,
      axisLabel: {
        // 这种做法就是在y轴的数据的值旁边拼接单位,貌似也挺方便的
        formatter: function(value) {
          if (formatYAxis) {
            return `${value / 10000}万`
          } else {
            return value
          }
        }
      }
    },
    grid: {
      left: '0',
      right: '4%',
      top: '20%',
      bottom: '4%',
      containLabel: true
    },
    series: [
      {
        name: 'bar',
        type: 'bar',
        barWidth: '50%',
        // barGap: '10%',
        data: [],
        label: {
          show: false
        },
        itemStyle: {
          barBorderRadius: [2, 2, 0, 0], // 柱体圆角
          color: function(params) {
            var colorItem = []
            if (multiple) {
              colorItem = params.dataIndex % 2 === 0 ? colorList[0] : colorList[1]
            }
            return new echarts.graphic.LinearGradient(
              0, 0, 0, 1, [{ // 代表渐变色从正上方开始
                offset: 0, // offset范围是0~1,用于表示位置,0是指0%处的颜色
                color: multiple ? colorItem[0] : colorList[0]
              }, // 柱图渐变色
              {
                offset: 0.5, // 指100%处的颜色
                color: multiple ? colorItem[1] : colorList[1]
              },
              {
                offset: 1, // 指100%处的颜色
                color: multiple ? colorItem[2] : colorList[2]
              }]
            )
          }
        }
      }
    ]
  }
  return barLineOption
}

export default getBarOptions