-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscatterplot.rs
47 lines (41 loc) · 1.32 KB
/
scatterplot.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
use serde::{Deserialize, Serialize};
use std::path::Path;
use vega_lite_4::*;
#[derive(Serialize, Deserialize)]
pub struct Item {
pub x: f64,
pub y: f64,
pub cluster: usize,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// input data: a CSV serialized to a `Vec<Item>`
let mut rdr = csv::Reader::from_path(Path::new("examples/res/data/clustered_data.csv"))?;
let values = rdr
.deserialize()
.collect::<Result<Vec<Item>, csv::Error>>()?;
// the chart
let chart = VegaliteBuilder::default()
.title("Clusters")
.description("Dots colored by their cluster.")
.data(&values)
.mark(Mark::Point)
.encoding(
EdEncodingBuilder::default()
.x(XClassBuilder::default()
.field("x")
.position_def_type(Type::Quantitative)
.build()?)
.y(YClassBuilder::default()
.field("y")
.position_def_type(Type::Quantitative)
.build()?)
.color(ColorClassBuilder::default().field("cluster").build()?)
.build()?,
)
.build()?;
// display the chart using `showata`
chart.show()?;
// print the vega lite spec
eprint!("{}", chart.to_string()?);
Ok(())
}