Skip to content

Instantly share code, notes, and snippets.

@domoritz
Last active May 17, 2019 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domoritz/392c4af55556ca4c35274960fa72868a to your computer and use it in GitHub Desktop.
Save domoritz/392c4af55556ca4c35274960fa72868a to your computer and use it in GitHub Desktop.
Line bias
license: bsd-3-clause

Estimate average

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@4"></script>
</head>
<body>
<p>
Click and drag anywhere in the chart to set the average.
</p>
<div id="vis"></div>
<script>
const spec = "line.json";
vegaEmbed('#vis', spec, {actions: false})
// result.view provides access to the Vega View API
.then(result => {
result.view.addSignalListener("average", console.log);
})
.catch(console.warn);
</script>
</body>
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"width": 500,
"height": 200,
"padding": 10,
"signals": [{
"name": "average",
"value": 0,
"on": [{
"events": "mousedown, [mousedown, mouseup] > mousemove!",
"update": "invert('y', clamp(y(), 0, height))"
}]
}],
"data": [
{
"name": "table",
"values": [
{"x": 0, "y": 28},
{"x": 1, "y": 43},
{"x": 2, "y": 81},
{"x": 3, "y": 19},
{"x": 4, "y": 52},
{"x": 5, "y": 24},
{"x": 6, "y": 87},
{"x": 7, "y": 17},
{"x": 8, "y": 68},
{"x": 9, "y": 49}
]
}
],
"scales": [
{
"name": "x",
"type": "point",
"range": "width",
"domain": {"data": "table", "field": "x"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"zero": true,
"domain": {"data": "table", "field": "y"}
}
],
"axes": [
{"orient": "bottom", "scale": "x", "ticks": false, "labels": false},
{"orient": "left", "scale": "y", "ticks": false, "labels": false}
],
"marks": [
{
"type": "rect",
"encode": {
"enter": {
"cursor": {"value": "pointer"},
"x": {"value": 0},
"x2": {"signal": "width"},
"y": {"value": 0},
"y2": {"signal": "height"},
"fill": {"value": "transparent"}
}
}
},
{
"type": "line",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "x"},
"y": {"scale": "y", "field": "y"},
"stroke": {"value": "steelblue"},
"strokeWidth": {"value": 2},
"cursor": {"value": "pointer"}
}
}
}, {
"type": "rule",
"encode": {
"update": {
"x": {"value": 0},
"x2": {"signal": "width"},
"y": {"signal": "average", "scale": "y"},
"stroke": {"value": "firebrick"},
"strokeWidth": {"value": 2},
"cursor": {"value": "pointer"}
}
}
}, {
"type": "text",
"encode": {
"update": {
"x": {"value": 5},
"y": {
"signal": "average",
"scale": "y",
"offset": -2
},
"text": {"value": "Estimated Average"},
"stroke": {"value": "firebrick"},
"cursor": {"value": "pointer"}
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment