Skip to content

Instantly share code, notes, and snippets.

@marcprux
Created April 25, 2013 16:53
Show Gist options
  • Save marcprux/5461279 to your computer and use it in GitHub Desktop.
Save marcprux/5461279 to your computer and use it in GitHub Desktop.
Resizable stacked bar chart using vega+d3+svg
<!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>
@lrpina
Copy link

lrpina commented Mar 15, 2017

Hello! I tried rendering your viz_vega_spec var in the Vega Editor. I basically copied the code in between {} for the variable viz_vega_spec. However, I can get an error on the stats 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-Transforms

Here is the list of errors I get when I inspect:

[Vega Err] PARSE DATA FAILED: stats Error: "stats" is not a valid transformation
error @ index.js:8
onError @ data.js:11
(anonymous) @ data.js:40
parseData @ data.js:32
parse @ spec.js:72
parseSpec @ spec.js:34
embed @ embed.js:97
ved.parseVg @ editor.js:263
(anonymous) @ editor.js:352
(anonymous) @ d3.min.js:1
index.js:8 [Vega Err] Error: "stats" is not a valid transformation
at parseTransforms (http://vega.github.io/vega-editor/vendor/vega.js:16960:25)
at http://vega.github.io/vega-editor/vendor/vega.js:15439:16
at Array.map (native)
at Function.parseData.datasource (http://vega.github.io/vega-editor/vendor/vega.js:15438:39)
at http://vega.github.io/vega-editor/vendor/vega.js:15427:17
at Array.forEach (native)
at Object.parseData [as data] (http://vega.github.io/vega-editor/vendor/vega.js:15421:16)
at parse (http://vega.github.io/vega-editor/vendor/vega.js:16671:29)
at Object.parseSpec [as spec] (http://vega.github.io/vega-editor/vendor/vega.js:16633:5)
at Object.embed (http://vega.github.io/vega-editor/vendor/vega-embed.js:99:12)
error @ index.js:8
done @ spec.js:96
onError @ data.js:13
(anonymous) @ data.js:40
parseData @ data.js:32
parse @ spec.js:72
parseSpec @ spec.js:34
embed @ embed.js:97
ved.parseVg @ editor.js:263
(anonymous) @ editor.js:352
(anonymous) @ d3.min.js:1
editor.js:214 Error: "stats" is not a valid transformation
at parseTransforms (http://vega.github.io/vega-editor/vendor/vega.js:16960:25)
at http://vega.github.io/vega-editor/vendor/vega.js:15439:16
at Array.map (native)
at Function.parseData.datasource (http://vega.github.io/vega-editor/vendor/vega.js:15438:39)
at http://vega.github.io/vega-editor/vendor/vega.js:15427:17
at Array.forEach (native)
at Object.parseData [as data] (http://vega.github.io/vega-editor/vendor/vega.js:15421:16)
at parse (http://vega.github.io/vega-editor/vendor/vega.js:16671:29)
at Object.parseSpec [as spec] (http://vega.github.io/vega-editor/vendor/vega.js:16633:5)
at Object.embed (http://vega.github.io/vega-editor/vendor/vega-embed.js:99:12)
callback @ editor.js:214
(anonymous) @ editor.js:264
(anonymous) @ embed.js:98
done @ spec.js:102
onError @ data.js:13
(anonymous) @ data.js:40
parseData @ data.js:32
parse @ spec.js:72
parseSpec @ spec.js:34
embed @ embed.js:97
ved.parseVg @ editor.js:263
(anonymous) @ editor.js:352
(anonymous) @ d3.min.js:1

Any advice would be helpful. Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment