Skip to content

Instantly share code, notes, and snippets.

@sniperliu
Last active June 16, 2017 04:19
Show Gist options
  • Save sniperliu/659f857bc459466131b59a440c065764 to your computer and use it in GitHub Desktop.
Save sniperliu/659f857bc459466131b59a440c065764 to your computer and use it in GitHub Desktop.
Vega-Lite update example
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega/3.0.0-beta.34/vega.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-lite/2.0.0-beta.4/vega-lite.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-embed/3.0.0-beta.18/vega-embed.js"></script>
<style>
body {
font-family: sans-serif;
}
.vega-actions a {
padding: 0.2em;
}
</style>
</head>
<body>
<div id="vis"></div>
<script>
// const spec = "stackarea.vega.v3.json";
// const spec = "stackarea.v2.json";
const spec = "stackarea.work.v3.json";
var values =
[{"bounds": [1493382600000,1493383499999], "value":8574741,"start":"2017-04-28T20:30:00", "categories": "ABC"},
{"bounds": [1493382600000,1493383499999], "value":7574741,"start":"2017-04-28T20:45:00", "categories": "ABC"},
{"bounds": [1493382600000,1493383499999], "value":8074741,"start":"2017-04-28T21:00:00", "categories": "ABC"}];
var count = 0;
function cb (error, res) {
window.setInterval(function () {
if (count < 3) {
console.log("count " + count)
var changeSet = vega.changeset().insert(values[count++]);
res.view.change('table', changeSet).run();
}
}, 1000);
}
vega.embed('#vis', spec,
{"mode": "vega-lite"},
cb);
</script>
</body>
{
"width": 1000,
"height": 600,
"data": {"name": "table"
},
"mark": "area",
"encoding": {
"x": {
"timeUnit": "yearmonthutcyearmonthdatehoursminutes",
"field": "start",
"type": "temporal",
"scale": {"nice": "minute"},
"axis": {
"domain": false,
"format": "%Y%m%d %H%M",
"labelAngle": 0
}
},
"y": {
"aggregate": "sum",
"field": "value",
"type": "quantitative"
},
"color": {
"field": "categories",
"type": "nominal",
"scale": {"scheme": "category20b"}
}
}
}
{
"$schema": "http://vega.github.io/schema/vega/v3.0.json",
"autosize": "pad",
"padding": 5,
"data": [
{
"name": "table",
"transform": [
{
"type": "filter",
"expr": "datum[\"start\"] !== null && !isNaN(datum[\"start\"]) && datum[\"value\"] !== null && !isNaN(datum[\"value\"])"
},
{
"type": "formula",
"as": "yearmonthutcyearmonthdatehoursminutes_start",
"expr": "datetime(year(datum[\"start\"]), month(datum[\"start\"]), date(datum[\"start\"]), hours(datum[\"start\"]), minutes(datum[\"start\"]), 0, 0)"
},
{
"type": "aggregate",
"groupby": [
"yearmonthutcyearmonthdatehoursminutes_start",
"categories"
],
"ops": ["sum"],
"fields": ["value"]
},
{
"type": "impute",
"field": "sum_value",
"groupby": ["categories"],
"orderby": ["yearmonthutcyearmonthdatehoursminutes_start"],
"method": "value",
"value": 0
},
{
"type": "stack",
"groupby": ["yearmonthutcyearmonthdatehoursminutes_start"],
"field": "sum_value",
"sort": {"field": ["categories"],"order": ["descending"]},
"as": ["sum_value_start","sum_value_end"],
"offset": "zero"
},
{
"type": "collect",
"sort": {
"field": "yearmonthutcyearmonthdatehoursminutes_start",
"order": "descending"
}
}
],
"format": {"parse": {"start": "date","value": "number"}}
}
],
"signals": [
{"name": "width","update": "1000"},
{"name": "height","update": "600"},
{
"name": "unit",
"value": {},
"on": [
{
"events": "mousemove",
"update": "group()._id ? group() : unit"
}
]
}
],
"marks": [
{
"name": "nested_main_group",
"type": "group",
"encode": {
"update": {
"width": {"signal": "width"},
"height": {"signal": "height"},
"fill": {"value": "transparent"}
}
},
"marks": [
{
"name": "pathgroup",
"type": "group",
"from": {
"facet": {
"name": "faceted_path_main",
"data": "table",
"groupby": ["categories"]
}
},
"encode": {
"update": {
"width": {"field": {"group": "width"}},
"height": {"field": {"group": "height"}}
}
},
"marks": [
{
"name": "marks",
"type": "area",
"from": {"data": "faceted_path_main"},
"encode": {
"update": {
"x": {
"scale": "x",
"field": "yearmonthutcyearmonthdatehoursminutes_start"
},
"y": {"scale": "y","field": "sum_value_end"},
"y2": {"scale": "y","field": "sum_value_start"},
"fill": {"scale": "color","field": "categories"},
"orient": {"value": "vertical"}
}
}
}
]
}
]
}
],
"scales": [
{
"name": "x",
"type": "time",
"domain": {
"data": "table",
"field": "yearmonthutcyearmonthdatehoursminutes_start"
},
"range": [0,1000],
"round": true,
"nice": "minute"
},
{
"name": "y",
"type": "linear",
"domain": {
"data": "table",
"fields": ["sum_value_start","sum_value_end"]
},
"range": [600,0],
"round": true,
"nice": true,
"zero": true
},
{
"name": "color",
"type": "ordinal",
"domain": {"data": "table","field": "categories","sort": true},
"range": {"scheme": "category20b"}
}
],
"axes": [
{
"scale": "x",
"domain": false,
"orient": "bottom",
"tickCount": 5,
"title": "YEARMONTHUTCYEARMONTHDATEHOURSMINUTES(start)",
"zindex": 1,
"encode": {
"labels": {
"update": {
"text": {
"signal": "timeFormat(datum.value, '%Y%m%d %H%M')"
},
"angle": {"value": 0}
}
}
}
},
{
"scale": "x",
"domain": false,
"grid": true,
"labels": false,
"orient": "bottom",
"tickCount": 5,
"ticks": false,
"zindex": 0,
"gridScale": "y"
},
{
"scale": "y",
"format": "s",
"orient": "left",
"title": "SUM(value)",
"zindex": 1
},
{
"scale": "y",
"domain": false,
"format": "s",
"grid": true,
"labels": false,
"orient": "left",
"ticks": false,
"zindex": 0,
"gridScale": "x"
}
],
"legends": [{"fill": "color","title": "categories"}]
}
{
"$schema": "http://vega.github.io/schema/vega/v3.0.json",
"autosize": "pad",
"padding": 5,
"data": [
{
"name": "table",
"transform": [
{
"type": "filter",
"expr": "datum[\"start\"] !== null && datum[\"value\"] !== null && !isNaN(datum[\"value\"])"
},
{
"type": "formula",
"as": "yearmonthutcyearmonthdatehoursminutes_start",
"expr": "datetime(year(datum[\"start\"]), month(datum[\"start\"]), date(datum[\"start\"]), hours(datum[\"start\"]), minutes(datum[\"start\"]), 0, 0)"
},
{
"type": "aggregate",
"groupby": [
"yearmonthutcyearmonthdatehoursminutes_start",
"categories"
],
"ops": ["sum"],
"fields": ["value"]
},
{
"type": "impute",
"field": "sum_value",
"groupby": ["categories"],
"orderby": ["yearmonthutcyearmonthdatehoursminutes_start"],
"method": "value",
"value": 0
},
{
"type": "stack",
"groupby": ["yearmonthutcyearmonthdatehoursminutes_start"],
"field": "sum_value",
"sort": {"field": ["categories"],"order": ["descending"]},
"as": ["sum_value_start","sum_value_end"],
"offset": "zero"
},
{
"type": "collect",
"sort": {
"field": "yearmonthutcyearmonthdatehoursminutes_start",
"order": "descending"
}
}
],
"format": {"parse": {"start": "date","value": "number"}}
}
],
"signals": [
{"name": "width","update": "1000"},
{"name": "height","update": "600"},
{
"name": "unit",
"value": {},
"on": [
{
"events": "mousemove",
"update": "group()._id ? group() : unit"
}
]
}
],
"marks": [
{
"name": "nested_main_group",
"type": "group",
"encode": {
"update": {
"width": {"signal": "width"},
"height": {"signal": "height"},
"fill": {"value": "transparent"}
}
},
"marks": [
{
"name": "pathgroup",
"type": "group",
"from": {
"facet": {
"name": "faceted_path_main",
"data": "table",
"groupby": ["categories"]
}
},
"encode": {
"update": {
"width": {"field": {"group": "width"}},
"height": {"field": {"group": "height"}}
}
},
"marks": [
{
"name": "marks",
"type": "area",
"from": {"data": "faceted_path_main"},
"encode": {
"update": {
"x": {
"scale": "x",
"field": "yearmonthutcyearmonthdatehoursminutes_start"
},
"y": {"scale": "y","field": "sum_value_end"},
"y2": {"scale": "y","field": "sum_value_start"},
"fill": {"scale": "color","field": "categories"},
"orient": {"value": "vertical"}
}
}
}
]
}
]
}
],
"scales": [
{
"name": "x",
"type": "time",
"domain": {
"data": "table",
"field": "yearmonthutcyearmonthdatehoursminutes_start"
},
"range": [0,1000],
"round": true,
"nice": "minute"
},
{
"name": "y",
"type": "linear",
"domain": {
"data": "table",
"fields": ["sum_value_start","sum_value_end"]
},
"range": [600,0],
"round": true,
"nice": true,
"zero": true
},
{
"name": "color",
"type": "ordinal",
"domain": {"data": "table","field": "categories","sort": true},
"range": {"scheme": "category20b"}
}
],
"axes": [
{
"scale": "x",
"domain": false,
"orient": "bottom",
"tickCount": 5,
"title": "YEARMONTHUTCYEARMONTHDATEHOURSMINUTES(start)",
"zindex": 1,
"encode": {
"labels": {
"update": {
"text": {
"signal": "timeFormat(datum.value, '%Y%m%d %H%M')"
},
"angle": {"value": 0}
}
}
}
},
{
"scale": "x",
"domain": false,
"grid": true,
"labels": false,
"orient": "bottom",
"tickCount": 5,
"ticks": false,
"zindex": 0,
"gridScale": "y"
},
{
"scale": "y",
"format": "s",
"orient": "left",
"title": "SUM(value)",
"zindex": 1
},
{
"scale": "y",
"domain": false,
"format": "s",
"grid": true,
"labels": false,
"orient": "left",
"ticks": false,
"zindex": 0,
"gridScale": "x"
}
],
"legends": [{"fill": "color","title": "categories"}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment