Skip to main content

Importing Data

The data block defines where data is to be loaded form, and how it will be loaded and transformed.

The specification of data loading in NetPanorama is very similar to that in Vega, and NetPanorama reuses the vega-loader library (GitHub, npm). Like Vega (and unlike Vega-lite), the data block contains a list of named data sources, rather than a single data source.

Data can be loaded from JSON or character-delimited files (CSV, TSC, DSV). It can then be processed by applying a transformation.

In some cases, a visualization may rely on the several different transformations of the same dataset. To reduce repetition (and prevent the data from being fetched multiple times), you can instead provide the name of an existing dataset as the source of a dataset.

caution

Whilst the API is very similar to Vega, there are differences in what transformations are supported. Additionally, loading data from LocalStorage is a feature specific to NetPanorama.

Limitation

It is not currently possible to load data dynamically at runtime through a view API.

Examples

Data defined inline:

{ "name": "table", "values": [12, 23, 47, 6, 52, 19] }

Data loaded from a URL:

{ "name": "points", "url": "data/points.json" }

Loading a dataset that was saved to LocalStorage using the Upload Data button in the editor:

{ "name": "networkData", "localStorage": "networkData" }

Importing a CSV file, and parsing one column as a Date:

{
"name": "marieboucher",
"url": "./data/marieboucher.csv",
"format": {"type": "csv", "parse": {"Date": "date:'%d/%m/%Y'"}}
}

Creating a new derived dataset from an existing dataset:

{
"name": "stats",
"source": "table",
"transform": [
{
"type": "aggregate",
"groupby": ["x"],
"ops": ["average", "sum", "min", "max"],
"fields": ["y", "y", "y", "y"]
}
]
}

Transformations

When defining a data source, the transformations attribute can be used to define a list of transformations that should be applied to the data.

It is also possible to make a transformed copy of dataset by defining a new dataset, with source set to the name of the existing dataset. For example,

    {
"name": "roles",
"source": "credits",
"transform": [
{
"type": "unroll",
"fields": [
"cast",
"crew"
]
}
]
}
caution

Transformations that require understanding of the network structure of a dataset (i.e., that an edge connects a source and target nodes), rather than treating the data as simply a table, are instead defined in the network block.

Adding columns or fields

Add Index

Add a sequential id field to each element in a data array.

AttributeTypeDescription
type"addIndex "
as?stringThe name of the field in which to record the id.

Lookup

Extend the elements in a (primary) data array by looking up additional values in another (secondary) data array.

AttributeTypeDescription
type"lookup"
fromstringThe name of the data array to join with.
keystringThe field in the secondary array containing the key that will be used for matching.
fieldsstring[]The fields in the primary data array that will be matched against the key in the secondary array.
valuesstring[]The fields on the secondary data array hose values should be copied.
as?string[]The name of the fields on the primary data array in which to store the looked-up values.
defaultanyThe default value to use if lookup fails.

Geocode

Geocode locations, by finding a latitude and longitude from a place name.

AttributeTypeDescription
type"geocode"
locationField?stringThe name of a field containing a location name.
streetField?stringThe name of a field containing a street name.
cityField?stringThe name of a field containing a city name.
stateField?stringThe name of a field containing a state name.
countryField?stringThe name of a field containing a country name.
postalCodeField?stringThe name of a field containing a postal code.
lonAs?stringThe name of a field in which the longitude should be saved.
latAs?stringThe name of a field in which the latitude should be saved.
nameAs?stringIf specified, the displayName of the geocoded location will be recorded as a field of this name.This can be helpful in identifying where the geocoder has picked the wrong location for an ambiguous place name.

Geocoding uses the API provided by https://geocode.maps.co, which is based on OpenStreetMap and Nominatim. The results of queries are saved in the browser's LocalStorage, so that identical queries are not made repeatedly.

Calculate

Evaluate an expression, and record the resulting value as a field.

AttributeTypeDescription
type"calculate"
calculatestringThe expression to evaluate.
asstringThe name of the field in which to record the calculated value.

Transforming values

Bin values

Group continuous numerical data into discrete bins.

