ggplot-rs 0.31.4

A Rust implementation of ggplot2's Grammar of Graphics
Documentation
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
//! Binned fill/colour scale with *explicit* class breaks (R's
//! `scale_fill_steps`/`scale_fill_fermenter` generalised to arbitrary,
//! caller-supplied edges — ggplot2's `scale_fill_binned` with `breaks` set
//! "manually"), backing a `::CLASSES` choropleth (sipemu/ggplot-rs#180).
//!
//! The break *maths* — quantile/Fisher-Jenks/equal-width/custom edges —
//! stays the caller's job (anofox-visualization's `map/classes.rs`); this
//! scale only bins against the edges it's given, tallies how many trained
//! values fall in each bin, and draws a stepped legend with those counts
//! plus a "no data" key for `Value::Na` (ggplot2's `na.value`).

use std::collections::HashSet;

use crate::aes::Aesthetic;
use crate::data::Value;
use crate::format::format_value;
use crate::render::backend::FillPattern;

use super::Scale;

/// A coarse, order-sensitive fingerprint of a `train()` batch (FNV-1a over
/// each value's length/discriminant/bits). See [`ScaleFillBinned::seen`]'s
/// doc for why this exists.
fn signature(values: &[Value]) -> u64 {
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    let mut mix = |x: u64| {
        h ^= x;
        h = h.wrapping_mul(0x0000_0100_0000_01b3);
    };
    mix(values.len() as u64);
    for v in values {
        match v {
            Value::Na => mix(1),
            _ => match v.as_f64() {
                Some(f) => {
                    mix(2);
                    mix(f.to_bits());
                }
                None => mix(3),
            },
        }
    }
    h
}

/// Binned fill/colour scale: `n` classes from `n + 1` increasing edges,
/// each with its own colour. See the [module docs](self) for how it
/// relates to `scale_fill_steps`/`::CLASSES`.
#[derive(Clone, Debug)]
pub struct ScaleFillBinned {
    aesthetic: Aesthetic,
    /// `n + 1` increasing edges (`colors.len() == n`).
    breaks: Vec<f64>,
    colors: Vec<(u8, u8, u8)>,
    /// Trained-value count per bin, same indices as `colors`.
    counts: Vec<usize>,
    /// Trained `Value::Na` count.
    na_count: usize,
    /// Fingerprints of every batch already folded into `counts`/`na_count`
    /// (see [`signature`]). `Scale::train` can run more than once over the
    /// *same* layer data — `build.rs`'s own "final scale training pass"
    /// re-trains every layer's already-built data on top of the per-layer
    /// pass `build_layer` just did, and faceting clones a scale set and
    /// retrains it again per panel — which every other scale in this crate
    /// tolerates for free because re-expanding a min/max domain with data
    /// it has already seen is a no-op. A running *count* isn't naturally
    /// idempotent that way, so this scale has to recognise "I've already
    /// counted this exact batch" itself and skip it, rather than double
    /// (or triple) class counts and the no-data count.
    seen: HashSet<u64>,
    /// ggplot2's `na.value` — colour for `Value::Na` and for the legend's
    /// "no data" key. Default grey50 `(127, 127, 127)`. Overridden by
    /// `na_pattern` when that's set (#206).
    na_color: (u8, u8, u8),
    /// A pattern fill (a thin hatch, by default) for `Value::Na` and the
    /// legend's "no data" key, drawn instead of the flat `na_color` by
    /// `geom_sf`/`geom_polygon`/`geom_rect`/`geom_tile` — a classed
    /// choropleth's no-data cells then read as "we have no reading here"
    /// rather than a greyed-out *value* easily mistaken for one (#206).
    /// `None` (the default): `na_color` draws flat, as before.
    na_pattern: Option<FillPattern>,
    na_label: String,
    name: String,
    corrected: bool,
}

/// At least two increasing, finite edges: non-finite values dropped and
/// sorted; fewer than two left falls back to `[0.0, 1.0]` (a single
/// degenerate bin) rather than panicking.
///
/// Equal adjacent edges are kept rather than collapsed (sipemu/ggplot-rs#240):
/// a quantile split (or any other caller-supplied break set) can legitimately
/// repeat an edge, e.g. `[35.0, 150.0, 150.0]` for a class whose upper bin
/// holds only the maximum. Deduping such edges away used to merge two
/// classes into one and change the caller's requested class count; keeping
/// them instead produces a *zero-width* bin, which [`bin_index`] and
/// [`ScaleFillBinned::class_label`] give dedicated handling.
fn sorted_edges(breaks: Vec<f64>) -> (Vec<f64>, bool) {
    let mut v: Vec<f64> = breaks.into_iter().filter(|f| f.is_finite()).collect();
    v.sort_by(f64::total_cmp);
    if v.len() >= 2 {
        (v, false)
    } else {
        (vec![0.0, 1.0], true)
    }
}

/// Linear RGB interpolation at `t` in `[0, 1]`.
fn lerp_color(low: (u8, u8, u8), high: (u8, u8, u8), t: f64) -> (u8, u8, u8) {
    let t = t.clamp(0.0, 1.0);
    let lerp = |a: u8, b: u8| (a as f64 + (b as f64 - a as f64) * t).round() as u8;
    (
        lerp(low.0, high.0),
        lerp(low.1, high.1),
        lerp(low.2, high.2),
    )
}

/// `n` colours evenly interpolated from `low` to `high` (`n == 1` picks the
/// midpoint).
fn default_colors(n: usize, low: (u8, u8, u8), high: (u8, u8, u8)) -> Vec<(u8, u8, u8)> {
    (0..n)
        .map(|i| {
            let t = if n <= 1 {
                0.5
            } else {
                i as f64 / (n - 1) as f64
            };
            lerp_color(low, high, t)
        })
        .collect()
}

/// This crate's default two-colour ramp (matches [`super::steps::ScaleColorSteps`]'s).
const DEFAULT_LOW: (u8, u8, u8) = (50, 50, 200);
const DEFAULT_HIGH: (u8, u8, u8) = (200, 50, 50);

impl ScaleFillBinned {
    /// `n + 1` increasing `breaks` with an explicit colour per bin
    /// (`colors.len()` should be `breaks.len() - 1`; a mismatch falls back
    /// to the default two-colour ramp and is reported by
    /// [`validate`](Scale::validate)).
    pub fn new(aesthetic: Aesthetic, breaks: Vec<f64>, colors: Vec<(u8, u8, u8)>) -> Self {
        let (breaks, edges_corrected) = sorted_edges(breaks);
        let n = breaks.len() - 1;
        let (colors, colors_corrected) = if colors.len() == n {
            (colors, false)
        } else {
            (default_colors(n, DEFAULT_LOW, DEFAULT_HIGH), true)
        };
        ScaleFillBinned {
            aesthetic,
            breaks,
            colors,
            counts: vec![0; n],
            na_count: 0,
            seen: HashSet::new(),
            na_color: (127, 127, 127),
            na_pattern: None,
            na_label: "no data".to_string(),
            name: String::new(),
            corrected: edges_corrected || colors_corrected,
        }
    }

    /// `n + 1` increasing `breaks`, coloured by interpolating `low`→`high`
    /// across the `n` bins (R's `scale_fill_steps(breaks = ...)`).
    pub fn two(
        aesthetic: Aesthetic,
        breaks: Vec<f64>,
        low: (u8, u8, u8),
        high: (u8, u8, u8),
    ) -> Self {
        let (breaks, corrected) = sorted_edges(breaks);
        let n = breaks.len() - 1;
        ScaleFillBinned {
            aesthetic,
            colors: default_colors(n, low, high),
            counts: vec![0; n],
            breaks,
            na_count: 0,
            seen: HashSet::new(),
            na_color: (127, 127, 127),
            na_pattern: None,
            na_label: "no data".to_string(),
            name: String::new(),
            corrected,
        }
    }

