Skip to content

Commit

Permalink
Custom Sort Order (#3423)
Browse files Browse the repository at this point in the history
* reuse calculate node to generate new sort field

* rename the created data source to fieldname_sort_index

* update docs to include examples

fix jekyll error and linting

* remove comments and comment dataflow debug

create examples and fix jekyll error in base.html

* uses vega-util function for array and string

undo jekyll errors

* add custom sort examples

* update documentation

* add unit tests and fix comment per @kanitw

* fix documentation for style issue and clariy

* update schema and fix import error

* fixes rebase issue

* Update sort.md

* remove iterating in parse and instead do for loop in each node, create export function for sort index field name

* update schema
  • Loading branch information
sirahd authored and kanitw committed Mar 29, 2018
1 parent 621a1c7 commit 766e010
Show file tree
Hide file tree
Showing 20 changed files with 886 additions and 49 deletions.
32 changes: 28 additions & 4 deletions build/vega-lite-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,12 @@
},
"sort": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"$ref": "#/definitions/SortOrder"
},
Expand All @@ -977,7 +983,7 @@
"type": "null"
}
],
"description": "Sort order for the encoded field.\nSupported `sort` values include `\"ascending\"`, `\"descending\"` and `null` (no sorting).\nFor fields with discrete domains, `sort` can also be a [sort field definition object](https://vega.github.io/vega-lite/docs/sort.html#sort-field).\n\n__Default value:__ `\"ascending\"`"
"description": "Sort order for the encoded field.\nSupported `sort` values include `\"ascending\"`, `\"descending\"`, `null` (no sorting), or an array specifying the preferred order of values.\nFor fields with discrete domains, `sort` can also be a [sort field definition object](https://vega.github.io/vega-lite/docs/sort.html#sort-field).\nFor `sort` as an [array specifying the preferred order of values](https://vega.github.io/vega-lite/docs/sort.html#sort-array), the sort order will obey the values in the array, followed by any unspecified values in their original order.\n\n__Default value:__ `\"ascending\"`"
},
"test": {
"$ref": "#/definitions/LogicalOperand<Predicate>"
Expand Down Expand Up @@ -1174,6 +1180,12 @@
},
"sort": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"$ref": "#/definitions/SortOrder"
},
Expand All @@ -1184,7 +1196,7 @@
"type": "null"
}
],
"description": "Sort order for the encoded field.\nSupported `sort` values include `\"ascending\"`, `\"descending\"` and `null` (no sorting).\nFor fields with discrete domains, `sort` can also be a [sort field definition object](https://vega.github.io/vega-lite/docs/sort.html#sort-field).\n\n__Default value:__ `\"ascending\"`"
"description": "Sort order for the encoded field.\nSupported `sort` values include `\"ascending\"`, `\"descending\"`, `null` (no sorting), or an array specifying the preferred order of values.\nFor fields with discrete domains, `sort` can also be a [sort field definition object](https://vega.github.io/vega-lite/docs/sort.html#sort-field).\nFor `sort` as an [array specifying the preferred order of values](https://vega.github.io/vega-lite/docs/sort.html#sort-array), the sort order will obey the values in the array, followed by any unspecified values in their original order.\n\n__Default value:__ `\"ascending\"`"
},
"timeUnit": {
"$ref": "#/definitions/TimeUnit",
Expand Down Expand Up @@ -2304,6 +2316,12 @@
},
"sort": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"$ref": "#/definitions/SortOrder"
},
Expand All @@ -2314,7 +2332,7 @@
"type": "null"
}
],
"description": "Sort order for the encoded field.\nSupported `sort` values include `\"ascending\"`, `\"descending\"` and `null` (no sorting).\nFor fields with discrete domains, `sort` can also be a [sort field definition object](https://vega.github.io/vega-lite/docs/sort.html#sort-field).\n\n__Default value:__ `\"ascending\"`"
"description": "Sort order for the encoded field.\nSupported `sort` values include `\"ascending\"`, `\"descending\"`, `null` (no sorting), or an array specifying the preferred order of values.\nFor fields with discrete domains, `sort` can also be a [sort field definition object](https://vega.github.io/vega-lite/docs/sort.html#sort-field).\nFor `sort` as an [array specifying the preferred order of values](https://vega.github.io/vega-lite/docs/sort.html#sort-array), the sort order will obey the values in the array, followed by any unspecified values in their original order.\n\n__Default value:__ `\"ascending\"`"
},
"timeUnit": {
"$ref": "#/definitions/TimeUnit",
Expand Down Expand Up @@ -4353,6 +4371,12 @@
},
"sort": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"$ref": "#/definitions/SortOrder"
},
Expand All @@ -4363,7 +4387,7 @@
"type": "null"
}
],
"description": "Sort order for the encoded field.\nSupported `sort` values include `\"ascending\"`, `\"descending\"` and `null` (no sorting).\nFor fields with discrete domains, `sort` can also be a [sort field definition object](https://vega.github.io/vega-lite/docs/sort.html#sort-field).\n\n__Default value:__ `\"ascending\"`"
"description": "Sort order for the encoded field.\nSupported `sort` values include `\"ascending\"`, `\"descending\"`, `null` (no sorting), or an array specifying the preferred order of values.\nFor fields with discrete domains, `sort` can also be a [sort field definition object](https://vega.github.io/vega-lite/docs/sort.html#sort-field).\nFor `sort` as an [array specifying the preferred order of values](https://vega.github.io/vega-lite/docs/sort.html#sort-array), the sort order will obey the values in the array, followed by any unspecified values in their original order.\n\n__Default value:__ `\"ascending\"`"
},
"stack": {
"anyOf": [
Expand Down
1 change: 1 addition & 0 deletions examples/compiled/bar_custom_sort.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
213 changes: 213 additions & 0 deletions examples/compiled/bar_custom_sort.vg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
{
"$schema": "https://vega.github.io/schema/vega/v3.0.json",
"description": "A simple bar chart with embedded data.",
"autosize": "pad",
"padding": 5,
"height": 200,
"style": "cell",
"data": [
{
"name": "source_0",
"values": [
{
"a": "A",
"b": 28
},
{
"a": "B",
"b": 55
},
{
"a": "C",
"b": 43
},
{
"a": "D",
"b": 91
},
{
"a": "E",
"b": 81
},
{
"a": "F",
"b": 53
},
{
"a": "G",
"b": 19
},
{
"a": "H",
"b": 87
},
{
"a": "I",
"b": 52
}
]
},
{
"name": "data_0",
"source": "source_0",
"transform": [
{
"type": "formula",
"expr": "toNumber(datum[\"b\"])",
"as": "b"
},
{
"type": "formula",
"expr": "datum.a === 'D' ? 0 : datum.a === 'G' ? 1 : datum.a === 'F' ? 2 : 3",
"as": "a_sort_index"
}
]
},
{
"name": "data_1",
"source": "data_0",
"transform": [
{
"type": "filter",
"expr": "datum[\"b\"] !== null && !isNaN(datum[\"b\"])"
}
]
}
],
"signals": [
{
"name": "x_step",
"value": 21
},
{
"name": "width",
"update": "bandspace(domain('x').length, 0.1, 0.05) * x_step"
}
],
"marks": [
{
"name": "marks",
"type": "rect",
"style": [
"bar"
],
"from": {
"data": "data_1"
},
"encode": {
"update": {
"fill": {
"value": "#4c78a8"
},
"x": {
"scale": "x",
"field": "a"
},
"width": {
"scale": "x",
"band": true
},
"y": {
"scale": "y",
"field": "b"
},
"y2": {
"scale": "y",
"value": 0
}
}
}
}
],
"scales": [
{
"name": "x",
"type": "band",
"domain": {
"data": "data_0",
"field": "a",
"sort": {
"op": "min",
"field": "a_sort_index"
}
},
"range": {
"step": {
"signal": "x_step"
}
},
"paddingInner": 0.1,
"paddingOuter": 0.05
},
{
"name": "y",
"type": "linear",
"domain": {
"data": "data_1",
"field": "b"
},
"range": [
{
"signal": "height"
},
0
],
"nice": true,
"zero": true
}
],
"axes": [
{
"scale": "x",
"orient": "bottom",
"labelOverlap": true,
"title": "a",
"zindex": 1,
"encode": {
"labels": {
"update": {
"angle": {
"value": 270
},
"align": {
"value": "right"
},
"baseline": {
"value": "middle"
}
}
}
}
},
{
"scale": "y",
"orient": "left",
"labelOverlap": true,
"tickCount": {
"signal": "ceil(height/40)"
},
"title": "b",
"zindex": 1
},
{
"scale": "y",
"orient": "left",
"domain": false,
"grid": true,
"labels": false,
"maxExtent": 0,
"minExtent": 0,
"tickCount": {
"signal": "ceil(height/40)"
},
"ticks": false,
"zindex": 0,
"gridScale": "x"
}
],
"config": {
"axisY": {
"minExtent": 30
}
}
}
Loading

0 comments on commit 766e010

Please sign in to comment.