Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional line to Points PlotItem #4143

Closed
wants to merge 3 commits into from
Closed
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
69 changes: 68 additions & 1 deletion crates/egui_plot/src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,15 @@ pub struct Points {
/// The maximum extent of the marker from its center.
pub(super) radius: f32,

/// Draw line between points
pub(super) line: bool,

/// Color of the line (if drawn)
pub(super) stroke: Stroke,

/// Style of the line (if drawn)
pub(super) style: LineStyle,

pub(super) name: String,

pub(super) highlight: bool,
Expand All @@ -868,6 +877,9 @@ impl Points {
color: Color32::TRANSPARENT,
filled: true,
radius: 1.0,
line: false,
stroke: Stroke::new(1.5, Color32::TRANSPARENT), // Note: a stroke of 1.0 (or less) can look bad on low-dpi-screens
style: LineStyle::Solid,
name: Default::default(),
highlight: false,
stems: None,
Expand All @@ -889,13 +901,28 @@ impl Points {
self
}

/// Set the marker's color.
/// Set the both marker and line 's color.
#[inline]
pub fn color(mut self, color: impl Into<Color32>) -> Self {
self.color = color.into();
self.stroke.color = self.color.clone();
self
}

/// Set the marker's color.
#[inline]
pub fn marker_color(mut self, color: impl Into<Color32>) -> Self {
self.color = color.into();
self
}

/// Set the line's color.
#[inline]
pub fn line_color(mut self, color: impl Into<Color32>) -> Self {
self.stroke.color = color.into();
self
}

/// Whether to fill the marker.
#[inline]
pub fn filled(mut self, filled: bool) -> Self {
Expand All @@ -917,6 +944,34 @@ impl Points {
self
}

/// Toggle line between points
#[inline]
pub fn line(mut self, shown: bool) -> Self {
self.line = shown;
self
}

/// Add a stroke to the line
#[inline]
pub fn line_stroke(mut self, stroke: impl Into<Stroke>) -> Self {
self.stroke = stroke.into();
self
}

/// Stroke width. A high value means the plot thickens.
#[inline]
pub fn line_width(mut self, width: impl Into<f32>) -> Self {
self.stroke.width = width.into();
self
}

/// Set the line's style. Default is `LineStyle::Solid`.
#[inline]
pub fn line_style(mut self, style: LineStyle) -> Self {
self.style = style;
self
}

/// Name of this set of points.
///
/// This name will show up in the plot legend, if legends are turned on.
Expand Down Expand Up @@ -950,11 +1005,15 @@ impl PlotItem for Points {
color,
filled,
mut radius,
line,
stroke,
style,
highlight,
stems,
..
} = self;

let line_stroke = *stroke;
let stroke_size = radius / 5.0;

let default_stroke = Stroke::new(stroke_size, *color);
Expand Down Expand Up @@ -1065,6 +1124,14 @@ impl PlotItem for Points {
}
}
});
if *line {
let values_tf: Vec<_> = series
.points()
.iter()
.map(|v| transform.position_from_point(v))
.collect();
style.style_line(values_tf, line_stroke, *highlight, shapes);
}
}

fn initialize(&mut self, x_range: RangeInclusive<f64>) {
Expand Down
Loading