@alphacharts/core 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,53 +1,70 @@
1
+ //#region src/fault.ts
2
+ var e = /* @__PURE__ */ new Set();
3
+ function t(t) {
4
+ return e.add(t), () => void e.delete(t);
5
+ }
6
+ function n(t) {
7
+ for (let n of [...e]) try {
8
+ n(t);
9
+ } catch {}
10
+ }
11
+ //#endregion
1
12
  //#region src/render/color.ts
2
- var e = /* @__PURE__ */ new Map(), t = 4096;
3
- function n(n) {
4
- let i = e.get(n);
5
- if (i !== void 0) return i;
6
- let a = r(n.trim().toLowerCase());
7
- if (a === void 0) throw TypeError(`unsupported colour "${n}": use #rgb, #rrggbb, #rrggbbaa, rgb() or rgba()`);
8
- return e.size >= t && e.clear(), e.set(n, a), a;
13
+ var r = /* @__PURE__ */ new Map(), i = 4096;
14
+ function a(e) {
15
+ let t = r.get(e);
16
+ if (t !== void 0) return t;
17
+ let n = o(e.trim().toLowerCase());
18
+ if (n === void 0) throw TypeError(`unsupported colour "${e}": use #rgb, #rrggbb, #rrggbbaa, rgb() or rgba()`);
19
+ return r.size >= i && r.clear(), r.set(e, n), n;
9
20
  }
10
- function r(e) {
21
+ function o(e) {
11
22
  if (e.startsWith("#")) {
12
23
  let t = e.slice(1);
13
24
  if (!/^[0-9a-f]+$/.test(t)) return;
14
25
  if (t.length === 3 || t.length === 4) {
15
26
  let e = [...t].map((e) => e + e).join("");
16
- return r("#" + (t.length === 3 ? e + "ff" : e));
27
+ return o("#" + (t.length === 3 ? e + "ff" : e));
17
28
  }
18
29
  return t.length === 6 ? parseInt(t, 16) * 256 + 255 >>> 0 : t.length === 8 ? parseInt(t, 16) >>> 0 : void 0;
19
30
  }
20
31
  let t = /^rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)(?:[\s,/]+([\d.]+%?))?\s*\)$/.exec(e);
21
32
  if (!t) return;
22
- let n = (e) => Math.max(0, Math.min(255, Math.round(+e))), i = 1;
23
- return t[4] !== void 0 && (i = t[4].endsWith("%") ? parseFloat(t[4]) / 100 : +t[4]), (n(t[1]) << 24 | n(t[2]) << 16 | n(t[3]) << 8 | Math.round(Math.max(0, Math.min(1, i)) * 255)) >>> 0;
33
+ let n = (e) => Math.max(0, Math.min(255, Math.round(+e))), r = 1;
34
+ return t[4] !== void 0 && (r = t[4].endsWith("%") ? parseFloat(t[4]) / 100 : +t[4]), (n(t[1]) << 24 | n(t[2]) << 16 | n(t[3]) << 8 | Math.round(Math.max(0, Math.min(1, r)) * 255)) >>> 0;
24
35
  }
