Skip to content

Instantly share code, notes, and snippets.

@manzt
Last active February 9, 2019 09:50
Show Gist options
  • Save manzt/face8b3d831bc2a292826d9a58e05ee8 to your computer and use it in GitHub Desktop.
Save manzt/face8b3d831bc2a292826d9a58e05ee8 to your computer and use it in GitHub Desktop.
Playfair's Wheat and Wages example
license: mit

Playfair's Wheat and Wages with Vega-Lite

Reproduced from examples in Vega and Protovis.

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/vega@4"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@3.0.0-rc12"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@3"></script>
</head>
<body>
<div id="vis"></div>
<script>
const spec = "playfair.vl.json";
vegaEmbed('#vis', spec)
// result.view provides access to the Vega View API
.then(result => console.log(result))
.catch(console.warn);
</script>
</body>
{
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"width": 900,
"height": 465,
"data": {
"url": "https://raw.githubusercontent.com/vega/vega/master/docs/data/wheat.json"
},
"transform": [
{"calculate": "+datum.year + 5","as": "year_end"}
],
"layer": [
{
"mark": {
"type": "bar"
},
"encoding": {
"x": {
"field": "year",
"type": "quantitative",
"axis": {
"tickCount": 5,
"format": "d"
}
},
"x2": {
"field": "year_end"
},
"y": {
"field": "wheat",
"type": "quantitative",
"axis": {"zindex": 1}
},
"fill": {"value": "#AAAAAA"},
"stroke": {"value": "#999999"}
}
},
{
"data": {
"values": [
{"year": "1600"},{"year": "1650"},
{"year": "1700"},{"year": "1750"},
{"year": "1800"}
]
},
"mark": "rule",
"encoding": {
"x": {
"field": "year",
"type": "quantitative"
},
"stroke": {"value": "black"},
"strokeWidth": {"value": 0.6},
"opacity": {"value": 0.5}
}
},
{
"mark": "area",
"encoding": {
"x": {
"field": "year",
"type": "quantitative"
},
"y": {
"field": "wages",
"type": "quantitative"
},
"color": {"value": "#a4cedb"},
"opacity": {"value": 0.7}
}
},
{
"mark": "line",
"encoding": {
"x": {
"field": "year",
"type": "quantitative"
},
"y": {
"field": "wages",
"type": "quantitative"
},
"color": {"value": "#000"},
"opacity": {"value": 0.7}
}
},
{
"mark": {
"type": "line",
"yOffset": -2
},
"encoding": {
"x": {
"field": "year",
"type": "quantitative"
},
"y": {
"field": "wages",
"type": "quantitative"
},
"color": {"value": "#EE8182"}
}
},
{
"data": {
"url": "https://raw.githubusercontent.com/vega/vega/master/docs/data/monarchs.json"
},
"transform": [
{"calculate": "((!datum.commonwealth && datum.index % 2) ? -1: 1) * 2 + 95","as": "offset"},
{"calculate": "95","as": "y"}
],
"mark": "bar",
"encoding": {
"x": {
"field": "start",
"type": "quantitative"
},
"x2": {"field": "end"},
"y": {
"field": "y",
"type": "quantitative"
},
"y2": {"field": "offset"},
"fill": {
"field": "commonwealth",
"scale": {"range": ["black","white"]},
"legend": null
},
"stroke": {"value": "black"}
}
},
{
"data": {
"url": "https://raw.githubusercontent.com/vega/vega/master/docs/data/monarchs.json"
},
"transform": [
{"calculate": "((!datum.commonwealth && datum.index % 2) ? -1: 1) * 1 + 95", "as": "offset2"},
{"calculate": "+datum.start + (+datum.end - +datum.start)/2", "as": "x"}
],
"mark": {
"type": "text",
"yOffset": 18,
"fontSize": 10,
"fontStyle": "italic"
},
"encoding": {
"x": {
"field": "x",
"type": "quantitative"
},
"y": {
"field": "offset2",
"type": "quantitative"
},
"text": {
"field": "name",
"type": "nominal"
}
}
}
],
"config": {
"axis": {
"title": null,
"gridColor": "white",
"gridOpacity": 0.25,
"domain": false
},
"mark": {"tooltip": null},
"view": {"stroke": "transparent"}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment