chartlet 0.14.0

Compile structured data into deterministic, accessible static charts
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
use super::{LABEL_LINE, LABEL_SIZE, count, fit_text, title::two_lines};
use crate::{
    error::ChartWarning,
    metrics::TextMetrics,
    scene::{Element, Text, TextAnchor},
    spec::{AxisScale, ChartSpec, Locale, NumberStyle, ValueAxisSpec, ValueFormat},
};

/// Centers the category axis title below a plot whose categories run horizontally.
pub(crate) fn add_bottom_category_title(
    spec: &ChartSpec,
    left: f64,
    plot_width: f64,
    chart_height: f64,
    elements: &mut Vec<Element>,
    warnings: &mut Vec<ChartWarning>,
    metrics: &impl TextMetrics,
) {
    if let Some(title) = &spec.category_axis.title {
        let title = fit_text(
            title,
            plot_width,
            LABEL_SIZE,
            metrics,
            warnings,
            "/categoryAxis/title",
        );
        elements.push(Element::Text(Text {
            x: left + plot_width / 2.0,
            y: chart_height - 14.0,
            class: "chartlet-axis-title",
            anchor: TextAnchor::Middle,
            content: title,
        }));
    }
}

/// The lines of a category label: one if it fits `max_width`, otherwise two, broken at a space,
/// before anything is shortened.
fn label_lines(
    label: &str,
    max_width: f64,
    metrics: &impl TextMetrics,
    warnings: &mut Vec<ChartWarning>,
    path: &str,
) -> Vec<String> {
    match two_lines(label, max_width, LABEL_SIZE, metrics) {
        Some((first, rest)) => vec![
            first.to_owned(),
            fit_text(rest, max_width, LABEL_SIZE, metrics, warnings, path),
        ],
        None => vec![fit_text(
            label, max_width, LABEL_SIZE, metrics, warnings, path,
        )],
    }
}

/// Above this many categories an axis may label only every few of them.
const DENSE_CATEGORIES: usize = 16;

/// How many categories one label covers on an axis that runs horizontally with bands `band`
/// wide: 1, every category labelled, unless there are more than [`DENSE_CATEGORIES`] and some
/// label would not fit its band even on two lines; then every `step`-th category is labelled, with
/// the room of `step` bands. The data table still lists every category.
pub(crate) fn label_step(categories: &[String], band: f64, metrics: &impl TextMetrics) -> usize {
    if categories.len() <= DENSE_CATEGORIES {
        return 1;
    }
    let fits = |label: &str, room: f64| {
        metrics.width(label, LABEL_SIZE) <= room
            || two_lines(label, room, LABEL_SIZE, metrics)
                .is_some_and(|(_, rest)| metrics.width(rest, LABEL_SIZE) <= room)
    };
    (1..categories.len())
        .find(|step| {
            categories
                .iter()
                .all(|label| fits(label, band * count(*step) - 8.0))
        })
        .unwrap_or(categories.len())
}

/// Draws the label of category `index` below the plot if the axis labels it, see [`label_step`],
/// with the room of `step` bands.
#[allow(clippy::too_many_arguments)]
pub(crate) fn push_stepped_category_label(
    elements: &mut Vec<Element>,
    label: &str,
    at: (f64, f64),
    (index, step, band): (usize, usize, f64),
    metrics: &impl TextMetrics,
    warnings: &mut Vec<ChartWarning>,
    path: &str,
) {
    if index.is_multiple_of(step) {
        let room = (band * count(step) - 8.0).max(20.0);
        push_category_label(elements, label, at, room, metrics, warnings, path);
    }
}

/// Reports that a dense axis labels only every `step`-th category.
pub(crate) fn warn_if_labels_thinned(
    spec: &ChartSpec,
    step: usize,
    warnings: &mut Vec<ChartWarning>,
) {
    if step > 1 {
        // The categories come from `data` or, with several series, from `categories`.
        let path = if spec.data.is_empty() {
            "/categories"
        } else {
            "/data"
        };
        warnings.push(ChartWarning::new(
            "labels_thinned",
            path,
            format!(
                "one category in {step} is labelled, so that the labels fit; the data table lists every category"
            ),
        ));
    }
}

/// Draws the label of a category below a plot whose categories run horizontally, centered at
/// `x` with its first baseline at `y`, on up to two lines, see [`label_lines`].
pub(crate) fn push_category_label(
    elements: &mut Vec<Element>,
    label: &str,
    (x, y): (f64, f64),
    max_width: f64,
    metrics: &impl TextMetrics,
    warnings: &mut Vec<ChartWarning>,
    path: &str,
) {
    let lines = label_lines(label, max_width, metrics, warnings, path);
    for (index, content) in lines.into_iter().enumerate() {
        elements.push(Element::Text(Text {
            x,
            y: y + count(index) * LABEL_LINE,
            class: "chartlet-label",
            anchor: TextAnchor::Middle,
            content,
        }));
    }
}

/// Draws the label of a category left of a plot whose categories run vertically, ending at `x`
/// and centered on the band around `center`. A band too narrow for two lines keeps the label on
/// one, shortened if need be; otherwise see [`label_lines`].
pub(crate) fn push_side_label(
    elements: &mut Vec<Element>,
    label: &str,
    (x, center): (f64, f64),
    (max_width, band): (f64, f64),
    metrics: &impl TextMetrics,
    warnings: &mut Vec<ChartWarning>,
    path: &str,
) {
    let lines = if band >= 2.0 * LABEL_LINE + 4.0 {
        label_lines(label, max_width, metrics, warnings, path)
    } else {
        vec![fit_text(
            label, max_width, LABEL_SIZE, metrics, warnings, path,
        )]
    };
    let first = center + 4.0 - count(lines.len() - 1) * LABEL_LINE / 2.0;
    for (index, content) in lines.into_iter().enumerate() {
        elements.push(Element::Text(Text {
            x,
            y: first + count(index) * LABEL_LINE,
            class: "chartlet-label",
            anchor: TextAnchor::End,
            content,
        }));
    }
}

/// Writes a value for reading: percent scaled and suffixed, fixed decimals if the style asks for
/// them, a true minus sign (U+2212, which screen readers announce as “minus”), and the locale's
/// decimal separator.
pub(crate) fn format_value(value: f64, style: impl Into<NumberStyle>) -> String {
    let style = style.into();
    let value = match style.format {
        ValueFormat::Number => value,
        ValueFormat::Percent => value * 100.0,
    };
    let digits = match style.decimals {
        Some(decimals) => format!("{:.*}", usize::from(decimals), tidy(value)),
        None => format!("{}", tidy(value)),
    };
    // Rounding to fixed decimals can turn a small negative value into `-0.0`.
    let digits = if digits.starts_with('-') && digits.trim_start_matches(['-', '0', '.']).is_empty()
    {
        digits[1..].to_owned()
    } else {
        digits
    };
    let digits = digits.replace('-', "\u{2212}");
    let digits = if style.thousands {
        separate_thousands(&digits)
    } else {
        digits
    };
    let digits = match style.locale {
        Locale::En => digits,
        Locale::De => digits
            .chars()
            .map(|c| match c {
                '.' => ',',
                ',' => '.',
                c => c,
            })
            .collect(),
    };
    let suffix = match (style.format, style.locale) {
        (ValueFormat::Number, _) => "",
        (ValueFormat::Percent, Locale::En) => "%",
        (ValueFormat::Percent, Locale::De) => "\u{202f}%",
    };
    format!("{digits}{suffix}")
}

/// Puts a comma between every three digits of the integer part: `\u{2212}12345.6` becomes
/// `\u{2212}12,345.6`.
fn separate_thousands(digits: &str) -> String {
    let start = digits.find(|c: char| c.is_ascii_digit()).unwrap_or(0);
    let end = digits[start..]
        .find(|c: char| !c.is_ascii_digit())
        .map_or(digits.len(), |offset| start + offset);
    let integer = &digits[start..end];
    let mut grouped = String::with_capacity(digits.len() + integer.len() / 3);
    grouped.push_str(&digits[..start]);
    for (index, digit) in integer.chars().enumerate() {
        if index > 0 && (integer.len() - index).is_multiple_of(3) {
            grouped.push(',');
        }
        grouped.push(digit);
    }
    grouped.push_str(&digits[end..]);
    grouped
}

/// Writes an axis tick of an axis that starts at `start` and ticks every `step`: as many decimals
/// as the step or the start needs, whichever is more, so that every tick of an axis carries the
/// same number of digits (`0.0, 0.5, 1.0` rather than `0, 0.5, 1`) and −2.5, −1.5 … stay apart.
pub(crate) fn format_tick(value: f64, step: f64, start: f64, style: NumberStyle) -> String {
    let scaled = |number: f64| match style.format {
        ValueFormat::Number => number,
        ValueFormat::Percent => number * 100.0,
    };
    let decimals_of = |number: f64| {
        format!("{}", tidy(scaled(number)))
            .split_once('.')
            .map_or(0, |(_, fraction)| fraction.len())
    };
    let decimals = decimals_of(step).max(decimals_of(start));
    format_value(
        value,
        NumberStyle {
            decimals: Some(u8::try_from(decimals).unwrap_or(u8::MAX)),
            ..style
        },
    )
}

/// Removes binary floating-point noise (`0.07 * 100`, `0.1 + 0.2`) by keeping 12 significant
/// digits, and normalizes `-0.0`. Rust's float formatting does not depend on the platform, so
/// the result stays deterministic.
fn tidy(value: f64) -> f64 {
    let rounded: f64 = format!("{value:.11e}")
        .parse()
        .expect("a formatted float parses back");
    if rounded == 0.0 { 0.0 } else { rounded }
}

