Skip to content

Commit

Permalink
added #4 built-in vega examples
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomFractals committed Feb 11, 2019
1 parent 37bacfa commit 1ffa03b
Show file tree
Hide file tree
Showing 138 changed files with 447,991 additions and 17 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ in [VSCode](https://github.com/Microsoft/vscode)
- Vega and Vega-Lite charts Preview
- Local data files support
- SVG and PNG export
- Built-in Vega Examples

# Usage

1. Run `Vega: Create Vega document` command from `View -> Command Pallette...` menu
1. Run `Vega: Create Vega 📊` command from `View -> Command Pallette...` menu
to create and save new Vega or Vega-Lite document with the corresponding Vega JSON schema reference.

2. Run `Vega: Preview` command from `View -> Command Pallette...` menu
on open `.vega`, `.vg.json` or `.vl.json` Vega spec document to Preview it.
2. Run `Vega: Preview 📊` on open `.vega`, `.vg.json` or `.vl.json` Vega spec document to Preview it.

3. Save updated Vega spec JSON document to Preview updated Vega viz.

OR

Run `Vega: Examples 📊` command to view the list of built-in Vega specification examples.

## Example: [Vega Contour Plot Preview](https://vega.github.io/vega/examples/contour-plot/)

![Alt text](https://github.com/RandomFractals/vscode-vega-viewer/blob/master/images/vega-viewer-contour.png?raw=true
Expand Down
226 changes: 226 additions & 0 deletions examples/airport-connections.vg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
{
"$schema": "https://vega.github.io/schema/vega/v4.json",
"width": 900,
"height": 560,
"padding": {"top": 25, "left": 0, "right": 0, "bottom": 0},
"autosize": "none",

"signals": [
{
"name": "scale", "value": 1200,
"bind": {"input": "range", "min": 500, "max": 3000}
},
{
"name": "translateX", "value": 450,
"bind": {"input": "range", "min": -500, "max": 1200}
},
{
"name": "translateY", "value": 260,
"bind": {"input": "range", "min": -300, "max": 700}
},
{
"name": "shape", "value": "line",
"bind": {"input": "radio", "options": ["line", "curve"]}
},
{
"name": "hover",
"value": null,
"on": [
{"events": "@cell:mouseover", "update": "datum"},
{"events": "@cell:mouseout", "update": "null"}
]
},
{
"name": "title",
"value": "U.S. Airports, 2008",
"update": "hover ? hover.name + ' (' + hover.iata + ')' : 'U.S. Airports, 2008'"
},
{
"name": "cell_stroke",
"value": null,
"on": [
{"events": "dblclick", "update": "cell_stroke ? null : 'brown'"},
{"events": "mousedown!", "update": "cell_stroke"}
]
}
],

"data": [
{
"name": "states",
"url": "data/us-10m.json",
"format": {"type": "topojson", "feature": "states"},
"transform": [
{
"type": "geopath",
"projection": "projection"
}
]
},
{
"name": "traffic",
"url": "data/flights-airport.csv",
"format": {"type": "csv", "parse": "auto"},
"transform": [
{
"type": "aggregate",
"groupby": ["origin"],
"fields": ["count"], "ops": ["sum"], "as": ["flights"]
}
]
},
{
"name": "airports",
"url": "data/airports.csv",
"format": {"type": "csv","parse": "auto"
},
"transform": [
{
"type": "lookup",
"from": "traffic", "key": "origin",
"fields": ["iata"], "as": ["traffic"]
},
{
"type": "filter",
"expr": "datum.traffic != null"
},
{
"type": "geopoint",
"projection": "projection",
"fields": ["longitude", "latitude"]
},
{
"type": "filter",
"expr": "datum.x != null && datum.y != null"
},
{
"type": "voronoi", "x": "x", "y": "y"
},
{
"type": "collect", "sort": {
"field": "traffic.flights",
"order": "descending"
}
}
]
},
{
"name": "routes",
"url": "data/flights-airport.csv",
"format": {"type": "csv", "parse": "auto"},
"transform": [
{
"type": "filter",
"expr": "hover && hover.iata == datum.origin"
},
{
"type": "lookup",
"from": "airports", "key": "iata",
"fields": ["origin", "destination"], "as": ["source", "target"]
},
{
"type": "filter",
"expr": "datum.source && datum.target"
},
{
"type": "linkpath",
"shape": {"signal": "shape"}
}
]
}
],

"projections": [
{
"name": "projection",
"type": "albersUsa",
"scale": {"signal": "scale"},
"translate": [{"signal": "translateX"}, {"signal": "translateY"}]
}
],

"scales": [
{
"name": "size",
"type": "linear",
"domain": {"data": "traffic", "field": "flights"},
"range": [16, 1000]
}
],

"marks": [
{
"type": "path",
"from": {"data": "states"},
"encode": {
"enter": {
"fill": {"value": "#dedede"},
"stroke": {"value": "white"}
},
"update": {
"path": {"field": "path"}
}
}
},
{
"type": "symbol",
"from": {"data": "airports"},
"encode": {
"enter": {
"size": {"scale": "size", "field": "traffic.flights"},
"fill": {"value": "steelblue"},
"fillOpacity": {"value": 0.8},
"stroke": {"value": "white"},
"strokeWidth": {"value": 1.5}
},
"update": {
"x": {"field": "x"},
"y": {"field": "y"}
}
}
},
{
"type": "path",
"name": "cell",
"from": {"data": "airports"},
"encode": {
"enter": {
"fill": {"value": "transparent"},
"strokeWidth": {"value": 0.35}
},
"update": {
"path": {"field": "path"},
"stroke": {"signal": "cell_stroke"}
}
}
},
{
"type": "path",
"interactive": false,
"from": {"data": "routes"},
"encode": {
"enter": {
"path": {"field": "path"},
"stroke": {"value": "black"},
"strokeOpacity": {"value": 0.35}
}
}
},
{
"type": "text",
"interactive": false,
"encode": {
"enter": {
"x": {"value": 895},
"y": {"value": 0},
"fill": {"value": "black"},
"fontSize": {"value": 20},
"align": {"value": "right"}
},
"update": {
"text": {"signal": "title"}
}
}
}
]
}
105 changes: 105 additions & 0 deletions examples/annual-temperature.vg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"$schema": "https://vega.github.io/schema/vega/v4.json",
"width": 800,
"padding": 5,

"config": {
"title": {"fontSize": 14}
},

"title": {
"text": "Seattle Annual Temperatures",
"anchor": "start", "offset": 4
},

"signals": [
{"name": "rangeStep", "value": 25},
{"name": "height", "update": "rangeStep * 24"}
],

"data": [
{
"name": "temperature",
"url": "data/seattle-temps.csv",
"format": {"type": "csv", "parse": {"temp": "number", "date": "date"}},
"transform": [
{"type": "formula", "as": "hour", "expr": "hours(datum.date)"},
{ "type": "formula", "as": "date",
"expr": "datetime(year(datum.date), month(datum.date), date(datum.date))"}
]
}
],

"scales": [
{
"name": "row",
"type": "band",
"domain": [
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
0, 1, 2, 3, 4, 5
],
"range": {"step": {"signal": "rangeStep"}}
},
{
"name": "x",
"type": "time",
"domain": {"data": "temperature", "field": "date"},
"range": "width"
},
{
"name": "y",
"type": "linear", "zero": false,
"domain": {"data": "temperature", "field": "temp"},
"range": [{"signal": "rangeStep"}, 1]
}
],

"axes": [
{"orient": "bottom", "scale": "x", "domain": false, "title": "Month", "format": "%b"},
{
"orient": "left", "scale": "row", "domain": false, "title": "Hour",
"tickSize": 0,
"encode": {
"labels": {
"update": {
"text": {"signal": "datum.value === 0 ? 'Midnight' : datum.value === 12 ? 'Noon' : datum.value < 12 ? datum.value + ':00 am' : (datum.value - 12) + ':00 pm'"}
}
}
}
}
],

"marks": [
{
"type": "group",
"from": {
"facet": {
"name": "hour",
"data": "temperature",
"groupby": "hour"
}
},
"encode": {
"enter": {
"x": {"value": 0},
"y": {"scale": "row", "field": "hour"},
"width": {"signal": "width"},
"height": {"signal": "rangeStep"}
}
},
"marks": [
{
"type": "area",
"from": {"data": "hour"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "date"},
"y": {"scale": "y", "field": "temp"},
"y2": {"signal": "rangeStep"}
}
}
}
]
}
]
}

0 comments on commit 1ffa03b

Please sign in to comment.