Utilities to facilitate working with vega/vega-datasets
Vega datasets are a collection of datasets used in Vega and in Vega-Lite examples.
The data lives at https://github.com/vega/vega-datasets and https://cdn.jsdelivr.net/npm/vega-datasets
For those of you familiar with Pandas, please consider looking at danfo.js and DataFrame.js
- list() - retrieves the list of the datasets available
- fetch(datasetName) - returns a promise and fetches the dataset
Example
datasets.list(); //-- prints the list of the datasets supported
myDataset = datasets.fetch('cars.json');
Methods
(static) fetch(library) → {Promise.<any>}
- See:
Fetches a specific dataset from within the list available from vega-datasets
Example:
Using utils.datasets.list()
we can see the list of datasets
['barley.json', 'cars.json', ...]
We can then fetch the dataset using that key, and accepting the promise. (Note that this option does not pause execution before running the next cell)
datasets.fetch('cars.json').then(results => cars = results);
[
{
Name: 'chevrolet chevelle malibu',
Miles_per_Gallon: 18,
Cylinders: 8,
Displacement: 307,
Horsepower: 130,
Weight_in_lbs: 3504,
Acceleration: 12,
Year: '1970-01-01',
Origin: 'USA'
},
...
];
Note - the utils.ijs.await method can simplify this call, to support await.
utils.ijs.await(async ($$, console) => {
gapMinder = await utils.datasets.load('gapminder.json');
});
Parameters:
Name | Type | Description |
---|---|---|
library |
string | one of the names of the libraries available from list |
Returns:
- results from the dataset
- Type
- Promise.<any>
(static) fetchJSON(targetAddress, options) → {Object}
Simple fetch
call for JSON handling simple cases.
const response = await fetch(targetAddress, options);
if (!response.ok) throw new Error(`unexpected response ${response.statusText}`);
return response.json();
Example
utils.ijs.await(async ($$, console) => {
worldJSON = await utils.datasets.fetchJSON('https://unpkg.com/world-atlas@1/world/110m.json');
console.log(worldJSON.type); // Topology
});
// use worldJSON as global variable
Parameters:
Name | Type | Description |
---|---|---|
targetAddress |
String | Address of the file to load |
options |
Object | options to pass to fetch |
Returns:
- parsed JSON of the response
- Type
- Object
(static) fetchText(targetAddress, options) → {Object}
Simple fetch
call for Text handling simple cases.
const response = await fetch(targetAddress, options);
if (!response.ok) throw new Error(`unexpected response ${response.statusText}`);
return response.text();
Example
utils.ijs.await(async ($$, console) => {
myText = await utils.datasets.fetchText('https://unpkg.com/qr-image@3.2.0/LICENSE');
return myText;
});
// use myText as global variable
Parameters:
Name | Type | Description |
---|---|---|
targetAddress |
String | Address of the file to load |
options |
Object | options to pass to fetch |
Returns:
- parsed JSON of the response
- Type
- Object
(static) list() → {Array.<String>}
Prints the lists of datasets available
Example
datasets.list();
// [
// 'annual-precip.json',
// 'anscombe.json',
// 'barley.json',
// 'budget.json',
// 'budgets.json',
// 'burtin.json',
// 'cars.json',
// 'countries.json',
// 'crimea.json',
// 'driving.json',
// ... ];
Returns:
- list of dataset ids that can be fetched.
- Type
- Array.<String>
(static) polyfillFetch()
Polyfill for 'global.fetch' if your nodejs instance does not have an implementation (and will only polyfill if so);
Once executed, global.fetch will have an implementation.