Skip to content

Instantly share code, notes, and snippets.

@espinielli
Last active August 29, 2015 14:27
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 espinielli/e368182a17297a806d01 to your computer and use it in GitHub Desktop.
Save espinielli/e368182a17297a806d01 to your computer and use it in GitHub Desktop.
Vega 2.0 events and marks

Learning Vega 2.0: events and marks

Capture mousemove and print mouse coordinates.

<html>
<head>
<title>Vega Experiment: Follow the mouse and print its coordinates</title>
<script src="http://vega.github.io/vega-editor/vendor/d3.min.js"></script>
<script src="http://vega.github.io/vega-editor/vendor/vega.min.js"></script>
</head>
<body>
<div id="vis"></div>
</body>
<script type="text/javascript">
// parse a spec and create a visualization view
function parse(spec) {
vg.parse.spec(spec, function(chart) { chart({el:"#vis"}).update(); });
}
parse("spec.json");
</script>
</html>
{
"width": 200,
"height": 200,
"background": "#edf1f7",
"signals": [
{"name": "w", "init": 200},
{"name": "h", "init": 200},
{
"name": "position",
"init": {"x": 0, "y": 0},
"streams": [{
"type": "mousemove",
"expr": "{x: eventX(), y: eventY()}"
}]
}
],
"marks": [
{
"name": "marker",
"type": "symbol",
"properties": {
"enter": {
"x": {"value": 0},
"y": {"value": 0},
"fill": {"value": "grey"},
"fillOpacity": {"value": 0.5},
"shape": {"value":"cross"},
"size": {"value": 100},
"stroke": {"value": "#000"},
"strokeWidth": {"value": 0.5}
},
"update": {
"x": {"signal": "position.x"},
"y": {"signal": "position.y"}
}
}
},
{
"name": "coordinates",
"type": "text",
"properties": {
"enter": {
"fill": {"value": "#333"}
},
"update": {
"text": {"template": "({{position.x}}, {{position.y}})"},
"x": {"signal": "position.x", "offset": 5},
"y": {"signal": "position.y", "offset": -5}
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment