emilk / egui_plot

2D plotting library in Rust for egui
Apache License 2.0
95 stars 31 forks source link

Help !!! How to set proper bounds on Custom X & Y axis ? #38

Closed Shrav108 closed 2 months ago

Shrav108 commented 3 months ago

Hi, I'm trying to plot 2 Y axes with a common X axis. So far I am able to plot the 2 graphs. I found the answer to plotting 2 Y axes from here : https://github.com/emilk/egui/discussions/4186

My code is as follows:

let sin: PlotPoints = (0..1000).map(|i| {
                    let x = i as f64 * 0.01;
                    [x, x.sin()]
                }).collect();
let line_1 = Line::new(sin); 

let cos: PlotPoints = (0..1000).map(|i| {
                    let x = i as f64 * 0.01;
                    [x, x.cos()]
                }).collect();
let line_2 = Line::new(cos); 

let f_formatter = |mark: GridMark, _range: &RangeInclusive<f64>| {
                    let value = 0.5 * mark.value;
                    if value < 1e-06 {
                        String::new()
                    } else {
                        format!("{:.2}", value)
                    }
                };
let z_formatter = |mark: GridMark, _range: &RangeInclusive<f64>| {
                    let value = 0.5 * mark.value;
                    if value < 1e-06 {
                        String::new()
                    } else {
                        format!("{:.2}", value)
                    }
                };

 // Create the plot with custom axes

let f_y_axis = AxisHints::new_y()
                .label("sin")
                .formatter(f_formatter)
                .placement(egui_plot::HPlacement::Left);

                let z_y_axis = AxisHints::new_y()
                                                 .label("cos")
                                                 .formatter(z_formatter)
                                                 .placement(egui_plot::HPlacement::Right);

// Define x-axis hints
let x_axes = vec![AxisHints::new_x().label("time")];

// Construct custom y-axis hints vector
let y_axes = vec![f_y_axis, z_y_axis];

let plot = Plot::new("custom_axes")
                                     .allow_zoom(false)
                                     .allow_boxed_zoom(false)
                                     .allow_drag(false)
                                     .legend(Legend::default())
                                     .custom_x_axes(x_axes)
                                     .custom_y_axes(y_axes);

plot.show(ui, |plot_ui| {
                        plot_ui.line(line_1.name("sin"));       // HOW DO I SET THE MAX and MIN RANGE ?
                        plot_ui.line(line_2.name("cos"));    // HOW DO I SET THE MAX and MIN RANGE ?
                    });

The cos and sin are just example lines. The main question is as follows :

1 . Line 1 : It has bounds between 0 & 100. How do I set the ranges on Y(left) axis ? 2 . Line 2 : It has bounds between 0 & 1000. How do I set the ranges on Y(right) axis ? 3 . X : It has bounds between 0 & 10 . How do I set the ranges on X axis ?

How properly use the formatter function (with GridMark & RangeInclusive) ?