timeseriesplot/
timeseriesplot.rs

1use polars::prelude::*;
2
3use plotlars::{Axis, Legend, Line, Plot, Rgb, Shape, Text, TimeSeriesPlot};
4
5fn main() {
6    // Example 1: Revenue and Cost with advanced styling
7    let revenue_dataset = LazyCsvReader::new(PlPath::new("data/revenue_and_cost.csv"))
8        .finish()
9        .unwrap()
10        .select([
11            col("Date").cast(DataType::String),
12            col("Revenue").cast(DataType::Int32),
13            col("Cost").cast(DataType::Int32),
14        ])
15        .collect()
16        .unwrap();
17
18    TimeSeriesPlot::builder()
19        .data(&revenue_dataset)
20        .x("Date")
21        .y("Revenue")
22        .additional_series(vec!["Cost"])
23        .size(8)
24        .colors(vec![Rgb(0, 0, 255), Rgb(255, 0, 0)])
25        .lines(vec![Line::Dash, Line::Solid])
26        .with_shape(true)
27        .shapes(vec![Shape::Circle, Shape::Square])
28        .plot_title(Text::from("Time Series Plot").font("Arial").size(18))
29        .legend(&Legend::new().x(0.05).y(0.9))
30        .x_title("x")
31        .y_title(Text::from("y").color(Rgb(0, 0, 255)))
32        .y_title2(Text::from("y2").color(Rgb(255, 0, 0)))
33        .y_axis(
34            &Axis::new()
35                .value_color(Rgb(0, 0, 255))
36                .show_grid(false)
37                .zero_line_color(Rgb(0, 0, 0)),
38        )
39        .y_axis2(
40            &Axis::new()
41                .axis_side(plotlars::AxisSide::Right)
42                .value_color(Rgb(255, 0, 0))
43                .show_grid(false),
44        )
45        .build()
46        .plot();
47
48    // Example 2: Temperature data with date parsing
49    let temperature_dataset = LazyCsvReader::new(PlPath::new("data/debilt_2023_temps.csv"))
50        .with_has_header(true)
51        .with_try_parse_dates(true)
52        .finish()
53        .unwrap()
54        .with_columns(vec![
55            (col("tavg") / lit(10)).alias("tavg"),
56            (col("tmin") / lit(10)).alias("tmin"),
57            (col("tmax") / lit(10)).alias("tmax"),
58        ])
59        .collect()
60        .unwrap();
61
62    TimeSeriesPlot::builder()
63        .data(&temperature_dataset)
64        .x("date")
65        .y("tavg")
66        .additional_series(vec!["tmin", "tmax"])
67        .colors(vec![Rgb(128, 128, 128), Rgb(0, 122, 255), Rgb(255, 128, 0)])
68        .lines(vec![Line::Solid, Line::Dot, Line::Dot])
69        .plot_title("Temperature at De Bilt (2023)")
70        .legend_title("Legend")
71        .build()
72        .plot();
73}