(dataFile: DataFile)
| 9 | import { SandDance } from '@msrvida/sanddance-react'; |
| 10 | |
| 11 | export const loadDataFile = (dataFile: DataFile) => new Promise<DataContent>((resolve, reject) => { |
| 12 | const vega = SandDance.VegaDeckGl.base.vega; |
| 13 | const loader = vega.loader(); |
| 14 | |
| 15 | function handleRawText(text: string) { |
| 16 | let data: object[]; |
| 17 | try { |
| 18 | data = vega.read(text, { type: dataFile.type, parse: {} }); |
| 19 | } |
| 20 | catch (e) { |
| 21 | reject(e); |
| 22 | } |
| 23 | if (data) { |
| 24 | loadDataArray(data, dataFile.type).then(dc => { |
| 25 | if (dataFile.snapshotsUrl) { |
| 26 | fetch(dataFile.snapshotsUrl) |
| 27 | .then(response => response.json()) |
| 28 | .then(snapshots => { |
| 29 | dc.snapshots = snapshots; |
| 30 | resolve(dc); |
| 31 | }) |
| 32 | .catch(reject); |
| 33 | } else if (dataFile.snapshots) { |
| 34 | dc.snapshots = dataFile.snapshots; |
| 35 | resolve(dc); |
| 36 | } else { |
| 37 | resolve(dc); |
| 38 | } |
| 39 | }).catch(reject); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if (dataFile.dataUrl) { |
| 44 | loader.load(dataFile.dataUrl).then(handleRawText).catch(reject); |
| 45 | } else if (dataFile.rawText) { |
| 46 | handleRawText(dataFile.rawText); |
| 47 | } else { |
| 48 | reject('dataFile object must have either dataUrl or rawText property set.'); |
| 49 | } |
| 50 | }); |
| 51 | |
| 52 | export const loadDataArray = (data: object[], type: DataFileType) => new Promise<DataContent>((resolve, reject) => { |
| 53 | const parse = type === 'csv' || type === 'tsv'; |
no test coverage detected