Name: vega-loader
Owner: Vega
Description: Network request and file loading utilities.
Created: 2016-07-07 16:42:25.0
Updated: 2016-11-05 15:47:56.0
Pushed: 2017-12-01 00:33:09.0
Homepage: null
Size: 79
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Network request and file loading utilities.
Creates a new loader instance with default options. A loader object provides methods for loading files from the network or disk, and for sanitizing requested URLs and filenames. If provided, the key-value pairs in the options object will be passed as default options to the various loader methods.
The options object can include the following entries:
'file'
(server-side only) or 'http'
. If set to 'file'
mode, later uri parameters
may safely omit a 'file://'
prefix.'//vega.github.io'
). Defaults to 'http'
.# loader.load(uri[, options]) <>
Loads a file from either the network or disk, and returns a Promise for asyncronously accessing the loaded content. This method does not perform any parsing, it simply returns the loaded data as either a Buffer or String instance, depending on the execution environment. To subsequently parse loaded data, use the read method.
The uri argument is a value indicating the file to load. This is typically
either an absolute or relative URL string. If running server-side via node.js,
this argument might also be a file path (e.g., 'file:///path/to/file.txt'
).
If provided, the options argument will be combined with any default options passed to the loader constructor. In the case of identical property names, values from the options argument for this method will be used.
loader = vega.loader();
er.load('data.json').then(function(data) {
do something with loaded data
atch(function(error) {
error handling here
# loader.sanitize(uri, options) <>
URI sanitizer function, which takes a uri and options object as input, and returns a Promise that resolves to a return object that includes a sanitized URL under the href property. This method is used internally by load to ensure the URL is valid and to add additional protocol and hostname information, if needed. This method accepts the same options object accepted by load and returns a Promise. If sanitization is successful, the Promise resolves to a return object containing the URL string as (href), along with a non-enumerable boolean localFile flag, indicating if the file should be loaded from the local filesystem. The Promise rejects if the uri is invalid or disallowed. This method is over-writable for clients who wish to implement custom sanitization.
If provided, the options argument will be combined with any default options passed to the loader constructor. In the case of identical property names, values from the options argument for this method will be used.
# loader.http(url, options) <>
Function used internally by load for servicing HTTP requests. Uses fetch by default. Clients may overwrite this method to perform custom HTTP request handling.
If provided, the options argument may include any valid fetch RequestInit properties. The provided options will be combined with any default options passed to the loader constructor under the http property. In the case of identical property names, values from the options argument for this method will be used.
Function used internally by load for local file system requests. This method is over-writable for clients who wish to implement custom file loading. Uses the node.js fs module by default.
# vega.read(data, schema[, dateParse]) <>
Parse loaded data according to a given format schema. The data argument should be either a String or Buffer instance, typically the result of calling load.
The schema object contents may depend on the data format (see below). Common options include:
json
, csv
, tsv
, or topojson
.'auto'
(the default), the method will perform type
inference (using the inferTypes method) to determine data types
of each field. Alternatively, callers can specify parsing rules by providing
an object mapping field names to data types (for example: {'timestamp':
'date', 'price': 'number'}
). The valid data type options are 'boolean'
,
'integer'
, 'number'
, 'date'
, and 'string'
.The 'date'
data type also accepts an optional format string
('date:format'
). If provided, the optional dateParse function is used to
generate date-time parsers for a date format string. If dateParse is
unspecified, the d3-time-format
library is used by default. Date-time format strings may be quoted
(date:'%A'
), but quoting is not required. In addition, parsing of
date-time format strings to UTC time is supported ('utc:format'
).
ead loaded csv data, automatically infer value types
data = null;
er.load('data/stocks.csv').then(function(data) {
ta = vega.read(csv_data, {type: 'csv', parse: 'auto'});
s
ead loaded csv data, using provided value types
data = null;
er.load('data/stocks.csv').then(function(data) {
ta = vega.read(data, {
type: 'csv',
parse: {'date': 'date', 'price': 'number'}
;
s
ead loaded topojson data, extract mesh of countries
topojson = null;
er.load('data/world-110m.json').then(function(data) {
pojson = vega.read(data, {type: 'topojson', mesh: 'countries'});
# vega.inferType(values[, field]) <>
Given an array of values, infers their data type as one of 'boolean'
,
'integer'
, 'number'
, 'date'
, or 'string'
. An optional field accessor
can be used to first extract values from the input array, and is equivalent to
first calling values.map(field)
.
# vega.inferTypes(data, fields) <>
Given an array of data objects and a list of string-typed field names (fields), infers the data type for each field. Returns an object that maps field names to inferred types, determined using the inferType method.
An object containing a set of parsing functions for converting input values
to a specified data type. All parsing functions return null
if the input
is null
, undefined
or the empty string (''
).
The supported functions are:
Date.parse
is used.# vega.formats(name[, format]) <>
Registry function for data format parsers. If invoked with two arguments, adds a new format parser with the provided name. Otherwise, returns an existing parser with the given name. The method signature of a format parser is:
A format parser that accepts two arguments, the input data to parse (e.g., a block of CSV text) and a set of format-specific options. The following data formats are registered by default:
,
)
delimiter.\t
)
delimiter.