Skip to content
Merged
Show file tree
Hide file tree
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
44 changes: 25 additions & 19 deletions egui_plot/src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::ops::RangeInclusive;
use egui::{
emath::Rot2,
epaint::{CircleShape, TextShape},
pos2, vec2, Align2, Color32, CornerRadius, Id, ImageOptions, Mesh, NumExt as _, Pos2, Rect,
Rgba, Shape, Stroke, TextStyle, TextureId, Ui, Vec2, WidgetText,
pos2, vec2, Align2, Color32, CornerRadius, Id, ImageOptions, Mesh, NumExt as _, PopupAnchor,
Pos2, Rect, Rgba, Shape, Stroke, TextStyle, TextureId, Ui, Vec2, WidgetText,
};

use emath::Float as _;
Expand Down Expand Up @@ -155,6 +155,7 @@ pub trait PlotItem {

fn on_hover(
&self,
plot_area_response: &egui::Response,
elem: ClosestElem,
shapes: &mut Vec<Shape>,
cursors: &mut Vec<Cursor>,
Expand Down Expand Up @@ -182,12 +183,11 @@ pub trait PlotItem {
let pointer = plot.transform.position_from_point(&value);
shapes.push(Shape::circle_filled(pointer, 3.0, line_color));

rulers_at_value(
pointer,
rulers_and_tooltip_at_value(
plot_area_response,
value,
self.name(),
plot,
shapes,
cursors,
label_formatter,
);
Expand Down Expand Up @@ -1372,6 +1372,7 @@ impl PlotItem for BarChart {

fn on_hover(
&self,
_plot_area_response: &egui::Response,
elem: ClosestElem,
shapes: &mut Vec<Shape>,
cursors: &mut Vec<Cursor>,
Expand Down Expand Up @@ -1504,6 +1505,7 @@ impl PlotItem for BoxPlot {

fn on_hover(
&self,
_plot_area_response: &egui::Response,
elem: ClosestElem,
shapes: &mut Vec<Shape>,
cursors: &mut Vec<Cursor>,
Expand Down Expand Up @@ -1631,12 +1633,11 @@ fn add_rulers_and_text(
///
/// `value` is used to for text displaying X/Y coordinates.
#[allow(clippy::too_many_arguments)]
pub(super) fn rulers_at_value(
pointer: Pos2,
pub(super) fn rulers_and_tooltip_at_value(
plot_area_response: &egui::Response,
value: PlotPoint,
name: &str,
plot: &PlotConfig<'_>,
shapes: &mut Vec<Shape>,
cursors: &mut Vec<Cursor>,
label_formatter: &LabelFormatter<'_>,
) {
Expand Down Expand Up @@ -1673,17 +1674,22 @@ pub(super) fn rulers_at_value(
}
};

let font_id = TextStyle::Body.resolve(plot.ui.style());
let ui = plot.ui;
let text_color = ui.visuals().text_color();
let galley = ui.fonts(|f| f.layout_no_wrap(text, font_id, text_color));
let rect = Align2::LEFT_BOTTOM.anchor_size(pointer + vec2(3.0, -2.0), galley.size());
shapes.push(Shape::rect_filled(
rect.expand(4.0),
ui.style().visuals.window_corner_radius,
ui.style().visuals.extreme_bg_color.gamma_multiply(0.75),
));
shapes.push(Shape::galley(rect.min, galley, text_color));
// We show the tooltip as soon as we're hovering the plot area:
let mut tooltip = egui::Tooltip::new(
plot_area_response.id,
plot_area_response.ctx.clone(),
PopupAnchor::Pointer,
plot_area_response.layer_id,
);

let tooltip_width = plot_area_response.ctx.style().spacing.tooltip_width;

tooltip.popup = tooltip.popup.width(tooltip_width);

tooltip.gap(12.0).show(|ui| {
ui.set_max_width(tooltip_width);
ui.label(text);
});
}

fn find_closest_rect<'a, T>(
Expand Down
18 changes: 14 additions & 4 deletions egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,7 @@ impl<'a> Plot<'a> {
}

let prepared = PreparedPlot {
plot_area_response: &response,
items,
show_x,
show_y,
Expand Down Expand Up @@ -1591,6 +1592,8 @@ pub fn uniform_grid_spacer<'a>(spacer: impl Fn(GridInput) -> [f64; 3] + 'a) -> G
// ----------------------------------------------------------------------------

struct PreparedPlot<'a> {
/// The response of the whole plot area
plot_area_response: &'a Response,
items: Vec<Box<dyn PlotItem + 'a>>,
show_x: bool,
show_y: bool,
Expand Down Expand Up @@ -1801,6 +1804,7 @@ impl PreparedPlot<'_> {

fn hover(&self, ui: &Ui, pointer: Pos2, shapes: &mut Vec<Shape>) -> (Vec<Cursor>, Option<Id>) {
let Self {
plot_area_response,
transform,
show_x,
show_y,
Expand Down Expand Up @@ -1839,16 +1843,22 @@ impl PreparedPlot<'_> {
let mut cursors = Vec::new();

let hovered_plot_item_id = if let Some((item, elem)) = closest {
item.on_hover(elem, shapes, &mut cursors, &plot, label_formatter);
item.on_hover(
plot_area_response,
elem,
shapes,
&mut cursors,
&plot,
label_formatter,
);
Some(item.id())
} else {
let value = transform.value_from_position(pointer);
items::rulers_at_value(
pointer,
items::rulers_and_tooltip_at_value(
plot_area_response,
value,
"",
&plot,
shapes,
&mut cursors,
label_formatter,
);
Expand Down
Loading