duhuizhe
2023-10-16 3aa55dd3f62cee2c1c4c0aa74e1570acf83f8927
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
<template>
  <div v-loading="loading" style="width: 100%; height: 100%" ref="projectYear"></div>
</template>
 
<script>
import echarts from 'echarts';
import dataVRequest from '@/api/dataV.js';
 
export default {
  name: 'project-year',
  data() {
    return {
      loading: true,
    };
  },
  mounted() {
    // 监听屏幕宽度变化:当浏览器发生resize事件的时候,触发echart的resize事件,重绘canvas
    // 基于准备好的dom,初始化echarts实例
    const projectYear = echarts.init(this.$refs.projectYear);
    window.addEventListener('resize', () => {
      this.$nextTick(() => {
        projectYear.resize();
      });
    });
    dataVRequest.queryProjectYear().then((res) => {
      console.log('resqueryProjectYear', res);
      this.loading = false;
    });
    // 绘制图表
    projectYear.setOption({
      tooltip: {
        trigger: 'axis',
      },
      grid: {
        right: 20,
        left: 50,
        top: 30,
        bottom: 20,
      },
      legend: {
        right: 0,
        top: 0,
        textStyle: {
          color: '#ADB1B4',
          fontSize: 13,
        },
      },
      xAxis: [
        {
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#0D496E',
            },
          },
          axisLabel: {
            show: true,
            textStyle: {
              fontSize: 12,
              color: '#ADB1B4',
            },
          },
          data: ['2017年', '2018年', '2018年', '2019年'],
        },
        // 生成背景阴影需要
        {
          type: 'category',
          show: false,
          data: ['2017年', '2018年', '2018年', '2019年'],
        },
      ],
      yAxis: {
        type: 'value',
        splitLine: {
          show: false,
        },
        axisLine: {
          show: false,
        },
        axisLabel: {
          show: true,
          textStyle: {
            fontSize: 12,
            color: '#ADB1B4',
          },
        },
      },
      series: [
        {
          name: '完工项目',
          data: [256, 856, 354, 26],
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                {
                  offset: 0,
                  color: '#E8B351', // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: '#DBA33A', // 100% 处的颜色
                },
              ],
              global: false, // 缺省为 false
            },
          },
        },
        {
          name: '新增项目',
          data: [56, 856, 54, 26],
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                {
                  offset: 0,
                  color: '#00F0FF', // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: '#23ACE6', // 100% 处的颜色
                },
              ],
              global: false, // 缺省为 false
            },
          },
        },
        // 制造背景阴影
        {
          tooltip: {
            show: false,
          },
          xAxisIndex: 1,
          itemStyle: {
            color: 'rgba(180, 180, 180, 0.2)',
          },
          data: [1000, 1000, 1000, 1000],
          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',
        },
      ],
    });
  },
};
</script>