    /// Colour for `Value::Na` and the legend's "no data" key (ggplot2's
    /// `na.value`). Default grey50 `(127, 127, 127)`. Overridden for
    /// drawing (not for the legend count) by [`na_pattern`](Self::na_pattern)
    /// when that's also set.
    pub fn na_color(mut self, color: (u8, u8, u8)) -> Self {
        self.na_color = color;
        self
    }

    /// A pattern fill for `Value::Na` and the legend's "no data" key,
    /// drawn instead of the flat [`na_color`](Self::na_color) — a classed
    /// choropleth's no-data cells read as "no reading", not a muted
    /// *value* (#206). `FillPattern::stripe(fg, bg)` is a reasonable
    /// default hatch; `bg` is what a pattern-less backend (`plotters`)
    /// falls back to.
    pub fn na_pattern(mut self, pattern: FillPattern) -> Self {
        self.na_pattern = Some(pattern);
        self
    }

    /// Label for the "no data" legend key. Default `"no data"`.
    pub fn na_label(mut self, label: &str) -> Self {
        self.na_label = label.to_string();
        self
    }

    /// The bin index for `v` (clamped into range first).
    ///
    /// An ordinary bin `i` is half-open, `[breaks[i], breaks[i+1])`; the
    /// last bin is closed at its upper edge too, so the maximum
    /// trained/data value lands in it rather than falling just outside
    /// every bin. A **zero-width** bin (`breaks[i] == breaks[i+1]`, e.g.
    /// from a quantile split whose top class holds only the maximum,
    /// sipemu/ggplot-rs#240) is closed at *both* ends instead, so it
    /// captures values equal to its single shared edge rather than losing
    /// them to whichever neighbour the half-open rule would otherwise hand
    /// them to. Bins are scanned low to high and the first match wins, so
    /// when several consecutive bins share the same point the lowest-index
    /// one claims it (ggplot2's `cut(..., right = TRUE)` convention, with
    /// the first bin additionally closed on the left so the global minimum
    /// is never dropped).
    fn bin_index(&self, v: f64) -> usize {
        let n = self.colors.len();
        let v = v.clamp(self.breaks[0], self.breaks[n]);
        for i in 0..n {
            let (lo, hi) = (self.breaks[i], self.breaks[i + 1]);
            let hit = if i == n - 1 {
                v >= lo && v <= hi // last bin: closed at both ends
            } else if lo == hi {
                v == lo // zero-width bin: closed at both ends
            } else {
                v >= lo && v < hi // ordinary bin: half-open
            };
            if hit {
                return i;
            }
        }
        n - 1
    }

    /// `"{lo}–{hi} ({count})"` for bin `i`, or just `"{v} ({count})"` when
    /// `i` is a zero-width bin (`breaks[i] == breaks[i + 1]`, #240) — a
    /// range like "150–150" would misstate a bin that in fact holds only
    /// the single value 150.
    fn class_label(&self, i: usize) -> String {
        let (lo, hi) = (self.breaks[i], self.breaks[i + 1]);
        if lo == hi {
            format!("{} ({})", format_value(&Value::Float(lo)), self.counts[i])
        } else {
            format!(
                "{}–{} ({})",
                format_value(&Value::Float(lo)),
                format_value(&Value::Float(hi)),
                self.counts[i]
            )
        }
    }

    /// `"{na_label} ({na_count})"`.
    fn na_label_with_count(&self) -> String {
        format!("{} ({})", self.na_label, self.na_count)
    }
}

impl Scale for ScaleFillBinned {
    fn aesthetic(&self) -> Aesthetic {
        self.aesthetic.clone()
    }

    fn train(&mut self, values: &[Value]) {
        // See `seen`'s doc: the pipeline can submit the exact same layer's
        // data to `train` more than once, which every other scale's
        // min/max expansion tolerates for free but a running count can't.
        if !self.seen.insert(signature(values)) {
            return;
        }
        for v in values {
            match v {
                Value::Na => self.na_count += 1,
                _ => {
                    if let Some(f) = v.as_f64() {
                        if f.is_finite() {
                            let i = self.bin_index(f);
                            self.counts[i] += 1;
                        }
                    }
                }
            }
        }
    }

    /// Not used for position — like [`super::steps::ScaleColorSteps`], the
    /// legend looks a value's colour up by its break label instead.
    fn map(&self, _value: &Value) -> f64 {
        0.0
    }

    fn breaks(&self) -> Vec<(f64, String)> {
        let n = self.colors.len();
        let (lo0, hi_n) = (self.breaks[0], self.breaks[n]);
        let span = (hi_n - lo0).max(f64::EPSILON);
        let mut out: Vec<(f64, String)> = (0..n)
            .map(|i| {
                let center = (self.breaks[i] + self.breaks[i + 1]) / 2.0;
                ((center - lo0) / span, self.class_label(i))
            })
            .collect();
        if self.na_count > 0 {
            out.push((1.0, self.na_label_with_count()));
        }
        out
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn set_name(&mut self, name: &str) {
        self.name = name.to_string();
    }

    fn is_discrete(&self) -> bool {
        true
    }

    fn map_to_color(&self, value: &Value) -> Option<(u8, u8, u8)> {
        if matches!(value, Value::Na) {
            return Some(self.na_color);
        }
        if let Some(f) = value.as_f64() {
            if f.is_finite() {
                return Some(self.colors[self.bin_index(f)]);
            }
        }
        if let Value::Str(s) = value {
            if s == &self.na_label_with_count() {
                return Some(self.na_color);
            }
            for i in 0..self.colors.len() {
                if self.class_label(i) == *s {
                    return Some(self.colors[i]);
                }
            }
        }
        None
    }

    fn domain(&self) -> Option<(f64, f64)> {
        let n = self.colors.len();
        Some((self.breaks[0], self.breaks[n]))
    }

    /// This scale's configured `na_pattern`, if any — the caller (a
    /// geom's fill resolution, or the legend's "no data" key) only
    /// consults this for a row/key that's already otherwise headed for
    /// the flat `na_color` (`Value::Na`, or the "no data" legend-key
    /// label itself); every ordinary bin colour is unaffected (#206).
    fn na_fill_pattern(&self) -> Option<FillPattern> {
        self.na_pattern
    }

    fn is_na_break(&self, label: &str) -> bool {
        self.na_count > 0 && label == self.na_label_with_count()
    }

    fn validate(&self, warnings: &mut Vec<String>) {
        if self.corrected {
            warnings.push(
                "scale_fill_binned: breaks need >= 2 increasing finite edges, with one colour \
                 per bin — falling back to a default range/palette"
                    .to_string(),
            );
        }
    }

    fn clone_box(&self) -> Box<dyn Scale> {
        Box::new(self.clone())
    }

    fn reset_training(&mut self) {
        self.counts.iter_mut().for_each(|c| *c = 0);
        self.na_count = 0;
        self.seen.clear();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn scale() -> ScaleFillBinned {
        ScaleFillBinned::new(
            Aesthetic::Fill,
            vec![0.0, 10.0, 50.0, 100.0],
            vec![(0, 0, 255), (0, 255, 0), (255, 0, 0)],
        )
    }

    #[test]
    fn bins_are_half_open_except_the_last() {
        let s = scale();
        assert_eq!(s.bin_index(0.0), 0);
        assert_eq!(s.bin_index(9.999), 0);
        assert_eq!(s.bin_index(10.0), 1); // right edge moves to the next bin...
        assert_eq!(s.bin_index(49.999), 1);
        assert_eq!(s.bin_index(50.0), 2);
        assert_eq!(s.bin_index(100.0), 2); // ...except the very last, which is closed
        assert_eq!(
            s.bin_index(1000.0),
            2,
            "out-of-range clamps to the last bin"
        );
        assert_eq!(
            s.bin_index(-50.0),
            0,
            "out-of-range clamps to the first bin"
        );
    }

    #[test]
    fn train_tallies_counts_and_na_separately() {
        let mut s = scale();
        s.train(&[
            Value::Float(1.0),
            Value::Float(5.0),
            Value::Float(20.0),
            Value::Float(99.0),
            Value::Float(100.0),
            Value::Na,
            Value::Na,
        ]);
        assert_eq!(s.counts, vec![2, 1, 2]);
        assert_eq!(s.na_count, 2);
    }

    /// `build.rs`'s own "final scale training pass" (and, for a faceted
    /// plot, `compute_panel_scales`) re-submits a layer's already-trained
    /// data to `Scale::train` a second time — every other scale tolerates
    /// that for free (re-expanding a min/max domain with data it's already
    /// seen is a no-op); a running count must recognise the repeat itself.
    #[test]
    fn training_the_same_batch_twice_does_not_double_count() {
        let batch = [Value::Float(1.0), Value::Float(20.0), Value::Na];
        let mut s = scale();
        s.train(&batch);
        s.train(&batch); // the pipeline's redundant re-training pass
        assert_eq!(s.counts, vec![1, 1, 0]);
        assert_eq!(s.na_count, 1);
        // A genuinely different batch afterward still counts normally.
        s.train(&[Value::Float(60.0)]);
        assert_eq!(s.counts, vec![1, 1, 1]);
    }

    #[test]
    fn breaks_label_each_class_with_its_count_and_na_with_its_own() {
        let mut s = scale();
        s.train(&[Value::Float(1.0), Value::Float(20.0), Value::Na]);
        let breaks = s.breaks();
        let labels: Vec<&str> = breaks.iter().map(|(_, l)| l.as_str()).collect();
        assert_eq!(
            labels,
            vec!["0–10 (1)", "10–50 (1)", "50–100 (0)", "no data (1)"]
        );
    }

    #[test]
    fn no_na_key_when_nothing_is_missing() {
        let mut s = scale();
        s.train(&[Value::Float(1.0)]);
        assert_eq!(s.breaks().len(), 3, "no trailing no-data entry");
    }

    #[test]
    fn map_to_color_matches_the_bin_and_roundtrips_legend_labels() {
        let mut s = scale();
        s.train(&[Value::Float(1.0), Value::Float(20.0), Value::Na]);
        assert_eq!(s.map_to_color(&Value::Float(5.0)), Some((0, 0, 255)));
        assert_eq!(s.map_to_color(&Value::Float(20.0)), Some((0, 255, 0)));
        assert_eq!(s.map_to_color(&Value::Float(75.0)), Some((255, 0, 0)));
        assert_eq!(s.map_to_color(&Value::Na), Some((127, 127, 127)));
        for (_, label) in s.breaks() {
            let via_label = s.map_to_color(&Value::Str(label.clone()));
            assert!(via_label.is_some(), "{label} should round-trip to a colour");
        }
    }

    #[test]
    fn na_color_and_label_are_configurable() {
        let mut s = scale().na_color((10, 20, 30)).na_label("missing");
        s.train(&[Value::Na]);
        assert_eq!(s.map_to_color(&Value::Na), Some((10, 20, 30)));
        assert!(s.breaks().iter().any(|(_, l)| l == "missing (1)"));
        assert_eq!(
            s.map_to_color(&Value::Str("missing (1)".to_string())),
            Some((10, 20, 30))
        );
    }

    #[test]
    fn na_pattern_is_none_until_set() {
        let s = scale();
        assert_eq!(s.na_fill_pattern(), None);
    }

    #[test]
    fn na_pattern_is_reported_only_for_the_no_data_break_label() {
        let pattern = FillPattern::stripe((10, 20, 30), (200, 210, 220));
        let mut s = scale().na_pattern(pattern);
        s.train(&[Value::Na, Value::Float(5.0)]);
        assert_eq!(s.na_fill_pattern(), Some(pattern));
        assert!(s.is_na_break(&s.na_label_with_count()));
        // An ordinary bin's own label is never mistaken for the na key.
        let ordinary = s.class_label(0);
        assert!(!s.is_na_break(&ordinary));
    }

    #[test]
    fn no_na_break_when_nothing_trained_as_na() {
        // `na_count == 0`: `is_na_break` is false even for the label text
        // that *would* be the na key, since the legend never shows one.
        let s = scale().na_pattern(FillPattern::stripe((1, 2, 3), (4, 5, 6)));
        assert!(!s.is_na_break(&s.na_label_with_count()));
    }

    #[test]
    fn two_interpolates_colors_across_bins() {
        let s = ScaleFillBinned::two(
            Aesthetic::Fill,
            vec![0.0, 25.0, 50.0, 75.0, 100.0],
            (0, 0, 0),
            (255, 255, 255),
        );
        assert_eq!(s.colors[0], (0, 0, 0));
        assert_eq!(s.colors[3], (255, 255, 255));
        assert!(s.colors[1].0 < s.colors[2].0);
    }

    #[test]
    fn mismatched_colors_fall_back_and_warn() {
        let s = ScaleFillBinned::new(
            Aesthetic::Fill,
            vec![0.0, 10.0, 20.0],
            vec![(0, 0, 0)], // 2 bins need 2 colours, only 1 given
        );
        assert_eq!(s.colors.len(), 2);
        let mut warnings = Vec::new();
        s.validate(&mut warnings);
        assert_eq!(warnings.len(), 1);
    }

    #[test]
    fn too_few_breaks_falls_back_to_a_single_bin_rather_than_panicking() {
        let s = ScaleFillBinned::new(Aesthetic::Fill, vec![5.0], vec![]);
        assert_eq!(s.breaks.len(), 2);
        assert_eq!(s.colors.len(), 1);
        let mut warnings = Vec::new();
        s.validate(&mut warnings);
        assert_eq!(warnings.len(), 1);
    }

    #[test]
    fn domain_spans_the_outer_edges() {
        let s = scale();
        assert_eq!(s.domain(), Some((0.0, 100.0)));
    }

    #[test]
    fn reset_training_clears_counts_but_keeps_breaks_and_colors() {
        let mut s = scale();
        s.train(&[Value::Float(1.0), Value::Na]);
        s.reset_training();
        assert_eq!(s.counts, vec![0, 0, 0]);
        assert_eq!(s.na_count, 0);
        assert_eq!(s.breaks, vec![0.0, 10.0, 50.0, 100.0]);
    }

    // --- #240: duplicate (zero-width) class edges ----------------------

    /// A duplicate edge at the *maximum* — the motivating case from #240
    /// (a quantile split whose top class holds only the maximum, e.g.
    /// `[35, 150, 150]` over two cells 35 and 150).
    #[test]
    fn duplicate_edge_at_the_max_keeps_both_classes_and_colours() {
        let mut s = ScaleFillBinned::new(
            Aesthetic::Fill,
            vec![35.0, 150.0, 150.0],
            vec![(0, 0, 255), (255, 0, 0)],
        );
        assert!(
            !s.corrected,
            "a legitimate zero-width bin is not a fallback"
        );
        assert_eq!(s.colors.len(), 2, "caller's requested class count is kept");
        assert_eq!(
            s.colors,
            vec![(0, 0, 255), (255, 0, 0)],
            "distinct colours per class"
        );

        s.train(&[Value::Float(35.0), Value::Float(150.0)]);
        assert_eq!(s.counts, vec![1, 1], "each cell counted into its own class");
        assert_eq!(s.bin_index(35.0), 0);
        assert_eq!(
            s.bin_index(150.0),
            1,
            "the zero-width bin claims its own edge value"
        );
        assert_eq!(s.map_to_color(&Value::Float(150.0)), Some((255, 0, 0)));

        let breaks = s.breaks();
        let labels: Vec<&str> = breaks.iter().map(|(_, l)| l.as_str()).collect();
        assert_eq!(
            labels,
            vec!["35–150 (1)", "150 (1)"],
            "the zero-width class's label/count is a single value, not a 150–150 range"
        );
    }

    /// A duplicate edge at the *minimum* — the lowest bin is zero-width and
    /// must still claim the global minimum (left-closed first bin).
    #[test]
    fn duplicate_edge_at_the_min_keeps_both_classes_and_colours() {
        let mut s = ScaleFillBinned::new(
            Aesthetic::Fill,
            vec![0.0, 0.0, 10.0],
            vec![(0, 0, 255), (255, 0, 0)],
        );
        assert!(!s.corrected);
        assert_eq!(s.colors.len(), 2);

        s.train(&[Value::Float(0.0), Value::Float(0.0), Value::Float(5.0)]);
        assert_eq!(s.counts, vec![2, 1]);
        assert_eq!(s.bin_index(0.0), 0, "the zero-width bin claims the minimum");
        assert_eq!(s.bin_index(5.0), 1);
        assert_eq!(s.map_to_color(&Value::Float(0.0)), Some((0, 0, 255)));

        let breaks = s.breaks();
        let labels: Vec<&str> = breaks.iter().map(|(_, l)| l.as_str()).collect();
        assert_eq!(labels, vec!["0 (2)", "0–10 (1)"]);
    }

    /// A duplicate edge in the *middle* — between two otherwise ordinary
    /// classes.
    #[test]
    fn duplicate_edge_in_the_middle_keeps_all_classes_and_colours() {
        let mut s = ScaleFillBinned::new(
            Aesthetic::Fill,
            vec![0.0, 50.0, 50.0, 100.0],
            vec![(0, 0, 255), (0, 255, 0), (255, 0, 0)],
        );
        assert!(!s.corrected);
        assert_eq!(s.colors.len(), 3, "all three requested classes survive");
        assert_eq!(s.colors, vec![(0, 0, 255), (0, 255, 0), (255, 0, 0)]);

        s.train(&[Value::Float(25.0), Value::Float(50.0), Value::Float(75.0)]);
        assert_eq!(
            s.counts,
            vec![1, 1, 1],
            "the shared edge value goes to the middle (zero-width) bin"
        );
        assert_eq!(s.bin_index(25.0), 0);
        assert_eq!(
            s.bin_index(50.0),
            1,
            "the zero-width middle bin wins the tie over its neighbours"
        );
        assert_eq!(s.bin_index(75.0), 2);
        assert_eq!(s.map_to_color(&Value::Float(50.0)), Some((0, 255, 0)));

        let breaks = s.breaks();
        let labels: Vec<&str> = breaks.iter().map(|(_, l)| l.as_str()).collect();
        assert_eq!(labels, vec!["0–50 (1)", "50 (1)", "50–100 (1)"]);
    }

    /// The fix must not perturb the ordinary (non-degenerate) case this
    /// crate already shipped: same inputs, same bins/colours/labels as
    /// before #240.
    #[test]
    fn non_degenerate_edges_are_unaffected() {
        let s = scale();
        assert_eq!(s.breaks, vec![0.0, 10.0, 50.0, 100.0]);
        assert_eq!(s.colors, vec![(0, 0, 255), (0, 255, 0), (255, 0, 0)]);
        assert_eq!(s.bin_index(0.0), 0);
        assert_eq!(s.bin_index(9.999), 0);
        assert_eq!(s.bin_index(10.0), 1);
        assert_eq!(s.bin_index(49.999), 1);
        assert_eq!(s.bin_index(50.0), 2);
        assert_eq!(s.bin_index(100.0), 2);
    }
}