The implementation of the horizontal version of the Vega tutorial bar plot.
Last active
March 4, 2016 10:39
-
-
Save espinielli/64b0be9bc33d1405bc92 to your computer and use it in GitHub Desktop.
Vega 2.0 horizontal bar plot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cat | amt | |
---|---|---|
A | 28 | |
B | 55 | |
C | 43 | |
D | 91 | |
E | 81 | |
F | 53 | |
G | 19 | |
H | 87 | |
I | 52 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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