From f18cbfce74d8a6a6910e17cfd2070d73edd26ff1 Mon Sep 17 00:00:00 2001 From: Keegan Wilgermein Date: Thu, 9 Apr 2026 16:43:58 +1200 Subject: [PATCH] Added option to change background color of graph --- src/plot_widget.rs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/plot_widget.rs b/src/plot_widget.rs index dbdb214..340a4ec 100644 --- a/src/plot_widget.rs +++ b/src/plot_widget.rs @@ -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; @@ -85,6 +79,7 @@ pub struct PlotWidget { pub(crate) tick_label_size: f32, pub(crate) axis_label_size: f32, pub(crate) data_aspect: Option, + pub(crate) background_color: Option, // UI state /// Map of picked point id to highlight point data & tooltip text. pub(crate) picked_points: IndexMap)>, @@ -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(), @@ -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) { + 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) { @@ -559,9 +562,19 @@ impl PlotWidget { .width(Length::Fill) .height(Length::Fill); + let color_decision = |color: Option, 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) @@ -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() }