Skip to main content

scatter_categories/
scatter_categories.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Scatter plot with color-coded categories — no feature flags needed.
3
4use esoc_chart::v2::scatter;
5
6fn main() -> esoc_chart::error::Result<()> {
7    // Three clusters of points
8    let mut x = Vec::new();
9    let mut y = Vec::new();
10    let mut cats = Vec::new();
11
12    // Simple LCG for reproducibility (no deps)
13    let mut seed: u64 = 42;
14    let mut rng = || -> f64 {
15        seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
16        (seed >> 11) as f64 / (1u64 << 53) as f64
17    };
18    let mut normal = || -> f64 {
19        let u1 = rng().max(1e-15);
20        let u2 = rng();
21        (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos()
22    };
23
24    for (cx, cy, label) in [(2.0, 2.0, "Alpha"), (6.0, 6.0, "Beta"), (6.0, 2.0, "Gamma")] {
25        for _ in 0..40 {
26            x.push(cx + normal() * 0.7);
27            y.push(cy + normal() * 0.7);
28            cats.push(label);
29        }
30    }
31
32    let svg = scatter(&x, &y)
33        .color_by(&cats)
34        .title("Three Clusters")
35        .x_label("feature 1")
36        .y_label("feature 2")
37        .to_svg()?;
38
39    std::fs::write("scatter_categories.svg", &svg)?;
40    println!("Saved scatter_categories.svg ({} bytes)", svg.len());
41    Ok(())
42}