Skip to content

Instantly share code, notes, and snippets.

@domoritz
Created April 21, 2018 14:59
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 domoritz/503e5e5ebd9f53dc549b4478942a85be to your computer and use it in GitHub Desktop.
Save domoritz/503e5e5ebd9f53dc549b4478942a85be to your computer and use it in GitHub Desktop.
Simple Vega in HTML
license: mit
<head>
<script src="https://vega.github.io/vega/vega.min.js"></script>
</head>
<body>
<div id="view"></div>
<script type="text/javascript">
var chartSpec1 = {
"$schema": "https://vega.github.io/schema/vega/v3.json",
"width": 400,
"height": 400,
"padding": 5,
"title": "blah blah blah blah blah blah blah blah blah",
"data": [
{
"name": "table",
"values": [
{"x": 2000, "y": 0.28},
{"x": 2000.5, "y": 0.49},
{"x": 2001, "y": 0.49},
{"x": 2001.5, "y": 0.29},
{"x": 2002, "y": 0.32},
{"x": 2003, "y": 0.59},
{"x": 2004, "y": 0.62}
]
}
],
"scales": [
{
"name": "x",
"type": "linear",
"range": "width",
"zero": false,
"domain": [2000, 2010]
},
{
"name": "y",
"type": "linear",
"range": "height",
"domain": [0, 1]
}
],
"axes": [
{"orient": "bottom", "scale": "x", "format": ".0f"},
{"orient": "left", "scale": "y", "format": "%"}
],
"marks": [
{
"type": "group",
"from": {
"facet": {
"name": "series",
"data": "table",
"groupby": "c"
}
},
"marks": [
{
"type": "line",
"from": {"data": "series"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "x"},
"y": {"scale": "y", "field": "y"},
"stroke": {"value": "#276EB8"},
"strokeWidth": {"value": 4}
}
}
}
]
}
]
}
</script>
<script type="text/javascript">
var view;
// vega.loader()
// .load('https://vega.github.io/vega/examples/bar-chart.vg.json')
// .then(function(data) { render(chartSpec1); });
// function render(spec) {
// view = new vega.View(vega.parse(spec))
// .renderer('svg') // set renderer (canvas or svg)
// .initialize('#view') // initialize view within parent DOM container
// //.hover() // enable hover encode set processing
// .run();
// }
view = new vega.View(vega.parse(chartSpec1))
.renderer('svg') // set renderer (canvas or svg)
.initialize('#view') // initialize view within parent DOM container
.run();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment