石广澎
2023-10-20 4cb068fb1d51129be7199cbd83fb0ef1f97915e2
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<template>
  <div class="module-box">
    <div class="chang-btn"
      @click="
        isUp = !isUp;
        setOption();
      "
    >
      {{ isUp ? '升序▲' : '降序▼' }}
    </div>
    <div class="city-score" ref="cityScore"></div>
  </div>
</template>
 
<script>
import echarts from 'echarts';
let cityChart = null;
export default {
  name: 'city-score',
  data() {
    return {
      isUp: true, // 当前是否为升序
      // 缓存下来的异步获取的数据列表
      dataList: [
        {
          name: '郑州',
          score: 153,
        },
        {
          name: '洛阳',
          score: 153,
        },
        {
          name: '南阳',
          score: 243,
        },
        {
          name: '信阳',
          score: 53,
        },
      ],
    };
  },
  methods: {
    // 绘制图表
    setOption() {
      if (!cityChart) {
        return;
      }
      const sortArr = this.dataList.sort((a, b) => (this.isUp ? a.score - b.score : b.score - a.score)); // 排序后的数据列表
      const nameArr = sortArr.map((i) => i.name);
      const dataArr = sortArr.map((i) => i.score);
      let maxScore = Math.floor(Math.max(...dataArr) * 1.4); // 为了视觉阴影效果  留点余地
      maxScore = sortArr.map(() => maxScore);
      cityChart.setOption({
        tooltip: {
          trigger: 'axis',
        },
        grid: {
          right: 20,
          left: 50,
          top: 30,
          bottom: 20,
        },
        xAxis: [
          {
            type: 'category',
            axisLine: {
              lineStyle: {
                color: '#0D496E',
              },
            },
            axisLabel: {
              show: true,
              textStyle: {
                fontSize: 12,
                color: '#ADB1B4',
              },
            },
            data: nameArr,
          },
          // 生成背景阴影需要
          {
            type: 'category',
            show: false,
            data: nameArr,
          },
        ],
        yAxis: {
          type: 'value',
          splitLine: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: true,
            textStyle: {
              fontSize: 12,
              color: '#ADB1B4',
            },
          },
        },
        series: [
          {
            name: '评分',
            data: dataArr,
            type: 'pictorialBar',
            barWidth: 48,
            symbol: 'triangle',
            label: {
              show: true,
              position: 'top',
              textStyle: {
                fontSize: 16,
                color: '#fff',
              },
            },
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: 'rgba(30, 231, 231, 0.87)', // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: 'rgba(30, 231, 231, 0)', // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
          },
          // 制造背景阴影
          {
            tooltip: {
              show: false,
            },
            xAxisIndex: 1,
            itemStyle: {
              color: 'rgba(180, 180, 180, 0.2)',
            },
            data: maxScore,
            barWidth: 48,
            emphasis: {
              itemStyle: {
                color: {
                  type: 'linear',
                  x: 0,
                  x2: 0,
                  y: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'rgba(64, 247, 176, 0.25)',
                    },
                    {
                      offset: 1,
                      color: 'rgba(17, 34, 64, 0.25)',
                    },
                  ],
                },
              },
            },
            type: 'bar',
          },
        ],
      });
    },
  },
  mounted() {
    // 监听屏幕宽度变化:当浏览器发生resize事件的时候,触发echart的resize事件,重绘canvas
    // 基于准备好的dom,初始化echarts实例
    cityChart = echarts.init(this.$refs.cityScore);
    window.addEventListener('resize', () => {
      this.$nextTick(() => {
        cityChart.resize();
      });
    });
    this.setOption();
  },
};
</script>
 
<style lang="scss" scoped>
.module-box {
  width: 100%;
  height: 100%;
  position: relative;
}
.city-score {
  width: 100%;
  height: 100%;
}
 
.chang-btn {
  position: absolute;
  white-space: nowrap;
  right: -5px;
  top: -5px;
  font-size: 14px;
  color: #ADB1B4;
  cursor: pointer;
  z-index: 9;
}
</style>