-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdiverging_stacked_bar_chart.rs
54 lines (50 loc) · 2.29 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_3::*;
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(EncodingBuilder::default()
.x(XClassBuilder::default()
.aggregate(AggregateOp::Sum)
.field("signed_people")
.def_type(StandardType::Quantitative)
.axis(AxisBuilder::default().title("population").format("s").build()?)
.build()?)
.y(YClassBuilder::default()
.field("age")
.def_type(StandardType::Ordinal)
.sort(SortOrder::Descending)
.axis(RemovableValue::Remove)
.build()?)
.color(DefWithConditionMarkPropFieldDefStringNullBuilder::default()
.field("gender")
.def_with_condition_mark_prop_field_def_string_null_type(StandardType::Nominal)
.scale(ScaleBuilder::default().range(vec![
RangeRange::String("#e377c2".to_string()),
RangeRange::String("#1f77b4".to_string())
]).build()?)
.legend(LegendBuilder::default().orient(LegendOrient::Top).title(RemovableValue::Remove).build()?)
.build()?)
.build()?)
.config(ConfigBuilder::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(())
}