Skip to content

Instantly share code, notes, and snippets.

@espinielli
Last active March 4, 2016 10:39
Show Gist options
  • Save espinielli/64b0be9bc33d1405bc92 to your computer and use it in GitHub Desktop.
Save espinielli/64b0be9bc33d1405bc92 to your computer and use it in GitHub Desktop.
Vega 2.0 horizontal bar plot

The implementation of the horizontal version of the Vega tutorial bar plot.

cat amt
A 28
B 55
C 43
D 91
E 81
F 53
G 19
H 87
I 52
<html>
<head>
<title>Vega Experiment</title>
<script src="https://vega.github.io/vega-editor/vendor/d3.min.js"></script>
<script src="https://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": 400,
"height": 200,
"data": [
{
"name": "table",
"url": "bar.csv",
"format": {"type": "csv"}
}
],
"signals": [
{
"name": "tooltip",
"init": {},
"streams": [
{"type": "rect:mouseover", "expr": "datum"},
{"type": "rect:mouseout", "expr": "{}"}
]
}
],
"predicates": [
{
"name": "tooltip", "type": "==",
"operands": [{"signal": "tooltip._id"}, {"arg": "id"}]
}
],
"scales": [
{"name": "categories", "type": "ordinal", "range": "height", "domain": {"data":"table", "field":"cat"}},
{"name": "amounts", "range": "width", "nice": true, "domain": {"data":"table", "field":"amt"}}
],
"axes": [
{"type": "x", "scale": "amounts", "orient":"top"},
{"type": "y", "scale": "categories"}
],
"marks": [
{
"type": "rect",
"from": {"data":"table"},
"properties": {
"enter": {
"y": {"scale":"categories", "field":"cat"},
"height": {"scale":"categories", "band":true, "offset":-1},
"x": {"value": 0},
"width": {"scale":"amounts", "field":"amt"}
},
"update": { "fill": {"value":"steelblue"} },
"hover": { "fill": {"value":"red"} }
}
},
{
"type": "text",
"properties": {
"enter": {
"align": {"value": "center"},
"fill": {"value": "#333"}
},
"update": {
"x": {"scale": "amounts", "signal": "tooltip.amt", "offset": 10},
"y": {"scale": "categories", "signal": "tooltip.cat", "offset": 15},
"text": {"signal": "tooltip.amt"},
"fillOpacity": {
"rule": [
{
"predicate": {
"name": "tooltip",
"id": {"value": null}
},
"value": 0
},
{"value": 1}
]
}
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment