Skip to content

Commit d46cf06

Browse files
authored
Return plot transforms (#2935)
* Expose the plot transform to users * Rename plot::ScreenTransform to PlotTransform * Plot: return a PlotResponse with a transform member
1 parent ce761e5 commit d46cf06

File tree

7 files changed

+85
-58
lines changed

7 files changed

+85
-58
lines changed

crates/egui/src/widgets/plot/items/bar.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::emath::NumExt;
22
use crate::epaint::{Color32, RectShape, Rounding, Shape, Stroke};
33

44
use super::{add_rulers_and_text, highlighted_color, Orientation, PlotConfig, RectElement};
5-
use crate::plot::{BarChart, Cursor, PlotPoint, ScreenTransform};
5+
use crate::plot::{BarChart, Cursor, PlotPoint, PlotTransform};
66

77
/// One bar in a [`BarChart`]. Potentially floating, allowing stacked bar charts.
88
/// Width can be changed to allow variable-width histograms.
@@ -116,7 +116,7 @@ impl Bar {
116116

117117
pub(super) fn add_shapes(
118118
&self,
119-
transform: &ScreenTransform,
119+
transform: &PlotTransform,
120120
highlighted: bool,
121121
shapes: &mut Vec<Shape>,
122122
) {
@@ -183,7 +183,7 @@ impl RectElement for Bar {
183183
self.orientation
184184
}
185185

186-
fn default_values_format(&self, transform: &ScreenTransform) -> String {
186+
fn default_values_format(&self, transform: &PlotTransform) -> String {
187187
let scale = transform.dvalue_dpos();
188188
let scale = match self.orientation {
189189
Orientation::Horizontal => scale[0],

crates/egui/src/widgets/plot/items/box_elem.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::emath::NumExt;
22
use crate::epaint::{Color32, RectShape, Rounding, Shape, Stroke};
33

44
use super::{add_rulers_and_text, highlighted_color, Orientation, PlotConfig, RectElement};
5-
use crate::plot::{BoxPlot, Cursor, PlotPoint, ScreenTransform};
5+
use crate::plot::{BoxPlot, Cursor, PlotPoint, PlotTransform};
66

77
/// Contains the values of a single box in a box plot.
88
#[derive(Clone, Debug, PartialEq)]
@@ -136,7 +136,7 @@ impl BoxElem {
136136

137137
pub(super) fn add_shapes(
138138
&self,
139-
transform: &ScreenTransform,
139+
transform: &PlotTransform,
140140
highlighted: bool,
141141
shapes: &mut Vec<Shape>,
142142
) {
@@ -267,7 +267,7 @@ impl RectElement for BoxElem {
267267
self.point_at(self.argument, self.spread.upper_whisker)
268268
}
269269

270-
fn default_values_format(&self, transform: &ScreenTransform) -> String {
270+
fn default_values_format(&self, transform: &PlotTransform) -> String {
271271
let scale = transform.dvalue_dpos();
272272
let scale = match self.orientation {
273273
Orientation::Horizontal => scale[0],

crates/egui/src/widgets/plot/items/mod.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use epaint::Mesh;
77

88
use crate::*;
99

10-
use super::{Cursor, LabelFormatter, PlotBounds, ScreenTransform};
10+
use super::{Cursor, LabelFormatter, PlotBounds, PlotTransform};
1111
use rect_elem::*;
1212
use values::{ClosestElem, PlotGeometry};
1313

@@ -25,14 +25,14 @@ const DEFAULT_FILL_ALPHA: f32 = 0.05;
2525
/// Container to pass-through several parameters related to plot visualization
2626
pub(super) struct PlotConfig<'a> {
2727
pub ui: &'a Ui,
28-
pub transform: &'a ScreenTransform,
28+
pub transform: &'a PlotTransform,
2929
pub show_x: bool,
3030
pub show_y: bool,
3131
}
3232

3333
/// Trait shared by things that can be drawn in the plot.
3434
pub(super) trait PlotItem {
35-
fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>);
35+
fn shapes(&self, ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>);
3636

3737
/// For plot-items which are generated based on x values (plotting functions).
3838
fn initialize(&mut self, x_range: RangeInclusive<f64>);
@@ -49,7 +49,7 @@ pub(super) trait PlotItem {
4949

5050
fn bounds(&self) -> PlotBounds;
5151

52-
fn find_closest(&self, point: Pos2, transform: &ScreenTransform) -> Option<ClosestElem> {
52+
fn find_closest(&self, point: Pos2, transform: &PlotTransform) -> Option<ClosestElem> {
5353
match self.geometry() {
5454
PlotGeometry::None => None,
5555

@@ -177,7 +177,7 @@ impl HLine {
177177
}
178178

179179
impl PlotItem for HLine {
180-
fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
180+
fn shapes(&self, ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
181181
let HLine {
182182
y,
183183
stroke,
@@ -293,7 +293,7 @@ impl VLine {
293293
}
294294

295295
impl PlotItem for VLine {
296-
fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
296+
fn shapes(&self, ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
297297
let VLine {
298298
x,
299299
stroke,
@@ -423,7 +423,7 @@ fn y_intersection(p1: &Pos2, p2: &Pos2, y: f32) -> Option<f32> {
423423
}
424424

425425
impl PlotItem for Line {
426-
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
426+
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
427427
let Self {
428428
series,
429429
stroke,
@@ -584,7 +584,7 @@ impl Polygon {
584584
}
585585

586586
impl PlotItem for Polygon {
587-
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
587+
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
588588
let Self {
589589
series,
590590
stroke,
@@ -696,7 +696,7 @@ impl Text {
696696
}
697697

698698
impl PlotItem for Text {
699-
fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
699+
fn shapes(&self, ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
700700
let color = if self.color == Color32::TRANSPARENT {
701701
ui.style().visuals.text_color()
702702
} else {
@@ -836,7 +836,7 @@ impl Points {
836836
}
837837

838838
impl PlotItem for Points {
839-
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
839+
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
840840
let sqrt_3 = 3_f32.sqrt();
841841
let frac_sqrt_3_2 = 3_f32.sqrt() / 2.0;
842842
let frac_1_sqrt_2 = 1.0 / 2_f32.sqrt();
@@ -1039,7 +1039,7 @@ impl Arrows {
10391039
}
10401040

10411041
impl PlotItem for Arrows {
1042-
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
1042+
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
10431043
use crate::emath::*;
10441044
let Self {
10451045
origins,
@@ -1178,7 +1178,7 @@ impl PlotImage {
11781178
}
11791179

11801180
impl PlotItem for PlotImage {
1181-
fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
1181+
fn shapes(&self, ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
11821182
let Self {
11831183
position,
11841184
texture_id,
@@ -1369,7 +1369,7 @@ impl BarChart {
13691369
}
13701370

13711371
impl PlotItem for BarChart {
1372-
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
1372+
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
13731373
for b in &self.bars {
13741374
b.add_shapes(transform, self.highlight, shapes);
13751375
}
@@ -1407,7 +1407,7 @@ impl PlotItem for BarChart {
14071407
bounds
14081408
}
14091409

1410-
fn find_closest(&self, point: Pos2, transform: &ScreenTransform) -> Option<ClosestElem> {
1410+
fn find_closest(&self, point: Pos2, transform: &PlotTransform) -> Option<ClosestElem> {
14111411
find_closest_rect(&self.bars, point, transform)
14121412
}
14131413

@@ -1512,7 +1512,7 @@ impl BoxPlot {
15121512
}
15131513

15141514
impl PlotItem for BoxPlot {
1515-
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
1515+
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
15161516
for b in &self.boxes {
15171517
b.add_shapes(transform, self.highlight, shapes);
15181518
}
@@ -1550,7 +1550,7 @@ impl PlotItem for BoxPlot {
15501550
bounds
15511551
}
15521552

1553-
fn find_closest(&self, point: Pos2, transform: &ScreenTransform) -> Option<ClosestElem> {
1553+
fn find_closest(&self, point: Pos2, transform: &PlotTransform) -> Option<ClosestElem> {
15541554
find_closest_rect(&self.boxes, point, transform)
15551555
}
15561556

@@ -1582,7 +1582,7 @@ pub(crate) fn rulers_color(ui: &Ui) -> Color32 {
15821582

15831583
pub(crate) fn vertical_line(
15841584
pointer: Pos2,
1585-
transform: &ScreenTransform,
1585+
transform: &PlotTransform,
15861586
line_color: Color32,
15871587
) -> Shape {
15881588
let frame = transform.frame();
@@ -1597,7 +1597,7 @@ pub(crate) fn vertical_line(
15971597

15981598
pub(crate) fn horizontal_line(
15991599
pointer: Pos2,
1600-
transform: &ScreenTransform,
1600+
transform: &PlotTransform,
16011601
line_color: Color32,
16021602
) -> Shape {
16031603
let frame = transform.frame();
@@ -1731,7 +1731,7 @@ pub(super) fn rulers_at_value(
17311731
fn find_closest_rect<'a, T>(
17321732
rects: impl IntoIterator<Item = &'a T>,
17331733
point: Pos2,
1734-
transform: &ScreenTransform,
1734+
transform: &PlotTransform,
17351735
) -> Option<ClosestElem>
17361736
where
17371737
T: 'a + RectElement,

crates/egui/src/widgets/plot/items/rect_elem.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{Orientation, PlotPoint};
2-
use crate::plot::transform::{PlotBounds, ScreenTransform};
2+
use crate::plot::transform::{PlotBounds, PlotTransform};
33
use epaint::emath::NumExt;
44
use epaint::{Color32, Rgba, Stroke};
55

@@ -48,7 +48,7 @@ pub(super) trait RectElement {
4848
}
4949

5050
/// Debug formatting for hovered-over value, if none is specified by the user
51-
fn default_values_format(&self, transform: &ScreenTransform) -> String;
51+
fn default_values_format(&self, transform: &PlotTransform) -> String;
5252
}
5353

5454
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)