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

Fix axis thickness for multiple X or Y axes #60

Merged
merged 1 commit into from
Dec 17, 2024

Conversation

raymanfx
Copy link
Contributor

Previously, the axis widgets and the plot memory would calculate conflicting thickness values for an axis because a reverse iterator was used to push the axis values.

Later, the axis thickness would be inserted using an index-based operation into plot memory, which did not take into account the reverse order.

See: mem.y_axis_thickness.insert(i, thickness);

Example code illustrating the issue:

use std::ops::RangeInclusive;

use eframe::egui;
use egui_plot::{AxisHints, GridMark, Legend, Line, Plot};

fn main() -> eframe::Result {
    let native_options = eframe::NativeOptions::default();
    eframe::run_native(
        "plotbug",
        native_options,
        Box::new(|cc| Ok(Box::new(App::new(cc)))),
    )
}

#[derive(Default)]
struct App {}

impl App {
    fn new(_cc: &eframe::CreationContext<'_>) -> Self {
        // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals.
        // Restore app state using cc.storage (requires the "persistence" feature).
        // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
        // for e.g. egui::PaintCallback.
        Self::default()
    }
}

impl eframe::App for App {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            let y_axis_formatter = |mark: GridMark, _range: &RangeInclusive<f64>| {
                format!("{} - and some very long text", mark.value)
            };

            let y_axes = vec![
                AxisHints::new_y()
                    .label("Y Axis Left")
                    .formatter(y_axis_formatter)
                    .placement(egui_plot::HPlacement::Left),
                AxisHints::new_y()
                    .label("Y Axis Right")
                    .placement(egui_plot::HPlacement::Right),
            ];

            let plot = Plot::new("plot")
                .show_grid(false)
                .custom_y_axes(y_axes)
                .legend(Legend::default());

            let _ = plot.show(ui, |plot_ui| {
                let points = [0.0, 1.0];
                plot_ui.line(Line::new(points.clone()));
            });
        });
    }
}

Before:
before

After:
after

Previously, the axis widgets and the plot memory would calculate
conflicting thickness values for an axis because a reverse iterator
was used to push the axis values.

Later, the axis thickness would be inserted using an index-based
operation into plot memory, which did not take into account the
reverse order.

See: `mem.y_axis_thickness.insert(i, thickness);`

Signed-off-by: Christopher N. Hesse <raymanfx@gmail.com>
@emilk
Copy link
Owner

emilk commented Dec 17, 2024

Good catch!

@emilk emilk added the include in changelog This change will be included in the changelog label Dec 17, 2024
@emilk emilk changed the title Fix axis labels for multiple X or Y axes Fix axis thickness for multiple X or Y axes Dec 17, 2024
@emilk emilk added the bug Something isn't working label Dec 17, 2024
@emilk emilk merged commit 64eb474 into emilk:main Dec 17, 2024
6 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working include in changelog This change will be included in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants