use crate::Color;
use crate::ui::types::PixelRect;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Scale {
#[default]
Linear,
Log10,
Symlog,
}
pub const SYMLOG_LINTHRESH: f32 = 1.0;
pub(super) fn scale_value(value: f32, scale: Scale) -> f32 {
match scale {
Scale::Linear => value,
Scale::Log10 => {
if value > 0.0 && value.is_finite() {
value.log10()
} else {
f32::NAN
}
}
Scale::Symlog => {
if !value.is_finite() {
return f32::NAN;
}
let magnitude = value.abs();
if magnitude <= SYMLOG_LINTHRESH {
value
} else {
value.signum() * SYMLOG_LINTHRESH * (1.0 + (magnitude / SYMLOG_LINTHRESH).log10())
}
}
}
}
pub(super) fn unscale_value(value: f32, scale: Scale) -> f32 {
match scale {
Scale::Linear => value,
Scale::Log10 => 10f32.powf(value),
Scale::Symlog => {
let magnitude = value.abs();
if magnitude <= SYMLOG_LINTHRESH {
value
} else {
value.signum() * SYMLOG_LINTHRESH * 10f32.powf(magnitude / SYMLOG_LINTHRESH - 1.0)
}
}
}
}
fn scaled_axis(lo: f32, hi: f32, scale: Scale, positive_min: Option<f32>) -> (f32, f32) {
match scale {
Scale::Linear => (lo, hi),
Scale::Log10 => {
let lo = if lo.is_finite() && lo > 0.0 {
lo
} else if let Some(min) = positive_min {
min
} else {
return (0.0, 1.0);
};
let hi = if hi.is_finite() && hi > lo {
hi
} else {
lo * 10.0
};
(lo.log10(), hi.log10())
}
Scale::Symlog => {
let lo = scale_value(lo, scale);
let hi = scale_value(hi, scale);
if lo.is_finite() && hi.is_finite() && lo < hi {
(lo, hi)
} else {
(-SYMLOG_LINTHRESH, SYMLOG_LINTHRESH)
}
}
}
}
#[derive(Clone, Debug, Default)]
pub enum TickFormat {
#[default]
Auto,
Decimals(usize),
Scientific,
Labels(Vec<String>),
}
#[derive(Clone, Debug)]
pub struct LineStyle {
pub color: Color,
pub label: String,
pub width: f32,
pub antialias_samples: u32,
pub step: bool,
pub hidden: bool,
}
impl Default for LineStyle {
fn default() -> Self {
Self {
color: Color::new(0.0, 0.0, 0.0, 0.0), label: String::new(),
width: 1.35,
antialias_samples: 4,
step: false,
hidden: false,
}
}
}
#[derive(Clone, Debug)]
pub struct ScatterStyle {
pub color: Color,
pub label: String,
pub radius: f32,
pub hidden: bool,
}
impl Default for ScatterStyle {
fn default() -> Self {
Self {
color: Color::new(0.0, 0.0, 0.0, 0.0),
label: String::new(),
radius: 3.0,
hidden: false,
}
}
}
#[derive(Clone, Debug)]
pub struct BarStyle {
pub color: Color,
pub label: String,
pub gap: f32,
pub hidden: bool,
}
impl Default for BarStyle {
fn default() -> Self {
Self {
color: Color::new(0.0, 0.0, 0.0, 0.0),
label: String::new(),
gap: 0.18,
hidden: false,
}
}
}
#[derive(Clone, Debug)]
pub struct HeatmapStyle {
pub v_min: f32,
pub v_max: f32,
pub colormap: u32,
pub auto_scale: bool,
pub show_grid: bool,
pub colorbar: bool,
}
#[derive(Clone, Debug)]
pub struct BoxStyle {
pub color: Color,
pub label: String,
pub width: f32,
pub whisk: f32,
pub hidden: bool,
}
impl Default for BoxStyle {
fn default() -> Self {
Self {
color: Color::new(0.0, 0.0, 0.0, 0.0),
label: String::new(),
width: 0.5,
whisk: 1.5,
hidden: false,
}
}
}
impl Default for HeatmapStyle {
fn default() -> Self {
Self {
v_min: 0.0,
v_max: 1.0,
colormap: 1, auto_scale: true,
show_grid: true,
colorbar: false,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum ArtistKind {
Line,
Scatter,
Bar,
LineMatrix,
Stream,
Band,
BoxPlot,
}
#[derive(Default, Clone)]
pub(super) struct AxesCommands {
pub(super) title: String,
pub(super) title_color: Color,
pub(super) caption: String,
pub(super) caption_color: Color,
pub(super) x_label: String,
pub(super) y_label: String,
pub(super) imshow: Option<ImshowCmd>,
pub(super) heatmap: Option<Heatmap>,
pub(super) heatmap_matrix: Option<HeatmapMatrixCmd>,
pub(super) lines: Vec<LineCmd>,
pub(super) scatters: Vec<ScatterCmd>,
pub(super) bars: Vec<BarCmd>,
pub(super) line_matrices: Vec<LineMatrixCmd>,
pub(super) streams: Vec<StreamCmd>,
pub(super) bands: Vec<BandCmd>,
pub(super) boxes: Vec<BoxCmd>,
pub(super) annotations: Vec<AnnotationCmd>,
pub(super) artists: Vec<ArtistKind>,
pub(super) limits: Option<LimitsCmd>,
pub(super) x_scale: Scale,
pub(super) y_scale: Scale,
pub(super) x_ticks: Option<Vec<f32>>,
pub(super) y_ticks: Option<Vec<f32>>,
pub(super) x_tick_format: TickFormat,
pub(super) y_tick_format: TickFormat,
pub(super) categories: Vec<String>,
pub(super) show_grid: bool,
pub(super) show_legend: bool,
pub(super) border_color: Option<Color>,
}
impl AxesCommands {
pub(super) fn new() -> Self {
Self {
show_grid: true,
show_legend: true,
..Default::default()
}
}
}
#[derive(Clone)]
pub(super) struct ImshowCmd {
pub(super) texture: crate::Texture,
}
#[derive(Clone)]
pub(super) struct Heatmap {
pub(super) values: Vec<f32>,
pub(super) rows: i32,
pub(super) cols: i32,
pub(super) style: HeatmapStyle,
}
#[derive(Clone)]
pub(super) struct HeatmapMatrixCmd {
pub(super) values: crate::Matrix,
pub(super) style: HeatmapStyle,
}
#[derive(Clone)]
pub(super) struct LineCmd {
pub(super) x: Vec<f32>,
pub(super) y: Vec<f32>,
pub(super) style: LineStyle,
}
#[derive(Clone)]
pub(super) struct ScatterCmd {
pub(super) x: Vec<f32>,
pub(super) y: Vec<f32>,
pub(super) style: ScatterStyle,
}
#[derive(Clone)]
pub(super) struct BarCmd {
pub(super) y: Vec<f32>,
pub(super) group: Option<(usize, usize)>,
pub(super) stack_base: Option<Vec<f32>>,
pub(super) style: BarStyle,
}
#[derive(Clone)]
pub(super) struct LineMatrixCmd {
pub(super) values: crate::Matrix,
pub(super) xy: bool,
pub(super) style: LineStyle,
}
#[derive(Clone)]
pub(super) struct StreamCmd {
pub(super) values: Vec<f32>,
pub(super) head: usize,
pub(super) len: usize,
pub(super) style: LineStyle,
}
impl StreamCmd {
fn push(&mut self, value: f32) {
let capacity = self.values.len();
if self.len < capacity {
self.values[(self.head + self.len) % capacity] = value;
self.len += 1;
} else {
self.values[self.head] = value;
self.head = (self.head + 1) % capacity;
}
}
pub(super) fn ordered(&self) -> impl Iterator<Item = f32> + '_ {
let capacity = self.values.len();
(0..self.len).map(move |index| self.values[(self.head + index) % capacity])
}
}
#[derive(Clone)]
pub(super) struct BoxCmd {
pub(super) summaries: Vec<(f32, f32, f32, f32, f32, f32)>,
pub(super) style: BoxStyle,
}
#[derive(Clone)]
pub(super) struct BandCmd {
pub(super) y_lo: Vec<f32>,
pub(super) y_hi: Vec<f32>,
pub(super) style: LineStyle,
}
#[derive(Clone)]
pub(super) struct AnnotationCmd {
pub(super) text: String,
pub(super) x: f32,
pub(super) y: f32,
}
#[derive(Clone, Debug)]
pub(super) struct LimitsCmd {
pub(super) x_min: f32,
pub(super) x_max: f32,
pub(super) y_min: f32,
pub(super) y_max: f32,
}
pub(super) struct LegendEntry {
pub(super) artist: usize,
pub(super) label: String,
pub(super) color: Color,
pub(super) hidden: bool,
pub(super) row: usize,
}
pub(super) const COLORBAR_RESERVE: u32 = 46;
pub(super) const LEGEND_WIDTH: f32 = 84.0;
pub(super) const LEGEND_ROW_STEP: f32 = 13.0;
pub(super) const LEGEND_FIRST_BASELINE: f32 = 14.0;
pub(super) fn legend_row_rect(rect: PixelRect, row: usize) -> PixelRect {
PixelRect::new(
rect.x + rect.w as i32 - LEGEND_WIDTH as i32 - 2,
rect.y + (LEGEND_FIRST_BASELINE - 10.0 + row as f32 * LEGEND_ROW_STEP) as i32,
LEGEND_WIDTH as u32 + 2,
12,
)
}
#[derive(Clone)]
pub struct Axes {
pub(super) cmds: AxesCommands,
pub(super) highlight: Option<usize>,
}
impl Default for Axes {
fn default() -> Self {
Self {
cmds: AxesCommands::new(),
highlight: None,
}
}
}
impl Axes {
pub fn title(&mut self, text: &str) {
self.title_color(text, Color::new(0.0, 0.0, 0.0, 0.0));
}
pub fn title_color(&mut self, text: &str, color: Color) {
self.cmds.title = text.to_owned();
self.cmds.title_color = color;
}
pub fn caption(&mut self, text: &str) {
self.cmds.caption = text.to_owned();
self.cmds.caption_color = Color::new(0.0, 0.0, 0.0, 0.0);
}
pub fn caption_color(&mut self, text: &str, color: Color) {
self.cmds.caption = text.to_owned();
self.cmds.caption_color = color;
}
pub fn x_label(&mut self, text: &str) {
self.cmds.x_label = text.to_owned();
}
pub fn y_label(&mut self, text: &str) {
self.cmds.y_label = text.to_owned();
}
pub fn imshow(&mut self, texture: crate::Texture) {
self.cmds.imshow = Some(ImshowCmd { texture });
}
pub fn heatmap(&mut self, values: &[f32], rows: i32, cols: i32, style: HeatmapStyle) {
let Some(count) = usize::try_from(rows).ok().and_then(|rows| {
usize::try_from(cols)
.ok()
.and_then(|cols| rows.checked_mul(cols))
}) else {
return;
};
if count == 0 || values.len() < count {
return;
}
self.cmds.heatmap = Some(Heatmap {
values: values[..count].to_vec(),
rows,
cols,
style,
});
}
pub fn heatmap_matrix(&mut self, values: crate::Matrix, style: HeatmapStyle) {
if values.dtype() != crate::DType::F32 || values.shape().len() != 2 {
return;
}
self.cmds.heatmap_matrix = Some(HeatmapMatrixCmd { values, style });
}
pub fn plot(&mut self, y: &[f32], style: LineStyle) {
if y.is_empty() {
return;
}
let x: Vec<f32> = (0..y.len()).map(|i| i as f32).collect();
self.cmds.lines.push(LineCmd {
x,
y: y.to_vec(),
style,
});
self.cmds.artists.push(ArtistKind::Line);
}
pub fn plot_xy(&mut self, x: &[f32], y: &[f32], style: LineStyle) {
if x.is_empty() || x.len() != y.len() {
return;
}
self.cmds.lines.push(LineCmd {
x: x.to_vec(),
y: y.to_vec(),
style,
});
self.cmds.artists.push(ArtistKind::Line);
}
pub fn plot_matrix(&mut self, values: &crate::Matrix, style: LineStyle) {
let shape = values.shape();
if values.dtype() != crate::DType::F32 || shape.len() != 2 || shape[0] != 2 || shape[1] == 0 {
return;
}
self.cmds.line_matrices.push(LineMatrixCmd {
values: values.clone(),
xy: true,
style,
});
self.cmds.artists.push(ArtistKind::LineMatrix);
}
pub fn plot_matrix_y(&mut self, values: &crate::Matrix, style: LineStyle) {
if values.dtype() != crate::DType::F32 || values.shape().len() != 1 || values.shape()[0] == 0 {
return;
}
self.cmds.line_matrices.push(LineMatrixCmd {
values: values.clone(),
xy: false,
style,
});
self.cmds.artists.push(ArtistKind::LineMatrix);
}
pub fn annotate(&mut self, text: &str, x: f32, y: f32) {
if text.is_empty() {
return;
}
self.cmds.annotations.push(AnnotationCmd {
text: text.to_owned(),
x,
y,
});
}
pub fn categories(&mut self, labels: &[&str]) {
self.cmds.categories = labels.iter().map(|label| (*label).to_owned()).collect();
}
pub fn xscale(&mut self, scale: Scale) {
self.cmds.x_scale = scale;
}
pub fn yscale(&mut self, scale: Scale) {
self.cmds.y_scale = scale;
}
pub fn xticks(&mut self, values: &[f32]) {
self.cmds.x_ticks = if values.is_empty() {
None
} else {
Some(values.to_vec())
};
}
pub fn yticks(&mut self, values: &[f32]) {
self.cmds.y_ticks = if values.is_empty() {
None
} else {
Some(values.to_vec())
};
}
pub fn xformat(&mut self, format: TickFormat) {
self.cmds.x_tick_format = format;
}
pub fn yformat(&mut self, format: TickFormat) {
self.cmds.y_tick_format = format;
}
pub fn scatter(&mut self, x: &[f32], y: &[f32], style: ScatterStyle) {
if x.is_empty() || x.len() != y.len() {
return;
}
self.cmds.scatters.push(ScatterCmd {
x: x.to_vec(),
y: y.to_vec(),
style,
});
self.cmds.artists.push(ArtistKind::Scatter);
}
pub fn bar(&mut self, y: &[f32], style: BarStyle) {
if y.is_empty() {
return;
}
self.cmds.bars.push(BarCmd {
y: y.to_vec(),
group: None,
stack_base: None,
style,
});
self.cmds.artists.push(ArtistKind::Bar);
}
pub fn bar_grouped(&mut self, series: &[&[f32]], styles: &[BarStyle]) {
let size = series.len();
if size == 0 || series.iter().any(|layer| layer.is_empty()) {
return;
}
for (index, layer) in series.iter().enumerate() {
self.cmds.bars.push(BarCmd {
y: (*layer).to_vec(),
group: Some((index, size)),
stack_base: None,
style: styles.get(index).cloned().unwrap_or_default(),
});
self.cmds.artists.push(ArtistKind::Bar);
}
}
pub fn bar_stacked(&mut self, series: &[&[f32]], styles: &[BarStyle]) {
if series.is_empty() || series.iter().any(|layer| layer.is_empty()) {
return;
}
let count = series[0].len();
if series.iter().any(|layer| layer.len() != count) {
return;
}
let mut base = vec![0.0_f32; count];
for (index, layer) in series.iter().enumerate() {
self.cmds.bars.push(BarCmd {
y: (*layer).to_vec(),
group: None,
stack_base: Some(base.clone()),
style: styles.get(index).cloned().unwrap_or_default(),
});
self.cmds.artists.push(ArtistKind::Bar);
for (acc, &value) in base.iter_mut().zip(layer.iter()) {
*acc += if value.is_finite() { value } else { 0.0 };
}
}
}
pub fn stream(&mut self, capacity: usize, style: LineStyle) -> usize {
let index = self.cmds.streams.len();
self.cmds.streams.push(StreamCmd {
values: vec![0.0; capacity.max(1)],
head: 0,
len: 0,
style,
});
self.cmds.artists.push(ArtistKind::Stream);
index
}
pub fn stream_push(&mut self, series: usize, values: &[f32]) {
if let Some(stream) = self.cmds.streams.get_mut(series) {
for &value in values {
stream.push(value);
}
}
}
pub fn band(&mut self, y_lo: &[f32], y_hi: &[f32], style: LineStyle) {
if y_lo.is_empty() || y_lo.len() != y_hi.len() {
return;
}
self.cmds.bands.push(BandCmd {
y_lo: y_lo.to_vec(),
y_hi: y_hi.to_vec(),
style,
});
self.cmds.artists.push(ArtistKind::Band);
}
pub fn boxplot(&mut self, datasets: &[&[f32]], style: BoxStyle) {
if datasets.is_empty() {
return;
}
let mut summaries = Vec::with_capacity(datasets.len());
for (index, dataset) in datasets.iter().enumerate() {
let mut sorted: Vec<f32> = dataset.iter().copied().filter(|v| v.is_finite()).collect();
if sorted.is_empty() {
continue;
}
sorted.sort_by(f32::total_cmp);
let q1 = percentile(&sorted, 0.25);
let median = percentile(&sorted, 0.5);
let q3 = percentile(&sorted, 0.75);
let iqr = (q3 - q1) * style.whisk;
let lo = sorted
.iter()
.copied()
.find(|&v| v >= q1 - iqr)
.unwrap_or(sorted[0]);
let hi = sorted
.iter()
.copied()
.rev()
.find(|&v| v <= q3 + iqr)
.unwrap_or(sorted[sorted.len() - 1]);
summaries.push((index as f32, q1, median, q3, lo, hi));
}
if summaries.is_empty() {
return;
}
self.cmds.boxes.push(BoxCmd { summaries, style });
self.cmds.artists.push(ArtistKind::BoxPlot);
}
pub fn histogram(&mut self, values: &[f32], bins: i32, style: BarStyle) {
if bins <= 0 || values.is_empty() {
return;
}
let bins = (bins as usize).min(4096);
let finite: Vec<f32> = values.iter().copied().filter(|v| v.is_finite()).collect();
if finite.is_empty() {
return;
}
let v_min = finite.iter().cloned().fold(f32::INFINITY, f32::min);
let v_max = finite.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
let range = (v_max - v_min).max(1e-8);
let mut counts = vec![0.0f32; bins];
for &v in &finite {
let idx = (((v - v_min) / range) * bins as f32) as usize;
counts[idx.min(bins - 1)] += 1.0;
}
self.bar(&counts, style);
}
pub fn limits(&mut self, x_min: f32, x_max: f32, y_min: f32, y_max: f32) {
if [x_min, x_max, y_min, y_max]
.iter()
.all(|value| value.is_finite())
&& x_min < x_max
&& y_min < y_max
{
self.cmds.limits = Some(LimitsCmd {
x_min,
x_max,
y_min,
y_max,
});
} else {
self.auto_limits();
}
}
pub fn auto_limits(&mut self) {
self.cmds.limits = None;
}
pub fn grid(&mut self, visible: bool) {
self.cmds.show_grid = visible;
}
pub fn legend(&mut self, visible: bool) {
self.cmds.show_legend = visible;
}
pub fn border_color(&mut self, color: Color) {
self.cmds.border_color = Some(color);
}
pub fn clear(&mut self) {
self.cmds = AxesCommands::new();
}
pub fn artist_count(&self) -> usize {
self.cmds.artists.len()
}
pub fn artist_hidden(&self, index: usize) -> Option<bool> {
let (mut line, mut scatter, mut bar, mut matrix, mut stream, mut band, mut bx) =
(0usize, 0usize, 0usize, 0usize, 0usize, 0usize, 0usize);
for (position, kind) in self.cmds.artists.iter().enumerate() {
let hidden = match kind {
ArtistKind::Line => self.cmds.lines.get(line).map(|cmd| cmd.style.hidden),
ArtistKind::Scatter => self.cmds.scatters.get(scatter).map(|cmd| cmd.style.hidden),
ArtistKind::Bar => self.cmds.bars.get(bar).map(|cmd| cmd.style.hidden),
ArtistKind::LineMatrix => self
.cmds
.line_matrices
.get(matrix)
.map(|cmd| cmd.style.hidden),
ArtistKind::Stream => self.cmds.streams.get(stream).map(|cmd| cmd.style.hidden),
ArtistKind::Band => self.cmds.bands.get(band).map(|cmd| cmd.style.hidden),
ArtistKind::BoxPlot => self.cmds.boxes.get(bx).map(|cmd| cmd.style.hidden),
};
if position == index {
return hidden;
}
match kind {
ArtistKind::Line => line += 1,
ArtistKind::Scatter => scatter += 1,
ArtistKind::Bar => bar += 1,
ArtistKind::LineMatrix => matrix += 1,
ArtistKind::Stream => stream += 1,
ArtistKind::Band => band += 1,
ArtistKind::BoxPlot => bx += 1,
}
}
None
}
pub fn set_artist_hidden(&mut self, index: usize, hidden: bool) {
let (mut line, mut scatter, mut bar, mut matrix, mut stream, mut band, mut bx) =
(0usize, 0usize, 0usize, 0usize, 0usize, 0usize, 0usize);
for (position, kind) in self.cmds.artists.iter().enumerate() {
if position == index {
match kind {
ArtistKind::Line => {
if let Some(cmd) = self.cmds.lines.get_mut(line) {
cmd.style.hidden = hidden;
}
}
ArtistKind::Scatter => {
if let Some(cmd) = self.cmds.scatters.get_mut(scatter) {
cmd.style.hidden = hidden;
}
}
ArtistKind::Bar => {
if let Some(cmd) = self.cmds.bars.get_mut(bar) {
cmd.style.hidden = hidden;
}
}
ArtistKind::LineMatrix => {
if let Some(cmd) = self.cmds.line_matrices.get_mut(matrix) {
cmd.style.hidden = hidden;
}
}
ArtistKind::Stream => {
if let Some(cmd) = self.cmds.streams.get_mut(stream) {
cmd.style.hidden = hidden;
}
}
ArtistKind::Band => {
if let Some(cmd) = self.cmds.bands.get_mut(band) {
cmd.style.hidden = hidden;
}
}
ArtistKind::BoxPlot => {
if let Some(cmd) = self.cmds.boxes.get_mut(bx) {
cmd.style.hidden = hidden;
}
}
}
return;
}
match kind {
ArtistKind::Line => line += 1,
ArtistKind::Scatter => scatter += 1,
ArtistKind::Bar => bar += 1,
ArtistKind::LineMatrix => matrix += 1,
ArtistKind::Stream => stream += 1,
ArtistKind::Band => band += 1,
ArtistKind::BoxPlot => bx += 1,
}
}
}
pub fn set_series_highlight(&mut self, index: Option<usize>) {
self.highlight = index;
}
pub(super) fn plot_area(&self, cell: PixelRect) -> PixelRect {
let colorbar = self
.cmds
.heatmap
.as_ref()
.is_some_and(|heatmap| heatmap.style.colorbar)
|| self
.cmds
.heatmap_matrix
.as_ref()
.is_some_and(|heatmap| heatmap.style.colorbar);
if !colorbar {
return cell;
}
PixelRect {
w: cell.w.saturating_sub(COLORBAR_RESERVE),
..cell
}
}
pub(super) fn legend_entries(&self, palette: &[crate::Color]) -> Vec<LegendEntry> {
let mut entries = Vec::new();
let (mut line, mut scatter, mut bar, mut matrix, mut stream, mut band, mut bx) =
(0usize, 0usize, 0usize, 0usize, 0usize, 0usize, 0usize);
let mut palette_idx = 0usize;
for (position, kind) in self.cmds.artists.iter().enumerate() {
let (label, color, hidden) = match kind {
ArtistKind::Line => {
let Some(cmd) = self.cmds.lines.get(line) else {
continue;
};
line += 1;
(&cmd.style.label, cmd.style.color, cmd.style.hidden)
}
ArtistKind::Scatter => {
let Some(cmd) = self.cmds.scatters.get(scatter) else {
continue;
};
scatter += 1;
(&cmd.style.label, cmd.style.color, cmd.style.hidden)
}
ArtistKind::Bar => {
let Some(cmd) = self.cmds.bars.get(bar) else {
continue;
};
bar += 1;
(&cmd.style.label, cmd.style.color, cmd.style.hidden)
}
ArtistKind::LineMatrix => {
let Some(cmd) = self.cmds.line_matrices.get(matrix) else {
continue;
};
matrix += 1;
(&cmd.style.label, cmd.style.color, cmd.style.hidden)
}
ArtistKind::Stream => {
let Some(cmd) = self.cmds.streams.get(stream) else {
continue;
};
stream += 1;
(&cmd.style.label, cmd.style.color, cmd.style.hidden)
}
ArtistKind::Band => {
let Some(cmd) = self.cmds.bands.get(band) else {
continue;
};
band += 1;
(&cmd.style.label, cmd.style.color, cmd.style.hidden)
}
ArtistKind::BoxPlot => {
let Some(cmd) = self.cmds.boxes.get(bx) else {
continue;
};
bx += 1;
(&cmd.style.label, cmd.style.color, cmd.style.hidden)
}
};
let color = if color.a == 0.0 {
let resolved = palette[palette_idx % palette.len()];
palette_idx += 1;
resolved
} else {
color
};
entries.push(LegendEntry {
artist: position,
label: label.clone(),
color,
hidden,
row: usize::MAX,
});
}
let mut row = 0usize;
for entry in &mut entries {
if !entry.label.is_empty() {
entry.row = row;
row += 1;
}
}
entries
}
pub(super) fn data_y_range(&self) -> (f32, f32) {
let mut y_min = f32::INFINITY;
let mut y_max = f32::NEG_INFINITY;
for line in &self.cmds.lines {
for &v in &line.y {
if v.is_finite() {
y_min = y_min.min(v);
y_max = y_max.max(v);
}
}
}
for scatter in &self.cmds.scatters {
for &value in &scatter.y {
if value.is_finite() {
y_min = y_min.min(value);
y_max = y_max.max(value);
}
}
}
for bar in &self.cmds.bars {
for (index, &v) in bar.y.iter().enumerate() {
if v.is_finite() {
let base = bar.stack_base.as_ref().map_or(0.0, |bases| bases[index]);
y_min = y_min.min((base + v).min(base.min(0.0)));
y_max = y_max.max((base + v).max(base.max(0.0)));
}
}
}
for stream in &self.cmds.streams {
for value in stream.ordered() {
if value.is_finite() {
y_min = y_min.min(value);
y_max = y_max.max(value);
}
}
}
for band in &self.cmds.bands {
for &value in band.y_lo.iter().chain(&band.y_hi) {
if value.is_finite() {
y_min = y_min.min(value);
y_max = y_max.max(value);
}
}
}
for boxes in &self.cmds.boxes {
for &(_, _, _, _, lo, hi) in &boxes.summaries {
y_min = y_min.min(lo);
y_max = y_max.max(hi);
}
}
if !y_min.is_finite() || !y_max.is_finite() {
return (0.0, 1.0);
}
if (y_max - y_min).abs() < 1e-6 {
return (y_min - 1.0, y_max + 1.0);
}
(y_min, y_max)
}
pub fn data_limits(&self) -> [f32; 4] {
let limits = self.effective_limits();
[limits.x_min, limits.x_max, limits.y_min, limits.y_max]
}
pub fn explicit_limits(&self) -> Option<[f32; 4]> {
self
.cmds
.limits
.as_ref()
.map(|l| [l.x_min, l.x_max, l.y_min, l.y_max])
}
pub(super) fn effective_limits(&self) -> LimitsCmd {
if let Some(lim) = &self.cmds.limits {
return lim.clone();
}
let (y_min, y_max) = self.data_y_range();
let mut x_min = f32::INFINITY;
let mut x_max = f32::NEG_INFINITY;
for value in self
.cmds
.lines
.iter()
.flat_map(|line| line.x.iter())
.chain(
self
.cmds
.scatters
.iter()
.flat_map(|scatter| scatter.x.iter()),
)
.copied()
.filter(|value| value.is_finite())
{
x_min = x_min.min(value);
x_max = x_max.max(value);
}
for bar in &self.cmds.bars {
x_min = x_min.min(0.0);
x_max = x_max.max(bar.y.len().saturating_sub(1) as f32);
}
for matrix in &self.cmds.line_matrices {
if !matrix.xy {
let count = matrix.values.shape().first().copied().unwrap_or(0);
x_min = x_min.min(0.0);
x_max = x_max.max(count.saturating_sub(1) as f32);
}
}
for stream in &self.cmds.streams {
if stream.len == 0 {
continue;
}
x_min = x_min.min(0.0);
x_max = x_max.max(stream.values.len().saturating_sub(1) as f32);
}
for band in &self.cmds.bands {
x_min = x_min.min(0.0);
x_max = x_max.max(band.y_lo.len().saturating_sub(1) as f32);
}
for boxes in &self.cmds.boxes {
x_min = x_min.min(0.0);
x_max = x_max.max(boxes.summaries.len().saturating_sub(1) as f32);
}
if !x_min.is_finite() || !x_max.is_finite() || (x_max - x_min).abs() < 1e-6 {
x_min = 0.0;
x_max = 1.0;
}
LimitsCmd {
x_min,
x_max,
y_min,
y_max,
}
}
pub(super) fn scaled_limits(&self) -> LimitsCmd {
let raw = self.effective_limits();
let (x_min, x_max) = scaled_axis(
raw.x_min,
raw.x_max,
self.cmds.x_scale,
self.min_positive_x(),
);
let (y_min, y_max) = scaled_axis(
raw.y_min,
raw.y_max,
self.cmds.y_scale,
self.min_positive_y(),
);
LimitsCmd {
x_min,
x_max,
y_min,
y_max,
}
}
fn min_positive_x(&self) -> Option<f32> {
let mut min = f32::INFINITY;
for value in self.cmds.lines.iter().flat_map(|line| line.x.iter()).chain(
self
.cmds
.scatters
.iter()
.flat_map(|scatter| scatter.x.iter()),
) {
if value.is_finite() && *value > 0.0 {
min = min.min(*value);
}
}
for bars in &self.cmds.bars {
if bars.y.len() > 1 {
min = min.min(1.0);
}
}
min.is_finite().then_some(min)
}
fn min_positive_y(&self) -> Option<f32> {
let mut min = f32::INFINITY;
for value in self
.cmds
.lines
.iter()
.flat_map(|line| line.y.iter())
.chain(
self
.cmds
.scatters
.iter()
.flat_map(|scatter| scatter.y.iter()),
)
.chain(self.cmds.bars.iter().flat_map(|bars| bars.y.iter()))
{
if value.is_finite() && *value > 0.0 {
min = min.min(*value);
}
}
min.is_finite().then_some(min)
}
}
fn percentile(sorted: &[f32], q: f32) -> f32 {
let pos = q * (sorted.len() - 1) as f32;
let lo = pos.floor() as usize;
let hi = (lo + 1).min(sorted.len() - 1);
sorted[lo] + (sorted[hi] - sorted[lo]) * (pos - lo as f32)
}