react-native-vroom-chart 0.15.0 → 0.17.0

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.
Files changed (53) hide show
  1. package/cpp/VroomChartHostObject.cpp +252 -1
  2. package/cpp/_core_include/vroom/vroom_chart.h +178 -4
  3. package/cpp/_core_src/atr.cpp +67 -0
  4. package/cpp/_core_src/atr.h +44 -0
  5. package/cpp/_core_src/atr_pane.cpp +162 -0
  6. package/cpp/_core_src/atr_pane.h +48 -0
  7. package/cpp/_core_src/candles.cpp +15 -14
  8. package/cpp/_core_src/chart.cpp +758 -200
  9. package/cpp/_core_src/chart.h +221 -3
  10. package/cpp/_core_src/chart_facade.cpp +236 -23
  11. package/cpp/_core_src/color_lerp.h +28 -0
  12. package/cpp/_core_src/fair_value_gaps.cpp +76 -0
  13. package/cpp/_core_src/fair_value_gaps.h +54 -0
  14. package/cpp/_core_src/fvg_overlay.cpp +262 -0
  15. package/cpp/_core_src/fvg_overlay.h +43 -0
  16. package/cpp/_core_src/ichimoku.cpp +65 -0
  17. package/cpp/_core_src/ichimoku.h +45 -0
  18. package/cpp/_core_src/labels.cpp +3 -0
  19. package/cpp/_core_src/line_morph.h +159 -0
  20. package/cpp/_core_src/loading_line.cpp +183 -0
  21. package/cpp/_core_src/loading_line.h +38 -0
  22. package/cpp/_core_src/loading_wave.h +140 -0
  23. package/cpp/_core_src/ma_overlay.cpp +273 -45
  24. package/cpp/_core_src/ma_overlay.h +64 -2
  25. package/cpp/_core_src/macd.cpp +24 -1
  26. package/cpp/_core_src/macd.h +16 -0
  27. package/cpp/_core_src/macd_pane.cpp +71 -62
  28. package/cpp/_core_src/macd_pane.h +13 -1
  29. package/cpp/_core_src/pane_series.h +169 -0
  30. package/cpp/_core_src/price_indicator.cpp +21 -7
  31. package/cpp/_core_src/price_indicator.h +11 -1
  32. package/cpp/_core_src/price_indicator_anim.h +81 -0
  33. package/cpp/_core_src/rsi.cpp +4 -0
  34. package/cpp/_core_src/rsi.h +7 -0
  35. package/cpp/_core_src/rsi_pane.cpp +85 -29
  36. package/cpp/_core_src/rsi_pane.h +11 -1
  37. package/cpp/_core_src/theme.cpp +5 -0
  38. package/cpp/_core_src/tip_geometry.h +55 -0
  39. package/cpp/_core_src/viewport.h +68 -0
  40. package/lib/index.d.mts +281 -3
  41. package/lib/index.d.ts +281 -3
  42. package/lib/index.js +292 -14
  43. package/lib/index.js.map +1 -1
  44. package/lib/index.mjs +292 -14
  45. package/lib/index.mjs.map +1 -1
  46. package/package.json +1 -1
  47. package/src/VroomChart.tsx +33 -3
  48. package/src/dataTransitions.ts +47 -0
  49. package/src/index.ts +5 -0
  50. package/src/jsi.d.ts +139 -1
  51. package/src/theme.ts +1 -0
  52. package/src/types.ts +5 -0
  53. package/src/useChartCore.ts +402 -8
@@ -0,0 +1,183 @@
1
+ #include "loading_line.h"
2
+
3
+ #pragma clang diagnostic push
4
+ #pragma clang diagnostic ignored "-Wdocumentation"
5
+ #include "include/core/SkCanvas.h"
6
+ #include "include/core/SkColor.h"
7
+ #include "include/core/SkPaint.h"
8
+ #include "include/core/SkPath.h"
9
+ #include "include/core/SkPathBuilder.h"
10
+ #include "include/core/SkPoint.h"
11
+ #pragma clang diagnostic pop
12
+
13
+ #include <algorithm>
14
+ #include <cstddef>
15
+ #include <vector>
16
+
17
+ #include "chart.h"
18
+ #include "curve.h"
19
+ #include "loading_wave.h"
20
+ #include "theme.h"
21
+ #include "viewport.h"
22
+
23
+ namespace vroom::loading_line {
24
+
25
+ namespace {
26
+
27
+ constexpr float kStrokeWidthPx = 2.f;
28
+
29
+ // Multiplies into whatever alpha the resolved color already carries.
30
+ SkColor faded(SkColor c, float a) {
31
+ const float scaled = static_cast<float>(SkColorGetA(c)) *
32
+ std::clamp(a, 0.f, 1.f);
33
+ return SkColorSetA(c, static_cast<U8CPU>(scaled + 0.5f));
34
+ }
35
+
36
+ // The loading line's color. A transparent `skeleton` inherits the gridline
37
+ // color, the same sentinel convention the candle border and wick colors use.
38
+ // The gridlines are the chart's existing vocabulary for "structure, not data",
39
+ // which is exactly what the line is: drawing it in that tone keeps it from
40
+ // reading as a series, without needing to be scaled down to stay quiet.
41
+ SkColor stroke_color(const VroomChart& chart) {
42
+ const SkColor c = chart.theme.colors[VROOM_COLOR_SKELETON];
43
+ return SkColorGetA(c) == 0 ? chart.theme.colors[VROOM_COLOR_GRID] : c;
44
+ }
45
+
46
+ // Emits `pts` as a monotone cubic — the same curve the line chart draws (see
47
+ // curve.h), so the loading line and the series it hands off to are shaped by
48
+ // the same math and the transition has no renderer seam to hide.
49
+ //
50
+ // Monotone rather than a plain spline matters even here: by the end of the
51
+ // morph these vertices *are* real price data, and an unconstrained curve would
52
+ // overshoot a sharp reversal and bow outside the candle it is supposed to pass
53
+ // through.
54
+ SkPath spline(const std::vector<SkPoint>& pts) {
55
+ SkPathBuilder path;
56
+ if (pts.empty()) return path.detach();
57
+ path.moveTo(pts[0]);
58
+ // Below three points there is no interior vertex to fit a tangent to.
59
+ if (pts.size() < 3) {
60
+ for (std::size_t i = 1; i < pts.size(); ++i) path.lineTo(pts[i]);
61
+ return path.detach();
62
+ }
63
+
64
+ const auto secant_at = [&](std::size_t i) {
65
+ return curve::secant(pts[i].fX, pts[i].fY,
66
+ pts[i + 1].fX, pts[i + 1].fY);
67
+ };
68
+ const auto tangent_at = [&](std::size_t i) {
69
+ if (i == 0) return secant_at(0);
70
+ if (i + 1 == pts.size()) return secant_at(pts.size() - 2);
71
+ return curve::monotone_tangent(secant_at(i - 1), secant_at(i));
72
+ };
73
+
74
+ for (std::size_t i = 0; i + 1 < pts.size(); ++i) {
75
+ // Flat or backwards spacing has no meaningful tangent, and curving
76
+ // through it would fold the line back on itself.
77
+ if (!(pts[i + 1].fX > pts[i].fX)) {
78
+ path.lineTo(pts[i + 1]);
79
+ continue;
80
+ }
81
+ const curve::Controls c = curve::segment_controls(
82
+ pts[i].fX, pts[i].fY, pts[i + 1].fX, pts[i + 1].fY,
83
+ tangent_at(i), tangent_at(i + 1), 1.f);
84
+ path.cubicTo(c.c1x, c.c1y, c.c2x, c.c2y, pts[i + 1].fX, pts[i + 1].fY);
85
+ }
86
+ return path.detach();
87
+ }
88
+
89
+ void stroke_path(SkCanvas* canvas,
90
+ const VroomChart& chart,
91
+ const SkPath& path,
92
+ float alpha) {
93
+ SkPaint p;
94
+ p.setStyle(SkPaint::kStroke_Style);
95
+ p.setStrokeWidth(kStrokeWidthPx);
96
+ p.setStrokeCap(SkPaint::kRound_Cap);
97
+ p.setStrokeJoin(SkPaint::kRound_Join);
98
+ p.setAntiAlias(true);
99
+ p.setColor(faded(stroke_color(chart), alpha));
100
+ canvas->drawPath(path, p);
101
+ }
102
+
103
+ // Pinned to phase 0 under reduced motion, which leaves a still curve at
104
+ // mid-breath rather than nothing — the placeholder is still worth showing.
105
+ float phase(const VroomChart& chart) {
106
+ return chart.loading_animate ? chart.loading_elapsed_s : 0.f;
107
+ }
108
+
109
+ // Stage 1: the curve alone, sampled across the whole plot.
110
+ SkPath idle_path(const VroomChart& chart, float area_w, float pane_h) {
111
+ const int samples = loading_wave::sample_count(area_w);
112
+ const float elapsed = phase(chart);
113
+ std::vector<SkPoint> pts;
114
+ pts.reserve(static_cast<std::size_t>(samples) + 1);
115
+ for (int i = 0; i <= samples; ++i) {
116
+ const float xf = static_cast<float>(i) / static_cast<float>(samples);
117
+ pts.push_back(SkPoint{xf * area_w,
118
+ loading_wave::y_frac(elapsed, xf) * pane_h});
119
+ }
120
+ return spline(pts);
121
+ }
122
+
123
+ // Stages 2 and 3: the captured vertices, eased from the frozen curve toward the
124
+ // candle centres. `t` of 1 is the shape the candles grow out of.
125
+ SkPath morph_path(const VroomChart& chart,
126
+ const Layout& lay,
127
+ float area_w,
128
+ float pane_h,
129
+ float t) {
130
+ std::vector<SkPoint> pts;
131
+ pts.reserve(chart.loading_line.size());
132
+ for (const LinePoint& pt : chart.loading_line) {
133
+ const float from_y = pt.from_y * pane_h;
134
+ const float to_y = y_at_fraction(lay, pt.to_y);
135
+ pts.push_back(SkPoint{pt.x * area_w, from_y + (to_y - from_y) * t});
136
+ }
137
+ return spline(pts);
138
+ }
139
+
140
+ } // namespace
141
+
142
+ void draw(SkCanvas* canvas, const VroomChart& chart, const Layout& lay) {
143
+ if (!canvas) return;
144
+ const float area_w = candle_area_width(lay);
145
+ const float pane_h = price_pane_bottom(lay);
146
+ if (area_w <= 0.f || pane_h <= 0.f) return;
147
+
148
+ const float b = loading_wave::breath(phase(chart));
149
+
150
+ // A capture present means the data has landed and the line is on its way to
151
+ // the candle centres; absent means nothing has arrived yet.
152
+ if (chart.loading_line.empty()) {
153
+ stroke_path(canvas, chart, idle_path(chart, area_w, pane_h), b);
154
+ return;
155
+ }
156
+ // Settle out of the breath as the line resolves, so it holds still at full
157
+ // strength by the time it reaches the candle centres — subtle at the
158
+ // default gridline tone, but it stops a bright custom color from pulsing
159
+ // while the data lands. Stage 3 then fades from full, where draw_fading
160
+ // picks up.
161
+ const float alpha = b + (1.f - b) * chart.loading_line_t;
162
+ stroke_path(canvas, chart,
163
+ morph_path(chart, lay, area_w, pane_h, chart.loading_line_t),
164
+ alpha);
165
+ }
166
+
167
+ void draw_fading(SkCanvas* canvas,
168
+ const VroomChart& chart,
169
+ const Layout& lay,
170
+ float alpha) {
171
+ if (!canvas || alpha <= 0.f) return;
172
+ if (chart.loading_line.empty()) return;
173
+ const float area_w = candle_area_width(lay);
174
+ const float pane_h = price_pane_bottom(lay);
175
+ if (area_w <= 0.f || pane_h <= 0.f) return;
176
+
177
+ // Held at the morph's end state: the line has already reached the centres,
178
+ // and the candles are growing out of exactly these pixels.
179
+ stroke_path(canvas, chart, morph_path(chart, lay, area_w, pane_h, 1.f),
180
+ alpha);
181
+ }
182
+
183
+ } // namespace vroom::loading_line
@@ -0,0 +1,38 @@
1
+ // The loading line: a single stroke across the plot standing in for a series
2
+ // that hasn't arrived yet, and the bridge the real candles grow out of.
3
+ //
4
+ // Three stages, all drawn by the one function here:
5
+ //
6
+ // 1. idle — a travelling sine across the full width (see loading_wave.h)
7
+ // 2. morph — that sine, frozen, easing into a polyline through the vertical
8
+ // centre of every candle about to be drawn
9
+ // 3. reveal — the polyline holding still at those centres while it fades
10
+ // out and the candles grow outward from it
11
+ //
12
+ // Stages 1 and 2 run instead of the chart, so the axis text, price badge,
13
+ // crosshair and indicator panes stay suppressed until the data is really
14
+ // there. Stage 3 runs over the top of the normal scene.
15
+
16
+ #pragma once
17
+
18
+ class SkCanvas;
19
+ struct VroomChart;
20
+
21
+ namespace vroom {
22
+ struct Layout;
23
+ }
24
+
25
+ namespace vroom::loading_line {
26
+
27
+ // Stages 1 and 2, in place of the chart. Which one it draws depends on whether
28
+ // the chart holds a capture (see VroomChart::begin_loading_morph).
29
+ void draw(SkCanvas* canvas, const VroomChart& chart, const Layout& lay);
30
+
31
+ // Stage 3, over the settled scene. `alpha` is the line's remaining opacity, so
32
+ // the caller can ride it on the same clock as the candles' growth.
33
+ void draw_fading(SkCanvas* canvas,
34
+ const VroomChart& chart,
35
+ const Layout& lay,
36
+ float alpha);
37
+
38
+ } // namespace vroom::loading_line
@@ -0,0 +1,140 @@
1
+ // The idle animation the loading line rides while a series is being fetched.
2
+ // A position across the plot and an elapsed time in; a vertical offset and a
3
+ // brightness out.
4
+ //
5
+ // Three sines, not one. A single sine is too regular to read as anything but a
6
+ // test pattern — the eye finds the repeat immediately and the line stops
7
+ // looking like it's waiting for something. Summing three frequencies that
8
+ // aren't multiples of each other, each drifting at its own rate, gives a curve
9
+ // that keeps rearranging itself.
10
+ //
11
+ // The other half of the effect is restraint: a small amplitude and a faint,
12
+ // slowly breathing opacity. The line is a hint that the chart is alive, not a
13
+ // feature competing with the data about to replace it.
14
+ //
15
+ // Approach adapted from Liveline (github.com/benjitaylor/liveline, MIT), whose
16
+ // loading state solves the same problem well.
17
+ //
18
+ // Skia-free and header-only so the unit tests can cover it; see
19
+ // tests/test_loading_wave.cpp.
20
+
21
+ #pragma once
22
+
23
+ #include <cmath>
24
+
25
+ namespace vroom::loading_wave {
26
+
27
+ constexpr float kTwoPi = 6.283185307179586f;
28
+
29
+ // One component of the curve.
30
+ struct Harmonic {
31
+ float freq; // radians across the full plot width
32
+ float weight; // share of the amplitude
33
+ float drift; // multiple of the base phase speed
34
+ };
35
+
36
+ // Frequencies deliberately not integer multiples of one another: harmonics
37
+ // would lock into one repeating shape, which is the thing being avoided. The
38
+ // drifts are all multiples of a quarter only so the whole curve has an exact
39
+ // period to wrap the phase clock on — see kPeriodSeconds.
40
+ //
41
+ // Four components, weighted toward the middle of the range, because the curve
42
+ // has to sit convincingly next to candlesticks. A gentler two-or-three-crest
43
+ // undulation suits a smooth live line chart, but against minute-resolution
44
+ // price action it reads as decorative, and it makes the hand-off a real change
45
+ // of shape rather than the same curve coming into focus.
46
+ constexpr Harmonic kHarmonics[] = {
47
+ {14.0f, 0.40f, 1.00f}, // carrier — a little over two crests per screen
48
+ {25.0f, 0.28f, 1.25f}, // ripple, running ahead
49
+ {45.0f, 0.16f, 1.50f}, // fine texture, ahead again
50
+ {6.0f, 0.16f, 0.75f}, // a slow swell, running behind
51
+ };
52
+ constexpr int kHarmonicCount = 4;
53
+
54
+ // Base phase speed. Slow on purpose: this plays under a chart that has nothing
55
+ // to say yet, and anything quicker reads as activity.
56
+ constexpr float kDriftRadPerSecond = 1.0f;
57
+
58
+ // One full cycle of the *whole* curve. The drifts differ by quarters, so every
59
+ // component returns to its starting phase only after four base cycles — which
60
+ // is also why the shape takes ~25s to come back around. Callers wrap elapsed
61
+ // time by this so a chart left loading doesn't lose float precision on the
62
+ // phase, and wrapping here is seamless because it is a true period.
63
+ constexpr float kPeriodSeconds = 4.f * kTwoPi / kDriftRadPerSecond;
64
+
65
+ // Peak displacement from the centreline, as a fraction of the pane height. The
66
+ // components rarely peak together, so the curve typically occupies well under
67
+ // this — it is a bound, not the height it looks.
68
+ constexpr float kAmplitudeFrac = 0.10f;
69
+
70
+ // Breathing opacity. A pulse around full strength, not a dimmer: how quiet the
71
+ // line reads is the *color's* job — it inherits the gridline tone by default —
72
+ // and scaling an already-recessive grey down by a third again would leave the
73
+ // curve indistinguishable from the background.
74
+ constexpr float kBreathMin = 0.75f;
75
+ constexpr float kBreathMax = 1.00f;
76
+ // Ten breaths per curve cycle, so the breath wraps with the phase (~2.5s each)
77
+ // and never syncs with the drift into one combined pulse.
78
+ constexpr float kBreathCyclesPerPeriod = 10.f;
79
+
80
+ // Offset at `x_frac` (0 = left edge of the plot, 1 = right) and `elapsed_s`, in
81
+ // -1 .. 1. The weights sum to 1, so that range is tight. Phase runs as
82
+ // (k*x - w*t), which walks the crests rightward — the direction the series
83
+ // grows. Time outside one period is fine; sin wraps on its own.
84
+ inline float at(float elapsed_s, float x_frac) {
85
+ const float base = kDriftRadPerSecond * elapsed_s;
86
+ float sum = 0.f;
87
+ for (int i = 0; i < kHarmonicCount; ++i) {
88
+ const Harmonic& h = kHarmonics[i];
89
+ sum += h.weight * std::sin(h.freq * x_frac - h.drift * base);
90
+ }
91
+ return sum;
92
+ }
93
+
94
+ // The curve's y as a fraction of the pane height (0 = pane top, 1 = bottom),
95
+ // centred on the pane. This is the form the line capture stores, so a resize
96
+ // mid-morph rescales instead of stranding the curve at old pixels.
97
+ inline float y_frac(float elapsed_s, float x_frac) {
98
+ return 0.5f + kAmplitudeFrac * at(elapsed_s, x_frac);
99
+ }
100
+
101
+ // The line's opacity at `elapsed_s`, in kBreathMin .. kBreathMax.
102
+ inline float breath(float elapsed_s) {
103
+ const float mid = (kBreathMin + kBreathMax) * 0.5f;
104
+ const float half = (kBreathMax - kBreathMin) * 0.5f;
105
+ return mid + half * std::sin(kTwoPi * kBreathCyclesPerPeriod * elapsed_s /
106
+ kPeriodSeconds);
107
+ }
108
+
109
+ // The tightest component in the table above.
110
+ constexpr float max_freq() {
111
+ float m = 0.f;
112
+ for (int i = 0; i < kHarmonicCount; ++i) {
113
+ if (kHarmonics[i].freq > m) m = kHarmonics[i].freq;
114
+ }
115
+ return m;
116
+ }
117
+
118
+ // Vertices per cycle of that component needed to keep it from aliasing into a
119
+ // shape the curve doesn't have. Ten is comfortably past the point where the
120
+ // spline through the samples is indistinguishable from the real curve.
121
+ constexpr float kSamplesPerCycle = 10.f;
122
+
123
+ // How many vertices to sample the curve at across a plot `width_px` wide.
124
+ //
125
+ // The floor comes from the harmonic table rather than a literal, so retuning
126
+ // the curve can't silently undersample it — a narrow chart still gets enough
127
+ // vertices to resolve the finest component, it just spaces them more tightly.
128
+ //
129
+ // Both the idle curve and the morph capture size themselves through here, so
130
+ // the two are always sampled the same way and the hand-off has no shape
131
+ // discontinuity to hide.
132
+ inline int sample_count(float width_px) {
133
+ const int floor_n =
134
+ static_cast<int>(max_freq() / kTwoPi * kSamplesPerCycle) + 1;
135
+ const int n = static_cast<int>(width_px / 8.f);
136
+ const int ceil_n = 4 * floor_n;
137
+ return n < floor_n ? floor_n : (n > ceil_n ? ceil_n : n);
138
+ }
139
+
140
+ } // namespace vroom::loading_wave