-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
data-consumer.ts
232 lines (209 loc) · 6.4 KB
/
data-consumer.ts
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
import { Time } from './horz-scale-behavior-time/types';
import { CustomData, CustomSeriesWhitespaceData } from './icustom-series';
import { Series } from './series';
import { SeriesType } from './series-options';
/**
* Represents a whitespace data item, which is a data point without a value.
*
* @example
* ```js
* const data = [
* { time: '2018-12-03', value: 27.02 },
* { time: '2018-12-04' }, // whitespace
* { time: '2018-12-05' }, // whitespace
* { time: '2018-12-06' }, // whitespace
* { time: '2018-12-07' }, // whitespace
* { time: '2018-12-08', value: 23.92 },
* { time: '2018-12-13', value: 30.74 },
* ];
* ```
*/
export interface WhitespaceData<HorzScaleItem = Time> {
/**
* The time of the data.
*/
time: HorzScaleItem;
/**
* Additional custom values which will be ignored by the library, but
* could be used by plugins.
*/
customValues?: Record<string, unknown>;
}
/**
* A base interface for a data point of single-value series.
*/
export interface SingleValueData<HorzScaleItem = Time> extends WhitespaceData<HorzScaleItem> {
/**
* The time of the data.
*/
time: HorzScaleItem;
/**
* Price value of the data.
*/
value: number;
}
/**
* Structure describing a single item of data for line series
*/
export interface LineData<HorzScaleItem = Time> extends SingleValueData<HorzScaleItem> {
/**
* Optional color value for certain data item. If missed, color from options is used
*/
color?: string;
}
/**
* Structure describing a single item of data for histogram series
*/
export interface HistogramData<HorzScaleItem = Time> extends SingleValueData<HorzScaleItem> {
/**
* Optional color value for certain data item. If missed, color from options is used
*/
color?: string;
}
/**
* Structure describing a single item of data for area series
*/
export interface AreaData<HorzScaleItem = Time> extends SingleValueData<HorzScaleItem> {
/**
* Optional line color value for certain data item. If missed, color from options is used
*/
lineColor?: string;
/**
* Optional top color value for certain data item. If missed, color from options is used
*/
topColor?: string;
/**
* Optional bottom color value for certain data item. If missed, color from options is used
*/
bottomColor?: string;
}
/**
* Structure describing a single item of data for baseline series
*/
export interface BaselineData<HorzScaleItem = Time> extends SingleValueData<HorzScaleItem> {
/**
* Optional top area top fill color value for certain data item. If missed, color from options is used
*/
topFillColor1?: string;
/**
* Optional top area bottom fill color value for certain data item. If missed, color from options is used
*/
topFillColor2?: string;
/**
* Optional top area line color value for certain data item. If missed, color from options is used
*/
topLineColor?: string;
/**
* Optional bottom area top fill color value for certain data item. If missed, color from options is used
*/
bottomFillColor1?: string;
/**
* Optional bottom area bottom fill color value for certain data item. If missed, color from options is used
*/
bottomFillColor2?: string;
/**
* Optional bottom area line color value for certain data item. If missed, color from options is used
*/
bottomLineColor?: string;
}
/**
* Represents a bar with a {@link Time} and open, high, low, and close prices.
*/
export interface OhlcData<HorzScaleItem = Time> extends WhitespaceData<HorzScaleItem> {
/**
* The bar time.
*/
time: HorzScaleItem;
/**
* The open price.
*/
open: number;
/**
* The high price.
*/
high: number;
/**
* The low price.
*/
low: number;
/**
* The close price.
*/
close: number;
}
/**
* Structure describing a single item of data for bar series
*/
export interface BarData<HorzScaleItem = Time> extends OhlcData<HorzScaleItem> {
/**
* Optional color value for certain data item. If missed, color from options is used
*/
color?: string;
}
/**
* Structure describing a single item of data for candlestick series
*/
export interface CandlestickData<HorzScaleItem = Time> extends OhlcData<HorzScaleItem> {
/**
* Optional color value for certain data item. If missed, color from options is used
*/
color?: string;
/**
* Optional border color value for certain data item. If missed, color from options is used
*/
borderColor?: string;
/**
* Optional wick color value for certain data item. If missed, color from options is used
*/
wickColor?: string;
}
export function isWhitespaceData<HorzScaleItem = Time>(data: SeriesDataItemTypeMap<HorzScaleItem>[SeriesType]): data is WhitespaceData<HorzScaleItem> {
return (data as Partial<BarData<HorzScaleItem>>).open === undefined && (data as Partial<LineData<HorzScaleItem>>).value === undefined;
}
export function isFulfilledData<HorzScaleItem, T extends SeriesDataItemTypeMap<HorzScaleItem>[SeriesType]>(
data: T
): data is Extract<T, BarData<HorzScaleItem> | LineData<HorzScaleItem> | HistogramData<HorzScaleItem>> {
return (
(data as Partial<BarData<HorzScaleItem>>).open !== undefined ||
(data as Partial<LineData<HorzScaleItem>>).value !== undefined
);
}
/**
* Represents the type of data that a series contains.
*
* For example a bar series contains {@link BarData} or {@link WhitespaceData}.
*/
export interface SeriesDataItemTypeMap<HorzScaleItem = Time> {
/**
* The types of bar series data.
*/
Bar: BarData<HorzScaleItem> | WhitespaceData<HorzScaleItem>;
/**
* The types of candlestick series data.
*/
Candlestick: CandlestickData<HorzScaleItem> | WhitespaceData<HorzScaleItem>;
/**
* The types of area series data.
*/
Area: AreaData<HorzScaleItem> | WhitespaceData<HorzScaleItem>;
/**
* The types of baseline series data.
*/
Baseline: BaselineData<HorzScaleItem> | WhitespaceData<HorzScaleItem>;
/**
* The types of line series data.
*/
Line: LineData<HorzScaleItem> | WhitespaceData<HorzScaleItem>;
/**
* The types of histogram series data.
*/
Histogram: HistogramData<HorzScaleItem> | WhitespaceData<HorzScaleItem>;
/**
* The base types of an custom series data.
*/
Custom: CustomData<HorzScaleItem> | CustomSeriesWhitespaceData<HorzScaleItem>;
}
export interface DataUpdatesConsumer<TSeriesType extends SeriesType, HorzScaleItem = Time> {
applyNewData(series: Series<TSeriesType>, data: SeriesDataItemTypeMap<HorzScaleItem>[TSeriesType][]): void;
updateData(series: Series<TSeriesType>, data: SeriesDataItemTypeMap<HorzScaleItem>[TSeriesType]): void;
}