forked from procyon-rs/vega_lite_4.rs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiverging_stacked_bar_chart.rs
54 lines (50 loc) · 2.25 KB
/
diverging_stacked_bar_chart.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
50
51
52
53
54
use vega_lite_5::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// the chart
let chart = VegaliteBuilder::default()
.description("A population pyramid for the US in 2000.")
.data(UrlDataBuilder::default().url(
"https://raw.githubusercontent.com/vega/vega-datasets/master/data/population.json"
).build()?)
.height(200.)
.width(300.)
.transform(vec![
TransformBuilder::default().filter("datum.year == 2000").build()?,
TransformBuilder::default().calculate("datum.sex == 2 ? 'Female' : 'Male'").transform_as("gender").build()?,
TransformBuilder::default().calculate("datum.sex == 2 ? -datum.people : datum.people").transform_as("signed_people").build()?,
])
.mark(Mark::Bar)
.encoding(EdEncodingBuilder::default()
.x(XClassBuilder::default()
.aggregate(NonArgAggregateOp::Sum)
.field("signed_people")
.position_def_type(Type::Quantitative)
.axis(AxisBuilder::default().title("population").format("s").build()?)
.build()?)
.y(YClassBuilder::default()
.field("age")
.position_def_type(Type::Ordinal)
.sort(Sort::Descending)
.axis(RemovableValue::Remove)
.build()?)
.color(ColorClassBuilder::default()
.field("gender")
.mark_prop_def_gradient_string_null_type(Type::Nominal)
.scale(ScaleBuilder::default().range(vec![
FluffyRange::String("#e377c2".to_string()),
FluffyRange::String("#1f77b4".to_string())
]).build()?)
.legend(LegendBuilder::default().orient(LegendOrient::Top).title(RemovableValue::Remove).build()?)
.build()?)
.build()?)
.config(ConfigClassBuilder::default()
.view(ViewConfigBuilder::default().stroke(RemovableValue::Remove).build()?)
.axis(AxisConfigBuilder::default().grid(false).build()?)
.build()?)
.build()?;
// display the chart using `showata`
chart.show()?;
// print the vega lite spec
eprint!("{}", chart.to_string()?);
Ok(())
}