Skip to content

Instantly share code, notes, and snippets.

@domoritz
Last active November 26, 2017 13:10
Show Gist options
  • Save domoritz/93f0a78264516930211b9f047d53d7a3 to your computer and use it in GitHub Desktop.
Save domoritz/93f0a78264516930211b9f047d53d7a3 to your computer and use it in GitHub Desktop.
Line normalizer
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega/3.0.7/vega.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-embed/3.0.0-rc7/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 = "spec.json";
vegaEmbed('#vis', spec, {actions: false});
</script>
</body>
{
"$schema": "https://vega.github.io/schema/vega/v3.0.json",
"width": 400,
"height": 200,
"padding": 5,
"signals": [
{
"name": "count",
"bind": {
"input": "range",
"min": 3,
"max": 200
},
"value": 20
}
],
"data": [
{
"name": "table",
"transform": [
{
"type": "sequence",
"start": 0,
"stop": {"signal": "count"}
},
{
"type": "formula",
"expr": "datum.data",
"as": "x"
},
{
"type": "formula",
"expr": "random()",
"as": "y"
},
{
"type": "window",
"sort": {"field": "x", "order": "ascending"},
"ops": ["lag", "lag"],
"fields": ["y", "x"],
"as": ["prev_y", "prev_x"]
},
{
"type": "filter",
"expr": "datum.prev_x || datum.prev_y"
},
{
"type": "formula",
"expr": "sqrt(pow(datum.y - datum.prev_y, 2) + pow(datum.x - datum.prev_x, 2))",
"as": "segment_length"
},
{
"type": "formula",
"expr": "datum.segment_length / (datum.x - datum.prev_x)",
"as": "normalizer"
}
]
}
],
"scales": [
{
"name": "x",
"type": "linear",
"range": "width",
"domain": {"data": "table", "fields": ["prev_x", "x"]}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"zero": true,
"domain": {"data": "table", "fields": ["prev_y", "y"]}
},
{
"name": "color",
"type": "sequential",
"range": "ramp",
"domain": {"data": "table", "field": "normalizer"},
"reverse": true,
"zero": false
},
{
"name": "size",
"type": "linear",
"range": [1, 5],
"domain": {"data": "table", "field": "normalizer"},
"zero": false,
"reverse": true
}
],
"layout": {
"padding": {
"row": 10,
"column": 10
},
"offset": 10,
"columns": 1,
"bounds": "full",
"align": "each"
},
"marks": [{
"type": "group",
"encode": {
"update": {
"height": {"signal": "height"}
}
},
"axes": [
{"orient": "bottom", "scale": "x"},
{"orient": "left", "scale": "y"}
],
"marks": [
{
"type": "rule",
"from": {
"data": "table"
},
"encode": {
"enter": {
"x": {"scale": "x", "field": "prev_x"},
"y": {"scale": "y", "field": "prev_y"},
"x2": {"scale": "x", "field": "x"},
"y2": {"scale": "y", "field": "y"},
"stroke": {"value": "steelblue"},
"strokeWidth": {"scale": "size", "field": "normalizer"}
}
}
}
]
}, {
"type": "group",
"encode": {
"update": {
"height": {"signal": "height"}
}
},
"axes": [
{"orient": "bottom", "scale": "x"},
{"orient": "left", "scale": "y"}
],
"marks": [
{
"type": "rule",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "prev_x"},
"y": {"scale": "y", "field": "prev_y"},
"x2": {"scale": "x", "field": "x"},
"y2": {"scale": "y", "field": "y"},
"stroke": {"scale": "color", "field": "normalizer"},
"strokeWidth": {"value": 2}
}
}
}
]
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment