# 引言

项目中需要画一幅收益概览图,研究了一下 Highcharts 的用法。特此收录方便以后再次使用时查阅。
1ef80bfe8b5ee82aec0e2549e8c68e2c.png

# 代码

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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// x轴时间集合
var chartXAxisDateTimes = ['2022-01-01 00:00:00', '2022-02-01 00:00:00', '2022-03-01 00:00:00', '2022-04-01 00:00:00', '2022-05-01 00:00:00']

// y轴数据集合
var chartYAxisDatas = [
{
title: '累计收益',
name: '我的策略累计收益',
type: 'spline',
unit: '%',
valueDecimals: 3,
height: '28%',
yAxisIndex: 0, // y轴分区index(从上到下,默认从0开始)
data: [{
x: new Date('2022-01-01 00:00:00').getTime(),
y: 1,
color: ''
},
{
x: new Date('2022-02-01 00:00:00').getTime(),
y: 2,
color: ''
},
{
x: new Date('2022-03-01 00:00:00').getTime(),
y: 3,
color: ''
},
{
x: new Date('2022-04-01 00:00:00').getTime(),
y: 4,
color: ''
},
{
x: new Date('2022-05-01 00:00:00').getTime(),
y: 5,
color: ''
}]
},
{
title: '每月盈亏',
name: '当月盈利',
type: 'column',
unit: 'k',
valueDecimals: 0,
top: '34%',
height: '28%',
yAxisIndex: 1, // y轴分区index(从上到下,默认从0开始)
formatter: function () {
return this.value < 0 ? (-this.value) + 'k' : this.value + 'k'
},
data: [{
x: new Date('2022-01-01 00:00:00').getTime(),
y: 1,
color: ''
},
{
x: new Date('2022-02-01 00:00:00').getTime(),
y: 2,
color: ''
},
{
x: new Date('2022-03-01 00:00:00').getTime(),
y: 3,
color: ''
},
{
x: new Date('2022-04-01 00:00:00').getTime(),
y: 4,
color: ''
},
{
x: new Date('2022-05-01 00:00:00').getTime(),
y: 5,
color: ''
}]
},
{
title: '成交记录',
name: '当月买入',
type: 'column',
unit: 'k',
valueDecimals: 0,
top: '68%',
height: '28%',
yAxisIndex: 2, // y轴分区index(从上到下,默认从0开始)
data: [{
x: new Date('2022-01-01 00:00:00').getTime(),
y: 1,
color: 'red'
},
{
x: new Date('2022-02-01 00:00:00').getTime(),
y: 2,
color: 'red'
},
{
x: new Date('2022-03-01 00:00:00').getTime(),
y: 3,
color: 'red'
},
{
x: new Date('2022-04-01 00:00:00').getTime(),
y: 4,
color: 'red'
},
{
x: new Date('2022-05-01 00:00:00').getTime(),
y: 5,
color: 'red'
}]
},
{
title: '成交记录',
name: '当月卖出',
type: 'column',
unit: 'k',
valueDecimals: 0,
top: '68%',
height: '28%',
yAxisIndex: 2, // y轴分区index(从上到下,默认从0开始)
data: [{
x: new Date('2022-01-01 00:00:00').getTime(),
y: 1,
color: 'green'
},
{
x: new Date('2022-02-01 00:00:00').getTime(),
y: 2,
color: 'green'
},
{
x: new Date('2022-03-01 00:00:00').getTime(),
y: 3,
color: 'green'
},
{
x: new Date('2022-04-01 00:00:00').getTime(),
y: 4,
color: 'green'
},
{
x: new Date('2022-05-01 00:00:00').getTime(),
y: 5,
color: 'green'
}]
}
]

// 最大回测范围
var plotBandScope = [new Date('2022-02-01 00:00:00').getTime(), new Date('2022-04-01 00:00:00').getTime()]

// 图表配置
var option = {
chart: {
height: 600,
type: 'spline' // 指定图表的类型,默认是折线图(line)
},
plotOptions: {
spline: {
borderWidth: 0,
lineWidth: 1,
states: {
hover: {
lineWidth: 2
}
},
// pointWidth: 4, // 柱子内宽度容
dataLabels: {
style: {
fontSize: 11
},
enabled: false
},
showInLegend: true
},
column: {
borderWidth: 0,
pointWidth: 4, // 柱子内宽度容
dataLabels: {
style: {
fontSize: 11
},
enabled: false
},
showInLegend: true,
grouping: true
}
},
rangeSelector: {
enabled: false
},
credits: {
enabled: false
},
// legend: {
// enabled: true,
// verticalAlign: 'top'
// },
navigator: { // 导航器样式
enabled: true,
height: 20
},
scrollbar: { // 滚动条样式
barBackgroundColor: 'rgb(209, 218, 237)',
barBorderRadius: 0,
barBorderWidth: 0,
buttonBackgroundColor: 'rgb(242, 242, 242)',
buttonBorderWidth: 0,
buttonBorderRadius: 0,
trackBackgroundColor: 'none',
trackBorderWidth: 1,
trackBorderRadius: 0,
trackBorderColor: '#CCC'
},
xAxis: {
lineWidth: 1,
type: 'datetime',
categories: [],
dateTimeLabelFormats: {
// millisecond: '%H:%M:%S.%L',
// second: '%H:%M:%S',
// minute: '%H:%M',
// hour: '%m-%d %H:%M',
day: '%m-%d',
week: '%m-%d',
month: '%Y-%m',
year: '%Y'
},
gridLineWidth: 1
},
yAxis: [],
tooltip: {
xDateFormat: '%Y-%m-%d %H:%M:%S',
style: {
fontSize: 16,
fontFamily: '微软雅黑',
fontWeight: 'normal',
color: '#666',
padding: 10,
borderWidth: 0,
backgroundColor: '#ddd'
},
shared: true,
useHTML: true,
headerFormat: '<small style="font-size: 12px">{point.key}</small><table>', // 标题格式,
pointFormat: '<tr><td><li style="color:{series.color};padding:0px">{series.name}: </li>' +
'<div style="float: right;">{point.y} {series.tooltip.valueDecimals}</div></td></tr>',
footerFormat: '</table>',
shadow: true,
followPointer: true // 跟随鼠标
},
series: []
}

// 根据最大回测范围修改图表配置
if (plotBandScope && plotBandScope.length === 2) {
option.xAxis.plotBands = [{
from: plotBandScope[0],
to: plotBandScope[1],
color: '#FCFFC5',
label: {
text: '最大回测区间',
align: 'center',
verticalAlign: 'top',
y: 14
}
}]
}

// 根据x轴时间集合修改图表配置
if (chartXAxisDateTimes && chartXAxisDateTimes.length > 0) {
option.categories = chartXAxisDateTimes
}

// 根据y轴数据集合修改图表配置
if (chartYAxisDatas && chartYAxisDatas.length > 0) {
chartYAxisDatas.forEach(function (item, i) {
if (item.type === 'spline' || item.type === 'line') {
option.yAxis.push({
plotLines: [{
color: 'black', // 线的颜色,定义为红色
dashStyle: 'solid', // 默认值,这里定义为实线
value: 0, // 定义在那个值上显示标示线,这里是在x轴上刻度为3的值处垂直化一条线
width: 2 // 标示线的宽度,2px
}],
tickAmount: 4, // 规定坐标轴上的刻度总数
gridLineColor: '#ddd',
gridLineDashStyle: 'Solid',
labels: { // 坐标轴上刻度的样式及单位
align: 'right',
x: 6,
style: {
color: '#999999',
fontSize: 12
},
format: '{value}' + item.unit // 坐标轴上的单位
},
title: {
text: item.title,
margin: 20,
style: {
color: '#999999',
fontSize: 12,
align: 'middle' // 坐标轴对齐方式(相对于坐标轴的值) 默认是:middle居中对齐
}
},
height: item.height,
lineWidth: 0
})
option.series.push({
name: item.name,
data: item.data,
type: item.type,
color: 'rgb(170, 70, 67)',
tooltip: {
valueSuffix: item.unit, // 数值后缀
valueDecimals: item.valueDecimals // 提示框数据精度(保留小数点后3位)
},
yAxis: item.yAxisIndex,
showInNavigator: false
})
} else if (item.type === 'column') {
option.yAxis.push({
type: item.type,
tickAmount: 4, // 规定坐标轴上的刻度总数
startOnTick: true,
reversed: false, // y轴刻度反转
showFirstLabel: false,
showLastLabel: true,
gridLineColor: '#ddd',
gridLineDashStyle: 'Solid',
labels: {
align: 'right',
x: 12,
style: {
color: '#999999',
fontSize: 12
},
formatter: item.formatter,
format: '{value}' + item.unit // 坐标轴上的单位
},
title: {
text: item.title,
margin: 20,
style: {
color: '#999999',
fontSize: 12,
align: 'middle' // 坐标轴对齐方式(相对于坐标轴的值) 默认是:middle居中对齐
}
},
top: item.top,
height: item.height,
offset: 0,
lineWidth: 0
})
option.series.push({
name: item.name,
type: item.type,
data: item.data,
tooltip: {
pointFormatter: function () {
return '<tr><td><li style="color: ' + this.color + ';padding:0px;"></span> ' + item.name +
': </li><div style="float: right;">' + this.y.toFixed(item.valueDecimals) + item.unit + '</div></td></tr>'
}
},
yAxis: item.yAxisIndex,
showInNavigator: false
})
}
})
}

// 画图(container为div的id)
Highcharts.stockChart('container', option)

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Hito Li 微信支付

微信支付

Hito Li 支付宝

支付宝