zhou zhou
10 小时以前 46d872c1a5b77aa8799de4a64888a0a24a1422d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
<!-- 柱状图 -->
<template>
  <div ref="chartRef" :style="{ height: props.height }" v-loading="props.loading"> </div>
</template>
 
<script setup>
  import { useChartOps, useChartComponent } from '@/hooks/core/useChart'
  import { getCssVar } from '@/utils/ui'
  import { graphic } from '@/plugins/echarts'
  defineOptions({ name: 'ArtBarChart' })
  const props = defineProps({
    height: { required: false, default: useChartOps().chartHeight },
    loading: { required: false, default: false },
    isEmpty: { required: false, default: false },
    colors: { required: false, default: () => useChartOps().colors },
    borderRadius: { required: false, default: 4 },
    data: { required: false, default: () => [0, 0, 0, 0, 0, 0, 0] },
    xAxisData: { required: false, default: () => [] },
    barWidth: { required: false, default: '40%' },
    stack: { required: false, default: false },
    showAxisLabel: { required: false, default: true },
    showAxisLine: { required: false, default: true },
    showSplitLine: { required: false, default: true },
    showTooltip: { required: false, default: true },
    showLegend: { required: false, default: false },
    legendPosition: { required: false, default: 'bottom' }
  })
  const isMultipleData = computed(() => {
    return (
      Array.isArray(props.data) &&
      props.data.length > 0 &&
      typeof props.data[0] === 'object' &&
      'name' in props.data[0]
    )
  })
  const getColor = (customColor, index) => {
    if (customColor) return customColor
    if (index !== void 0) {
      return props.colors[index % props.colors.length]
    }
    return new graphic.LinearGradient(0, 0, 0, 1, [
      {
        offset: 0,
        color: getCssVar('--el-color-primary-light-4')
      },
      {
        offset: 1,
        color: getCssVar('--el-color-primary')
      }
    ])
  }
  const createGradientColor = (color) => {
    return new graphic.LinearGradient(0, 0, 0, 1, [
      {
        offset: 0,
        color
      },
      {
        offset: 1,
        color
      }
    ])
  }
  const getBaseItemStyle = (color) => ({
    borderRadius: props.borderRadius,
    color: typeof color === 'string' ? createGradientColor(color) : color
  })
  const createSeriesItem = (config) => {
    const animationConfig = getAnimationConfig()
    return {
      name: config.name,
      data: config.data,
      type: 'bar',
      stack: config.stack,
      itemStyle: getBaseItemStyle(config.color),
      barWidth: config.barWidth || props.barWidth,
      ...animationConfig
    }
  }
  const {
    chartRef,
    getAxisLineStyle,
    getAxisLabelStyle,
    getAxisTickStyle,
    getSplitLineStyle,
    getAnimationConfig,
    getTooltipStyle,
    getLegendStyle,
    getGridWithLegend
  } = useChartComponent({
    props,
    checkEmpty: () => {
      if (Array.isArray(props.data) && typeof props.data[0] === 'number') {
        const singleData = props.data
        return !singleData.length || singleData.every((val) => val === 0)
      }
      if (Array.isArray(props.data) && typeof props.data[0] === 'object') {
        const multiData = props.data
        return (
          !multiData.length ||
          multiData.every((item) => !item.data?.length || item.data.every((val) => val === 0))
        )
      }
      return true
    },
    watchSources: [() => props.data, () => props.xAxisData, () => props.colors],
    generateOptions: () => {
      const options = {
        grid: getGridWithLegend(props.showLegend && isMultipleData.value, props.legendPosition, {
          top: 15,
          right: 0,
          left: 0
        }),
        tooltip: props.showTooltip ? getTooltipStyle() : void 0,
        xAxis: {
          type: 'category',
          data: props.xAxisData,
          axisTick: getAxisTickStyle(),
          axisLine: getAxisLineStyle(props.showAxisLine),
          axisLabel: getAxisLabelStyle(props.showAxisLabel)
        },
        yAxis: {
          type: 'value',
          axisLabel: getAxisLabelStyle(props.showAxisLabel),
          axisLine: getAxisLineStyle(props.showAxisLine),
          splitLine: getSplitLineStyle(props.showSplitLine)
        }
      }
      if (props.showLegend && isMultipleData.value) {
        options.legend = getLegendStyle(props.legendPosition)
      }
      if (isMultipleData.value) {
        const multiData = props.data
        options.series = multiData.map((item, index) => {
          const computedColor = getColor(props.colors[index], index)
          return createSeriesItem({
            name: item.name,
            data: item.data,
            color: computedColor,
            barWidth: item.barWidth,
            stack: props.stack ? item.stack || 'total' : void 0
          })
        })
      } else {
        const singleData = props.data
        const computedColor = getColor()
        options.series = [
          createSeriesItem({
            data: singleData,
            color: computedColor
          })
        ]
      }
      return options
    }
  })
</script>