Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Created October 12, 2015 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save randyzwitch/229f2cccf5cdaa90b569 to your computer and use it in GitHub Desktop.
Save randyzwitch/229f2cccf5cdaa90b569 to your computer and use it in GitHub Desktop.
Vega Stacked Area 100%
{
"width": 500,
"height": 200,
"padding": "auto",
"data": [
{
"name": "table",
"values": [
{"x": 0, "y": 28, "c":0}, {"x": 0, "y": 55, "c":1},
{"x": 1, "y": 43, "c":0}, {"x": 1, "y": 91, "c":1},
{"x": 2, "y": 81, "c":0}, {"x": 2, "y": 53, "c":1},
{"x": 3, "y": 19, "c":0}, {"x": 3, "y": 87, "c":1},
{"x": 4, "y": 52, "c":0}, {"x": 4, "y": 48, "c":1},
{"x": 5, "y": 24, "c":0}, {"x": 5, "y": 49, "c":1},
{"x": 6, "y": 87, "c":0}, {"x": 6, "y": 66, "c":1},
{"x": 7, "y": 17, "c":0}, {"x": 7, "y": 27, "c":1},
{"x": 8, "y": 68, "c":0}, {"x": 8, "y": 16, "c":1},
{"x": 9, "y": 49, "c":0}, {"x": 9, "y": 15, "c":1}
]
},
{
"name": "stats",
"source": "table",
"transform": [
{
"type": "aggregate",
"groupby": ["x"],
"summarize": [{"field": "y", "ops": ["sum"]}]
}
]
}
],
"scales": [
{
"name": "x",
"type": "ordinal",
"range": "width",
"points": true,
"domain": {"data": "table", "field": "x"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"domain": [0,1]
},
{
"name": "color",
"type": "ordinal",
"range": "category20",
"domain": {"data": "table", "field": "c"}
}
],
"axes": [
{"type": "x", "scale": "x"},
{"type": "y", "scale": "y", "format": "%"}
],
"marks": [
{
"type": "group",
"from": {
"data": "table",
"transform": [
{"type": "lookup", "on": "stats", "onKey": "x", "keys": "x", "as": "sum_y"},
{"type": "formula", "field": "pcttot", "expr": "datum.y/datum.sum_y.sum_y"},
{"type": "stack", "groupby": ["x"], "sortby": ["c"], "field": "pcttot"},
{"type": "facet", "groupby": ["c"]}
]
},
"marks": [
{
"type": "area",
"properties": {
"enter": {
"interpolate": {"value": "monotone"},
"x": {"scale": "x", "field": "x"},
"y": {"scale": "y", "field": "layout_start"},
"y2": {"scale": "y", "field": "layout_end"},
"fill": {"scale": "color", "field": "c"}
}
}
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment