Skip to content

Instantly share code, notes, and snippets.

@skoslitz
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save skoslitz/11545254 to your computer and use it in GitHub Desktop.

Select an option

Save skoslitz/11545254 to your computer and use it in GitHub Desktop.
vega stock chart

Tea growing area India

Chart implemented with vega.js

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Teeproduktion</title>
<script src="http://d3js.org/d3.v3.min.js">></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/vega/1.4.0/vega.min.js"></script>
<style type="text/css">
#growArea {
padding: 40px;
text-align: center;
}
h1 {
font-family: Helvetica;
text-align: center;
font-size: 23px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Teeproduktion in den Hauptanbauländern</h1>
<div id="growArea"</div>
<script type="text/javascript">
var spec = {
"width": 500,
"height": 300,
"data": [
{
"name": "teaProduction",
"url": "teaProduction.csv",
"format": {"type": "csv", "parse": {"production":"number", "date":"date"}}
}
],
"scales": [
{
"name": "x",
"type": "time",
"range": "width",
"domain": {"data": "teaProduction", "field": "data.date"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"domain": {"data": "teaProduction", "field": "data.production"}
},
{
"name": "color", "type": "ordinal", "range": "category10"
}
],
"axes": [
{"type": "x", "scale": "x", "tickSizeEnd": 0},
{"type": "y", "scale": "y"}
],
"marks": [
{
"type": "group",
"from": {
"data": "teaProduction",
"transform": [{"type": "facet", "keys": ["data.country"]}]
},
"marks": [
{
"type": "line",
"properties": {
"enter": {
"x": {"scale": "x", "field": "data.date"},
"y": {"scale": "y", "field": "data.production"},
"stroke": {"scale": "color", "field": "data.country"},
"strokeWidth": {"value": 2}
}
}
},
{
"type": "text",
"from": {
"transform": [{"type": "filter", "test": "index==data.length-1"}]
},
"properties": {
"enter": {
"x": {"scale": "x", "field": "data.date", "offset": 2},
"y": {"scale": "y", "field": "data.production"},
"fill": {"scale": "color", "field": "data.country"},
"text": {"field": "data.country"},
"baseline": {"value": "middle"}
}
}
}
]
}
]
};
vg.parse.spec(spec, function(chart) {
var growArea = chart({el:"#growArea"})
.update();
});
</script>
</body>
</html>
country date production
China 1940 100
China 1950 62.5
China 1960 136.0
China 1970 136.0
China 1980 303.7
China 1990 540.1
China 2000 683.3
China 2010 1475.1
India 1940 210.4
India 1950 278.0
India 1960 321.0
India 1970 418.5
India 1980 569.5
India 1990 720.3
India 2000 980.8
India 2010 966.4
Kenya 1940 5.4
Kenya 1950 6.7
Kenya 1960 13.7
Kenya 1970 41.0
Kenya 1980 89.8
Kenya 1990 197.0
Kenya 2000 345.8
Kenya 2010 399.0
Sri Lanka 1940 120.2
Sri Lanka 1950 143.4
Sri Lanka 1960 197.1
Sri Lanka 1970 212.2
Sri Lanka 1980 191.3
Sri Lanka 1990 234.0
Sri Lanka 2000 318.6
Sri Lanka 2010 331.4
Vietnam 1940 0
Vietnam 1950 0
Vietnam 1960 4.5
Vietnam 1970 5.5
Vietnam 1980 21.5
Vietnam 1990 32.2
Vietnam 2000 63.7
Vietnam 2010 157.0
Turkey 1940 0
Turkey 1950 0.2
Turkey 1960 5.9
Turkey 1970 33.4
Turkey 1980 95.8
Turkey 1990 126.7
Turkey 2000 155.0
Turkey 2010 148.0
Indonesia 1940 0
Indonesia 1950 35.3
Indonesia 1960 46.0
Indonesia 1970 44.0
Indonesia 1980 98.6
Indonesia 1990 145.1
Indonesia 2000 137.5
Indonesia 2010 129.2
Japan 1940 58.2
Japan 1950 41.7
Japan 1960 78.9
Japan 1970 91.1
Japan 1980 102.3
Japan 1990 89.9
Japan 2000 93.0
Japan 2010 93.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment