use std::sync::Arc;
use super::{
chart::{
ChartArea, ChartAxis, ChartFrame, ChartLook, ChartMotion, ChartPart, ChartPlot, ChartPoint, ChartProps,
ChartRenderer, ChartSeries, ChartSize, ChartTick, ChartTip, LinearScale, TipPlace, bits, chart_recipe,
signature_of,
},
names,
part_style::Opacity,
recipe::Recipe,
theme::{Themed, current_theme},
};
use crate::{
app::{
ctx::Ctx,
theme::{PaletteColor, Theme},
},
canvas::{ArcDirection, CanvasItem, Context2D, LineCap, LineJoin},
node::{Element, color::Color},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LineChartPart {
Line,
Area,
Point,
}
names!(LineChartPart {
Line => "line",
Area => "area",
Point => "point",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LineChartState {
Smooth,
Stacked,
}
names!(LineChartState {
Smooth => "smooth",
Stacked => "stacked",
});
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum LineCurve {
#[default]
Straight,
Smooth,
}
const LINE_WIDTH: f32 = 2.0;
const POINT_RADIUS: f32 = 5.0;
const POINT_RING: f32 = 2.0;
const AXIS_TEXTS: usize = 8;
#[derive(Clone)]
struct Lines {
categories: Arc<[Arc<str>]>,
series: Vec<ChartSeries>,
curve: LineCurve,
axis: Option<Vec<String>>,
}
impl Lines {
fn new<S: AsRef<str>>(
categories: impl IntoIterator<Item = S>,
series: impl IntoIterator<Item = ChartSeries>,
) -> Self {
Self {
categories: categories.into_iter().map(|name| Arc::from(name.as_ref())).collect(),
series: series.into_iter().collect(),
curve: LineCurve::Straight,
axis: None,
}
}
fn frame<C: Themed<Renderer = ChartRenderer>>(
&self,
props: &ChartProps,
theme: &Theme,
area: bool,
stacked: bool,
) -> ChartFrame {
let plot = LinePlot {
categories: self.categories.clone(),
names: self.series.iter().map(|series| Arc::from(series.name())).collect(),
values: self.series.iter().map(|series| Arc::from(series.values())).collect(),
shown: self.series.iter().map(|series| !props.is_hidden(series)).collect(),
curve: self.curve,
area,
stacked: area && stacked,
axis: self.axis.clone(),
extent: (f64::INFINITY, f64::NEG_INFINITY),
}
.measured();
let empty = plot.is_empty();
let case = props
.case(ChartSize::Sm, empty)
.state(LineChartState::Smooth, self.curve == LineCurve::Smooth)
.state(LineChartState::Stacked, plot.stacked);
let recipe = theme
.resolve::<C>(&case, Some(&props.rules))
.owned_by(props.id.as_ref());
let mut props = props.clone();
props.grid.get_or_insert(true);
props.frame(recipe, Arc::new(plot), self.series.clone(), theme.renderer::<C>())
}
}
#[derive(Clone)]
pub struct LineChart {
lines: Lines,
props: ChartProps,
}
impl LineChart {
pub fn new<S: AsRef<str>>(
categories: impl IntoIterator<Item = S>,
series: impl IntoIterator<Item = ChartSeries>,
) -> Self {
Self {
lines: Lines::new(categories, series),
props: ChartProps::default(),
}
}
pub fn curve(mut self, curve: LineCurve) -> Self {
self.lines.curve = curve;
self
}
pub fn axis_labels<S: AsRef<str>>(mut self, labels: impl IntoIterator<Item = S>) -> Self {
self.lines.axis = Some(labels.into_iter().map(|label| label.as_ref().to_owned()).collect());
self
}
crate::chart_props!();
pub fn build(self, theme: &Theme) -> Element {
self.lines.frame::<LineChart>(&self.props, theme, false, false).build()
}
pub fn mount(self, ctx: &mut Ctx) -> Element {
let key = self.props.key.clone();
ChartFrame::mount(ctx, key, move |theme| {
self.lines.frame::<LineChart>(&self.props, theme, false, false)
})
}
}
impl From<LineChart> for Element {
fn from(chart: LineChart) -> Self {
chart.build(¤t_theme())
}
}
impl crate::ui::Styled for LineChart {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for LineChart {
const NAME: &'static str = "line_chart";
type Renderer = ChartRenderer;
fn parts() -> crate::ui::Declared {
LineChartPart::declared()
}
fn states() -> crate::ui::Declared {
LineChartState::declared()
}
fn recipe() -> Recipe {
lines_recipe()
}
}
#[derive(Clone)]
pub struct AreaChart {
lines: Lines,
stacked: bool,
props: ChartProps,
}
impl AreaChart {
pub fn new<S: AsRef<str>>(
categories: impl IntoIterator<Item = S>,
series: impl IntoIterator<Item = ChartSeries>,
) -> Self {
Self {
lines: Lines::new(categories, series),
stacked: false,
props: ChartProps::default(),
}
}
pub fn stacked(mut self, stacked: bool) -> Self {
self.stacked = stacked;
self
}
pub fn curve(mut self, curve: LineCurve) -> Self {
self.lines.curve = curve;
self
}
pub fn axis_labels<S: AsRef<str>>(mut self, labels: impl IntoIterator<Item = S>) -> Self {
self.lines.axis = Some(labels.into_iter().map(|label| label.as_ref().to_owned()).collect());
self
}
crate::chart_props!();
pub fn build(self, theme: &Theme) -> Element {
self
.lines
.frame::<AreaChart>(&self.props, theme, true, self.stacked)
.build()
}
pub fn mount(self, ctx: &mut Ctx) -> Element {
let key = self.props.key.clone();
ChartFrame::mount(ctx, key, move |theme| {
self.lines.frame::<AreaChart>(&self.props, theme, true, self.stacked)
})
}
}
impl From<AreaChart> for Element {
fn from(chart: AreaChart) -> Self {
chart.build(¤t_theme())
}
}
impl crate::ui::Styled for AreaChart {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for AreaChart {
const NAME: &'static str = "area_chart";
type Renderer = ChartRenderer;
fn recipe() -> Recipe {
lines_recipe()
}
}
fn lines_recipe() -> Recipe {
chart_recipe()
.base(LineChartPart::Line, |s| s.opacity(1.0))
.base(LineChartPart::Area, |s| s.opacity(0.16))
.base(LineChartPart::Point, |s| s.background(PaletteColor::Card))
}
#[derive(Clone)]
struct LinePlot {
categories: Arc<[Arc<str>]>,
names: Vec<Arc<str>>,
values: Vec<Arc<[f64]>>,
shown: Vec<bool>,
curve: LineCurve,
area: bool,
stacked: bool,
axis: Option<Vec<String>>,
extent: (f64, f64),
}
std::thread_local! {
static FINDING: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
static LOOKED_AT: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
#[doc(hidden)]
pub fn line_values_looked_at() -> usize {
LOOKED_AT.with(|looked| looked.get())
}
type Piece = ((f32, f32), (f32, f32), (f32, f32), (f32, f32));
impl LinePlot {
fn shown(&self) -> Vec<usize> {
(0..self.values.len()).filter(|series| self.shown[*series]).collect()
}
fn value(&self, series: usize, index: usize) -> Option<f64> {
if FINDING.with(|finding| finding.get()) {
LOOKED_AT.with(|looked| looked.set(looked.get() + 1));
}
self
.values
.get(series)
.and_then(|values| values.get(index))
.copied()
.filter(|value| value.is_finite())
}
fn level(&self, series: usize, index: usize) -> Option<f64> {
if !self.stacked {
return self.value(series, index);
}
Some(self.below(series, index) + self.value(series, index).unwrap_or(0.0))
}
fn is_shown(&self, series: usize) -> bool {
self.shown.get(series).copied().unwrap_or(false)
}
fn below(&self, series: usize, index: usize) -> f64 {
self
.shown()
.into_iter()
.take_while(|other| *other < series)
.filter_map(|other| self.value(other, index))
.sum()
}
fn measured(mut self) -> Self {
let shown = self.shown();
let plot = &self;
let extent = (0..plot.categories.len())
.flat_map(|index| shown.iter().filter_map(move |series| plot.level(*series, index)))
.fold((f64::INFINITY, f64::NEG_INFINITY), |(least, most), level| {
(least.min(level), most.max(level))
});
self.extent = extent;
self
}
fn scale(&self, area: ChartArea) -> LinearScale {
LinearScale::nice([self.extent.0, self.extent.1], (area.height, 0.0), 4)
}
fn x(&self, area: ChartArea, index: usize) -> f32 {
match self.categories.len() {
0 | 1 => area.width / 2.0,
count => area.width * index as f32 / (count - 1) as f32,
}
}
fn index_at(&self, area: ChartArea, x: f32) -> Option<usize> {
match self.categories.len() {
0 => None,
1 => Some(0),
count => {
let step = area.width / (count - 1) as f32;
Some(((x / step).round().max(0.0) as usize).min(count - 1))
}
}
}
fn find(&self, area: ChartArea, x: f32, y: f32) -> Option<ChartPoint> {
let index = self.index_at(area, x)?;
let scale = self.scale(area);
self
.shown()
.into_iter()
.filter(|series| self.value(*series, index).is_some())
.filter_map(|series| Some((series, (scale.map(self.level(series, index)?) - y).abs())))
.min_by(|a, b| a.1.total_cmp(&b.1))
.map(|(series, _)| ChartPoint::new(series, index))
}
fn spot(&self, area: ChartArea, scale: &LinearScale, series: usize, index: usize) -> Spot {
let base = if self.stacked { self.below(series, index) } else { 0.0 };
Spot {
x: self.x(area, index),
top: self
.level(series, index)
.filter(|_| self.is_shown(series))
.map(|level| scale.map(level)),
base: scale.map(base),
}
}
fn tracks(&self, area: ChartArea) -> Vec<Track> {
let scale = self.scale(area);
self
.shown()
.into_iter()
.map(|series| {
let spots: Vec<Spot> = (0..self.categories.len())
.map(|index| self.spot(area, &scale, series, index))
.collect();
Track {
series,
tops: spots.iter().map(|spot| spot.top.map(|top| (spot.x, top))).collect(),
bases: spots.iter().map(|spot| (spot.x, spot.base)).collect(),
}
})
.collect()
}
fn tracks_from(&self, from: &LinePlot, area: ChartArea, motion: ChartMotion<'_>) -> Vec<Track> {
let (scale, was_scale) = (self.scale(area), from.scale(area));
let (count, was_count) = (self.categories.len(), from.categories.len());
(0..self.values.len().max(from.values.len()))
.filter(|series| self.is_shown(*series) || from.is_shown(*series))
.map(|series| {
let (mut tops, mut bases) = (Vec::new(), Vec::new());
for index in 0..count.max(was_count) {
let was = from.spot(area, &was_scale, series, index.min(was_count - 1));
let to = self.spot(area, &scale, series, index.min(count - 1));
let (here, was_here) = (
index < count && to.top.is_some(),
index < was_count && was.top.is_some(),
);
let gap = index < count && self.is_shown(series) && to.top.is_none();
let x = motion.lerp(was.x, to.x);
tops.push(
(here || (was_here && !gap))
.then(|| (x, motion.lerp(was.top.unwrap_or(was.base), to.top.unwrap_or(to.base)))),
);
bases.push((x, motion.lerp(was.base, to.base)));
}
Track { series, tops, bases }
})
.collect()
}
fn pieces(&self, points: &[(f32, f32)]) -> Vec<Piece> {
match self.curve {
LineCurve::Straight => points
.windows(2)
.map(|pair| (pair[0], pair[0], pair[1], pair[1]))
.collect(),
LineCurve::Smooth => monotone(points),
}
}
fn trace(canvas: &Context2D, pieces: &[Piece], first: (f32, f32)) {
canvas.move_to(first.0, first.1);
for (_, c1, c2, end) in pieces {
canvas.bezier_curve_to(c1.0, c1.1, c2.0, c2.1, end.0, end.1);
}
}
fn trace_back(canvas: &Context2D, pieces: &[Piece], last: (f32, f32)) {
canvas.line_to(last.0, last.1);
for (start, c1, c2, _) in pieces.iter().rev() {
canvas.bezier_curve_to(c2.0, c2.1, c1.0, c1.1, start.0, start.1);
}
}
fn paint_area(&self, canvas: &Context2D, track: &Track, color: Color) {
canvas.set_fill_style(color);
for (tops, bases) in track.runs() {
canvas.begin_path();
Self::trace(canvas, &self.pieces(&tops), tops[0]);
Self::trace_back(canvas, &self.pieces(&bases), bases[bases.len() - 1]);
canvas.close_path();
canvas.fill();
}
}
fn paint_line(&self, canvas: &Context2D, track: &Track, color: Color) {
canvas.set_stroke_style(color);
canvas.set_line_width(LINE_WIDTH);
canvas.set_line_join(LineJoin::Round);
canvas.set_line_cap(LineCap::Round);
for (run, _) in track.runs() {
canvas.begin_path();
if run.len() == 1 {
let _ = canvas.arc(
run[0].0,
run[0].1,
LINE_WIDTH,
0.0,
std::f32::consts::TAU,
ArcDirection::Clockwise,
);
canvas.set_fill_style(color);
canvas.fill();
continue;
}
Self::trace(canvas, &self.pieces(&run), run[0]);
canvas.stroke();
}
}
fn draw(&self, canvas: &Context2D, area: ChartArea, look: &ChartLook, active: Option<ChartPoint>, tracks: &[Track]) {
if self.area {
let opacity = match look.part(LineChartPart::Area).rest().opacity {
Some(Opacity::Value(opacity)) => opacity,
_ => 0.16,
};
for track in tracks {
self.paint_area(canvas, track, look.series(track.series).with_opacity(opacity));
}
}
let line = match look.part(LineChartPart::Line).rest().opacity {
Some(Opacity::Value(opacity)) => opacity,
_ => 1.0,
};
for track in tracks.iter().rev() {
self.paint_line(canvas, track, look.series(track.series).with_opacity(line));
}
let Some(point) = active else {
return;
};
let x = tracks
.first()
.and_then(|track| track.bases.get(point.index))
.map_or_else(|| self.x(area, point.index), |(x, _)| *x);
canvas.set_fill_style(look.color(ChartPart::Cursor));
canvas.fill_rect(x - 0.5, 0.0, 1.0, area.height);
let ring = look.color(LineChartPart::Point);
for track in tracks.iter().rev() {
let Some(Some((x, y))) = track
.tops
.get(point.index)
.filter(|_| self.value(track.series, point.index).is_some())
else {
continue;
};
canvas.begin_path();
let _ = canvas.arc(
*x,
*y,
POINT_RADIUS,
0.0,
std::f32::consts::TAU,
ArcDirection::Clockwise,
);
canvas.set_fill_style(ring);
canvas.fill();
canvas.begin_path();
let _ = canvas.arc(
*x,
*y,
POINT_RADIUS - POINT_RING,
0.0,
std::f32::consts::TAU,
ArcDirection::Clockwise,
);
canvas.set_fill_style(look.series(track.series));
canvas.fill();
}
}
}
#[derive(Clone, Copy)]
struct Spot {
x: f32,
top: Option<f32>,
base: f32,
}
struct Track {
series: usize,
tops: Vec<Option<(f32, f32)>>,
bases: Vec<(f32, f32)>,
}
impl Track {
fn runs(&self) -> Vec<(Vec<(f32, f32)>, Vec<(f32, f32)>)> {
let mut runs = Vec::new();
let mut run: (Vec<(f32, f32)>, Vec<(f32, f32)>) = (Vec::new(), Vec::new());
for (top, base) in self.tops.iter().zip(&self.bases) {
match top {
Some(top) => {
run.0.push(*top);
run.1.push(*base);
}
None if !run.0.is_empty() => runs.push(std::mem::take(&mut run)),
None => {}
}
}
if !run.0.is_empty() {
runs.push(run);
}
runs
}
}
fn monotone(points: &[(f32, f32)]) -> Vec<Piece> {
let count = points.len();
if count < 2 {
return Vec::new();
}
let slopes: Vec<f32> = points
.windows(2)
.map(|pair| {
let dx = pair[1].0 - pair[0].0;
if dx.abs() < f32::EPSILON {
0.0
} else {
(pair[1].1 - pair[0].1) / dx
}
})
.collect();
let mut tangents = vec![0.0; count];
tangents[0] = slopes[0];
tangents[count - 1] = slopes[count - 2];
for index in 1..count - 1 {
let (before, after) = (slopes[index - 1], slopes[index]);
tangents[index] = if before * after <= 0.0 {
0.0
} else {
(before + after) / 2.0
};
}
for index in 0..count - 1 {
let slope = slopes[index];
if slope.abs() < f32::EPSILON {
tangents[index] = 0.0;
tangents[index + 1] = 0.0;
continue;
}
let (a, b) = (tangents[index] / slope, tangents[index + 1] / slope);
let length = a * a + b * b;
if length > 9.0 {
let t = 3.0 / length.sqrt();
tangents[index] = t * a * slope;
tangents[index + 1] = t * b * slope;
}
}
points
.windows(2)
.enumerate()
.map(|(index, pair)| {
let (start, end) = (pair[0], pair[1]);
let third = (end.0 - start.0) / 3.0;
(
start,
(start.0 + third, start.1 + tangents[index] * third),
(end.0 - third, end.1 - tangents[index + 1] * third),
end,
)
})
.collect()
}
impl ChartPlot for LinePlot {
fn paint(&self, canvas: &Context2D, area: ChartArea, look: &ChartLook, active: Option<ChartPoint>) {
self.draw(canvas, area, look, active, &self.tracks(area));
}
fn paint_moving(
&self,
canvas: &Context2D,
area: ChartArea,
look: &ChartLook,
active: Option<ChartPoint>,
motion: ChartMotion<'_>,
) {
if motion.at_rest() || self.categories.is_empty() {
return self.paint(canvas, area, look, active);
}
if let Some(from) = motion.from_as::<LinePlot>().filter(|from| !from.categories.is_empty()) {
return self.draw(canvas, area, look, active, &self.tracks_from(from, area, motion));
}
canvas.save();
canvas.begin_path();
canvas.rect(0.0, 0.0, area.width * motion.progress, area.height);
canvas.clip();
self.paint(canvas, area, look, active);
canvas.restore();
}
fn ticks(&self, area: ChartArea, look: &ChartLook) -> Vec<ChartTick> {
let scale = self.scale(area);
scale
.ticks()
.into_iter()
.map(|tick| ChartTick {
position: scale.map(tick),
text: (look.value_text)(tick),
baseline: tick == 0.0,
})
.collect()
}
fn axis(&self) -> ChartAxis {
if let Some(labels) = &self.axis {
return ChartAxis::Spread(labels.clone());
}
let count = self.categories.len();
if count <= AXIS_TEXTS {
return ChartAxis::Spread(self.categories.iter().map(|name| name.to_string()).collect());
}
ChartAxis::Spread(
(0..AXIS_TEXTS)
.map(|place| self.categories[place * (count - 1) / (AXIS_TEXTS - 1)].to_string())
.collect(),
)
}
fn point_at(&self, area: ChartArea, x: f32, y: f32) -> Option<ChartPoint> {
FINDING.with(|finding| finding.set(true));
let found = self.find(area, x, y);
FINDING.with(|finding| finding.set(false));
found
}
fn anchor(&self, area: ChartArea, point: ChartPoint) -> Option<(f32, f32)> {
let level = self.level(point.series, point.index)?;
Some((self.x(area, point.index), self.scale(area).map(level)))
}
fn tip_place(&self) -> TipPlace {
TipPlace::Beside
}
fn counts(&self) -> Vec<usize> {
(0..self.values.len())
.map(|series| if self.shown[series] { self.categories.len() } else { 0 })
.collect()
}
fn tip(&self, point: ChartPoint) -> ChartTip {
ChartTip {
title: self
.categories
.get(point.index)
.map(|name| name.to_string())
.unwrap_or_default(),
rows: self
.shown()
.into_iter()
.filter_map(|series| Some((series, self.value(series, point.index)?)))
.collect(),
}
}
fn items(&self, area: ChartArea, look: &ChartLook) -> Vec<CanvasItem> {
let scale = self.scale(area);
let several = self.values.len() > 1;
let mut items = Vec::new();
for series in self.shown() {
for index in 0..self.categories.len() {
let (Some(value), Some(level)) = (self.value(series, index), self.level(series, index)) else {
continue;
};
let name = &self.categories[index];
let label = if several {
format!("{name}, {}", self.names[series])
} else {
name.to_string()
};
items.push(
CanvasItem::point(
format!("point-{series}-{index}"),
"point",
self.x(area, index),
scale.map(level),
POINT_RADIUS,
)
.label(label)
.value((look.value_text)(value)),
);
}
}
items
}
fn signature(&self) -> u64 {
signature_of((
self.categories.iter().map(|name| name.to_string()).collect::<Vec<_>>(),
self
.values
.iter()
.map(|values| values.iter().map(|value| bits(*value)).collect::<Vec<_>>())
.collect::<Vec<_>>(),
&self.shown,
self.curve,
self.area,
self.stacked,
&self.axis,
))
}
fn is_empty(&self) -> bool {
self.categories.is_empty()
|| self
.shown()
.iter()
.all(|series| self.values[*series].iter().all(|value| !value.is_finite()))
}
}