-
Notifications
You must be signed in to change notification settings - Fork 9
/
from_json_spec.rs
49 lines (44 loc) · 1.46 KB
/
from_json_spec.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use vega_lite_4::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let spec = r##"
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A population pyramid for the US in 2000, created using stack. See https://vega.github.io/vega-lite/examples/concat_population_pyramid.html for a variant of this created using concat.",
"data": { "url": "https://raw.githubusercontent.com/vega/vega-datasets/master/data/population.json"},
"transform": [
{"filter": "datum.year == 2000"},
{"calculate": "datum.sex == 2 ? 'Female' : 'Male'", "as": "gender"},
{"calculate": "datum.sex == 2 ? -datum.people : datum.people", "as": "signed_people"}
],
"width": 300,
"height": 200,
"mark": "bar",
"encoding": {
"y": {
"field": "age", "type": "ordinal",
"axis": null, "sort": "descending"
},
"x": {
"aggregate": "sum", "field": "signed_people", "type": "quantitative",
"axis": {"title": "population", "format": "s"}
},
"color": {
"field": "gender", "type": "nominal",
"scale": {"range": ["#e377c2", "#1f77b4"]},
"legend": {"orient": "top", "title": null}
}
},
"config": {
"view": {"stroke": null},
"axis": {"grid": false}
}
}
"##;
let chart: Vegalite = serde_json::from_str(spec)?;
// display the chart using `showata`
chart.show()?;
// print the vega lite spec
eprintln!("{}", chart.to_string()?);
eprintln!("{:#?}", chart);
Ok(())
}