Skip to content

Commit

Permalink
Return plot transforms (#2935)
Browse files Browse the repository at this point in the history
* Expose the plot transform to users

* Rename plot::ScreenTransform to PlotTransform

* Plot: return a PlotResponse with a transform member
  • Loading branch information
emilk authored Apr 19, 2023
1 parent ce761e5 commit d46cf06
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 58 deletions.
6 changes: 3 additions & 3 deletions crates/egui/src/widgets/plot/items/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::emath::NumExt;
use crate::epaint::{Color32, RectShape, Rounding, Shape, Stroke};

use super::{add_rulers_and_text, highlighted_color, Orientation, PlotConfig, RectElement};
use crate::plot::{BarChart, Cursor, PlotPoint, ScreenTransform};
use crate::plot::{BarChart, Cursor, PlotPoint, PlotTransform};

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

pub(super) fn add_shapes(
&self,
transform: &ScreenTransform,
transform: &PlotTransform,
highlighted: bool,
shapes: &mut Vec<Shape>,
) {
Expand Down Expand Up @@ -183,7 +183,7 @@ impl RectElement for Bar {
self.orientation
}

fn default_values_format(&self, transform: &ScreenTransform) -> String {
fn default_values_format(&self, transform: &PlotTransform) -> String {
let scale = transform.dvalue_dpos();
let scale = match self.orientation {
Orientation::Horizontal => scale[0],
Expand Down
6 changes: 3 additions & 3 deletions crates/egui/src/widgets/plot/items/box_elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::emath::NumExt;
use crate::epaint::{Color32, RectShape, Rounding, Shape, Stroke};

use super::{add_rulers_and_text, highlighted_color, Orientation, PlotConfig, RectElement};
use crate::plot::{BoxPlot, Cursor, PlotPoint, ScreenTransform};
use crate::plot::{BoxPlot, Cursor, PlotPoint, PlotTransform};

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

pub(super) fn add_shapes(
&self,
transform: &ScreenTransform,
transform: &PlotTransform,
highlighted: bool,
shapes: &mut Vec<Shape>,
) {
Expand Down Expand Up @@ -267,7 +267,7 @@ impl RectElement for BoxElem {
self.point_at(self.argument, self.spread.upper_whisker)
}

fn default_values_format(&self, transform: &ScreenTransform) -> String {
fn default_values_format(&self, transform: &PlotTransform) -> String {
let scale = transform.dvalue_dpos();
let scale = match self.orientation {
Orientation::Horizontal => scale[0],
Expand Down
38 changes: 19 additions & 19 deletions crates/egui/src/widgets/plot/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use epaint::Mesh;

use crate::*;

use super::{Cursor, LabelFormatter, PlotBounds, ScreenTransform};
use super::{Cursor, LabelFormatter, PlotBounds, PlotTransform};
use rect_elem::*;
use values::{ClosestElem, PlotGeometry};

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

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

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

fn bounds(&self) -> PlotBounds;

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

Expand Down Expand Up @@ -177,7 +177,7 @@ impl HLine {
}

impl PlotItem for HLine {
fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
fn shapes(&self, ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
let HLine {
y,
stroke,
Expand Down Expand Up @@ -293,7 +293,7 @@ impl VLine {
}

impl PlotItem for VLine {
fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
fn shapes(&self, ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
let VLine {
x,
stroke,
Expand Down Expand Up @@ -423,7 +423,7 @@ fn y_intersection(p1: &Pos2, p2: &Pos2, y: f32) -> Option<f32> {
}

impl PlotItem for Line {
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
let Self {
series,
stroke,
Expand Down Expand Up @@ -584,7 +584,7 @@ impl Polygon {
}

impl PlotItem for Polygon {
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
let Self {
series,
stroke,
Expand Down Expand Up @@ -696,7 +696,7 @@ impl Text {
}

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

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

impl PlotItem for Arrows {
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
use crate::emath::*;
let Self {
origins,
Expand Down Expand Up @@ -1178,7 +1178,7 @@ impl PlotImage {
}

impl PlotItem for PlotImage {
fn shapes(&self, ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
fn shapes(&self, ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
let Self {
position,
texture_id,
Expand Down Expand Up @@ -1369,7 +1369,7 @@ impl BarChart {
}

impl PlotItem for BarChart {
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
for b in &self.bars {
b.add_shapes(transform, self.highlight, shapes);
}
Expand Down Expand Up @@ -1407,7 +1407,7 @@ impl PlotItem for BarChart {
bounds
}

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

Expand Down Expand Up @@ -1512,7 +1512,7 @@ impl BoxPlot {
}

impl PlotItem for BoxPlot {
fn shapes(&self, _ui: &mut Ui, transform: &ScreenTransform, shapes: &mut Vec<Shape>) {
fn shapes(&self, _ui: &mut Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
for b in &self.boxes {
b.add_shapes(transform, self.highlight, shapes);
}
Expand Down Expand Up @@ -1550,7 +1550,7 @@ impl PlotItem for BoxPlot {
bounds
}

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

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

pub(crate) fn vertical_line(
pointer: Pos2,
transform: &ScreenTransform,
transform: &PlotTransform,
line_color: Color32,
) -> Shape {
let frame = transform.frame();
Expand All @@ -1597,7 +1597,7 @@ pub(crate) fn vertical_line(

pub(crate) fn horizontal_line(
pointer: Pos2,
transform: &ScreenTransform,
transform: &PlotTransform,
line_color: Color32,
) -> Shape {
let frame = transform.frame();
Expand Down Expand Up @@ -1731,7 +1731,7 @@ pub(super) fn rulers_at_value(
fn find_closest_rect<'a, T>(
rects: impl IntoIterator<Item = &'a T>,
point: Pos2,
transform: &ScreenTransform,
transform: &PlotTransform,
) -> Option<ClosestElem>
where
T: 'a + RectElement,
Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/widgets/plot/items/rect_elem.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Orientation, PlotPoint};
use crate::plot::transform::{PlotBounds, ScreenTransform};
use crate::plot::transform::{PlotBounds, PlotTransform};
use epaint::emath::NumExt;
use epaint::{Color32, Rgba, Stroke};

Expand Down Expand Up @@ -48,7 +48,7 @@ pub(super) trait RectElement {
}

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

// ----------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit d46cf06

Please sign in to comment.