Created
April 25, 2013 16:53
-
-
Save marcprux/5461279 to your computer and use it in GitHub Desktop.
Resizable stacked bar chart using vega+d3+svg
This file contains hidden or 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
<!DOCTYPE html> | |
<html | |
xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<title>Visualization</title> | |
<meta | |
charset="UTF-8" /> | |
<meta | |
http-equiv="Content-type" | |
content="text/html;charset=UTF-8" /> | |
<script | |
id="viz-script-d3" | |
type="text/javascript" | |
src="http://d3js.org/d3.v3.js"> | |
</script> | |
<script | |
id="viz-script-vega" | |
type="text/javascript" | |
src="http://trifacta.github.io/vega/vega.js"> | |
</script> | |
<script | |
id="viz-script-spec" | |
type="text/javascript"> | |
//<![CDATA[ | |
var viz_vega_spec = { | |
"width": 500.0, | |
"height": 200.0, | |
"padding": { | |
"top": 10.0, | |
"left": 30.0, | |
"bottom": 30.0, | |
"right": 10.0 | |
}, | |
"data": [ | |
{ | |
"name": "table", | |
"values": [ | |
{ | |
"x": 0, | |
"y": 28, | |
"c": 0 | |
}, | |
{ | |
"x": 0, | |
"y": 55, | |
"c": 1 | |
}, | |
{ | |
"x": 1, | |
"y": 43, | |
"c": 0 | |
}, | |
{ | |
"x": 1, | |
"y": 91, | |
"c": 1 | |
}, | |
{ | |
"x": 2, | |
"y": 81, | |
"c": 0 | |
}, | |
{ | |
"x": 2, | |
"y": 53, | |
"c": 1 | |
}, | |
{ | |
"x": 3, | |
"y": 19, | |
"c": 0 | |
}, | |
{ | |
"x": 3, | |
"y": 87, | |
"c": 1 | |
}, | |
{ | |
"x": 4, | |
"y": 52, | |
"c": 0 | |
}, | |
{ | |
"x": 4, | |
"y": 48, | |
"c": 1 | |
}, | |
{ | |
"x": 5, | |
"y": 24, | |
"c": 0 | |
}, | |
{ | |
"x": 5, | |
"y": 49, | |
"c": 1 | |
}, | |
{ | |
"x": 6, | |
"y": 87, | |
"c": 0 | |
}, | |
{ | |
"x": 6, | |
"y": 66, | |
"c": 1 | |
}, | |
{ | |
"x": 7, | |
"y": 17, | |
"c": 0 | |
}, | |
{ | |
"x": 7, | |
"y": 27, | |
"c": 1 | |
}, | |
{ | |
"x": 8, | |
"y": 68, | |
"c": 0 | |
}, | |
{ | |
"x": 8, | |
"y": 16, | |
"c": 1 | |
}, | |
{ | |
"x": 9, | |
"y": 49, | |
"c": 0 | |
}, | |
{ | |
"x": 9, | |
"y": 15, | |
"c": 1 | |
} | |
] | |
}, | |
{ | |
"name": "stats", | |
"source": "table", | |
"transform": [ | |
{ | |
"keys": [ | |
"data.x" | |
], | |
"type": "facet" | |
}, | |
{ | |
"value": "data.y", | |
"type": "stats" | |
} | |
] | |
} | |
], | |
"scales": [ | |
{ | |
"name": "x", | |
"type": "ordinal", | |
"domain": { | |
"data": "table", | |
"field": "data.x" | |
}, | |
"range": "width" | |
}, | |
{ | |
"name": "y", | |
"type": "linear", | |
"domain": { | |
"data": "stats", | |
"field": "sum" | |
}, | |
"range": "height", | |
"nice": true | |
}, | |
{ | |
"name": "color", | |
"type": "ordinal", | |
"range": "category10" | |
} | |
], | |
"axes": [ | |
{ | |
"type": "x", | |
"scale": "x" | |
}, | |
{ | |
"type": "y", | |
"scale": "y" | |
} | |
], | |
"marks": [ | |
{ | |
"type": "group", | |
"from": { | |
"data": "table", | |
"transform": [ | |
{ | |
"keys": [ | |
"data.c" | |
], | |
"type": "facet" | |
}, | |
{ | |
"point": "data.x", | |
"height": "data.y", | |
"type": "stack" | |
} | |
] | |
}, | |
"marks": [ | |
{ | |
"properties": { | |
"enter": { | |
"x": { | |
"field": "data.x", | |
"scale": "x" | |
}, | |
"width": { | |
"scale": "x", | |
"offset": -1.0, | |
"band": true | |
}, | |
"y": { | |
"field": "y", | |
"scale": "y" | |
}, | |
"y2": { | |
"field": "y2", | |
"scale": "y" | |
}, | |
"fill": { | |
"field": "data.c", | |
"scale": "color" | |
} | |
}, | |
"update": { | |
"fillOpacity": { | |
"value": 1.0 | |
} | |
}, | |
"hover": { | |
"fillOpacity": { | |
"value": 0.5 | |
} | |
} | |
}, | |
"type": "rect" | |
} | |
] | |
} | |
] | |
}; | |
//]]> | |
</script> | |
<script | |
id="viz-script-render" | |
type="text/javascript"> | |
//<![CDATA[ | |
if (typeof viz_vega_spec.padding == 'undefined') | |
viz_vega_spec.padding = {}; | |
if (typeof viz_vega_spec.padding.top == 'undefined') | |
viz_vega_spec.padding.top = 0; | |
if (typeof viz_vega_spec.padding.bottom == 'undefined') | |
viz_vega_spec.padding.bottom = 0; | |
if (typeof viz_vega_spec.padding.left == 'undefined') | |
viz_vega_spec.padding.left = 0; | |
if (typeof viz_vega_spec.padding.right == 'undefined') | |
viz_vega_spec.padding.right= 0; | |
viz_render = function() { | |
viz_view.width(window.innerWidth-viz_vega_spec.padding.left-viz_vega_spec.padding.right).height(window.innerHeight-viz_vega_spec.padding.top - viz_vega_spec.padding.bottom).renderer('svg').update({props:'enter'}).update(); | |
} | |
vg.parse.spec(viz_vega_spec, function(chart) { | |
self.viz_chart = chart; | |
self.viz_view = viz_chart({el:'#viz-visualization'}); | |
viz_render(); | |
}); | |
window.onresize = viz_render; | |
//]]> | |
</script> | |
<style | |
id="viz-styles" | |
type="text/css"> | |
</style> | |
</head> | |
<body | |
style="padding:0; margin:0; overflow:hidden;"> | |
<div | |
id="viz-visualization"> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello! I tried rendering your
viz_vega_spec
var in the Vega Editor. I basically copied the code in between {} for the variableviz_vega_spec
. However, I can get an error on thestats
data declaration. It seems to be about the transformation that's being applied to it, in line 36. I tried trying to debug but I'm still learning Vega and could not figure out the problem.I think it has to do with this line:
{"value": "data.y","type": "stats"}
If I understand correctly,
type
has to be one of the data transformations defined in the api. Listed here: https://github.com/vega/vega/wiki/Data-TransformsHere is the list of errors I get when I inspect:
Any advice would be helpful. Thanks for your help!