yxh
2023-03-04 464c604829aee38a5acedcbd0b9d3d85eacc874d
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
215
216
217
218
219
220
221
222
223
224
225
226
<template>
  <div>
    <Echart
      :id="id"
      ref="barChart"
      :options="options"
      :height="height"
      :width="width"
      @click="mapClick"
    ></Echart>
  </div>
</template>
 
<script>
import Echart from "@/common/echart";
 
export default {
  name: "mapechart",
  props: {
    id: {
      type: String,
      default: "barChart2",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100px",
    },
    cdata: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      options: {},
    };
  },
  components: {
    Echart,
  },
  watch: {
    cdata: {
      handler(newData) {
        this.resetOption(newData);
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    mapClick(data) {
      this.$emit("click", data);
    },
 
    resetOption(newData) {
      let xData = newData.map((item) => item.name);
      this.options = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        legend: {},
        grid: {
          left: "6%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: xData,
          axisLabel: {
            interval: 0,
            color: "#fff",
            fontSize:10,
            //rotate:-40
            formatter: function (value) {
              var ret = ""; //拼接加\n返回的类目项
              var maxLength = 2; //每项显示文字个数
              var valLength = value.length; //X轴类目项的文字个数
              var rowN = Math.ceil(valLength / maxLength); //类目项需要换行的行数
              if (rowN > 1) {
                //如果类目项的文字大于3,
                for (var i = 0; i < rowN; i++) {
                  var temp = ""; //每次截取的字符串
                  var start = i * maxLength; //开始截取的位置
                  var end = start + maxLength; //结束截取的位置
                  //这里也可以加一个是否是最后一行的判断,但是不加也没有影响,那就不加吧
                  temp = value.substring(start, end) + "\n";
                  ret += temp; //凭借最终的字符串
                }
                return ret;
              } else {
                return value;
              }
            },
          },
        },
        yAxis: {
          type: "value",
          axisLine: {
            show: true,
            Symbol: ["none", "arrow"],
            SymbolSize: [6, 6],
            symbolOffset: [0, 7],
            lineStyle: {
              color: "#ccc",
              width: 1,
              type: "solid",
            },
          },
          minInterval: 1,
          splitLine: {
            show: false,
            lineStyle: {
              color: "#fff",
            },
          },
          axisLabel: {
            color: "#fff",
          },
          max: function (value) {
            return value;
          },
        },
        animationDurationUpdate: 1000,
        series: [
          {
            name: "移植医院获取的器官总数",
            type: "bar",
            data: newData.map((item) => item.value),
            universalTransition: true,
            itemStyle: {
              normal: {
                label: {
                  show: true, //开启显示
                  position: "top", //在上方显示
                  textStyle: {
                    //数值样式
                    color: "#fff",
                  },
                },
              },
            },
          },
        ],
      };
    },
    // 开启定时器
    startInterval() {
      const _self = this;
      // 应通过接口获取配置时间,暂时写死5s
      const time = 2000;
      if (this.intervalId !== null) {
        clearInterval(this.intervalId);
      }
      this.intervalId = setInterval(() => {
        _self.reSelectMapRandomArea();
      }, time);
    },
    // 重新随机选中地图区域
    reSelectMapRandomArea() {
      const length = 9;
      this.$nextTick(() => {
        const map = this.$refs.mapChart.chart;
        let index = Math.floor(Math.random() * length);
        while (index === this.preSelectMapIndex || index >= length) {
          index = Math.floor(Math.random() * length);
        }
        map.dispatchAction({
          type: "mapUnSelect",
          seriesIndex: 0,
          dataIndex: this.preSelectMapIndex,
        });
        map.dispatchAction({
          type: "showTip",
          seriesIndex: 0,
          dataIndex: index,
        });
        map.dispatchAction({
          type: "mapSelect",
          seriesIndex: 0,
          dataIndex: index,
        });
        this.preSelectMapIndex = index;
      });
    },
    handleMapRandomSelect() {
      this.$nextTick(() => {
        const map = this.$refs.mapChart.chart;
        const _self = this;
        setTimeout(() => {
          _self.reSelectMapRandomArea();
        }, 0);
        // 移入区域,清除定时器、取消之前选中并选中当前
        map.on("mouseover", function (params) {
          clearInterval(_self.intervalId);
          map.dispatchAction({
            type: "mapUnSelect",
            seriesIndex: 0,
            dataIndex: _self.preSelectMapIndex,
          });
          map.dispatchAction({
            type: "mapSelect",
            seriesIndex: 0,
            dataIndex: params.dataIndex,
          });
          _self.preSelectMapIndex = params.dataIndex;
        });
        // 移出区域重新随机选中地图区域,并开启定时器
        map.on("globalout", function () {
          _self.reSelectMapRandomArea();
          _self.startInterval();
        });
        this.startInterval();
      });
    },
  },
};
</script>