【WEB前端】使用百度ECharts,绘制项目质量报表

一、下载ECharts的js库

下载地址:http://echarts.baidu.com/download.html

由于我们对体积无要求,所以我们采用了完整版本,功能齐全,在项目中,我们只需要像普通的 JavaScript 库一样用 script 标签引入即可。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <!-- 引入 ECharts 文件 -->
    <script src="echarts.min.js"></script>
</head>
</html>

二、绘制项目质量报表

根据需求,我们期望在绘制出如下的报表:

在绘图前我们需要为 ECharts 准备一个具备高宽的 DOM 容器。

<body>
    <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
    <div id="echarts-line-chart" style=" 600px;height:400px;"></div>
</body>

然后就可以通过 echarts.init 方法初始化一个 echarts 实例并通过 setOption 方法生成一个统计图,下面是完整代码:

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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//初始化图表
var lineChart = echarts.init(document.getElementById("echarts-line-chart"));
//加载数据时显示loading动画
lineChart.showLoading();
//通过Ajax异步加载数据
$.get('/gerrit/project').done(function (data) {
    lineChart.hideLoading();
    var xData = function () {
        var data = [];
        for (var i = 1; i < 15; i++) {
            data.push(i + "-项目");
        }
        return data;
    }();
    var lineoption = {
        backgroundColor: "#344b58",
        "title": {
            "text": "项目质量统计图",
            "subtext": "BUG占比分析",
            x: "4%",
 
            textStyle: {
                color: '#fff',
                fontSize: '22'
            },
            subtextStyle: {
                color: '#90979c',
                fontSize: '16',
 
            },
        },
        //提示框设置
        //formatter为提示框浮层内容格式器,支持字符串模板和回调函数两种形式。模板变量有 {a}, {b},{c},{d},{e},分别表示系列名,数据名,数据值等
        "tooltip": {
            formatter: "{b0}<br><span style='display:inline-block;margin-right:5px;border-radius:10px;9px;height:9px;background-color:rgba(255,144,128,1)'></span>{a0} : {c0}<br><span style='display:inline-block;margin-right:5px;border-radius:10px;9px;height:9px;background-color:rgba(0,191,183,1)'></span>{a1} : {c1}<br><span style='display:inline-block;margin-right:5px;border-radius:10px;9px;height:9px;background-color:rgba(252,230,48,1)'></span>{a2} : {c2}%",
            "trigger": "axis",
            "axisPointer": {
                "type": "shadow",
                textStyle: {
                    color: "#fff"
                }
 
            },
        },
        toolbox: {
            show: true,
            feature: {
                saveAsImage: {show: true}
            }
        },
        "grid": {
            "borderWidth": 0,
            "top": 110,
            "bottom": 95,
            textStyle: {
                color: "#fff"
            }
        },
        "legend": {
            x: '4%',
            top: '11%',
            textStyle: {
                color: '#90979c',
            },
            "data": ['Change总数', 'Bug总数']
        },
 
 
        "calculable": true,
        "xAxis": [{
            "type": "category",
            "axisLine": {
                lineStyle: {
                    color: '#90979c'
                }
            },
            "splitLine": {
                "show": false
            },
            "axisTick": {
                "show": false
            },
            "splitArea": {
                "show": false
            },
            "axisLabel": {
                "interval": 0,
 
            },
            "data": xData,
        }],
 
        //多行Y轴设置,数据中通过yAxisIndex选择 index从0开始
        "yAxis": [
            {
                "type": "value",
                "name": "总数",
                "splitLine": {
                    "show": false
                },
                "axisLine": {
                    lineStyle: {
                        color: '#90979c'
                    }
                },
                "axisTick": {
                    "show": false
                },
                "axisLabel": {
                    "interval": 0,
 
                },
                "splitArea": {
                    "show": false
                },
            },
 
            {
                type: 'value',
                name: "占比(%)",
                min: 0,
                max: 100,
                position: 'right',
                "splitLine": {
                    "show": false
                },
                "axisLine": {
                    lineStyle: {
                        color: '#90979c'
                    }
                },
                "axisTick": {
                    "show": false
                },
                "axisLabel": {
                    "interval": 0,
 
                },
                "splitArea": {
                    "show": false
                },
            },
 
        ],
 
        //数据区域缩放组件
        "dataZoom": [{
            "show": true,
            "height": 30,
            "xAxisIndex": [
                0
            ],
            bottom: 30,
            "start": 0,
            "end": 80,
            handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
            handleSize: '110%',
            handleStyle: {
                color: "#d3dee5",
 
            },
            textStyle: {
                color: "#fff"
            },
            borderColor: "#90979c"
 
 
        },
            {
                "type": "inside",
                "show": true,
                "height": 15,
                "start": 1,
                "end": 35
            }],
 
        //系列列表。每个系列通过 type 决定自己的图表类型
        //stack表示数据堆叠,同个类目轴上系列配置相同的stack值后,后一个系列的值会在前一个系列的值上相加。
        "series": [{
            "name": "Change总数",
            "type": "bar",
            "stack": "总量",
            "barMaxWidth": 35,
            "barGap": "10%",
            "itemStyle": {
                "normal": {
                    "color": "rgba(255,144,128,1)",
                    "label": {
                        "show": true,
                        "textStyle": {
                            "color": "#fff"
                        },
                        //数据显示位置
                        "position": "insideTop",
                        //数据显示格式
                        formatter: function (p) {
                            return p.value > 0 ? (p.value) : '';
                        }
                    }
                }
            },
            "data": [
                709,
                1917,
                2455,
                2610,
                1719,
                1433,
                1544,
                3285,
                5208,
                3372,
                2484,
                4078
            ],
        },
 
            {
                "name": "Bug总数",
                "type": "bar",
                "stack": "总量",
                "itemStyle": {
                    "normal": {
                        "color": "rgba(0,191,183,1)",
                        "barBorderRadius": 0,
                        "label": {
                            "show": true,
                            "position": "top",
                            formatter: function (p) {
                                return p.value > 0 ? (p.value) : '';
                            }
                        }
                    }
                },
                "data": [
                    327,
                    1776,
                    507,
                    1200,
                    800,
                    482,
                    204,
                    1390,
                    1001,
                    951,
                    381,
                    220
                ]
            },
            {
                "name": "占比",
                "type": "line",
                "yAxisIndex": 1,
                symbolSize: 10,
                symbol: 'circle',
                "itemStyle": {
                    "normal": {
                        "color": "rgba(252,230,48,1)",
                        "barBorderRadius": 0,
                        "label": {
                            "show": true,
                            "position": "top",
                            formatter: function (p) {
                                return p.value > 0 ? (p.value) + '%' : '';
                            }
                        }
                    }
                },
                "data": [
                    46,
                    92,
                    20,
                    46,
                    47,
                    34,
                    13,
                    42,
                    19,
                    28,
                    15,
                    5
                ]
            },
        ]
    };
    lineChart.setOption(lineoption);
    $(window).resize(lineChart.resize);
});
原文地址:https://www.cnblogs.com/perfe/p/6203284.html