#![expect(
clippy::suboptimal_flops,
reason = "compared against the TypeScript surface's own numbers; a fused \
multiply-add rounds once where JavaScript rounds twice."
)]
use crate::Error;
pub const GRID_DIVISIONS: u32 = 5;
pub const BAR_GROUP_SPACING: f64 = 0.2;
const PALETTE: [&str; 8] = [
"#4e79a7", "#f28e2c", "#e15759", "#76b7b2", "#59a14f", "#edc949",
"#af7aa1", "#ff9da7",
];
#[must_use]
pub fn series_color(index: usize, given: Option<&str>) -> String {
given.map_or_else(
|| PALETTE[index % PALETTE.len()].to_owned(),
ToOwned::to_owned,
)
}
#[must_use]
pub fn grid_lines(divisions: u32) -> Vec<f64> {
(0..=divisions)
.map(|line| f64::from(line) / f64::from(divisions))
.collect()
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Bar {
pub x: f64,
pub width: f64,
pub height: f64,
}
pub fn bar_layout(
labels: usize,
series: usize,
values: &[Vec<f64>],
max_value: f64,
) -> Result<Vec<Vec<Bar>>, Error> {
if values.iter().flatten().any(|value| *value < 0.0) {
return Err(Error::Chart("a chart cannot draw a negative value"));
}
#[expect(
clippy::cast_precision_loss,
reason = "a label or series count past 2^53 is not a chart"
)]
let (labels_f, series_f) = (labels as f64, series as f64);
let group_width = 1.0 / labels_f;
let spacing = group_width * BAR_GROUP_SPACING;
let width = (group_width - spacing) / series_f;
Ok((0..labels)
.map(|index| {
(0..series)
.map(|s| {
#[expect(
clippy::cast_precision_loss,
reason = "as the counts above"
)]
let (index_f, s_f) = (index as f64, s as f64);
Bar {
x: index_f * group_width + spacing / 2.0 + s_f * width,
width,
height: if max_value == 0.0 {
0.0
} else {
values
.get(s)
.and_then(|row| row.get(index))
.copied()
.unwrap_or(0.0)
/ max_value
},
}
})
.collect()
})
.collect())
}
const PIE_SPACE: f64 = 100.0;
#[must_use]
pub fn slice_angles(values: &[f64]) -> Vec<(f64, f64)> {
let total: f64 = values.iter().sum();
let mut cursor = -std::f64::consts::PI / 2.0;
values
.iter()
.map(|value| {
let sweep = if total == 0.0 {
0.0
} else {
(value / total) * std::f64::consts::PI * 2.0
};
let slice = (cursor, cursor + sweep);
cursor += sweep;
slice
})
.collect()
}
#[must_use]
pub fn slice_path(start: f64, end: f64, outer: f64, inner: f64) -> String {
let centre = PIE_SPACE / 2.0;
let at = |radius: f64, angle: f64| {
format!(
"{:.4} {:.4}",
centre + angle.cos() * radius,
centre + angle.sin() * radius
)
};
let large = i32::from(end - start > std::f64::consts::PI);
if inner <= 0.0 {
return format!(
"M {centre} {centre} L {} A {outer} {outer} 0 {large} 1 {} Z",
at(outer, start),
at(outer, end)
);
}
format!(
"M {} A {outer} {outer} 0 {large} 1 {} L {} A {inner} {inner} 0 {large} 0 {} Z",
at(outer, start),
at(outer, end),
at(inner, end),
at(inner, start)
)
}
pub const LINE_SPACE: f64 = 100.0;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point {
pub x: f64,
pub y: f64,
}
#[must_use]
pub fn line_points(
labels: usize,
values: &[f64],
max_value: f64,
) -> Vec<Point> {
#[expect(
clippy::cast_precision_loss,
reason = "a label count past 2^53 is not a chart"
)]
let spacing = if labels > 1 {
1.0 / (labels - 1) as f64
} else {
1.0
};
values
.iter()
.enumerate()
.map(|(index, value)| {
#[expect(
clippy::cast_precision_loss,
reason = "as the count above"
)]
let index = index as f64;
Point {
x: index * spacing,
y: if max_value == 0.0 {
1.0
} else {
1.0 - value / max_value
},
}
})
.collect()
}
#[must_use]
pub fn line_path(points: &[Point]) -> String {
points
.iter()
.enumerate()
.map(|(index, point)| {
let command = if index == 0 { "M" } else { "L" };
format!(
"{command} {:.4} {:.4}",
point.x * LINE_SPACE,
point.y * LINE_SPACE
)
})
.collect::<Vec<_>>()
.join(" ")
}