/// A round tick distance for a range: 1, 2, 5 or 10 times a power of ten, about five ticks.
fn nice_step(range: f64) -> f64 {
    let raw_step = range / 5.0;
    let magnitude = 10.0_f64.powf(raw_step.log10().floor());
    let fraction = raw_step / magnitude;
    let nice_fraction = if fraction <= 1.0 {
        1.0
    } else if fraction <= 2.0 {
        2.0
    } else if fraction <= 5.0 {
        5.0
    } else {
        10.0
    };
    nice_fraction * magnitude
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct NumericScale {
    min: f64,
    max: f64,
    pub step: f64,
    /// Powers of ten evenly spaced instead of values; `min` is then a power of ten and `max` one
    /// times 1, 2 or 5 of one.
    log: bool,
    /// Larger values toward the start of the axis instead of its end.
    reversed: bool,
}

impl NumericScale {
    /// The scale of `axis`: linear over the values, or logarithmic when the axis says so. A
    /// logarithmic scale never includes zero.
    pub(crate) fn for_axis(
        values: impl Iterator<Item = f64>,
        include_zero: bool,
        axis: &ValueAxisSpec,
    ) -> Self {
        let scale = match (axis.scale, axis.exact, axis.min, axis.max) {
            (AxisScale::Log, ..) => Self::logarithmic(values, axis.bounds()),
            // Validated: every value lies between the exact ends.
            (AxisScale::Linear, true, Some(min), Some(max)) => Self {
                min,
                max,
                step: axis.step.unwrap_or_else(|| nice_step(max - min)),
                log: false,
                reversed: false,
            },
            (AxisScale::Linear, ..) => {
                let scale = Self::from_values(values, include_zero, axis.bounds());
                match axis.step {
                    Some(step) => Self {
                        min: tidy(tidy(scale.min / step).floor() * step),
                        max: tidy(tidy(scale.max / step).ceil() * step),
                        step,
                        ..scale
                    },
                    None => scale,
                }
            }
        };
        Self {
            reversed: axis.reverse,
            ..scale
        }
    }

    /// A logarithmic scale from the power of ten at or below the smallest value to the next 1, 2
    /// or 5 times a power of ten at or above the largest; validated values are all above zero.
    fn logarithmic(
        values: impl Iterator<Item = f64>,
        (declared_min, declared_max): (Option<f64>, Option<f64>),
    ) -> Self {
        let (mut min, mut max) = values.fold((f64::INFINITY, 0.0_f64), |(min, max), value| {
            (min.min(value), max.max(value))
        });
        min = declared_min.map_or(min, |bound| min.min(bound));
        max = declared_max.map_or(max, |bound| max.max(bound));
        let low = tidy(10.0_f64.powf(min.log10().floor()));
        // The axis ends at the next 1, 2 or 5 times a power of ten, not at the next power of ten,
        // so that it does not leave almost a decade empty.
        let decade = 10.0_f64.powf(max.log10().floor());
        let high = [1.0, 2.0, 5.0, 10.0]
            .into_iter()
            .map(|multiple| tidy(decade * multiple))
            .find(|end| *end >= max * (1.0 - 1e-9))
            .expect("ten times the decade of a value is above it");
        Self {
            min: low,
            max: if high <= low { tidy(low * 10.0) } else { high },
            step: 1.0,
            log: true,
            reversed: false,
        }
    }

    /// Whether the scale spaces powers of ten evenly.
    pub(crate) const fn is_log(self) -> bool {
        self.log
    }

    /// Where bars start: zero, or the bottom of a logarithmic axis.
    pub(crate) const fn base(self) -> f64 {
        if self.log { self.min } else { 0.0 }
    }

    /// Whether a tick is the zero line, which is drawn stronger.
    pub(crate) fn is_zero(self, value: f64) -> bool {
        !self.log && value.abs() < self.step / 100.0
    }

    /// The text of a tick: as many decimals as the step on a linear axis, as many as the power
    /// of ten needs on a logarithmic one.
    pub(crate) fn tick_label(self, value: f64, style: NumberStyle) -> String {
        if !self.log {
            return format_tick(value, self.step, self.first_tick(), style);
        }
        // A power of ten below one needs a decimal for each step down: 0.1, 0.01.
        let mut decimals = 0_u8;
        let mut scaled = value;
        while scaled < 1.0 - 1e-9 && decimals < 6 {
            scaled *= 10.0;
            decimals += 1;
        }
        format_value(
            value,
            NumberStyle {
                decimals: Some(decimals),
                ..style
            },
        )
    }
    /// A scale over the values that also reaches the declared `(min, max)` of the value axis.
    /// Without zero in the scale, the values get a margin, which neither crosses zero nor goes
    /// beyond a declared bound; every end is then rounded outward to a tick.
    pub(crate) fn from_values(
        mut values: impl Iterator<Item = f64>,
        include_zero: bool,
        (declared_min, declared_max): (Option<f64>, Option<f64>),
    ) -> Self {
        let first = values.next().expect("validated charts contain values");
        let mut min = if include_zero { first.min(0.0) } else { first };
        let mut max = if include_zero { first.max(0.0) } else { first };
        for value in values {
            min = min.min(value);
            max = max.max(value);
        }
        min = declared_min.map_or(min, |bound| min.min(bound));
        max = declared_max.map_or(max, |bound| max.max(bound));
        if (max - min).abs() < f64::EPSILON {
            if max.abs() < f64::EPSILON {
                min = -1.0;
                max = 1.0;
            } else {
                let padding = max.abs() * 0.2;
                min -= padding;
                max += padding;
            }
        } else if !include_zero {
            let padding = (max - min) * 0.05;
            if declared_min.is_none() {
                min = if min >= 0.0 {
                    (min - padding).max(0.0)
                } else {
                    min - padding
                };
            }
            if declared_max.is_none() {
                max = if max <= 0.0 {
                    (max + padding).min(0.0)
                } else {
                    max + padding
                };
            }
        }
        let step = nice_step(max - min);
        Self {
            min: tidy(tidy(min / step).floor() * step),
            max: tidy(tidy(max / step).ceil() * step),
            step,
            log: false,
            reversed: false,
        }
    }

    pub(crate) fn map(self, value: f64, output_min: f64, output_max: f64) -> f64 {
        let ratio = if self.log {
            (value.log10() - self.min.log10()) / (self.max.log10() - self.min.log10())
        } else {
            (value - self.min) / (self.max - self.min)
        };
        let ratio = if self.reversed { 1.0 - ratio } else { ratio };
        output_min + ratio * (output_max - output_min)
    }

    /// The lowest and the highest value of the scale.
    pub(crate) const fn ends(self) -> (f64, f64) {
        (self.min, self.max)
    }

    /// The first tick of a linear axis: the first multiple of the step at or above its start, so
    /// that an exact axis from −2.5 with step 1 has its ticks at −2, −1, 0 and its zero line.
    fn first_tick(self) -> f64 {
        tidy((self.min / self.step - 1e-9).ceil() * self.step)
    }

    /// Ticks are computed from their index instead of by repeated addition, so rounding errors
    /// do not accumulate along the axis. A logarithmic axis ticks every power of ten, and over
    /// two decades or fewer also 2 and 5 times each.
    pub(crate) fn ticks(self) -> Box<dyn Iterator<Item = f64>> {
        if self.log {
            let multiples: &[f64] = if self.max / self.min <= 100.0 * (1.0 + 1e-9) {
                &[1.0, 2.0, 5.0]
            } else {
                &[1.0]
            };
            let top = self.max * (1.0 + 1e-9);
            let ticks: Vec<f64> =
                std::iter::successors(Some(self.min), |power| Some(tidy(power * 10.0)))
                    .take_while(|power| *power <= top)
                    .flat_map(|power| multiples.iter().map(move |multiple| tidy(power * multiple)))
                    .filter(|tick| *tick <= top)
                    .collect();
            return Box::new(ticks.into_iter());
        }
        let first = self.first_tick();
        Box::new(
            std::iter::successors(Some(0.0_f64), |index| Some(index + 1.0))
                .map(move |index| tidy(first + index * self.step))
                // Rounding noise only: an exact axis ends at its max, not half a step beyond.
                .take_while(move |tick| *tick <= self.max + self.step * 1e-6)
                // Validation keeps a declared step to a few dozen ticks; this only bounds the work.
                .take(201),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::{NumericScale, format_tick, format_value};
    use crate::spec::{Locale, NumberStyle, ValueFormat};

    #[test]
    fn scale_includes_zero_and_uses_nice_ticks() {
        let scale = NumericScale::from_values([12.0, 18.0, 15.0].into_iter(), true, (None, None));
        assert!(scale.min.abs() < f64::EPSILON);
        assert!((scale.max - 20.0).abs() < f64::EPSILON);
        assert_eq!(
            scale.ticks().collect::<Vec<_>>(),
            vec![0.0, 5.0, 10.0, 15.0, 20.0]
        );
    }

    #[test]
    fn a_logarithmic_axis_ends_at_the_next_one_two_or_five_not_at_the_next_decade() {
        use crate::spec::ValueAxisSpec;
        let axis = ValueAxisSpec {
            scale: crate::spec::AxisScale::Log,
            ..ValueAxisSpec::default()
        };
        for (largest, end) in [
            (13.75, 20.0),
            (1.5, 2.0),
            (4.0, 5.0),
            (7.0, 10.0),
            (20.0, 20.0),
            (100.0, 100.0),
            (101.0, 200.0),
        ] {
            let scale = NumericScale::for_axis([0.14, largest].into_iter(), false, &axis);
            assert!((scale.max - end).abs() < 1e-9, "{largest}: {}", scale.max);
            assert!((scale.min - 0.1).abs() < 1e-12);
        }
    }

    #[test]
    fn a_margin_never_crosses_zero() {
        let scale = NumericScale::from_values([0.0, 40.0, 100.0].into_iter(), false, (None, None));
        assert!(scale.min.abs() < f64::EPSILON);
        let scale = NumericScale::from_values([-100.0, -3.0].into_iter(), false, (None, None));
        assert!(scale.max.abs() < f64::EPSILON);
        let scale = NumericScale::from_values([-10.0, 90.0].into_iter(), false, (None, None));
        assert!(scale.min < -10.0 && scale.max > 90.0);
    }

    #[test]
    fn a_declared_range_widens_the_scale_without_a_margin() {
        let scale =
            NumericScale::from_values([12.0, 48.0].into_iter(), false, (Some(10.0), Some(60.0)));
        assert!((scale.min - 10.0).abs() < f64::EPSILON);
        assert!((scale.max - 60.0).abs() < f64::EPSILON);
        let scale = NumericScale::from_values([12.0, 48.0].into_iter(), true, (None, Some(100.0)));
        assert_eq!(
            scale.ticks().collect::<Vec<_>>(),
            vec![0.0, 20.0, 40.0, 60.0, 80.0, 100.0]
        );
    }

    #[test]
    fn formats_percent_without_duplicate_suffix() {
        assert_eq!(format_value(0.125, ValueFormat::Percent), "12.5%");
        assert_eq!(format_value(1.0, ValueFormat::Percent), "100%");
    }

    #[test]
    fn removes_floating_point_noise_from_labels() {
        assert_eq!(format_value(0.07, ValueFormat::Percent), "7%");
        assert_eq!(format_value(0.1 + 0.2, ValueFormat::Number), "0.3");
        assert_eq!(format_value(-0.0, ValueFormat::Number), "0");
    }

    #[test]
    fn writes_german_numbers_with_comma_and_true_minus() {
        let de = NumberStyle {
            locale: Locale::De,
            ..NumberStyle::default()
        };
        assert_eq!(format_value(-1.25, de), "\u{2212}1,25");
        assert_eq!(format_value(-1.25, ValueFormat::Number), "\u{2212}1.25");
        let fixed = NumberStyle {
            decimals: Some(2),
            ..de
        };
        assert_eq!(format_value(1.547, fixed), "1,55");
        assert_eq!(format_value(-0.001, fixed), "0,00");
        let percent = NumberStyle {
            format: ValueFormat::Percent,
            ..de
        };
        assert_eq!(format_value(0.125, percent), "12,5\u{202f}%");
    }

    #[test]
    fn separates_thousands_only_when_asked() {
        let en = NumberStyle {
            thousands: true,
            ..NumberStyle::default()
        };
        assert_eq!(format_value(1_234_567.5, en), "1,234,567.5");
        assert_eq!(format_value(-12_500.0, en), "\u{2212}12,500");
        assert_eq!(format_value(999.0, en), "999");
        assert_eq!(format_value(2024.0, NumberStyle::default()), "2024");
        let de = NumberStyle {
            locale: Locale::De,
            ..en
        };
        assert_eq!(format_value(12_500.25, de), "12.500,25");
        let percent = NumberStyle {
            format: ValueFormat::Percent,
            ..de
        };
        assert_eq!(format_value(12.5, percent), "1.250\u{202f}%");
    }

    #[test]
    fn ticks_share_the_decimals_of_their_step() {
        let style = NumberStyle::default();
        assert_eq!(format_tick(1.0, 0.5, 0.0, style), "1.0");
        assert_eq!(format_tick(-0.5, 0.5, 0.0, style), "\u{2212}0.5");
        assert_eq!(format_tick(20.0, 5.0, 0.0, style), "20");
        let percent = NumberStyle::from(ValueFormat::Percent);
        assert_eq!(format_tick(0.1, 0.05, 0.0, percent), "10%");
        assert_eq!(format_tick(0.1, 0.025, 0.0, percent), "10.0%");
    }

    #[test]
    fn ticks_do_not_accumulate_rounding_errors() {
        let scale = NumericScale::from_values([0.12, -0.04, 0.15].into_iter(), true, (None, None));
        assert_eq!(
            scale.ticks().collect::<Vec<_>>(),
            vec![-0.05, 0.0, 0.05, 0.1, 0.15]
        );
        let scale = NumericScale::from_values([0.07, -0.005, 0.3].into_iter(), true, (None, None));
        assert_eq!(
            scale
                .ticks()
                .map(|tick| format_value(tick, ValueFormat::Percent))
                .collect::<Vec<_>>(),
            vec!["−10%", "0%", "10%", "20%", "30%"]
        );
    }
}