-
Notifications
You must be signed in to change notification settings - Fork 592
/
data.ts
256 lines (216 loc) · 7.67 KB
/
data.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Constants and utilities for data.
*/
import {Vector2} from 'vega';
import {FieldName} from './channeldef';
import {VgData} from './vega.schema';
export type ParseValue = null | string | 'string' | 'boolean' | 'date' | 'number';
export interface Parse {
[field: string]: ParseValue;
}
export interface DataFormatBase {
/**
* If set to `null`, disable type inference based on the spec and only use type inference based on the data.
* Alternatively, a parsing directive object can be provided for explicit data types. Each property of the object corresponds to a field name, and the value to the desired data type (one of `"number"`, `"boolean"`, `"date"`, or null (do not parse the field)).
* For example, `"parse": {"modified_on": "date"}` parses the `modified_on` field in each input record a Date value.
*
* For `"date"`, we parse data based using Javascript's [`Date.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse).
* For Specific date formats can be provided (e.g., `{foo: "date:'%m%d%Y'"}`), using the [d3-time-format syntax](https://github.com/d3/d3-time-format#locale_format). UTC date format parsing is supported similarly (e.g., `{foo: "utc:'%m%d%Y'"}`). See more about [UTC time](https://vega.github.io/vega-lite/docs/timeunit.html#utc)
*/
parse?: Parse | null;
/**
* Type of input data: `"json"`, `"csv"`, `"tsv"`, `"dsv"`.
*
* __Default value:__ The default format type is determined by the extension of the file URL.
* If no extension is detected, `"json"` will be used by default.
*/
type?: 'csv' | 'tsv' | 'dsv' | 'json' | 'topojson';
}
export interface CsvDataFormat extends DataFormatBase {
type?: 'csv' | 'tsv';
}
export interface DsvDataFormat extends DataFormatBase {
type?: 'dsv';
/**
* The delimiter between records. The delimiter must be a single character (i.e., a single 16-bit code unit); so, ASCII delimiters are fine, but emoji delimiters are not.
*
* @minLength 1
* @maxLength 1
*/
delimiter: string;
}
export interface JsonDataFormat extends DataFormatBase {
type?: 'json';
/**
* The JSON property containing the desired data.
* This parameter can be used when the loaded JSON file may have surrounding structure or meta-data.
* For example `"property": "values.features"` is equivalent to retrieving `json.values.features`
* from the loaded JSON object.
*/
property?: string;
}
export interface TopoDataFormat extends DataFormatBase {
type?: 'topojson';
/**
* The name of the TopoJSON object set to convert to a GeoJSON feature collection.
* For example, in a map of the world, there may be an object set named `"countries"`.
* Using the feature property, we can extract this set and generate a GeoJSON feature object for each country.
*/
feature?: string;
/**
* The name of the TopoJSON object set to convert to mesh.
* Similar to the `feature` option, `mesh` extracts a named TopoJSON object set.
* Unlike the `feature` option, the corresponding geo data is returned as a single, unified mesh instance, not as individual GeoJSON features.
* Extracting a mesh is useful for more efficiently drawing borders or other geographic elements that you do not need to associate with specific regions such as individual countries, states or counties.
*/
mesh?: string;
}
export type DataFormat = CsvDataFormat | DsvDataFormat | JsonDataFormat | TopoDataFormat;
export type DataFormatType = 'json' | 'csv' | 'tsv' | 'dsv' | 'topojson';
export type DataSource = UrlData | InlineData | NamedData;
export type Data = DataSource | Generator;
// eslint-disable-next-line @typescript-eslint/ban-types
export type InlineDataset = number[] | string[] | boolean[] | object[] | string | object;
export interface DataBase {
/**
* An object that specifies the format for parsing the data.
*/
format?: DataFormat;
/**
* Provide a placeholder name and bind data at runtime.
*/
name?: string;
}
export interface UrlData extends DataBase {
/**
* An URL from which to load the data set. Use the `format.type` property
* to ensure the loaded data is correctly parsed.
*/
url: string;
}
export interface InlineData extends DataBase {
/**
* The full data set, included inline. This can be an array of objects or primitive values, an object, or a string.
* Arrays of primitive values are ingested as objects with a `data` property. Strings are parsed according to the specified format type.
*/
values: InlineDataset;
}
export interface NamedData extends DataBase {
/**
* Provide a placeholder name and bind data at runtime.
*/
name: string;
}
export function isUrlData(data: Partial<Data> | Partial<VgData>): data is UrlData {
return 'url' in data;
}
export function isInlineData(data: Partial<Data> | Partial<VgData>): data is InlineData {
return 'values' in data;
}
export function isNamedData(data: Partial<Data> | Partial<VgData>): data is NamedData {
return 'name' in data && !isUrlData(data) && !isInlineData(data) && !isGenerator(data);
}
export function isGenerator(data: Partial<Data> | Partial<VgData>): data is Generator {
return data && (isSequenceGenerator(data) || isSphereGenerator(data) || isGraticuleGenerator(data));
}
export function isSequenceGenerator(data: Partial<Data> | Partial<VgData>): data is SequenceGenerator {
return 'sequence' in data;
}
export function isSphereGenerator(data: Partial<Data> | Partial<VgData>): data is SphereGenerator {
return 'sphere' in data;
}
export function isGraticuleGenerator(data: Partial<Data> | Partial<VgData>): data is GraticuleGenerator {
return 'graticule' in data;
}
export enum DataSourceType {
Raw,
Main,
Row,
Column,
Lookup
}
export type Generator = SequenceGenerator | SphereGenerator | GraticuleGenerator;
export interface GeneratorBase {
/**
* Provide a placeholder name and bind data at runtime.
*/
name?: string;
}
export interface SequenceGenerator extends GeneratorBase {
/**
* Generate a sequence of numbers.
*/
sequence: SequenceParams;
}
export interface SequenceParams {
/**
* The starting value of the sequence (inclusive).
*/
start: number;
/**
* The ending value of the sequence (exclusive).
*/
stop: number;
/**
* The step value between sequence entries.
*
* __Default value:__ `1`
*/
step?: number;
/**
* The name of the generated sequence field.
*
* __Default value:__ `"data"`
*/
as?: FieldName;
}
export interface SphereGenerator extends GeneratorBase {
/**
* Generate sphere GeoJSON data for the full globe.
*/
// eslint-disable-next-line @typescript-eslint/ban-types
sphere: true | {};
}
export interface GraticuleGenerator extends GeneratorBase {
/**
* Generate graticule GeoJSON data for geographic reference lines.
*/
graticule: true | GraticuleParams;
}
export interface GraticuleParams {
/**
* The major extent of the graticule as a two-element array of coordinates.
*/
extentMajor?: Vector2<Vector2<number>>;
/**
* The minor extent of the graticule as a two-element array of coordinates.
*/
extentMinor?: Vector2<Vector2<number>>;
/**
* Sets both the major and minor extents to the same values.
*/
extent?: Vector2<Vector2<number>>;
/**
* The major step angles of the graticule.
*
*
* __Default value:__ `[90, 360]`
*/
stepMajor?: Vector2<number>;
/**
* The minor step angles of the graticule.
*
* __Default value:__ `[10, 10]`
*/
stepMinor?: Vector2<number>;
/**
* Sets both the major and minor step angles to the same values.
*/
step?: Vector2<number>;
/**
* The precision of the graticule in degrees.
*
* __Default value:__ `2.5`
*/
precision?: number;
}