vega-datasets
Version:
Common repository for example datasets used by Vega related projects.
293 lines (258 loc) • 13.5 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.vegaDatasets = factory());
}(this, function () { 'use strict';
var urls = {
'anscombe.json': 'https://vega.github.io/vega-datasets/data/anscombe.json',
'barley.json': 'https://vega.github.io/vega-datasets/data/barley.json',
'birdstrikes.json': 'https://vega.github.io/vega-datasets/data/birdstrikes.json',
'budget.json': 'https://vega.github.io/vega-datasets/data/budget.json',
'budgets.json': 'https://vega.github.io/vega-datasets/data/budgets.json',
'burtin.json': 'https://vega.github.io/vega-datasets/data/burtin.json',
'cars.json': 'https://vega.github.io/vega-datasets/data/cars.json',
'climate.json': 'https://vega.github.io/vega-datasets/data/climate.json',
'countries.json': 'https://vega.github.io/vega-datasets/data/countries.json',
'crimea.json': 'https://vega.github.io/vega-datasets/data/crimea.json',
'driving.json': 'https://vega.github.io/vega-datasets/data/driving.json',
'earthquakes.json': 'https://vega.github.io/vega-datasets/data/earthquakes.json',
'flare-dependencies.json': 'https://vega.github.io/vega-datasets/data/flare-dependencies.json',
'flare.json': 'https://vega.github.io/vega-datasets/data/flare.json',
'flights-10k.json': 'https://vega.github.io/vega-datasets/data/flights-10k.json',
'flights-200k.json': 'https://vega.github.io/vega-datasets/data/flights-200k.json',
'flights-20k.json': 'https://vega.github.io/vega-datasets/data/flights-20k.json',
'flights-2k.json': 'https://vega.github.io/vega-datasets/data/flights-2k.json',
'flights-5k.json': 'https://vega.github.io/vega-datasets/data/flights-5k.json',
'gapminder.json': 'https://vega.github.io/vega-datasets/data/gapminder.json',
'graticule.json': 'https://vega.github.io/vega-datasets/data/graticule.json',
'income.json': 'https://vega.github.io/vega-datasets/data/income.json',
'iris.json': 'https://vega.github.io/vega-datasets/data/iris.json',
'jobs.json': 'https://vega.github.io/vega-datasets/data/jobs.json',
'londonBoroughs.json': 'https://vega.github.io/vega-datasets/data/londonBoroughs.json',
'londonCentroids.json': 'https://vega.github.io/vega-datasets/data/londonCentroids.json',
'londonTubeLines.json': 'https://vega.github.io/vega-datasets/data/londonTubeLines.json',
'miserables.json': 'https://vega.github.io/vega-datasets/data/miserables.json',
'monarchs.json': 'https://vega.github.io/vega-datasets/data/monarchs.json',
'movies.json': 'https://vega.github.io/vega-datasets/data/movies.json',
'normal-2d.json': 'https://vega.github.io/vega-datasets/data/normal-2d.json',
'obesity.json': 'https://vega.github.io/vega-datasets/data/obesity.json',
'points.json': 'https://vega.github.io/vega-datasets/data/points.json',
'population.json': 'https://vega.github.io/vega-datasets/data/population.json',
'udistrict.json': 'https://vega.github.io/vega-datasets/data/udistrict.json',
'unemployment-across-industries.json': 'https://vega.github.io/vega-datasets/data/unemployment-across-industries.json',
'us-10m.json': 'https://vega.github.io/vega-datasets/data/us-10m.json',
'us-state-capitals.json': 'https://vega.github.io/vega-datasets/data/us-state-capitals.json',
'weather.json': 'https://vega.github.io/vega-datasets/data/weather.json',
'weball26.json': 'https://vega.github.io/vega-datasets/data/weball26.json',
'wheat.json': 'https://vega.github.io/vega-datasets/data/wheat.json',
'world-110m.json': 'https://vega.github.io/vega-datasets/data/world-110m.json',
'airports.csv': 'https://vega.github.io/vega-datasets/data/airports.csv',
'co2-concentration.csv': 'https://vega.github.io/vega-datasets/data/co2-concentration.csv',
'disasters.csv': 'https://vega.github.io/vega-datasets/data/disasters.csv',
'flights-3m.csv': 'https://vega.github.io/vega-datasets/data/flights-3m.csv',
'flights-airport.csv': 'https://vega.github.io/vega-datasets/data/flights-airport.csv',
'gapminder-health-income.csv': 'https://vega.github.io/vega-datasets/data/gapminder-health-income.csv',
'github.csv': 'https://vega.github.io/vega-datasets/data/github.csv',
'iowa-electricity.csv': 'https://vega.github.io/vega-datasets/data/iowa-electricity.csv',
'la-riots.csv': 'https://vega.github.io/vega-datasets/data/la-riots.csv',
'lookup_groups.csv': 'https://vega.github.io/vega-datasets/data/lookup_groups.csv',
'lookup_people.csv': 'https://vega.github.io/vega-datasets/data/lookup_people.csv',
'population_engineers_hurricanes.csv': 'https://vega.github.io/vega-datasets/data/population_engineers_hurricanes.csv',
'seattle-temps.csv': 'https://vega.github.io/vega-datasets/data/seattle-temps.csv',
'seattle-weather.csv': 'https://vega.github.io/vega-datasets/data/seattle-weather.csv',
'sf-temps.csv': 'https://vega.github.io/vega-datasets/data/sf-temps.csv',
'sp500.csv': 'https://vega.github.io/vega-datasets/data/sp500.csv',
'stocks.csv': 'https://vega.github.io/vega-datasets/data/stocks.csv',
'us-employment.csv': 'https://vega.github.io/vega-datasets/data/us-employment.csv',
'weather.csv': 'https://vega.github.io/vega-datasets/data/weather.csv',
'windvectors.csv': 'https://vega.github.io/vega-datasets/data/windvectors.csv',
'zipcodes.csv': 'https://vega.github.io/vega-datasets/data/zipcodes.csv',
'unemployment.tsv': 'https://vega.github.io/vega-datasets/data/unemployment.tsv',
'flights-200k.arrow': 'https://vega.github.io/vega-datasets/data/flights-200k.arrow',
};
var EOL = {},
EOF = {},
QUOTE = 34,
NEWLINE = 10,
RETURN = 13;
function objectConverter(columns) {
return new Function("d", "return {" + columns.map(function(name, i) {
return JSON.stringify(name) + ": d[" + i + "]";
}).join(",") + "}");
}
function customConverter(columns, f) {
var object = objectConverter(columns);
return function(row, i) {
return f(object(row), i, columns);
};
}
// Compute unique columns in order of discovery.
function inferColumns(rows) {
var columnSet = Object.create(null),
columns = [];
rows.forEach(function(row) {
for (var column in row) {
if (!(column in columnSet)) {
columns.push(columnSet[column] = column);
}
}
});
return columns;
}
function pad(value, width) {
var s = value + "", length = s.length;
return length < width ? new Array(width - length + 1).join(0) + s : s;
}
function formatYear(year) {
return year < 0 ? "-" + pad(-year, 6)
: year > 9999 ? "+" + pad(year, 6)
: pad(year, 4);
}
function formatDate(date) {
var hours = date.getUTCHours(),
minutes = date.getUTCMinutes(),
seconds = date.getUTCSeconds(),
milliseconds = date.getUTCMilliseconds();
return isNaN(date) ? "Invalid Date"
: formatYear(date.getUTCFullYear(), 4) + "-" + pad(date.getUTCMonth() + 1, 2) + "-" + pad(date.getUTCDate(), 2)
+ (milliseconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "." + pad(milliseconds, 3) + "Z"
: seconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "Z"
: minutes || hours ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + "Z"
: "");
}
function dsv(delimiter) {
var reFormat = new RegExp("[\"" + delimiter + "\n\r]"),
DELIMITER = delimiter.charCodeAt(0);
function parse(text, f) {
var convert, columns, rows = parseRows(text, function(row, i) {
if (convert) return convert(row, i - 1);
columns = row, convert = f ? customConverter(row, f) : objectConverter(row);
});
rows.columns = columns || [];
return rows;
}
function parseRows(text, f) {
var rows = [], // output rows
N = text.length,
I = 0, // current character index
n = 0, // current line number
t, // current token
eof = N <= 0, // current token followed by EOF?
eol = false; // current token followed by EOL?
// Strip the trailing newline.
if (text.charCodeAt(N - 1) === NEWLINE) --N;
if (text.charCodeAt(N - 1) === RETURN) --N;
function token() {
if (eof) return EOF;
if (eol) return eol = false, EOL;
// Unescape quotes.
var i, j = I, c;
if (text.charCodeAt(j) === QUOTE) {
while (I++ < N && text.charCodeAt(I) !== QUOTE || text.charCodeAt(++I) === QUOTE);
if ((i = I) >= N) eof = true;
else if ((c = text.charCodeAt(I++)) === NEWLINE) eol = true;
else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
return text.slice(j + 1, i - 1).replace(/""/g, "\"");
}
// Find next delimiter or newline.
while (I < N) {
if ((c = text.charCodeAt(i = I++)) === NEWLINE) eol = true;
else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
else if (c !== DELIMITER) continue;
return text.slice(j, i);
}
// Return last token before EOF.
return eof = true, text.slice(j, N);
}
while ((t = token()) !== EOF) {
var row = [];
while (t !== EOL && t !== EOF) row.push(t), t = token();
if (f && (row = f(row, n++)) == null) continue;
rows.push(row);
}
return rows;
}
function preformatBody(rows, columns) {
return rows.map(function(row) {
return columns.map(function(column) {
return formatValue(row[column]);
}).join(delimiter);
});
}
function format(rows, columns) {
if (columns == null) columns = inferColumns(rows);
return [columns.map(formatValue).join(delimiter)].concat(preformatBody(rows, columns)).join("\n");
}
function formatBody(rows, columns) {
if (columns == null) columns = inferColumns(rows);
return preformatBody(rows, columns).join("\n");
}
function formatRows(rows) {
return rows.map(formatRow).join("\n");
}
function formatRow(row) {
return row.map(formatValue).join(delimiter);
}
function formatValue(value) {
return value == null ? ""
: value instanceof Date ? formatDate(value)
: reFormat.test(value += "") ? "\"" + value.replace(/"/g, "\"\"") + "\""
: value;
}
return {
parse: parse,
parseRows: parseRows,
format: format,
formatBody: formatBody,
formatRows: formatRows
};
}
var csv = dsv(",");
var csvParse = csv.parse;
var tsv = dsv("\t");
function autoType(object) {
for (var key in object) {
var value = object[key].trim(), number;
if (!value) value = null;
else if (value === "true") value = true;
else if (value === "false") value = false;
else if (value === "NaN") value = NaN;
else if (!isNaN(number = +value)) value = number;
else if (/^([-+]\d{2})?\d{4}(-\d{2}(-\d{2})?)?(T\d{2}:\d{2}(:\d{2}(\.\d{3})?)?(Z|[-+]\d{2}:\d{2})?)?$/.test(value)) value = new Date(value);
else continue;
object[key] = value;
}
return object;
}
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const data = {};
for (const name of Object.keys(urls)) {
const url = urls[name];
const f = function () {
return __awaiter(this, void 0, void 0, function* () {
const result = yield fetch(url);
if (name.endsWith('.json')) {
return yield result.json();
}
else if (name.endsWith('.csv')) {
// TODO: remove "as any" once @types/d3-dsv has been updated
return csvParse(yield result.text(), autoType);
}
else {
return yield result.text();
}
});
};
f.url = url;
data[name] = f;
}
return data;
}));
//# sourceMappingURL=vega-datasets.js.map