Skip to content

Commit

Permalink
Declare ConditionLegend/TextDef, PositionDef to simplify declaration …
Browse files Browse the repository at this point in the history
…of encoding.ts
  • Loading branch information
kanitw committed Jun 8, 2017
1 parent 57d8176 commit e39ec50
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 53 deletions.
79 changes: 35 additions & 44 deletions build/vega-lite-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,15 @@
],
"type": "object"
},
"ConditionalLegendDef<Field,number>": {
"$ref": "#/definitions/Conditional<LegendFieldDef<Field,number>,ValueDef<number>>"
},
"ConditionalLegendDef<Field,string>": {
"$ref": "#/definitions/Conditional<LegendFieldDef<Field,string>,ValueDef<string>>"
},
"ConditionalTextDef": {
"$ref": "#/definitions/Conditional<TextFieldDef,ValueDef<(string|number|boolean)>>"
},
"ConditionalValueDef<LegendFieldDef<Field,number>,ValueDef<number>>": {
"additionalProperties": false,
"description": "A ValueDef with Condition<ValueDef | FieldDef>\n{\n condition: {field: ...} | {value: ...},\n value: ...,\n}",
Expand Down Expand Up @@ -1738,7 +1747,7 @@
"additionalProperties": false,
"properties": {
"color": {
"$ref": "#/definitions/Conditional<LegendFieldDef<Field,string>,ValueDef<string>>",
"$ref": "#/definitions/ConditionalLegendDef<Field,string>",
"description": "Color of the marks – either fill or stroke color based on mark type.\n(By default, fill color for `area`, `bar`, `tick`, `text`, `circle`, and `square` /\nstroke color for `line` and `point`.)"
},
"detail": {
Expand All @@ -1756,7 +1765,7 @@
"description": "Additional levels of detail for grouping data in aggregate views and\nin line and area marks without mapping data to a specific visual channel."
},
"opacity": {
"$ref": "#/definitions/Conditional<LegendFieldDef<Field,number>,ValueDef<number>>",
"$ref": "#/definitions/ConditionalLegendDef<Field,number>",
"description": "Opacity of the marks – either can be a value or a range."
},
"order": {
Expand All @@ -1774,30 +1783,23 @@
"description": "stack order for stacked marks or order of data points in line marks."
},
"shape": {
"$ref": "#/definitions/Conditional<LegendFieldDef<Field,string>,ValueDef<string>>",
"$ref": "#/definitions/ConditionalLegendDef<Field,string>",
"description": "The symbol's shape (only for `point` marks). The supported values are\n`\"circle\"` (default), `\"square\"`, `\"cross\"`, `\"diamond\"`, `\"triangle-up\"`,\nor `\"triangle-down\"`, or else a custom SVG path string."
},
"size": {
"$ref": "#/definitions/Conditional<LegendFieldDef<Field,number>,ValueDef<number>>",
"$ref": "#/definitions/ConditionalLegendDef<Field,number>",
"description": "Size of the mark.\n- For `point`, `square` and `circle`\n– the symbol size, or pixel area of the mark.\n- For `bar` and `tick` – the bar and tick's size.\n- For `text` – the text's font size.\n- Size is currently unsupported for `line` and `area`."
},
"text": {
"$ref": "#/definitions/Conditional<TextFieldDef,ValueDef<(string|number|boolean)>>",
"$ref": "#/definitions/ConditionalTextDef",
"description": "Text of the `text` mark."
},
"tooltip": {
"$ref": "#/definitions/Conditional<TextFieldDef,ValueDef<(string|number|boolean)>>",
"$ref": "#/definitions/ConditionalTextDef",
"description": "The tooltip text to show upon mouse hover."
},
"x": {
"anyOf": [
{
"$ref": "#/definitions/PositionFieldDef"
},
{
"$ref": "#/definitions/ValueDef<number>"
}
],
"$ref": "#/definitions/PositionDef",
"description": "X coordinates for `point`, `circle`, `square`,\n`line`, `rule`, `text`, and `tick`\n(or to width and height for `bar` and `area` marks)."
},
"x2": {
Expand All @@ -1812,14 +1814,7 @@
"description": "X2 coordinates for ranged `bar`, `rule`, `area`."
},
"y": {
"anyOf": [
{
"$ref": "#/definitions/PositionFieldDef"
},
{
"$ref": "#/definitions/ValueDef<number>"
}
],
"$ref": "#/definitions/PositionDef",
"description": "Y coordinates for `point`, `circle`, `square`,\n`line`, `rule`, `text`, and `tick`\n(or to width and height for `bar` and `area` marks)."
},
"y2": {
Expand All @@ -1840,7 +1835,7 @@
"additionalProperties": false,
"properties": {
"color": {
"$ref": "#/definitions/Conditional<LegendFieldDef<Field,string>,ValueDef<string>>",
"$ref": "#/definitions/ConditionalLegendDef<Field,string>",
"description": "Color of the marks – either fill or stroke color based on mark type.\n(By default, fill color for `area`, `bar`, `tick`, `text`, `circle`, and `square` /\nstroke color for `line` and `point`.)"
},
"column": {
Expand All @@ -1862,7 +1857,7 @@
"description": "Additional levels of detail for grouping data in aggregate views and\nin line and area marks without mapping data to a specific visual channel."
},
"opacity": {
"$ref": "#/definitions/Conditional<LegendFieldDef<Field,number>,ValueDef<number>>",
"$ref": "#/definitions/ConditionalLegendDef<Field,number>",
"description": "Opacity of the marks – either can be a value or a range."
},
"order": {
Expand All @@ -1884,30 +1879,23 @@
"description": "Vertical facets for trellis plots."
},
"shape": {
"$ref": "#/definitions/Conditional<LegendFieldDef<Field,string>,ValueDef<string>>",
"$ref": "#/definitions/ConditionalLegendDef<Field,string>",
"description": "The symbol's shape (only for `point` marks). The supported values are\n`\"circle\"` (default), `\"square\"`, `\"cross\"`, `\"diamond\"`, `\"triangle-up\"`,\nor `\"triangle-down\"`, or else a custom SVG path string."
},
"size": {
"$ref": "#/definitions/Conditional<LegendFieldDef<Field,number>,ValueDef<number>>",
"$ref": "#/definitions/ConditionalLegendDef<Field,number>",
"description": "Size of the mark.\n- For `point`, `square` and `circle`\n– the symbol size, or pixel area of the mark.\n- For `bar` and `tick` – the bar and tick's size.\n- For `text` – the text's font size.\n- Size is currently unsupported for `line` and `area`."
},
"text": {
"$ref": "#/definitions/Conditional<TextFieldDef,ValueDef<(string|number|boolean)>>",
"$ref": "#/definitions/ConditionalTextDef",
"description": "Text of the `text` mark."
},
"tooltip": {
"$ref": "#/definitions/Conditional<TextFieldDef,ValueDef<(string|number|boolean)>>",
"$ref": "#/definitions/ConditionalTextDef",
"description": "The tooltip text to show upon mouse hover."
},
"x": {
"anyOf": [
{
"$ref": "#/definitions/PositionFieldDef"
},
{
"$ref": "#/definitions/ValueDef<number>"
}
],
"$ref": "#/definitions/PositionDef",
"description": "X coordinates for `point`, `circle`, `square`,\n`line`, `rule`, `text`, and `tick`\n(or to width and height for `bar` and `area` marks)."
},
"x2": {
Expand All @@ -1922,14 +1910,7 @@
"description": "X2 coordinates for ranged `bar`, `rule`, `area`."
},
"y": {
"anyOf": [
{
"$ref": "#/definitions/PositionFieldDef"
},
{
"$ref": "#/definitions/ValueDef<number>"
}
],
"$ref": "#/definitions/PositionDef",
"description": "Y coordinates for `point`, `circle`, `square`,\n`line`, `rule`, `text`, and `tick`\n(or to width and height for `bar` and `area` marks)."
},
"y2": {
Expand Down Expand Up @@ -3624,6 +3605,16 @@
],
"minimum": 0
},
"PositionDef": {
"anyOf": [
{
"$ref": "#/definitions/PositionFieldDef"
},
{
"$ref": "#/definitions/ValueDef<number>"
}
]
},
"PositionFieldDef": {
"additionalProperties": false,
"properties": {
Expand Down
18 changes: 9 additions & 9 deletions src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
TextFieldDef,
ValueDef
} from './fielddef';
import {Conditional, normalizeFieldDef} from './fielddef';
import {Conditional, ConditionalLegendDef, ConditionalTextDef, normalizeFieldDef, PositionDef} from './fielddef';
import * as log from './log';
import {Mark} from './mark';
import {isArray, some} from './util';
Expand All @@ -30,14 +30,14 @@ export interface Encoding<F> {
* `line`, `rule`, `text`, and `tick`
* (or to width and height for `bar` and `area` marks).
*/
x?: PositionFieldDef<F> | ValueDef<number>;
x?: PositionDef<F>;

/**
* Y coordinates for `point`, `circle`, `square`,
* `line`, `rule`, `text`, and `tick`
* (or to width and height for `bar` and `area` marks).
*/
y?: PositionFieldDef<F> | ValueDef<number>;
y?: PositionDef<F>;

/**
* X2 coordinates for ranged `bar`, `rule`, `area`.
Expand All @@ -56,12 +56,12 @@ export interface Encoding<F> {
* (By default, fill color for `area`, `bar`, `tick`, `text`, `circle`, and `square` /
* stroke color for `line` and `point`.)
*/
color?: Conditional<LegendFieldDef<F, string>, ValueDef<string>>;
color?: ConditionalLegendDef<F, string>;

/**
* Opacity of the marks – either can be a value or a range.
*/
opacity?: Conditional<LegendFieldDef<F, number>, ValueDef<number>>;
opacity?: ConditionalLegendDef<F, number>;

/**
* Size of the mark.
Expand All @@ -71,14 +71,14 @@ export interface Encoding<F> {
* - For `text` – the text's font size.
* - Size is currently unsupported for `line` and `area`.
*/
size?: Conditional<LegendFieldDef<F, number>, ValueDef<number>>;
size?: ConditionalLegendDef<F, number>;

/**
* The symbol's shape (only for `point` marks). The supported values are
* `"circle"` (default), `"square"`, `"cross"`, `"diamond"`, `"triangle-up"`,
* or `"triangle-down"`, or else a custom SVG path string.
*/
shape?: Conditional<LegendFieldDef<F, string>, ValueDef<string>>; // TODO: maybe distinguish ordinal-only
shape?: ConditionalLegendDef<F, string>; // TODO: maybe distinguish ordinal-only

/**
* Additional levels of detail for grouping data in aggregate views and
Expand All @@ -89,12 +89,12 @@ export interface Encoding<F> {
/**
* Text of the `text` mark.
*/
text?: Conditional<TextFieldDef<F>, ValueDef<string|number|boolean>>;
text?: ConditionalTextDef<F>;

/**
* The tooltip text to show upon mouse hover.
*/
tooltip?: Conditional<TextFieldDef<F>, ValueDef<string|number|boolean>>;
tooltip?: ConditionalTextDef<F>;

/**
* stack order for stacked marks or order of data points in line marks.
Expand Down
7 changes: 7 additions & 0 deletions src/fielddef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,18 @@ export interface PositionFieldDef<F> extends ScaleFieldDef<F> {
*/
stack?: StackOffset;
}

export type PositionDef<F> = PositionFieldDef<F> | ValueDef<number>;

export interface LegendFieldDef<F, T> extends ScaleFieldDef<F> {
/**
* @nullable
*/
legend?: Legend;
}

export type ConditionalLegendDef<F, T> = Conditional<LegendFieldDef<F, T>, ValueDef<T>>;

// Detail

// Order Path have no scale
Expand All @@ -166,6 +171,8 @@ export interface TextFieldDef<F> extends FieldDef<F> {
format?: string;
}

export type ConditionalTextDef<F> = Conditional<TextFieldDef<F>, ValueDef<string|number|boolean>>;

export type ChannelDef<F> = FieldDef<F> | ValueDef<any>;

export function isFieldDef<F>(channelDef: ChannelDef<F>): channelDef is FieldDef<F> | PositionFieldDef<F> | LegendFieldDef<F, any> | OrderFieldDef<F> | TextFieldDef<F> {
Expand Down

0 comments on commit e39ec50

Please sign in to comment.