Skip to main content

plotly_scatterpolar/
plotly_scatterpolar.rs

1use plotlars::polars::prelude::*;
2use plotlars::{CsvReader, Fill, Legend, Line, Mode, Plot, Rgb, ScatterPolar, Shape, Text};
3
4fn main() {
5    // Example 1: Basic scatter polar plot with markers only
6    basic_scatter_polar();
7
8    // Example 2: Lines and markers with custom styling
9    styled_scatter_polar();
10
11    // Example 3: Grouped data with multiple traces
12    grouped_scatter_polar();
13
14    // Example 4: Filled area polar plot
15    filled_scatter_polar();
16}
17
18fn basic_scatter_polar() {
19    // Create sample data - wind direction and speed
20    let directions = vec![0., 45., 90., 135., 180., 225., 270., 315., 360.];
21    let speeds = vec![5.0, 7.5, 10.0, 8.5, 6.0, 4.5, 3.0, 2.5, 5.0];
22
23    let dataset = DataFrame::new(
24        directions.len(),
25        vec![
26            Column::new("direction".into(), directions),
27            Column::new("speed".into(), speeds),
28        ],
29    )
30    .unwrap();
31
32    ScatterPolar::builder()
33        .data(&dataset)
34        .theta("direction")
35        .r("speed")
36        .mode(Mode::Markers)
37        .color(Rgb(65, 105, 225))
38        .shape(Shape::Circle)
39        .size(10)
40        .plot_title(Text::from("Wind Speed by Direction").font("Arial").size(20))
41        .build()
42        .plot();
43}
44
45fn styled_scatter_polar() {
46    // Create sample data - radar chart style
47    let categories = vec![0., 72., 144., 216., 288., 360.];
48    let performance = vec![8.0, 6.5, 7.0, 9.0, 5.5, 8.0];
49
50    let dataset = DataFrame::new(
51        categories.len(),
52        vec![
53            Column::new("category".into(), categories),
54            Column::new("performance".into(), performance),
55        ],
56    )
57    .unwrap();
58
59    ScatterPolar::builder()
60        .data(&dataset)
61        .theta("category")
62        .r("performance")
63        .mode(Mode::LinesMarkers)
64        .color(Rgb(255, 0, 0))
65        .shape(Shape::Diamond)
66        .line(Line::Solid)
67        .width(3.0)
68        .size(12)
69        .opacity(0.8)
70        .plot_title(
71            Text::from("Performance Radar Chart")
72                .font("Arial")
73                .size(22)
74                .x(0.5),
75        )
76        .build()
77        .plot();
78}
79
80fn grouped_scatter_polar() {
81    let dataset = CsvReader::new("data/product_comparison_polar.csv")
82        .finish()
83        .unwrap();
84
85    ScatterPolar::builder()
86        .data(&dataset)
87        .theta("angle")
88        .r("score")
89        .group("product")
90        .mode(Mode::LinesMarkers)
91        .colors(vec![Rgb(255, 99, 71), Rgb(60, 179, 113)])
92        .shapes(vec![Shape::Circle, Shape::Square])
93        .lines(vec![Line::Solid, Line::Dash])
94        .width(2.5)
95        .size(8)
96        .plot_title(Text::from("Product Comparison").font("Arial").size(24))
97        .legend_title(Text::from("Products").font("Arial").size(14))
98        .legend(&Legend::new().x(0.85).y(0.95))
99        .build()
100        .plot();
101}
102
103fn filled_scatter_polar() {
104    // Create sample data - filled area chart
105    let angles: Vec<f64> = (0..=360).step_by(10).map(|x| x as f64).collect();
106    let radii: Vec<f64> = angles
107        .iter()
108        .map(|&angle| 5.0 + 3.0 * (angle * std::f64::consts::PI / 180.0).sin())
109        .collect();
110
111    let dataset = DataFrame::new(
112        angles.len(),
113        vec![
114            Column::new("angle".into(), angles),
115            Column::new("radius".into(), radii),
116        ],
117    )
118    .unwrap();
119
120    ScatterPolar::builder()
121        .data(&dataset)
122        .theta("angle")
123        .r("radius")
124        .mode(Mode::Lines)
125        .fill(Fill::ToSelf)
126        .color(Rgb(135, 206, 250))
127        .line(Line::Solid)
128        .width(2.0)
129        .opacity(0.6)
130        .plot_title(Text::from("Filled Polar Area Chart").font("Arial").size(20))
131        .build()
132        .plot();
133}