-
Notifications
You must be signed in to change notification settings - Fork 8
/
stock_graph.rs
64 lines (57 loc) · 1.89 KB
/
stock_graph.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
58
59
60
61
62
63
64
use serde::{Deserialize, Serialize};
use std::path::Path;
use vega_lite_4::*;
#[derive(Serialize, Deserialize)]
pub struct Item {
pub symbol: String,
pub date: String,
pub price: f64,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// {
// "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
// "description": "Google's stock price over time.",
// "data": {"url": "data/stocks.csv"},
// "transform": [{"filter": "datum.symbol==='GOOG'"}],
// "mark": "line",
// "encoding": {
// "x": {"field": "date", "type": "temporal"},
// "y": {"field": "price", "type": "quantitative"}
// }
// }
// input data: a CSV serialized to a `Vec<Item>`
let mut rdr = csv::Reader::from_path(Path::new("examples/res/data/stocks.csv"))?;
let values = rdr
.deserialize()
.collect::<Result<Vec<Item>, csv::Error>>()?;
// the chart
let chart = VegaliteBuilder::default()
.title("Stock price")
// .width(400.0)
// .height(200.0)
// .padding(Some(Padding::Double(5.0)))
.description("Google's stock price over time.")
.data(&values)
.transform(vec![TransformBuilder::default()
.filter("datum.symbol==='GOOG'")
.build()?])
.mark(Mark::Line)
.encoding(
EdEncodingBuilder::default()
.x(XClassBuilder::default()
.field("date")
.position_def_type(Type::Temporal)
.build()?)
.y(YClassBuilder::default()
.field("price")
.position_def_type(Type::Quantitative)
.build()?)
.build()?,
)
.build()?;
// display the chart using `showata`
chart.show()?;
// print the vega lite spec
eprint!("{}", chart.to_string()?);
Ok(())
}