Skip to content

Commit e96f4e1

Browse files
authored
Allow setting a line's fill area's alpha channel (#34)
Hey! I'm trying to achieve stacked plot lines and came across [Line::fill](https://docs.rs/egui_plot/latest/egui_plot/struct.Line.html#method.fill), which does exactly what I want. The only problem with drawing the fill area is that a user doesn't have control over the alpha channel. As a result, having multiple fill areas within a single plot causes the colors to blend, which is undesired in my case. See the images below: Current: ![image](https://github.com/user-attachments/assets/d6ece302-662f-44f7-854b-876a40b4b285) Desired: ![image](https://github.com/user-attachments/assets/146bdc6d-ca2f-4f0c-b89a-33eb34dd0af1) This change adds `Line::fill_alpha`, which allows a user to explicitly set the alpha channel used for drawing the fill area. The default value for `fill_alpha` remains the same as before, causing this PR to not be a breaking change.
1 parent 21c768f commit e96f4e1

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

egui_plot/src/items/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ pub struct Line {
427427
pub(super) highlight: bool,
428428
pub(super) allow_hover: bool,
429429
pub(super) fill: Option<f32>,
430+
pub(super) fill_alpha: f32,
430431
pub(super) style: LineStyle,
431432
id: Option<Id>,
432433
}
@@ -440,6 +441,7 @@ impl Line {
440441
highlight: false,
441442
allow_hover: true,
442443
fill: None,
444+
fill_alpha: DEFAULT_FILL_ALPHA,
443445
style: LineStyle::Solid,
444446
id: None,
445447
}
@@ -487,6 +489,13 @@ impl Line {
487489
self
488490
}
489491

492+
/// Set the fill area's alpha channel. Default is `0.05`.
493+
#[inline]
494+
pub fn fill_alpha(mut self, alpha: impl Into<f32>) -> Self {
495+
self.fill_alpha = alpha.into();
496+
self
497+
}
498+
490499
/// Set the line's style. Default is `LineStyle::Solid`.
491500
#[inline]
492501
pub fn style(mut self, style: LineStyle) -> Self {
@@ -545,7 +554,7 @@ impl PlotItem for Line {
545554
fill = None;
546555
}
547556
if let Some(y_reference) = fill {
548-
let mut fill_alpha = DEFAULT_FILL_ALPHA;
557+
let mut fill_alpha = self.fill_alpha;
549558
if *highlight {
550559
fill_alpha = (2.0 * fill_alpha).at_most(1.0);
551560
}

0 commit comments

Comments
 (0)