fei.wang
2025-04-18 11f6acee504c77a8919a4e0ddfe3e70a746e3522
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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
<template>
  <div ref="chartContainer" :style="{ height: height, width: width }"></div>
</template>
 
<script>
import * as echarts from 'echarts';
 
export default {
  name: 'BarChart',
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '700px'
    },
    // 通过 props 传递颜色数据
    colors: {
      type: Array,
      default: () => ['#074cad', '#91cc75', '#fac858', '#ee6666'],
    },
    // 通过 props 传递配置数据
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chartInstance: null,
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (this.chartInstance) {
      this.chartInstance.dispose();
    }
  },
  methods: {
    initChart() {
      // 初始化图表
      this.chartInstance = echarts.init(this.$refs.chartContainer);
 
      // 配置项
      const option = this.generateOption(this.chartData, this.colors);
 
      // 设置图表配置
      this.chartInstance.setOption(option);
 
      // 图表自适应窗口大小变化
      window.addEventListener('resize', this.resizeChart);
    },
    generateOption(data, colors) {
      const posList = [
        'left',
        'right',
        'top',
        'bottom',
        'inside',
        'insideTop',
        'insideLeft',
        'insideRight',
        'insideBottom',
        'insideTopLeft',
        'insideTopRight',
        'insideBottomLeft',
        'insideBottomRight',
      ];
 
      const labelOption = {
        show: true,
        position: data.position || 'insideBottom',
        distance: data.distance || 15,
        align: data.align || 'left',
        verticalAlign: data.verticalAlign || 'middle',
        rotate: data.rotate || 90,
        // formatter: '{c}  {name|{a}}',
        fontSize: 16,
        rich: {
          name: {},
        },
      };
 
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        legend: {
          data: data.legendData,
          textStyle: {
            color: '#1890ff' // 标题文字颜色
          }
        },
        toolbox: {
          show: true,
          orient: 'vertical',
          left: 'right',
          top: 'center',
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar', 'stack'] },
            restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        xAxis: [
          {
            position: 'bottom',
            name: '设备编号',
            type: 'category',
            axisTick: { show: false },
            data: data.xAxisData,
            axisLine: {
              onZero: false
            },//轴线是否在0刻度关闭,x轴名称时跟随轴线的
          },
        ],
        yAxis: [
          {
            name: '单位(毫米)',
            type: 'value',
 
          },
        ],
        series: data.seriesData
          ? data.seriesData.map((series, index) => ({
            ...series,
            type: 'bar',
            // label: labelOption,
            // label: {
            //   show: false, // 不显示标签
            // },
            itemStyle: {
              color: colors[index % colors.length], // 设置柱状图颜色
            },
            emphasis: {
              focus: 'series',
            },
          }))
          : [
            {
              name: '',
              data: [],
              itemStyle: {
                color: colors[0], // 设置柱状图颜色
              },
            },
            {
              name: '',
              data: [],
              itemStyle: {
                color: colors[1], // 设置柱状图颜色
              },
            },
            {
              name: '',
              data: [],
              itemStyle: {
                color: colors[2], // 设置柱状图颜色
              },
            },
            {
              name: '',
              data: [],
              itemStyle: {
                color: colors[3], // 设置柱状图颜色
              },
            },
          ],
      };
 
      return option;
    },
    resizeChart() {
      if (this.chartInstance) {
        this.chartInstance.resize();
      }
    },
  },
};
</script>
 
<style scoped>
.chart-container {
  width: 100%;
  height: 400px;
}
</style>