-
Notifications
You must be signed in to change notification settings - Fork 631
/
datacube.js
184 lines (144 loc) · 4.62 KB
/
datacube.js
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
// Main DataHelper class. A container for data,
// which works as a proxy and CRUD interface
import Utils from '../stuff/utils.js'
import DCCore from './dc_core.js'
import SettProxy from './sett_proxy.js'
import AggTool from './agg_tool.js'
// Interface methods. Private methods in dc_core.js
export default class DataCube extends DCCore {
constructor(data = {}, sett = {}) {
let def_sett = {
aggregation: 100, // Update aggregation interval
script_depth: 0, // 0 === Exec on all data
auto_scroll: true, // Auto scroll to a new candle
scripts: true, // Enable overlays scripts,
ww_ram_limit: 0, // WebWorker RAM limit (MB)
node_url: null, // Use node.js instead of WW
shift_measure: true // Draw measurment shift+click
}
sett = Object.assign(def_sett, sett)
super()
this.sett = sett
this.data = data
this.sett = SettProxy(sett, this.ww)
this.agg = new AggTool(this, sett.aggregation)
this.se_state = {}
//this.agg.update = this.agg_update.bind(this)
}
// Add new overlay
add(side, overlay) {
if (side !== 'onchart' && side !== 'offchart' &&
side !== 'datasets') {
return
}
this.data[side].push(overlay)
this.update_ids()
return overlay.id
}
// Get all objects matching the query
get(query) {
return this.get_by_query(query).map(x => x.v)
}
// Get first object matching the query
get_one(query) {
return this.get_by_query(query).map(x => x.v)[0]
}
// Set data (reactively)
set(query, data) {
let objects = this.get_by_query(query)
for (var obj of objects) {
let i = obj.i !== undefined ?
obj.i :
obj.p.indexOf(obj.v)
if (i !== -1) {
this.tv.$set(obj.p, i, data)
}
}
this.update_ids()
}
// Merge object or array (reactively)
merge(query, data) {
let objects = this.get_by_query(query)
for (var obj of objects) {
if (Array.isArray(obj.v)) {
if (!Array.isArray(data)) continue
// If array is a timeseries, merge it by timestamp
// else merge by item index
if (obj.v[0] && obj.v[0].length >= 2) {
this.merge_ts(obj, data)
} else {
this.merge_objects(obj, data, [])
}
} else if (typeof obj.v === 'object') {
this.merge_objects(obj, data)
}
}
this.update_ids()
}
// Remove an overlay by query (id/type/name/...)
del(query) {
let objects = this.get_by_query(query)
for (var obj of objects) {
// Find current index of the field (if not defined)
let i = typeof obj.i !== 'number' ?
obj.i : obj.p.indexOf(obj.v)
if (i !== -1) {
this.tv.$delete(obj.p, i)
}
}
this.update_ids()
}
// Update/append data point, depending on timestamp
update(data) {
if(data['candle']) {
return this.update_candle(data)
} else {
return this.update_tick(data)
}
}
// Lock overlays from being pulled by query_search
// TODO: subject to review
lock(query) {
let objects = this.get_by_query(query)
objects.forEach(x => {
if (x.v && x.v.id && x.v.type) {
x.v.locked = true
}
})
}
// Unlock overlays from being pulled by query_search
//
unlock(query) {
let objects = this.get_by_query(query, true)
objects.forEach(x => {
if (x.v && x.v.id && x.v.type) {
x.v.locked = false
}
})
}
// Show indicator
show(query) {
if (query === 'offchart' || query === 'onchart') {
query += '.'
} else if (query === '.') {
query = ''
}
this.merge(query + '.settings', { display: true })
}
// Hide indicator
hide(query) {
if (query === 'offchart' || query === 'onchart') {
query += '.'
} else if (query === '.') {
query = ''
}
this.merge(query + '.settings', { display: false })
}
// Set data loader callback
onrange(callback) {
this.loader = callback
setTimeout(() =>
this.tv.set_loader(callback ? this : null), 0
)
}
}