mirror of
https://github.com/emilk/egui.git
synced 2026-09-03 07:10:04 -04:00
plot-axis: Expose more conveniece functions to public API. Add axis labels to demo app
This commit is contained in:
@@ -43,7 +43,7 @@ pub(super) type YAxisWidget = AxisWidget<Y_AXIS>;
|
|||||||
pub struct AxisHints<const AXIS: usize> {
|
pub struct AxisHints<const AXIS: usize> {
|
||||||
pub(super) label: WidgetText,
|
pub(super) label: WidgetText,
|
||||||
pub(super) formatter: AxisFormatterFn,
|
pub(super) formatter: AxisFormatterFn,
|
||||||
digits: usize,
|
pub(super) digits: usize,
|
||||||
pub(super) placement: Placement,
|
pub(super) placement: Placement,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,8 +104,7 @@ impl<const AXIS: usize> AxisHints<AXIS> {
|
|||||||
|
|
||||||
/// Specify maximum number of digits for ticks.
|
/// Specify maximum number of digits for ticks.
|
||||||
///
|
///
|
||||||
/// This is considered by the default tick formatter
|
/// This is considered by the default tick formatter and affects the width of the y-axis
|
||||||
/// and affects the width of the internal y-axis widget
|
|
||||||
pub fn max_digits(mut self, digits: usize) -> Self {
|
pub fn max_digits(mut self, digits: usize) -> Self {
|
||||||
self.digits = digits;
|
self.digits = digits;
|
||||||
self
|
self
|
||||||
|
|||||||
@@ -565,22 +565,32 @@ impl Plot {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the x axis label of the bottom x-axis
|
/// Set the x axis label of the main x-axis
|
||||||
|
///
|
||||||
|
/// If no x-axis has been specified so far a new one will be created.
|
||||||
pub fn x_axis_label(mut self, label: impl Into<WidgetText>) -> Self {
|
pub fn x_axis_label(mut self, label: impl Into<WidgetText>) -> Self {
|
||||||
if !self.x_axes.is_empty() {
|
if self.x_axes.is_empty() {
|
||||||
|
self.x_axes.push(XAxisHints::default().label(label))
|
||||||
|
} else {
|
||||||
self.x_axes[0].label = label.into();
|
self.x_axes[0].label = label.into();
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
/// Set the y axis label of the left y-axis
|
/// Set the y axis label of the main y-axis
|
||||||
|
///
|
||||||
|
/// If no y-axis has been specified so far a new one will be created.
|
||||||
pub fn y_axis_label(mut self, label: impl Into<WidgetText>) -> Self {
|
pub fn y_axis_label(mut self, label: impl Into<WidgetText>) -> Self {
|
||||||
if !self.y_axes.is_empty() {
|
if self.y_axes.is_empty() {
|
||||||
|
self.y_axes.push(YAxisHints::default().label(label))
|
||||||
|
} else {
|
||||||
self.y_axes[0].label = label.into();
|
self.y_axes[0].label = label.into();
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the x-axis position
|
/// Set the position of the main x-axis
|
||||||
|
///
|
||||||
|
/// > Note: This requires an x-axis to exist, either by [`Self::custom_x_axes`] or [`Self::x_axis_label`]
|
||||||
pub fn x_axis_position(mut self, placement: axis::Placement) -> Self {
|
pub fn x_axis_position(mut self, placement: axis::Placement) -> Self {
|
||||||
if !self.x_axes.is_empty() {
|
if !self.x_axes.is_empty() {
|
||||||
self.x_axes[0].placement = placement;
|
self.x_axes[0].placement = placement;
|
||||||
@@ -588,7 +598,9 @@ impl Plot {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the y-axis position
|
/// Set the position of the main y-axis
|
||||||
|
///
|
||||||
|
/// > Note: This requires a y-axis to exist, either by [`Self::custom_y_axes`] or [`Self::y_axis_label`]
|
||||||
pub fn y_axis_position(mut self, placement: axis::Placement) -> Self {
|
pub fn y_axis_position(mut self, placement: axis::Placement) -> Self {
|
||||||
if !self.y_axes.is_empty() {
|
if !self.y_axes.is_empty() {
|
||||||
self.y_axes[0].placement = placement;
|
self.y_axes[0].placement = placement;
|
||||||
@@ -596,7 +608,9 @@ impl Plot {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specify custom formatter for ticks on x-axis
|
/// Specify custom formatter for ticks on the main x-axis
|
||||||
|
///
|
||||||
|
/// > Note: This requires an x-axis to exist, either by [`Self::custom_x_axes`] or [`Self::x_axis_label`]
|
||||||
///
|
///
|
||||||
/// The first parameter of `fmt` is the raw tick value as `f64`.
|
/// The first parameter of `fmt` is the raw tick value as `f64`.
|
||||||
/// The second paramter is the maximum requested number of characters per tick label.
|
/// The second paramter is the maximum requested number of characters per tick label.
|
||||||
@@ -608,7 +622,9 @@ impl Plot {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specify custom formatter for ticks on y-axis
|
/// Specify custom formatter for ticks on the main y-axis
|
||||||
|
///
|
||||||
|
/// > Note: This requires a y-axis to exist, either by [`Self::custom_y_axes`] or [`Self::y_axis_label`]
|
||||||
///
|
///
|
||||||
/// The first parameter of `formatter` is the raw tick value as `f64`.
|
/// The first parameter of `formatter` is the raw tick value as `f64`.
|
||||||
/// The second paramter is the maximum requested number of characters per tick label.
|
/// The second paramter is the maximum requested number of characters per tick label.
|
||||||
@@ -620,9 +636,23 @@ impl Plot {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the main y-axis-width by number of digits
|
||||||
|
///
|
||||||
|
/// The default is 5 digits.
|
||||||
|
///
|
||||||
|
/// > Note: This requires a y-axis to exist, either by [`Self::custom_y_axes`] or [`Self::y_axis_label`]
|
||||||
|
///
|
||||||
|
/// > Todo: This is experimental. Changing the font size might break this.
|
||||||
|
pub fn y_axis_width(mut self, digits: usize) -> Self {
|
||||||
|
if !self.y_axes.is_empty() {
|
||||||
|
self.y_axes[0].digits = digits;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set custom configuration for bottom x-axis
|
/// Set custom configuration for bottom x-axis
|
||||||
///
|
///
|
||||||
/// More than one axis may be specified.
|
/// More than one axis may be specified. The first specified axis is considered the main axis.
|
||||||
pub fn custom_x_axes(mut self, hints: Vec<XAxisHints>) -> Self {
|
pub fn custom_x_axes(mut self, hints: Vec<XAxisHints>) -> Self {
|
||||||
self.x_axes = hints;
|
self.x_axes = hints;
|
||||||
self
|
self
|
||||||
@@ -630,7 +660,7 @@ impl Plot {
|
|||||||
|
|
||||||
/// Set custom configuration for left y-axis
|
/// Set custom configuration for left y-axis
|
||||||
///
|
///
|
||||||
/// More than one axis may be specified.
|
/// More than one axis may be specified. The first specified axis is considered the main axis.
|
||||||
pub fn custom_y_axes(mut self, hints: Vec<YAxisHints>) -> Self {
|
pub fn custom_y_axes(mut self, hints: Vec<YAxisHints>) -> Self {
|
||||||
self.y_axes = hints;
|
self.y_axes = hints;
|
||||||
self
|
self
|
||||||
|
|||||||
@@ -264,7 +264,11 @@ impl LineDemo {
|
|||||||
ui.ctx().request_repaint();
|
ui.ctx().request_repaint();
|
||||||
self.time += ui.input(|i| i.unstable_dt).at_most(1.0 / 30.0) as f64;
|
self.time += ui.input(|i| i.unstable_dt).at_most(1.0 / 30.0) as f64;
|
||||||
};
|
};
|
||||||
let mut plot = Plot::new("lines_demo").legend(Legend::default());
|
let mut plot = Plot::new("lines_demo")
|
||||||
|
.legend(Legend::default())
|
||||||
|
.x_axis_label("")
|
||||||
|
.y_axis_label("")
|
||||||
|
.y_axis_width(4);
|
||||||
if self.square {
|
if self.square {
|
||||||
plot = plot.view_aspect(1.0);
|
plot = plot.view_aspect(1.0);
|
||||||
}
|
}
|
||||||
@@ -421,8 +425,10 @@ impl LegendDemo {
|
|||||||
);
|
);
|
||||||
ui.end_row();
|
ui.end_row();
|
||||||
});
|
});
|
||||||
|
|
||||||
let legend_plot = Plot::new("legend_demo")
|
let legend_plot = Plot::new("legend_demo")
|
||||||
|
.x_axis_label("")
|
||||||
|
.y_axis_label("")
|
||||||
|
.y_axis_width(2)
|
||||||
.legend(config.clone())
|
.legend(config.clone())
|
||||||
.data_aspect(1.0);
|
.data_aspect(1.0);
|
||||||
legend_plot
|
legend_plot
|
||||||
@@ -550,18 +556,16 @@ impl CustomAxisDemo {
|
|||||||
ui.label("Zoom in on the X-axis to see hours and minutes");
|
ui.label("Zoom in on the X-axis to see hours and minutes");
|
||||||
|
|
||||||
let x_axes = vec![
|
let x_axes = vec![
|
||||||
XAxisHints::default()
|
XAxisHints::default().label("Time").formatter(x_fmt),
|
||||||
.label("Time".to_owned())
|
XAxisHints::default().label("Value"),
|
||||||
.formatter(x_fmt),
|
|
||||||
XAxisHints::default().label("Value".to_owned()),
|
|
||||||
];
|
];
|
||||||
let y_axes = vec![
|
let y_axes = vec![
|
||||||
YAxisHints::default()
|
YAxisHints::default()
|
||||||
.label("Percent".to_owned())
|
.label("Percent")
|
||||||
.formatter(y_fmt)
|
.formatter(y_fmt)
|
||||||
.max_digits(4),
|
.max_digits(4),
|
||||||
YAxisHints::default()
|
YAxisHints::default()
|
||||||
.label("Absolute".to_owned())
|
.label("Absolute")
|
||||||
.placement(Placement::Opposite),
|
.placement(Placement::Opposite),
|
||||||
];
|
];
|
||||||
Plot::new("custom_axes")
|
Plot::new("custom_axes")
|
||||||
@@ -660,6 +664,9 @@ impl LinkedAxisDemo {
|
|||||||
.data_aspect(2.0)
|
.data_aspect(2.0)
|
||||||
.width(150.0)
|
.width(150.0)
|
||||||
.height(250.0)
|
.height(250.0)
|
||||||
|
.y_axis_label("y")
|
||||||
|
.y_axis_position(Placement::Opposite)
|
||||||
|
.y_axis_width(3)
|
||||||
.link_axis(link_group_id, self.link_x, self.link_y)
|
.link_axis(link_group_id, self.link_x, self.link_y)
|
||||||
.link_cursor(link_group_id, self.link_cursor_x, self.link_cursor_y)
|
.link_cursor(link_group_id, self.link_cursor_x, self.link_cursor_y)
|
||||||
.show(ui, LinkedAxisDemo::configure_plot);
|
.show(ui, LinkedAxisDemo::configure_plot);
|
||||||
@@ -668,6 +675,7 @@ impl LinkedAxisDemo {
|
|||||||
.data_aspect(0.5)
|
.data_aspect(0.5)
|
||||||
.width(250.0)
|
.width(250.0)
|
||||||
.height(150.0)
|
.height(150.0)
|
||||||
|
.x_axis_label("x")
|
||||||
.link_axis(link_group_id, self.link_x, self.link_y)
|
.link_axis(link_group_id, self.link_x, self.link_y)
|
||||||
.link_cursor(link_group_id, self.link_cursor_x, self.link_cursor_y)
|
.link_cursor(link_group_id, self.link_cursor_x, self.link_cursor_y)
|
||||||
.show(ui, LinkedAxisDemo::configure_plot)
|
.show(ui, LinkedAxisDemo::configure_plot)
|
||||||
@@ -899,6 +907,9 @@ impl ChartsDemo {
|
|||||||
Plot::new("Normal Distribution Demo")
|
Plot::new("Normal Distribution Demo")
|
||||||
.legend(Legend::default())
|
.legend(Legend::default())
|
||||||
.clamp_grid(true)
|
.clamp_grid(true)
|
||||||
|
.x_axis_label("")
|
||||||
|
.y_axis_label("")
|
||||||
|
.y_axis_width(3)
|
||||||
.allow_zoom(self.allow_zoom)
|
.allow_zoom(self.allow_zoom)
|
||||||
.allow_drag(self.allow_drag)
|
.allow_drag(self.allow_drag)
|
||||||
.show(ui, |plot_ui| plot_ui.bar_chart(chart))
|
.show(ui, |plot_ui| plot_ui.bar_chart(chart))
|
||||||
|
|||||||
Reference in New Issue
Block a user