拖旺项目:聊城公交辅助决策系统
zhaoxing
2022-04-28 76950affed3e62fbc4e322432f874a2a797c0ba7
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
<template>
  <div :id="ids" :style="{width: width, height: height}" :option="option"></div>
</template>
 
<script>
import * as echarts from "echarts";
import {setDispatchTip} from "@/components/echart/methods";
 
export default {
  name: "charts",
  props: {
    //这里子组件需要接收父组件传递过来的参数
    id: {
      type: String,
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "220px",
    },
    option: {
      type: Object,
    },
    showFont: {
      type: Boolean,
      default: true,
    }
  },
  data() {
    return {
      showFlag: false,
      chart: "",
      timer: null,
      ids: this.id,
      defaultOption: {
        backgroundColor: "transparent", // 设置背景色
        // 设置title 与 表格 距离
        grid: {
          left: "5%",
          right: "5%",
          bottom: "1%",
          top: "17%",
          containLabel: true,
        },
        tooltip: {
          show: true,
          trigger: "axis",
          borderWidth: 2,
          axisPointer: {
            type: 'line',
            lineStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                  offset: 0, color: 'rgba(0,255,222,0.2)'
                }, {
                  offset: 0.4, color: 'rgba(0,255,222,0.2)' // 100% 处的颜色
                }, {
                  offset: 0.5, color: 'rgba(176,196,222,0.5)' // 100% 处的颜色
                }, {
                  offset: 0.6, color: 'rgba(0,255,222,0.2)' // 100% 处的颜色
                }, {
                  offset: 1, color: 'rgba(0,255,222,0.2)' // 100% 处的颜色
                }],
                global: false // 缺省为 false,
              },
              width: 30,
              type: 'solid'
            }
          },
          backgroundColor: "transparent", //设置背景图片 rgba格式
          color: "#fff",
          borderColor: "transparent", //设置边框颜色
          textStyle: {
            color: "#fff", //设置文字颜色
          },
          formatter: function (params) {
            let res = '<p style="border-top: 1px solid #3BFFE6;background-color:rgba(37,61,80,0.6);padding:10px 0px;">';
            res = res + '<span style="font-weight:500;text-align:center;display:inline-block;line-height:20px;width: 180px;">' + params[0].name + '</span><br /><span style="padding-left:10px;display:inline-block;width:150px;line-height:20px;font-size: 14px;color: #01f9d9;letter-spacing: 1px;">' + params[0].seriesName + ':' + params[0].value + '</span></p>'
            return res
          }
        },
        xAxis: {
          type: "category",
          data: [],
          // 设置横坐标字体颜色
          axisLine: {
            lineStyle: {
              color: "#88A7D2",
              fontSize: 12,
            },
          },
          axisLabel: {
            show: this.showFlag,
            // rotate: 305,
            color: '#88A7D2'
          }               
        },
        yAxis: {
          type: "value",
          splitNumber: 4,
          nameTextStyle: {
            color: "#88A7D2",
          },
          // 设置纵坐标字体颜色
          axisLine: {
            show: true,
            lineStyle: {
              color: "#88A7D2",
            },
          },
          // 控制网格
          splitLine: {
            show: true,
            lineStyle: {
              color: "#222328",
            },
          },
        },
        series: [],
      },
      setOption: this.option,
    };
  },
  computed: {
    style() {
      return {
        width: this.width,
        height: this.height,
      };
    },
  },
  watch: {
    option: {
      handler(newVal, oldVal) {
        if (newVal && this.chart) {
          this.chart.setOption(this.initOption());
          this.timer = setDispatchTip(this.chart, this.option.xData.length, this.timer);
        }
      },
      deep: true,
      immediate: true
    },
  },
  created() {
  },
  mounted() {
    this.init();
  },
  activated() {
    this.timer = setDispatchTip(this.chart, this.option.xData.length, this.timer);
  },
  deactivated() {
    clearInterval(this.timer);
    this.timer = null;
  },
  methods: {
    initOption() {
      this.defaultOption.series[0] = {
        name: this.option.legendData[0],
        type: "bar",
        barWidth: "14",
        barGap: "100%",
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(0,169,255,1)",
            },
            {offset: 1, color: "rgba(0,169,255,0)"},
          ]),
        },
        label: {
          show: true,
          position: 'top',
          color: "rgba(59,255,230,1)",
          fontSize: 8,
        },
        data: this.option.yData[0],
      };
      let option = this.defaultOption;
      option.xAxis.data = this.option.xData;
      option.xAxis.axisLabel.show = document.body.clientWidth > 1480;
      return option;
    },
    init() {
      if (!document.getElementById(this.id)) {
        return;
      }
 
      this.chart = echarts.init(document.getElementById(this.id));
      this.chart.setOption(this.initOption());
      window.addEventListener('resize', this.chart.resize)
      this.timer = setDispatchTip(this.chart, this.option.xData.length, this.timer)
    },
  },
};
</script>