25
- function i(e, t = /* @__PURE__ */ new Float32Array(4)) {
36
+ function s(e, t = /* @__PURE__ */ new Float32Array(4)) {
26
37
  return t[0] = (e >>> 24 & 255) / 255, t[1] = (e >>> 16 & 255) / 255, t[2] = (e >>> 8 & 255) / 255, t[3] = (e & 255) / 255, t;
27
38
  }
28
- function a(e) {
39
+ function c(e) {
29
40
  return `rgba(${e >>> 24 & 255},${e >>> 16 & 255},${e >>> 8 & 255},${((e & 255) / 255).toFixed(3)})`;
30
41
  }
31
42
  //#endregion
32
43
  //#region src/data/deque.ts
33
- var o = {
44
+ var l = {
34
45
  ctor: Float64Array,
35
46
  fill: NaN
36
- }, s = {
47
+ }, u = {
37
48
  ctor: Float32Array,
38
49
  fill: NaN
39
- }, c = {
50
+ }, d = {
40
51
  ctor: Uint32Array,
41
52
  fill: 0
42
- }, l = 64, u = class {
43
- specs;
53
+ }, f = 64, p = class {
44
54
  cols;
45
55
  head = 0;
46
56
  first = 0;
47
57
  length = 0;
48
58
  capacity;
49
- constructor(e, t = l) {
50
- this.specs = e, this.capacity = Math.max(l, t), this.cols = e.map((e) => d(e, this.capacity));
59
+ specs;
60
+ constructor(e, t = f) {
61
+ this.specs = [...e], this.capacity = Math.max(f, t), this.cols = e.map((e) => m(e, this.capacity));
62
+ }
63
+ addColumn(e) {
64
+ return this.specs.push(e), this.cols.push(m(e, this.capacity)), this.cols.length - 1;
65
+ }
66
+ dropColumn() {
67
+ this.specs.pop(), this.cols.pop();
51
68
  }
52
69
  get end() {
53
70
  return this.first + this.length;
@@ -65,13 +82,13 @@ var o = {
65
82
  cover(e, t) {
66
83
  if (this.length === 0) {
67
84
  let n = t - e;
68
- n > this.capacity || this.capacity > Math.max(256, n * 4) ? this.realloc(Math.max(l, n + (n >> 1)), n >> 2) : this.head = Math.max(0, Math.min(this.capacity - n, this.capacity - n >> 1)), this.first = e, this.length = n;
85
+ n > this.capacity || this.capacity > Math.max(256, n * 4) ? this.realloc(Math.max(f, n + (n >> 1)), n >> 2) : this.head = Math.max(0, Math.min(this.capacity - n, this.capacity - n >> 1)), this.first = e, this.length = n;
69
86
  return;
70
87
  }
71
88
  let n = Math.max(0, this.first - e), r = Math.max(0, t - this.end);
72
89
  if (n !== 0 || r !== 0) {
73
90
  if (n > this.head || r > this.capacity - this.head - this.length) {
74
- let e = this.length + n + r, t = Math.max(l, e >> 1), i = n > 0 ? r > 0 ? t >> 1 : t : 0;
91
+ let e = this.length + n + r, t = Math.max(f, e >> 1), i = n > 0 ? r > 0 ? t >> 1 : t : 0;
75
92
  this.realloc(e + t, i + n);
76
93
  }
77
94
  this.head -= n, this.first -= n, this.length += n + r;
@@ -89,18 +106,18 @@ var o = {
89
106
  }
90
107
  }
91
108
  realloc(e, t) {
92
- let n = this.specs.map((t) => d(t, e));
109
+ let n = this.specs.map((t) => m(t, e));
93
110
  for (let e = 0; e < n.length; e++) n[e].set(this.cols[e].subarray(this.head, this.head + this.length), t);
94
111
  this.cols = n, this.capacity = e, this.head = t;
95
112
  }
96
113
  };
97
- function d(e, t) {
114
+ function m(e, t) {
98
115
  let n = new e.ctor(t);
99
116
  return e.fill !== 0 && n.fill(e.fill), n;
100
117
  }
101
118
  //#endregion
102
119
  //#region src/data/lod.ts
103
- var f = 3, p = 0, m = 1, h = 2, g = 3, _ = 4, v = class {
120
+ var h = 3, g = 0, _ = 1, v = 2, y = 3, b = 4, x = class {
104
121
  src;
105
122
  levels = [];
106
123
  constructor(e) {
@@ -126,21 +143,21 @@ var f = 3, p = 0, m = 1, h = 2, g = 3, _ = 4, v = class {
126
143
  if (e = Math.max(e, n.first), t = Math.min(t, n.end), t <= e) return;
127
144
  let r = n.end - 1;
128
145
  for (let i = 3;; i++) {
129
- let a = this.levels[i - 3], o = n.first >> i, l = r >> i, d = e >> i, f = t - 1 >> i;
130
- a || (a = new u(this.src.color === void 0 ? [
131
- s,
132
- s,
133
- s,
134
- s
146
+ let a = this.levels[i - 3], o = n.first >> i, s = r >> i, c = e >> i, l = t - 1 >> i;
147
+ a || (a = new p(this.src.color === void 0 ? [
148
+ u,
149
+ u,
150
+ u,
151
+ u
135
152
  ] : [
136
- s,
137
- s,
138
- s,
139
- s,
140
- c
141
- ]), this.levels.push(a), d = o, f = l), a.cover(o, l + 1);
142
- for (let e = d; e <= f; e++) i === 3 ? this.fromRows(a, e, i) : this.fromChildren(a, this.levels[i - 1 - 3], e);
143
- if (l - o <= 1) {
153
+ u,
154
+ u,
155
+ u,
156
+ u,
157
+ d
158
+ ]), this.levels.push(a), c = o, l = s), a.cover(o, s + 1);
159
+ for (let e = c; e <= l; e++) i === 3 ? this.fromRows(a, e, i) : this.fromChildren(a, this.levels[i - 1 - 3], e);
160
+ if (s - o <= 1) {
144
161
  this.levels.length = i - 3 + 1;
145
162
  return;
146
163
  }
@@ -179,34 +196,34 @@ var f = 3, p = 0, m = 1, h = 2, g = 3, _ = 4, v = class {
179
196
  return n[0] = i === Infinity ? NaN : i, n[1] = a === -Infinity ? NaN : a, n;
180
197
  }
181
198
  fromRows(e, t, n) {
182
- let r = this.src.store, i = r.cols[this.src.o], a = r.cols[this.src.h], o = r.cols[this.src.l], s = r.cols[this.src.c], c = this.src.color === void 0 ? void 0 : r.cols[this.src.color], l = r.head - r.first, u = Math.max(t << n, r.first), d = Math.min(t + 1 << n, r.end), f = NaN, p = -Infinity, m = Infinity, h = NaN, g = 0;
183
- for (let e = u; e < d; e++) {
184
- let t = l + e, n = i[t];
199
+ let r = this.src.store, i = r.cols[this.src.o], a = r.cols[this.src.h], o = r.cols[this.src.l], s = r.cols[this.src.c], c = this.src.color === void 0 ? void 0 : r.cols[this.src.color], l = this.src.colorMode === "any", u = r.head - r.first, d = Math.max(t << n, r.first), f = Math.min(t + 1 << n, r.end), p = NaN, m = -Infinity, h = Infinity, g = NaN, _ = 0;
200
+ for (let e = d; e < f; e++) {
201
+ let t = u + e, n = i[t];
185
202
  if (n !== n) continue;
186
- f !== f && (f = n);
203
+ p !== p && (p = n);
187
204
  let r = a[t];
188
- r > p && (p = r, c && (g = c[t]));
189
- let u = o[t];
190
- u < m && (m = u), h = s[t];
205
+ r > m && (m = r, c && !l && (_ = c[t])), c && l && c[t] !== 0 && (_ = c[t]);
206
+ let d = o[t];
207
+ d < h && (h = d), g = s[t];
191
208
  }
192
- y(e, t, f, p, m, h, g);
209
+ S(e, t, p, m, h, g, _);
193
210
  }
194
211
  fromChildren(e, t, n) {
195
- let r = NaN, i = -Infinity, a = Infinity, o = NaN, s = 0, c = t.cols[0], l = t.cols[1], u = t.cols[2], d = t.cols[3], f = t.cols[4];
212
+ let r = NaN, i = -Infinity, a = Infinity, o = NaN, s = 0, c = t.cols[0], l = t.cols[1], u = t.cols[2], d = t.cols[3], f = t.cols[4], p = this.src.colorMode === "any";
196
213
  for (let e = n << 1; e <= (n << 1) + 1; e++) {
197
214
  if (!t.has(e)) continue;
198
- let n = t.pos(e), p = c[n];
199
- if (p !== p) continue;
200
- r !== r && (r = p);
201
- let m = l[n];
202
- m > i && (i = m, f && (s = f[n]));
203
- let h = u[n];
204
- h < a && (a = h), o = d[n];
215
+ let n = t.pos(e), m = c[n];
216
+ if (m !== m) continue;
217
+ r !== r && (r = m);
218
+ let h = l[n];
219
+ h > i && (i = h, f && !p && (s = f[n])), f && p && f[n] !== 0 && (s = f[n]);
220
+ let g = u[n];
221
+ g < a && (a = g), o = d[n];
205
222
  }
206
- y(e, n, r, i, a, o, s);
223
+ S(e, n, r, i, a, o, s);
207
224
  }
208
225
  };
209
- function y(e, t, n, r, i, a, o) {
226
+ function S(e, t, n, r, i, a, o) {
210
227
  let s = e.pos(t), c = n !== n;
211
228
  e.cols[0][s] = n, e.cols[1][s] = c ? NaN : r, e.cols[2][s] = c ? NaN : i, e.cols[3][s] = a;
212
229
  let l = e.cols[4];
@@ -214,8 +231,8 @@ function y(e, t, n, r, i, a, o) {
214
231
  }
215
232
  //#endregion
216
233
  //#region src/data/timeline.ts
217
- var b = class {
218
- q = new u([o]);
234
+ var C = class {
235
+ q = new p([l]);
219
236
  get first() {
220
237
  return this.q.first;
221
238
  }
@@ -253,14 +270,14 @@ var b = class {
253
270
  return t < this.end && this.timeAt(t) === e ? t : void 0;
254
271
  }
255
272
  replace(e, t) {
256
- if (x(e, "timeline"), this.q.clear(t), e.length === 0) return;
273
+ if (w(e, "timeline"), this.q.clear(t), e.length === 0) return;
257
274
  this.q.cover(t, t + e.length);
258
275
  let n = this.q.cols[0], r = this.q.head;
259
276
  for (let t = 0; t < e.length; t++) n[r + t] = e[t];
260
277
  }
261
278
  prepend(e) {
262
279
  if (e.length === 0) return this.first;
263
- if (x(e, "prepended times"), this.length > 0 && e[e.length - 1] >= this.timeAt(this.first)) throw RangeError("prepended times must be earlier than the timeline's first time");
280
+ if (w(e, "prepended times"), this.length > 0 && e[e.length - 1] >= this.timeAt(this.first)) throw RangeError("prepended times must be earlier than the timeline's first time");
264
281
  let t = this.first - e.length;
265
282
  this.q.cover(t, this.end);
266
283
  let n = this.q.cols[0], r = this.q.head;
@@ -273,7 +290,7 @@ var b = class {
273
290
  return this.q.cover(this.first, t + 1), this.q.cols[0][this.q.pos(t)] = e, t;
274
291
  }
275
292
  };
276
- function x(e, t) {
293
+ function w(e, t) {
277
294
  for (let n = 0; n < e.length; n++) {
278
295
  let r = e[n];
279
296
  if (!Number.isFinite(r)) throw TypeError(`${t}: time at ${n} is not a finite number`);
@@ -282,30 +299,42 @@ function x(e, t) {
282
299
  }
283
300
  //#endregion
284
301
  //#region src/data/model.ts
285
- var S = 0, ee = 1, te = 2, C = 3, w = 0, T = 1, E = class {
302
+ var T = 0, E = 1, D = 2, O = 3, k = 0, ee = 1, te = 4, ne = class {
286
303
  shape;
287
304
  store;
288
305
  lod;
306
+ lodSrc;
307
+ pointColors = !1;
289
308
  constructor(e) {
290
- this.shape = e, this.store = e === "ohlc" ? new u([
291
- o,
292
- o,
293
- o,
294
- o
295
- ]) : new u([o, c]), this.lod = e === "ohlc" ? new v({
309
+ this.shape = e, this.store = e === "ohlc" ? new p([
310
+ l,
311
+ l,
312
+ l,
313
+ l
314
+ ]) : new p([l, d]), this.lodSrc = e === "ohlc" ? {
296
315
  store: this.store,
297
316
  o: 0,
298
317
  h: 1,
299
318
  l: 2,
300
- c: 3
301
- }) : new v({
319
+ c: 3,
320
+ colorMode: "any"
321
+ } : {
302
322
  store: this.store,
303
323
  o: 0,
304
324
  h: 0,
305
325
  l: 0,
306
326
  c: 0,
307
327
  color: 1
308
- });
328
+ }, this.lod = new x(this.lodSrc);
329
+ }
330
+ get colored() {
331
+ return this.shape === "ohlc" && this.store.cols.length > 4;
332
+ }
333
+ enableColors() {
334
+ this.shape !== "ohlc" || this.colored || (this.store.addColumn(d), this.lodSrc.color = 4, this.lod.rebuild());
335
+ }
336
+ disableColors() {
337
+ this.colored && (this.store.dropColumn(), delete this.lodSrc.color, this.lod.rebuild());
309
338
  }
310
339
  has(e) {
311
340
  let t = this.store;
@@ -326,15 +355,15 @@ var S = 0, ee = 1, te = 2, C = 3, w = 0, T = 1, E = class {
326
355
  indexAtOrBefore(e) {
327
356
  for (let t = Math.min(e, this.store.end - 1); t >= this.store.first; t--) if (this.has(t)) return t;
328
357
  }
329
- }, D = class {
330
- timeline = new b();
358
+ }, re = class {
359
+ timeline = new C();
331
360
  series = /* @__PURE__ */ new Set();
332
361
  listeners = /* @__PURE__ */ new Set();
333
362
  batchDepth = 0;
334
363
  deferred = /* @__PURE__ */ new Map();
335
364
  touched = /* @__PURE__ */ new Set();
336
365
  add(e) {
337
- let t = new E(e);
366
+ let t = new ne(e);
338
367
  return this.series.add(t), t;
339
368
  }
340
369
  remove(e) {
@@ -364,12 +393,16 @@ var S = 0, ee = 1, te = 2, C = 3, w = 0, T = 1, E = class {
364
393
  }
365
394
  setData(e, t) {
366
395
  this.live(e);
367
- let n = L(e.shape, t);
396
+ let n = V(e.shape, t);
397
+ if (e.colored && n.cols.length === 4) {
398
+ let t = P(e, this.timeline);
399
+ t && (n = F(n, t));
400
+ }
368
401
  if (this.batchDepth > 0) {
369
- this.touched.add(e), A(e, n, this.timeline) ? this.deferred.delete(e) : this.deferred.set(e, n);
402
+ this.touched.add(e), M(e, n, this.timeline) ? this.deferred.delete(e) : this.deferred.set(e, n);
370
403
  return;
371
404
  }
372
- if (A(e, n, this.timeline)) {
405
+ if (M(e, n, this.timeline)) {
373
406
  this.emit({
374
407
  series: e,
375
408
  kind: "replace",
@@ -411,7 +444,7 @@ var S = 0, ee = 1, te = 2, C = 3, w = 0, T = 1, E = class {
411
444
  }
412
445
  prepend(e, t) {
413
446
  this.settle(), this.live(e);
414
- let n = L(e.shape, t), r = n.time.length;
447
+ let n = V(e.shape, t), r = n.time.length;
415
448
  if (r === 0) return;
416
449
  let i = this.timeline, a = e.firstIndex();
417
450
  if (a !== void 0 && n.time[r - 1] >= i.timeAt(a)) throw RangeError("prepend: bars must be earlier than the series' first bar");
@@ -425,7 +458,7 @@ var S = 0, ee = 1, te = 2, C = 3, w = 0, T = 1, E = class {
425
458
  for (let t = s; t < r; t++) {
426
459
  let r = i.indexOf(n.time[t]);
427
460
  if (r === void 0) {
428
- this.rebuildAndEmit(e, N(n, M(e, i)));
461
+ this.rebuildAndEmit(e, L(n, I(e, i)));
429
462
  return;
430
463
  }
431
464
  c[t] = r;
@@ -433,7 +466,7 @@ var S = 0, ee = 1, te = 2, C = 3, w = 0, T = 1, E = class {
433
466
  let l = i.prepend(n.time.subarray(0, s));
434
467
  for (let e = 0; e < s; e++) c[e] = l + e;
435
468
  let u = c[0], d = c[r - 1] + 1;
436
- e.store.cover(u, Math.max(d, e.store.length > 0 ? e.store.end : d)), j(e, n, c), e.lod.update(u, d), this.emit({
469
+ e.store.cover(u, Math.max(d, e.store.length > 0 ? e.store.end : d)), N(e, n, c), e.lod.update(u, d), this.emit({
437
470
  series: e,
438
471
  kind: "prepend",
439
472
  lo: u,
@@ -444,13 +477,13 @@ var S = 0, ee = 1, te = 2, C = 3, w = 0, T = 1, E = class {
444
477
  }
445
478
  update(e, t) {
446
479
  this.settle(), this.live(e);
447
- let n = L(e.shape, [t]), r = n.time[0], i = this.timeline, a = e.lastIndex();
480
+ let n = V(e.shape, [t]), r = n.time[0], i = this.timeline, a = e.lastIndex();
448
481
  if (a !== void 0) {
449
482
  let e = i.timeAt(a);
450
483
  if (r < e) throw RangeError(`update: time ${r} is earlier than the series' last bar ${e}`);
451
484
  }
452
485
  let o, s = !1;
453
- i.length === 0 || r > i.timeAt(i.end - 1) ? (o = i.append(r), s = !0) : o = i.indexOf(r), o === void 0 ? this.rebuildAndEmit(e, N(M(e, i), n)) : (e.store.cover(e.store.length > 0 ? Math.min(e.store.first, o) : o, Math.max(e.store.length > 0 ? e.store.end : 0, o + 1)), j(e, n, Float64Array.of(o)), e.lod.update(o, o + 1), this.emit({
486
+ i.length === 0 || r > i.timeAt(i.end - 1) ? (o = i.append(r), s = !0) : o = i.indexOf(r), o === void 0 ? this.rebuildAndEmit(e, L(I(e, i), n)) : (e.store.cover(e.store.length > 0 ? Math.min(e.store.first, o) : o, Math.max(e.store.length > 0 ? e.store.end : 0, o + 1)), N(e, n, Float64Array.of(o)), e.lod.update(o, o + 1), this.emit({
454
487
  series: e,
455
488
  kind: "update",
456
489
  lo: o,
@@ -459,6 +492,48 @@ var S = 0, ee = 1, te = 2, C = 3, w = 0, T = 1, E = class {
459
492
  appended: s
460
493
  }));
461
494
  }
495
+ setColors(e, t) {
496
+ if (this.settle(), this.live(e), e.shape !== "ohlc") throw TypeError("setColors: only an OHLC series (candles, bars) has bar colours");
497
+ let n = t.time.length;
498
+ if (t.color.length !== n) throw RangeError(`setColors: color has ${t.color.length} rows, time has ${n}`);
499
+ if (w(t.time, "bar colours"), n === 0) return;
500
+ if (!e.colored) {
501
+ let r = !1;
502
+ for (let e = 0; e < n && !r; e++) r = t.color[e] !== 0;
503
+ if (!r) return;
504
+ e.enableColors();
505
+ }
506
+ let r = this.timeline, { buf: i, offset: a } = r.raw(), o = e.store, s = o.cols[4], c = Infinity, l = -Infinity, u = r.first;
507
+ for (let d = 0; d < n; d++) {
508
+ let n = t.time[d];
509
+ for (; u < r.end && i[a + u] < n;) u++;
510
+ if (u >= r.end) break;
511
+ if (i[a + u] !== n || !e.has(u)) continue;
512
+ let f = o.pos(u);
513
+ s[f] !== t.color[d] && (s[f] = t.color[d], u < c && (c = u), u > l && (l = u));
514
+ }
515
+ l < c || (e.lod.update(c, l + 1), this.emit({
516
+ series: e,
517
+ kind: "color",
518
+ lo: c,
519
+ hi: l + 1,
520
+ reindexed: !1,
521
+ appended: !1
522
+ }));
523
+ }
524
+ clearColors(e) {
525
+ if (this.settle(), this.live(e), !e.colored) return;
526
+ let t = e.store, n = t.cols[4], r = Infinity, i = -Infinity;
527
+ for (let e = t.first; e < t.end; e++) n[t.pos(e)] !== 0 && (e < r && (r = e), i = e);
528
+ e.disableColors(), !(i < r) && this.emit({
529
+ series: e,
530
+ kind: "color",
531
+ lo: r,
532
+ hi: i + 1,
533
+ reindexed: !1,
534
+ appended: !1
535
+ });
536
+ }
462
537
  rebuildAndEmit(e, t) {
463
538
  let n = this.rebuild([[e, t]]);
464
539
  this.emit({
@@ -474,15 +549,15 @@ var S = 0, ee = 1, te = 2, C = 3, w = 0, T = 1, E = class {
474
549
  let t = this.timeline, n = new Set(e.map(([e]) => e)), r = [];
475
550
  for (let e of this.series) !n.has(e) && e.store.length > 0 && r.push({
476
551
  s: e,
477
- rows: M(e, t)
552
+ rows: I(e, t)
478
553
  });
479
554
  let i = /* @__PURE__ */ new Float64Array();
480
- for (let [, t] of e) i = P(i, t.time);
481
- for (let e of r) i = P(i, e.rows.time);
482
- let a = i.length === t.length && I(i, t);
483
- a || t.replace(i, F(t, i));
484
- for (let [n, r] of e) O(n, r, t);
485
- if (!a) for (let e of r) O(e.s, e.rows, t);
555
+ for (let [, t] of e) i = R(i, t.time);
556
+ for (let e of r) i = R(i, e.rows.time);
557
+ let a = i.length === t.length && B(i, t);
558
+ a || t.replace(i, z(t, i));
559
+ for (let [n, r] of e) A(n, r, t);
560
+ if (!a) for (let e of r) A(e.s, e.rows, t);
486
561
  return !a;
487
562
  }
488
563
  live(e) {
@@ -492,7 +567,7 @@ var S = 0, ee = 1, te = 2, C = 3, w = 0, T = 1, E = class {
492
567
  for (let t of this.listeners) t(e);
493
568
  }
494
569
  };
495
- function O(e, t, n) {
570
+ function A(e, t, n) {
496
571
  if (e.store.clear(), t.time.length === 0) {
497
572
  e.lod.rebuild();
498
573
  return;
@@ -503,12 +578,12 @@ function O(e, t, n) {
503
578
  for (; i[a + o] < n;) o++;
504
579
  r[e] = o;
505
580
  }
506
- k(e, t, r);
581
+ j(e, t, r);
507
582
  }
508
- function k(e, t, n) {
509
- e.store.clear(), e.store.cover(n[0], n[n.length - 1] + 1), j(e, t, n), e.lod.rebuild();
583
+ function j(e, t, n) {
584
+ e.store.clear(), e.pointColors = !1, e.store.cover(n[0], n[n.length - 1] + 1), N(e, t, n), e.lod.rebuild();
510
585
  }
511
- function A(e, t, n) {
586
+ function M(e, t, n) {
512
587
  let r = t.time.length;
513
588
  if (n.length === 0 || r === 0) return !1;
514
589
  let { buf: i, offset: a } = n.raw(), o = new Float64Array(r), s = n.first;
@@ -526,16 +601,46 @@ function A(e, t, n) {
526
601
  if (t >= r || o[t] !== n) return !1;
527
602
  }
528
603
  }
529
- return k(e, t, o), !0;
604
+ return j(e, t, o), !0;
530
605
  }
531
- function j(e, t, n) {
606
+ function N(e, t, n) {
532
607
  let r = e.store;
533
- for (let e = 0; e < t.cols.length; e++) {
534
- let i = r.cols[e], a = t.cols[e], o = r.head - r.first;
535
- for (let e = 0; e < n.length; e++) i[o + n[e]] = a[e];
608
+ e.shape === "ohlc" && t.cols.length > 4 && e.enableColors();
609
+ for (let i = 0; i < t.cols.length; i++) {
610
+ let a = r.cols[i], o = t.cols[i], s = r.head - r.first;
611
+ for (let e = 0; e < n.length; e++) a[s + n[e]] = o[e];
612
+ if (e.shape === "value" && i === 1 && !e.pointColors) {
613
+ for (let t = 0; t < o.length; t++) if (o[t] !== 0) {
614
+ e.pointColors = !0;
615
+ break;
616
+ }
617
+ }
618
+ }
619
+ }
620
+ function P(e, t) {
621
+ let n = e.store, r = n.cols[4], i = 0;
622
+ for (let t = n.first; t < n.end; t++) r[n.pos(t)] !== 0 && e.has(t) && i++;
623
+ if (i === 0) return null;
624
+ let a = new Float64Array(i), o = new Uint32Array(i), s = 0;
625
+ for (let i = n.first; i < n.end; i++) r[n.pos(i)] !== 0 && e.has(i) && (a[s] = t.timeAt(i), o[s++] = r[n.pos(i)]);
626
+ return {
627
+ time: a,
628
+ cols: [o]
629
+ };
630
+ }
631
+ function F(e, t) {
632
+ let n = e.time.length, r = new Uint32Array(n), i = t.time, a = t.cols[0], o = 0;
633
+ for (let t = 0; t < n; t++) {
634
+ let n = e.time[t];
635
+ for (; o < i.length && i[o] < n;) o++;
636
+ o < i.length && i[o] === n && (r[t] = a[o]);
536
637
  }
638
+ return {
639
+ time: e.time,
640
+ cols: [...e.cols, r]
641
+ };
537
642
  }
538
- function M(e, t) {
643
+ function I(e, t) {
539
644
  let n = e.store, r = 0;
540
645
  for (let t = n.first; t < n.end; t++) e.has(t) && r++;
541
646
  let i = new Float64Array(r), a = n.cols.map((e) => new e.constructor(r)), o = 0;
@@ -551,19 +656,26 @@ function M(e, t) {
551
656
  cols: a
552
657
  };
553
658
  }
554
- function N(e, t) {
659
+ function L(e, t) {
660
+ if (e.cols.length !== t.cols.length) {
661
+ let n = (e) => e.cols.length === 4 ? {
662
+ time: e.time,
663
+ cols: [...e.cols, new Uint32Array(e.time.length)]
664
+ } : e;
665
+ e = n(e), t = n(t);
666
+ }
555
667
  let n = new Float64Array(e.time.length + t.time.length);
556
668
  n.set(e.time), n.set(t.time, e.time.length);
557
669
  let r = e.cols.map((e, r) => {
558
670
  let i = new e.constructor(n.length);
559
671
  return i.set(e), i.set(t.cols[r], e.length), i;
560
672
  });
561
- return x(n, "series"), {
673
+ return w(n, "series"), {
562
674
  time: n,
563
675
  cols: r
564
676
  };
565
677
  }
566
- function P(e, t) {
678
+ function R(e, t) {
567
679
  if (e.length === 0) return t;
568
680
  if (t.length === 0) return e;
569
681
  let n = new Float64Array(e.length + t.length), r = 0, i = 0, a = 0;
@@ -575,7 +687,7 @@ function P(e, t) {
575
687
  for (; i < t.length;) n[a++] = t[i++];
576
688
  return n.subarray(0, a);
577
689
  }
578
- function F(e, t) {
690
+ function z(e, t) {
579
691
  if (e.length === 0) return 0;
580
692
  let { buf: n, offset: r } = e.raw(), i = 0, a = e.first;
581
693
  for (; i < t.length && a < e.end;) {
@@ -585,43 +697,49 @@ function F(e, t) {
585
697
  }
586
698
  return e.first;
587
699
  }
588
- function I(e, t) {
700
+ function B(e, t) {
589
701
  let { buf: n, offset: r } = t.raw();
590
702
  for (let i = 0; i < e.length; i++) if (e[i] !== n[r + t.first + i]) return !1;
591
703
  return !0;
592
704
  }
593
- function L(e, t) {
705
+ function V(e, t) {
594
706
  if (e === "ohlc") {
595
- let e = Array.isArray(t) ? R(t) : z(t);
596
- x(e.time, "ohlc data");
597
- for (let t = 0; t < 4; t++) U(e.cols[t], [
707
+ let e = Array.isArray(t) ? H(t) : U(t);
708
+ w(e.time, "ohlc data");
709
+ for (let t = 0; t < 4; t++) q(e.cols[t], [
598
710
  "open",
599
711
  "high",
600
712
  "low",
601
713
  "close"
602
714
  ][t]);
603
- return H(e), e;
715
+ return K(e), e;
604
716
  }
605
- let n = Array.isArray(t) ? B(t) : V(t);
606
- return x(n.time, "value data"), U(n.cols[0], "value"), n;
717
+ let n = Array.isArray(t) ? W(t) : G(t);
718
+ return w(n.time, "value data"), q(n.cols[0], "value"), n;
607
719
  }
608
- function R(e) {
609
- let t = e.length, n = new Float64Array(t), r = new Float64Array(t), i = new Float64Array(t), a = new Float64Array(t), o = new Float64Array(t);
610
- for (let s = 0; s < t; s++) {
611
- let t = e[s];
612
- n[s] = t.time, r[s] = t.open, i[s] = t.high, a[s] = t.low, o[s] = t.close;
720
+ function H(e) {
721
+ let t = e.length, n = new Float64Array(t), r = new Float64Array(t), i = new Float64Array(t), o = new Float64Array(t), s = new Float64Array(t), c;
722
+ for (let l = 0; l < t; l++) {
723
+ let u = e[l];
724
+ n[l] = u.time, r[l] = u.open, i[l] = u.high, o[l] = u.low, s[l] = u.close, u.color !== void 0 && ((c ??= new Uint32Array(t))[l] = a(u.color));
613
725
  }
614
726
  return {
615
727
  time: n,
616
- cols: [
728
+ cols: c ? [
729
+ r,
730
+ i,
731
+ o,
732
+ s,
733
+ c
734
+ ] : [
617
735
  r,
618
736
  i,
619
- a,
620
- o
737
+ o,
738
+ s
621
739
  ]
622
740
  };
623
741
  }
624
- function z(e) {
742
+ function U(e) {
625
743
  let t = e.time.length;
626
744
  for (let n of [
627
745
  "open",
@@ -629,9 +747,16 @@ function z(e) {
629
747
  "low",
630
748
  "close"
631
749
  ]) if (e[n].length !== t) throw RangeError(`columnar ohlc: ${n} has ${e[n].length} rows, time has ${t}`);
750
+ if (e.color && e.color.length !== t) throw RangeError(`columnar ohlc: color has ${e.color.length} rows, time has ${t}`);
632
751
  return {
633
752
  time: e.time,
634
- cols: [
753
+ cols: e.color ? [
754
+ e.open,
755
+ e.high,
756
+ e.low,
757
+ e.close,
758
+ e.color
759
+ ] : [
635
760
  e.open,
636
761
  e.high,
637
762
  e.low,
@@ -639,18 +764,18 @@ function z(e) {
639
764
  ]
640
765
  };
641
766
  }
642
- function B(e) {
643
- let t = e.length, r = new Float64Array(t), i = new Float64Array(t), a = new Uint32Array(t);
767
+ function W(e) {
768
+ let t = e.length, n = new Float64Array(t), r = new Float64Array(t), i = new Uint32Array(t);
644
769
  for (let o = 0; o < t; o++) {
645
770
  let t = e[o];
646
- r[o] = t.time, i[o] = t.value, t.color !== void 0 && (a[o] = n(t.color));
771
+ n[o] = t.time, r[o] = t.value, t.color !== void 0 && (i[o] = a(t.color));
647
772
  }
648
773
  return {
649
- time: r,
650
- cols: [i, a]
774
+ time: n,
775
+ cols: [r, i]
651
776
  };
652
777
  }
653
- function V(e) {
778
+ function G(e) {
654
779
  let t = e.time.length;
655
780
  if (e.value.length !== t) throw RangeError(`columnar value: value has ${e.value.length} rows, time has ${t}`);
656
781
  if (e.color && e.color.length !== t) throw RangeError(`columnar value: color has ${e.color.length} rows, time has ${t}`);
@@ -659,58 +784,72 @@ function V(e) {
659
784
  cols: [e.value, e.color ?? new Uint32Array(t)]
660
785
  };
661
786
  }
662
- function H(e) {
787
+ function K(e) {
663
788
  let [t, n, r, i] = e.cols;
664
789
  for (let a = 0; a < t.length; a++) {
665
790
  let o = r[a], s = n[a];
666
791
  if (!(o <= t[a] && o <= i[a] && s >= t[a] && s >= i[a])) throw RangeError(`ohlc row ${a} (time ${e.time[a]}): needs low <= open, close <= high; got o=${t[a]} h=${s} l=${o} c=${i[a]}`);
667
792
  }
668
793
  }
669
- function U(e, t) {
794
+ function q(e, t) {
670
795
  for (let n = 0; n < e.length; n++) if (!Number.isFinite(e[n])) throw TypeError(`${t} at row ${n} is not a finite number`);
671
796
  }
672
797
  //#endregion
673
798
  //#region src/render/gl.ts
674
- var W = "#version 300 es\nprecision highp float;\nprecision highp int;\n// Pane size in device px.\nuniform vec2 u_view;\n// x_dev = x_rel * u_x.x + u_x.y; y_dev = u_y.y - p_rel * u_y.x (device px, y down).\nuniform vec2 u_x;\nuniform vec2 u_y;\nvec2 toClip(vec2 px) {\n return vec2(px.x / u_view.x * 2.0 - 1.0, 1.0 - px.y / u_view.y * 2.0);\n}\nfloat xDev(float xr) { return xr * u_x.x + u_x.y; }\nfloat yDev(float pr) { return u_y.y - pr * u_y.x; }\n// Corner of a two-triangle quad for vertex v in 0..5: (0,0) (1,0) (0,1) (0,1) (1,0) (1,1).\nvec2 corner(int v) {\n int c = v == 0 ? 0 : v == 1 ? 1 : v == 2 ? 2 : v == 3 ? 2 : v == 4 ? 1 : 3;\n return vec2(float(c & 1), float(c >> 1));\n}\nvec4 unpack(uint c) {\n return vec4(float((c >> 24) & 255u), float((c >> 16) & 255u), float((c >> 8) & 255u), float(c & 255u)) / 255.0;\n}\n", G = class {
799
+ var J = "#version 300 es\nprecision highp float;\nprecision highp int;\n// Pane size in device px.\nuniform vec2 u_view;\n// x_dev = x_rel * u_x.x + u_x.y; y_dev = u_y.y - p_rel * u_y.x (device px, y down).\nuniform vec2 u_x;\nuniform vec2 u_y;\nvec2 toClip(vec2 px) {\n return vec2(px.x / u_view.x * 2.0 - 1.0, 1.0 - px.y / u_view.y * 2.0);\n}\nfloat xDev(float xr) { return xr * u_x.x + u_x.y; }\nfloat yDev(float pr) { return u_y.y - pr * u_y.x; }\n// Corner of a two-triangle quad for vertex v in 0..5: (0,0) (1,0) (0,1) (0,1) (1,0) (1,1).\nvec2 corner(int v) {\n int c = v == 0 ? 0 : v == 1 ? 1 : v == 2 ? 2 : v == 3 ? 2 : v == 4 ? 1 : 3;\n return vec2(float(c & 1), float(c >> 1));\n}\nvec4 unpack(uint c) {\n return vec4(float((c >> 24) & 255u), float((c >> 16) & 255u), float((c >> 8) & 255u), float(c & 255u)) / 255.0;\n}\n", Y = class {
675
800
  gl;
676
801
  programs = /* @__PURE__ */ new Map();
677
802
  constructor(e) {
678
803
  this.gl = e;
679
804
  }
680
- program(e, t, n, r, i) {
681
- let a = this.programs.get(e);
682
- if (a) return a;
683
- let o = this.gl, s = o.createProgram();
684
- if (o.attachShader(s, K(o, o.VERTEX_SHADER, t, e)), o.attachShader(s, K(o, o.FRAGMENT_SHADER, n, e)), o.linkProgram(s), !o.getProgramParameter(s, o.LINK_STATUS) && !o.isContextLost()) throw Error(`alphacharts: link failed for ${e}: ${o.getProgramInfoLog(s)}`);
685
- let c = {
686
- program: s,
805
+ program(e, t, r, i, a) {
806
+ let o = this.programs.get(e);
807
+ if (o) return o;
808
+ let s = this.gl, c = s.createProgram();
809
+ if (s.attachShader(c, X(s, s.VERTEX_SHADER, t, e)), s.attachShader(c, X(s, s.FRAGMENT_SHADER, r, e)), s.linkProgram(c), !s.getProgramParameter(c, s.LINK_STATUS) && !s.isContextLost()) {
810
+ let t = /* @__PURE__ */ Error(`alphacharts: link failed for ${e}: ${s.getProgramInfoLog(c)}`);
811
+ throw n({
812
+ source: "shader",
813
+ error: t,
814
+ renderer: "webgl"
815
+ }), t;
816
+ }
817
+ let l = {
818
+ program: c,
687
819
  uniforms: Object.fromEntries([
688
820
  "u_view",
689
821
  "u_x",
690
822
  "u_y",
691
- ...r
692
- ].map((e) => [e, o.getUniformLocation(s, e)])),
693
- attribs: Object.fromEntries(i.map((e) => [e, o.getAttribLocation(s, e)]))
823
+ ...i
824
+ ].map((e) => [e, s.getUniformLocation(c, e)])),
825
+ attribs: Object.fromEntries(a.map((e) => [e, s.getAttribLocation(c, e)]))
694
826
  };
695
- return this.programs.set(e, c), c;
827
+ return this.programs.set(e, l), l;
696
828
  }
697
829
  lost() {
698
830
  this.programs.clear();
699
831
  }
700
832
  };
701
- function K(e, t, n, r) {
702
- let i = e.createShader(t);
703
- if (e.shaderSource(i, n), e.compileShader(i), !e.getShaderParameter(i, e.COMPILE_STATUS) && !e.isContextLost()) throw Error(`alphacharts: ${t === e.VERTEX_SHADER ? "vertex" : "fragment"} shader ${r}: ${e.getShaderInfoLog(i)}`);
704
- return i;
833
+ function X(e, t, r, i) {
834
+ let a = e.createShader(t);
835
+ if (e.shaderSource(a, r), e.compileShader(a), !e.getShaderParameter(a, e.COMPILE_STATUS) && !e.isContextLost()) {
836
+ let r = /* @__PURE__ */ Error(`alphacharts: ${t === e.VERTEX_SHADER ? "vertex" : "fragment"} shader ${i}: ${e.getShaderInfoLog(a)}`);
837
+ throw n({
838
+ source: "shader",
839
+ error: r,
840
+ renderer: "webgl"
841
+ }), r;
842
+ }
843
+ return a;
705
844
  }
706
845
  //#endregion
707
846
  //#region src/render/window.ts
708
- var q = 6, J = -1e9, Y = "-1e8", X = 24, Z = 8;
709
- function ne(e, t) {
847
+ var ie = 6, ae = -1e9, oe = "-1e8", se = 24, ce = 8;
848
+ function Z(e, t) {
710
849
  let n = 1 / t;
711
- return n <= Z || e.lod.levels.length === 0 ? 0 : Math.min(e.lod.topLevel, Math.max(3, Math.floor(Math.log2(n))));
850
+ return n <= ce || e.lod.levels.length === 0 ? 0 : Math.min(e.lod.topLevel, Math.max(3, Math.floor(Math.log2(n))));
712
851
  }
713
- var re = class {
852
+ var le = class {
714
853
  gl;
715
854
  data;
716
855
  zeroBase;
@@ -804,7 +943,7 @@ var re = class {
804
943
  fill(e, t) {
805
944
  let n = this.f32, r = this.u32, i = this.priceBase, a = this.level, o = this.data, s = this.log;
806
945
  if (a === 0) {
807
- let a = o.store, c = o.shape === "ohlc", l = a.cols[0], u = a.cols[+!!c], d = a.cols[c ? 2 : 0], f = a.cols[c ? 3 : 0], p = c ? void 0 : a.cols[1];
946
+ let a = o.store, c = o.shape === "ohlc", l = a.cols[0], u = a.cols[+!!c], d = a.cols[c ? 2 : 0], f = a.cols[c ? 3 : 0], p = c ? a.cols[4] : a.cols[1];
808
947
  for (let o = e; o <= t; o++) {
809
948
  let e = (o - this.lo) * 6, t = a.head + (o - a.first), c = o >= a.first && o < a.end ? l[t] : NaN, m = u[t], h = d[t], g = f[t];
810
949
  s && (c = $(c), m = $(m), h = $(h), g = $(g)), c !== c || m !== m || h !== h || g !== g ? Q(n, r, e) : (n[e] = o - this.base, n[e + 1] = c - i, n[e + 2] = m - i, n[e + 3] = h - i, n[e + 4] = g - i, r[e + 5] = p ? p[t] : 0);
@@ -819,12 +958,12 @@ var re = class {
819
958
  }
820
959
  };
821
960
  function Q(e, t, n) {
822
- e[n] = J, e[n + 1] = e[n + 2] = e[n + 3] = e[n + 4] = 0, t[n + 5] = 0;
961
+ e[n] = ae, e[n + 1] = e[n + 2] = e[n + 3] = e[n + 4] = 0, t[n + 5] = 0;
823
962
  }
824
963
  function $(e) {
825
964
  return e > 0 ? Math.log10(e) : NaN;
826
965
  }
827
966
  //#endregion
828
- export { i as C, n as S, _, ne as a, p as b, C as c, te as d, S as f, g, f as h, q as i, T as l, D as m, re as n, G as o, w as p, X as r, W as s, Y as t, ee as u, m as v, a as x, h as y };
967
+ export { a as C, t as E, c as S, n as T, y as _, Z as a, v as b, te as c, E as d, D as f, h as g, re as h, ie as i, O as l, k as m, le as n, Y as o, T as p, se as r, J as s, oe as t, ee as u, b as v, s as w, g as x, _ as y };
829
968
 
830
- //# sourceMappingURL=window-uK2QNvv5.js.map
969
+ //# sourceMappingURL=window-WvcPCDkK.js.map