-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfrom_polars.rs
57 lines (53 loc) · 1.8 KB
/
from_polars.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
55
56
57
use polars::prelude::*;
use vega_lite_4::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// input data: a CSV reader
let df = CsvReader::from_path("examples/res/data/stocks.csv")?
.has_header(true)
.finish()?;
let df = df
.lazy()
.filter(
col("symbol")
.eq(lit("AMZN"))
.or(col("symbol").eq(lit("AAPL"))),
)
.collect()?;
// the chart
let chart = VegaliteBuilder::default()
.title("Stock price: Amazon vs Apple")
.description("Amazon vs Apple stock price over time.")
.data(df)
.mark(Mark::Line)
.encoding(
EdEncodingBuilder::default()
.x(XClassBuilder::default()
.field("date")
.position_def_type(Type::Temporal)
.axis(AxisBuilder::default().title("date").build()?)
.build()?)
.y(YClassBuilder::default()
.field("price")
.position_def_type(Type::Quantitative)
.axis(AxisBuilder::default().title("price").build()?)
.build()?)
.color(
ColorClassBuilder::default()
.field("symbol")
.legend(
LegendBuilder::default()
.orient(LegendOrient::Right)
.title(RemovableValue::Remove)
.build()?,
)
.build()?,
)
.build()?,
)
.build()?;
// display the chart using `showata`
chart.show()?;
// print the vega lite spec
eprint!("{}", chart.to_string()?);
Ok(())
}