/
Chart.vue
435 lines (413 loc) 路 14.2 KB
/
Chart.vue
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<template>
<!-- Chart components combined together -->
<div class="trading-vue-chart" :style="styles">
<keyboard ref="keyboard"></keyboard>
<grid-section v-for="(grid, i) in this._layout.grids"
:key="grid.id" ref="sec"
v-bind:common="section_props(i)"
v-bind:grid_id="i"
v-on:register-kb-listener="register_kb"
v-on:remove-kb-listener="remove_kb"
v-on:range-changed="range_changed"
v-on:cursor-changed="cursor_changed"
v-on:cursor-locked="cursor_locked"
v-on:sidebar-transform="set_ytransform"
v-on:layer-meta-props="layer_meta_props"
v-on:custom-event="emit_custom_event"
v-on:legend-button-click="legend_button_click"
>
</grid-section>
<botbar v-bind="botbar_props"
:shaders="shaders" :timezone="timezone">
</botbar>
</div>
</template>
<script>
import Context from '../stuff/context.js'
import Layout from './js/layout.js'
import Utils from '../stuff/utils.js'
import CursorUpdater from './js/updater.js'
import GridSection from './Section.vue'
import Botbar from './Botbar.vue'
import Keyboard from './Keyboard.vue'
import Shaders from '../mixins/shaders.js'
import DataTrack from '../mixins/datatrack.js'
import TI from './js/ti_mapping.js'
import Const from '../stuff/constants.js'
export default {
name: 'Chart',
props: [
'title_txt', 'data', 'width', 'height', 'font', 'colors',
'overlays', 'tv_id', 'config', 'buttons', 'toolbar', 'ib',
'skin', 'timezone'
],
mixins: [Shaders, DataTrack],
components: {
GridSection,
Botbar,
Keyboard
},
created() {
// Context for text measurements
this.ctx = new Context(this.$props)
// Initial layout (All measurments for the chart)
this.init_range()
this.sub = this.subset()
Utils.overwrite(this.range, this.range) // Fix for IB mode
this._layout = new Layout(this)
// Updates current cursor values
this.updater = new CursorUpdater(this)
this.update_last_values()
this.init_shaders(this.skin)
},
methods: {
range_changed(r) {
// Overwite & keep the original references
// Quick fix for IB mode (switch 2 next lines)
// TODO: wtf?
var sub = this.subset(r)
Utils.overwrite(this.range, r)
Utils.overwrite(this.sub, sub)
this.update_layout()
this.$emit('range-changed', r)
if (this.$props.ib) this.save_data_t()
},
goto(t) {
const dt = this.range[1] - this.range[0]
this.range_changed([t - dt, t])
},
setRange(t1, t2) {
this.range_changed([t1, t2])
},
cursor_changed(e) {
if (e.mode) this.cursor.mode = e.mode
if (this.cursor.mode !== 'explore') {
this.updater.sync(e)
}
if (this._hook_xchanged) this.ce('?x-changed', e)
},
cursor_locked(state) {
if (this.cursor.scroll_lock && state) return
this.cursor.locked = state
if (this._hook_xlocked) this.ce('?x-locked', state)
},
calc_interval() {
let tf = Utils.parse_tf(this.forced_tf)
if (this.ohlcv.length < 2 && !tf) return
this.interval_ms = tf || Utils.detect_interval(this.ohlcv)
this.interval = this.$props.ib ? 1 : this.interval_ms
Utils.warn(
() => this.$props.ib && !this.chart.tf,
Const.IB_TF_WARN, Const.SECOND
)
},
set_ytransform(s) {
let obj = this.y_transforms[s.grid_id] || {}
Object.assign(obj, s)
this.$set(this.y_transforms, s.grid_id, obj)
this.update_layout()
Utils.overwrite(this.range, this.range)
},
default_range() {
const dl = this.$props.config.DEFAULT_LEN
const ml = this.$props.config.MINIMUM_LEN + 0.5
const l = this.ohlcv.length - 1
if (this.ohlcv.length < 2) return
if (this.ohlcv.length <= dl) {
var s = 0, d = ml
} else {
s = l - dl, d = 0.5
}
if (!this.$props.ib) {
Utils.overwrite(this.range, [
this.ohlcv[s][0] - this.interval * d,
this.ohlcv[l][0] + this.interval * ml
])
} else {
Utils.overwrite(this.range, [
s - this.interval * d,
l + this.interval * ml
])
}
},
subset(range = this.range) {
var [res, index] = this.filter(
this.ohlcv,
range[0] - this.interval,
range[1]
)
this.ti_map = new TI()
if (res) {
this.sub_start = index
this.ti_map.init(this, res)
if (!this.$props.ib) return res || []
return this.ti_map.sub_i
}
return []
},
common_props() {
return {
title_txt: this.chart.name || this.$props.title_txt,
layout: this._layout,
sub: this.sub,
range: this.range,
interval: this.interval,
cursor: this.cursor,
colors: this.$props.colors,
font: this.$props.font,
y_ts: this.y_transforms,
tv_id: this.$props.tv_id,
config: this.$props.config,
buttons: this.$props.buttons,
meta: this.meta,
skin: this.$props.skin
}
},
overlay_subset(source, side) {
return source.map((d, i) => {
let res = Utils.fast_filter(
d.data, this.ti_map.i2t_mode(
this.range[0] - this.interval,
d.indexSrc
),
this.ti_map.i2t_mode(this.range[1], d.indexSrc)
)
return {
type: d.type,
name: Utils.format_name(d),
data: this.ti_map.parse(res[0] || [], d.indexSrc || 'map'),
settings: d.settings || this.settings_ov,
grid: d.grid || {},
tf: Utils.parse_tf(d.tf),
i0: res[1],
loading: d.loading,
last: (this.last_values[side] || [])[i]
}
})
},
section_props(i) {
return i === 0 ?
this.main_section : this.sub_section
},
init_range() {
this.calc_interval()
this.default_range()
},
layer_meta_props(d) {
// TODO: check reactivity when layout is changed
if (!(d.grid_id in this.layers_meta)) {
this.$set(this.layers_meta, d.grid_id, {})
}
this.$set(this.layers_meta[d.grid_id],
d.layer_id, d)
// Rerender
this.update_layout()
},
remove_meta_props(grid_id, layer_id) {
if (grid_id in this.layers_meta) {
this.$delete(this.layers_meta[grid_id],layer_id)
}
},
emit_custom_event(d) {
this.on_shader_event(d, 'botbar')
this.$emit('custom-event', d)
if (d.event === 'remove-layer-meta') {
this.remove_meta_props(...d.args)
}
},
update_layout(clac_tf) {
if (clac_tf) this.calc_interval()
const lay = new Layout(this)
Utils.copy_layout(this._layout, lay)
if (this._hook_update) this.ce('?chart-update', lay)
},
legend_button_click(event) {
this.$emit('legend-button-click', event)
},
register_kb(event) {
if (!this.$refs.keyboard) return
this.$refs.keyboard.register(event)
},
remove_kb(event) {
if (!this.$refs.keyboard) return
this.$refs.keyboard.remove(event)
},
update_last_values() {
this.last_candle = this.ohlcv ?
this.ohlcv[this.ohlcv.length - 1] : undefined
this.last_values = { onchart: [], offchart: [] }
this.onchart.forEach((x, i) => {
let d = x.data || []
this.last_values.onchart[i] = d[d.length - 1]
})
this.offchart.forEach((x, i) => {
let d = x.data || []
this.last_values.offchart[i] = d[d.length - 1]
})
},
// Hook events for extensions
ce(event, ...args) {
this.emit_custom_event({ event, args })
},
// Set hooks list (called from an extension)
hooks(...list) {
list.forEach(x => this[`_hook_${x}`] = true)
}
},
computed: {
// Component-specific props subsets:
main_section() {
let p = Object.assign({}, this.common_props())
p.data = this.overlay_subset(this.onchart, 'onchart')
p.data.push({
type: this.chart.type || 'Candles',
main: true,
data: this.sub,
i0: this.sub_start,
settings: this.chart.settings || this.settings_ohlcv,
grid: this.chart.grid || {},
last: this.last_candle
})
p.overlays = this.$props.overlays
return p
},
sub_section() {
let p = Object.assign({}, this.common_props())
p.data = this.overlay_subset(this.offchart, 'offchart')
p.overlays = this.$props.overlays
return p
},
botbar_props() {
let p = Object.assign({}, this.common_props())
p.width = p.layout.botbar.width
p.height = p.layout.botbar.height
p.rerender = this.rerender
return p
},
offsub() {
return this.overlay_subset(this.offchart, 'offchart')
},
// Datasets: candles, onchart, offchart indicators
ohlcv() {
return this.$props.data.ohlcv || this.chart.data || []
},
chart() {
return this.$props.data.chart || { grid: {} }
},
onchart() {
return this.$props.data.onchart || []
},
offchart() {
return this.$props.data.offchart || []
},
filter() {
return this.$props.ib ?
Utils.fast_filter_i : Utils.fast_filter
},
styles() {
let w = this.$props.toolbar ? this.$props.config.TOOLBAR : 0
return { 'margin-left': `${w}px` }
},
meta() {
return {
last: this.last_candle,
sub_start: this.sub_start,
activated: this.activated
}
},
forced_tf() {
return this.chart.tf
}
},
data() {
return {
// Current data slice
sub: [],
// Time range
range: [],
// Candlestick interval
interval: 0,
// Crosshair states
cursor: {
x: null, y: null, t: null, y$: null,
grid_id: null, locked: false, values: {},
scroll_lock: false, mode: Utils.xmode()
},
// A trick to re-render botbar
rerender: 0,
// Layers meta-props (changing behaviour)
layers_meta: {},
// Y-transforms (for y-zoom and -shift)
y_transforms: {},
// Default OHLCV settings (when using DataStructure v1.0)
settings_ohlcv: {},
// Default overlay settings
settings_ov: {},
// Meta data
last_candle: [],
last_values: {},
sub_start: undefined,
activated: false
}
},
watch: {
width() {
this.update_layout()
if (this._hook_resize) this.ce('?chart-resize')
},
height() {
this.update_layout()
if (this._hook_resize) this.ce('?chart-resize')
},
ib(nw) {
if (!nw) {
// Change range index => time
let t1 = this.ti_map.i2t(this.range[0])
let t2 = this.ti_map.i2t(this.range[1])
Utils.overwrite(this.range, [t1, t2])
this.interval = this.interval_ms
} else {
this.init_range() // TODO: calc index range instead
Utils.overwrite(this.range, this.range)
this.interval = 1
}
let sub = this.subset()
Utils.overwrite(this.sub, sub)
this.update_layout()
},
timezone() {
this.update_layout()
},
colors() {
Utils.overwrite(this.range, this.range)
},
forced_tf(n, p) {
this.update_layout(true)
this.ce('exec-all-scripts')
},
data: {
handler: function(n, p) {
if (!this.sub.length) this.init_range()
const sub = this.subset()
// Fixes Infinite loop warn, when the subset is empty
// TODO: Consider removing 'sub' from data entirely
if (this.sub.length || sub.length) {
Utils.overwrite(this.sub, sub)
}
let nw = this.data_changed()
this.update_layout(nw)
Utils.overwrite(this.range, this.range)
this.cursor.scroll_lock = !!n.scrollLock
if (n.scrollLock && this.cursor.locked) {
this.cursor.locked = false
}
if (this._hook_data) this.ce('?chart-data', nw)
this.update_last_values()
// TODO: update legend values for overalys
this.rerender++
},
deep: true
}
}
}
</script>