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/c730e161e5619f3c4298 to your computer and use it in GitHub Desktop.
Save espinielli/c730e161e5619f3c4298 to your computer and use it in GitHub Desktop.
Vega 2.0 interact with DOM events

Capture events from input elements outside the visualization. Similar to what is done at min 3:19 in the Reactive Vega video

<iframe class="player" src="//player.vimeo.com/video/100936827" width="630" height="354" frameborder="0" webkitallowfullscreen="1" mozallowfullscreen="1" allowfullscreen="1"></iframe>
<html>
<head>
<title>Vega Experiment: interaction from events external to the viz</title>
<script src="http://vega.github.io/vega-editor/vendor/d3.min.js"></script>
<script src="http://vega.github.io/vega/vega.min.js"></script>
</head>
<body>
<div id="vis"></div>
<p>
<form>
<b>Color:</b>
<input type="radio" name="color" value="red" checked="checked">Red
<input type="radio" name="color" value="green">Green
<input type="radio" name="color" value="blue">Blue
</form>
</p>
</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": "selectcolor",
"init": {"value": "yellow"},
"streams": [
{"type": "input[name=color]:click", "expr": "{'value': event.target.value}"}
]
}
],
"marks": [
{
"type": "rect",
"properties": {
"enter": {
"x": {"value": 50},
"y": {"value": 50},
"width": {"value": 30},
"height": {"value": 30},
"fill": {"signal": "selectcolor.value"}
},
"update": {
"fill": {"signal": "selectcolor.value"}
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment