Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/plot_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ use std::{

use glam::{DVec2, Vec2};
use iced::{
Color, Element, Length, Rectangle, Theme,
alignment::{self, Horizontal, Vertical},
keyboard,
mouse::{self, Interaction},
padding::{self, Padding},
wgpu::TextureFormat,
widget::{
Background, Color, Element, Length, Rectangle, Theme, alignment::{self, Horizontal, Vertical}, keyboard, mouse::{self, Interaction}, padding::{self, Padding}, wgpu::TextureFormat, widget::{
self, container,
shader::{self, Pipeline, Viewport},
stack,
},
}
};
use indexmap::IndexMap;

Expand Down Expand Up @@ -85,6 +79,7 @@ pub struct PlotWidget {
pub(crate) tick_label_size: f32,
pub(crate) axis_label_size: f32,
pub(crate) data_aspect: Option<f64>,
pub(crate) background_color: Option<Color>,
// UI state
/// Map of picked point id to highlight point data & tooltip text.
pub(crate) picked_points: IndexMap<PointId, (HighlightPoint, Option<TooltipUiPayload>)>,
Expand Down Expand Up @@ -140,6 +135,7 @@ impl PlotWidget {
tick_label_size: 10.0,
axis_label_size: 16.0,
data_aspect: None,
background_color: None,
x_ticks: Vec::new(),
y_ticks: Vec::new(),
picked_points: IndexMap::new(),
Expand Down Expand Up @@ -228,6 +224,13 @@ impl PlotWidget {
Ok(())
}

/// Set the background color of the whole graph
///
/// Set to `None` to use the default values
pub fn with_background_color(&mut self, color: Option<Color>) {
self.background_color = color;
}

/// Add a horizontal reference line to the plot.
/// If there exists a line with the same `hline.id` ([ShapeId]), the old one will be replaced.
pub fn add_hline(&mut self, hline: HLine) {
Expand Down Expand Up @@ -559,9 +562,19 @@ impl PlotWidget {
.width(Length::Fill)
.height(Length::Fill);

let color_decision = |color: Option<Color>, theme: &Theme| {
if let Some(color) = color {
color
} else {
theme.palette().background
}
};

let inner_container = container(plot)
.padding(2.0)
.style(|theme: &Theme| container::background(theme.palette().background));
.style(move |theme: &Theme| container::background(
color_decision(self.background_color, theme)
));

let legend = if self.legend_enabled {
legend::legend(self, self.legend_collapsed)
Expand Down Expand Up @@ -590,7 +603,9 @@ impl PlotWidget {
self.axis_label_size,
))
.padding(3.0)
.style(|theme: &Theme| container::background(theme.palette().background))
.style(move |theme: &Theme| container::background(
color_decision(self.background_color, theme)
))
.into()
}

Expand Down