AttributeTypeDescription
type"bin"
fieldstringThe field to be binned.

Similar to Vega-Lite's Bin transform

Removing rows

Filter

Filter a list of data items, keeping only those for which the specified expression evaluates to true.

AttributeTypeDescription
type"filter"
exprstringThe expression to evaluate for each data item.

Sample

Select a random subset of a data array.

AttributeTypeDescription
type"sample"
samplenumberThe number of data elements to keep.

N.B., a similar thing could be achieved by using a filter transformation with an expression like random() < 0.5, but the exact number of items that this would produce is not guaranteed.

Reshaping

Aggregate

Aggregate rows of the data table that have the same values in one (or multiple) columns.

AttributeTypeDescription
type"aggregation"
groupby?string[]The data fields to group by.
fields?string[]The data fields for which to compute aggregate functions.
ops?AggregationFunc[],The aggregation functions to apply.
as?string,The field names to use for the computed aggregate fields.

Similar to Vega's Aggregate transform.

Flatten

Maps array-valued fields to a set of individual data objects, one per array entry.

AttributeTypeDescription
type"flatten"
flattenstring[]Array containing the names of fields to flattten.

For example, flattening on the fields ["foo", "bar"] will transform the input data:

[
{
"key": "alpha",
"foo": [
1,
2
],
"bar": [
"A",
"B"
]
},
{
"key": "beta",
"foo": [
3,
4,
5
],
"bar": [
"C",
"D"
]
}
]

into:

[
{
"key": "alpha",
"foo": 1,
"bar": "A"
},
{
"key": "alpha",
"foo": 2,
"bar": "B"
},
{
"key": "beta",
"foo": 3,
"bar": "C"
},
{
"key": "beta",
"foo": 4,
"bar": "D"
},
{
"key": "beta",
"foo": 5,
"bar": undefined
}
]

Fold

Collapses one or more data fields into a key and value.

AttributeTypeDescription
type"fold"
fieldsstring[]Array containing the name of fields to fold.

For example, flattening on the fields ["centrality", "degree"] will transform the input:

[
{
id: 0,
centrality: 2,
degree: 1
},
{
id: 1,
centrality: 5,
degree: 6
}
]

into:

[
{
field: 'centrality',
value: 2,
id: 0,
centrality: 2,
degree: 1
},
{
field: 'centrality',
value: 5,
id: 1,
centrality: 5,
degree: 6
},
{
field: 'degree',
value: 1,
id: 0,
centrality: 2,
degree: 1
},
{
field: 'degree',
value: 6,
id: 1,
centrality: 5,
degree: 6
}
]

Pivot

Maps unique values from a field to new aggregated fields.

AttributeTypeDescription
type"pivot"
pivotstringThe name of the field to pivot on; the unique values of this field become new field names in the output.
valuestringThe name of the field to populate pivoted fields; the aggregate values of this field become the values of the new pivoted fields.
groupbystring[]Names of (optional) fields to group by.
opstringThe aggregation operation to apply.

For example, the transformation:

{
"type": "pivot",
"pivot": "type",
"groupby": [
"country"
],
"value": "count"
}

will transform the input data:

[
{
"country": "Norway",
"type": "gold",
"count": 14
},
{
"country": "Norway",
"type": "silver",
"count": 14
},
{
"country": "Norway",
"type": "bronze",
"count": 11
},
{
"country": "Germany",
"type": "gold",
"count": 14
},
{
"country": "Germany",
"type": "silver",
"count": 10
},
{
"country": "Germany",
"type": "bronze",
"count": 7
},
{
"country": "Canada",
"type": "gold",
"count": 11
},
{
"country": "Canada",
"type": "silver",
"count": 8
},
{
"country": "Canada",
"type": "bronze",
"count": 10
}
]

into:

[
{
"country": "Norway",
"gold": 14,
"silver": 14,
"bronze": 11
},
{
"country": "Germany",
"gold": 14,
"silver": 10,
"bronze": 7
},
{
"country": "Canada",
"gold": 11,
"silver": 8,
"bronze": 10
}
]

(this example is borrowed from the Vega-Lite documentation).