Skip to content

Commit

Permalink
Allow naming any dataset. Fixes #3806
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed May 24, 2018
1 parent e8c0c87 commit 753cd5f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
12 changes: 10 additions & 2 deletions build/vega-lite-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3380,7 +3380,11 @@
"properties": {
"format": {
"$ref": "#/definitions/DataFormat",
"description": "An object that specifies the format for parsing the data values."
"description": "An object that specifies the format for parsing the data."
},
"name": {
"description": "Provide a placeholder name and bind data at runtime.",
"type": "string"
},
"values": {
"$ref": "#/definitions/InlineDataset",
Expand Down Expand Up @@ -7337,7 +7341,11 @@
"properties": {
"format": {
"$ref": "#/definitions/DataFormat",
"description": "An object that specifies the format for parsing the data file."
"description": "An object that specifies the format for parsing the data."
},
"name": {
"description": "Provide a placeholder name and bind data at runtime.",
"type": "string"
},
"url": {
"description": "An URL from which to load the data set. Use the `format.type` property\nto ensure the loaded data is correctly parsed.",
Expand Down
4 changes: 2 additions & 2 deletions site/docs/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Vega-Lite's `data` property describes the visualization's data source as part of
Inline Data can be specified using `values` property.
Here is a list of all properties of an inline `data` source:

{% include table.html props="values,format" source="InlineData" %}
{% include table.html props="values,name,format" source="InlineData" %}

For example, the following specification embeds an inline data table with nine rows and two columns (`a` and `b`).

Expand All @@ -46,7 +46,7 @@ Data can be loaded from a URL using the `url` property. In addition, the format

Here is a list of all properties describing a `data` source from URL:

{% include table.html props="url,format" source="UrlData" %}
{% include table.html props="url,name,format" source="UrlData" %}

For example, the following specification loads data from a relative `url`: `data/cars.json`. Note that the format type is implicitly `"json"` by default.

Expand Down
8 changes: 6 additions & 2 deletions src/compile/data/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ export class SourceNode extends DataFlowNode {
data.format.type = defaultExtension as DataFormatType;
}
} else if (isNamedData(data)) {
this._name = data.name;
this._data = {};
}

if (!isNamedData(data) && data.format) {
// any dataset can be named
if (data.name) {
this._name = data.name;
}

if (data.format) {
const {parse = null, ...format} = data.format;
this._data.format = format;
}
Expand Down
24 changes: 11 additions & 13 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,34 @@ export type Data = UrlData | InlineData | NamedData;

export type InlineDataset = number[] | string[] | boolean[] | object[] | string | object;

export interface UrlData {
export interface DataBase {
/**
* An object that specifies the format for parsing the data file.
* An object that specifies the format for parsing the data.
*/
format?: DataFormat;
/**
* Provide a placeholder name and bind data at runtime.
*/
name?: string;
}

export interface UrlData extends DataBase {
/**
* An URL from which to load the data set. Use the `format.type` property
* to ensure the loaded data is correctly parsed.
*/
url: string;
}

export interface InlineData {
/**
* An object that specifies the format for parsing the data values.
*/
format?: DataFormat;
export interface InlineData extends DataBase {
/**
* The full data set, included inline. This can be an array of objects or primitive values, an object, or a string.
* Arrays of primitive values are ingested as objects with a `data` property. Strings are parsed according to the specified format type.
*/
values: InlineDataset;
}

export interface NamedData {
/**
* An object that specifies the format for parsing the data.
*/
format?: DataFormat;
export interface NamedData extends DataBase {
/**
* Provide a placeholder name and bind data at runtime.
*/
Expand All @@ -137,7 +135,7 @@ export function isInlineData(data: Partial<Data> | Partial<VgData>): data is Inl
}

export function isNamedData(data: Partial<Data>): data is NamedData {
return !!data['name'];
return !isUrlData(data) && !isInlineData(data) && !!data[name];
}

export type DataSourceType = 'raw' | 'main' | 'row' | 'column' | 'lookup';
Expand Down

0 comments on commit 753cd5f

Please sign in to comment.