From 753cd5f6a6d4d040960b88863ae1d329dd4b5314 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Thu, 24 May 2018 09:01:01 -0700 Subject: [PATCH] Allow naming any dataset. Fixes #3806 --- build/vega-lite-schema.json | 12 ++++++++++-- site/docs/data.md | 4 ++-- src/compile/data/source.ts | 8 ++++++-- src/data.ts | 24 +++++++++++------------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/build/vega-lite-schema.json b/build/vega-lite-schema.json index e3738243e65..bf290750004 100644 --- a/build/vega-lite-schema.json +++ b/build/vega-lite-schema.json @@ -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", @@ -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.", diff --git a/site/docs/data.md b/site/docs/data.md index 4406157d349..fa5031c1069 100644 --- a/site/docs/data.md +++ b/site/docs/data.md @@ -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`). @@ -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. diff --git a/src/compile/data/source.ts b/src/compile/data/source.ts index d2d8088763c..b4c215d0648 100644 --- a/src/compile/data/source.ts +++ b/src/compile/data/source.ts @@ -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; } diff --git a/src/data.ts b/src/data.ts index ea4754c9062..93b15ec6a68 100644 --- a/src/data.ts +++ b/src/data.ts @@ -92,12 +92,18 @@ 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. @@ -105,11 +111,7 @@ export interface UrlData { 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. @@ -117,11 +119,7 @@ export interface InlineData { 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. */ @@ -137,7 +135,7 @@ export function isInlineData(data: Partial | Partial): data is Inl } export function isNamedData(data: Partial): data is NamedData { - return !!data['name']; + return !isUrlData(data) && !isInlineData(data) && !!data[name]; } export type DataSourceType = 'raw' | 'main' | 'row' | 'column' | 'lookup';