I am trying to use an array with my own data, but every method i have tried has errored in some way
Here is the Code:
// Destiny Telemetry App
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's an example
use eframe::egui::{self};
use egui_plot::{Plot, Legend};
fn main() -> eframe::Result {
let options = eframe::NativeOptions::default();
eframe::run_native(
"Destiny Telemetry",
options,
Box::new(|_cc| Ok(Box::<MultiPlotApp>::default())),
)
}
struct MultiPlotApp;
impl Default for MultiPlotApp {
fn default() -> Self {
Self
}
}
impl eframe::App for MultiPlotApp {
fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Telemetry from Destiny");
ui.separator();
// Top row: 3 plots
ui.horizontal(|ui| {
for i in 1..=3 {
ui.vertical(|ui| {
ui.label(format!("Plot {}: {}", i, plot_description(i)));
ui.group(|ui| {
Plot::new(format!("plot_{}", i))
.allow_zoom(true)
.allow_drag(true)
.allow_scroll(true)
.legend(Legend::default())
.height(265.0)
.width(405.0)
.show(ui, |plot_ui| {
let line = generate_plot_data(i);
plot_ui.line(line);
});
});
});
}
});
ui.separator();
// Bottom row: 3 plots
ui.horizontal(|ui| {
for i in 4..=6 {
ui.vertical(|ui| {
ui.label(format!("Plot {}: {}", i, plot_description(i)));
ui.group(|ui| {
Plot::new(format!("plot_{}", i))
.allow_zoom(true)
.allow_drag(true)
.allow_scroll(true)
.legend(Legend::default())
.height(265.0)
.width(405.0)
.show(ui, |plot_ui| {
let line = generate_plot_data(i);
plot_ui.line(line);
});
});
});
}
});
});
}
}
/// Generate data dynamically for each plot
fn generate_plot_data(index: usize) -> egui_plot::Line {
let g_1 = vec![];
// Define the function for each plot
let func = match index {
1 => |g_1: f64| g_1.sin(), // Plot 1: sine wave
2 => |g_2: f64| g_2.cos(), // Plot 2: cosine wave
3 => |g_3: f64| g_3.tan(), // Plot 3: tangent wave
4 => |g_4: f64| g_4.sinh(), // Plot 4: hyperbolic sine wave
5 => |g_5: f64| g_5.cosh(), // Plot 5: hyperbolic cosine wave
6 => |g_6: f64| g_6.tanh(), // Plot 6: hyperbolic tangent wave
_ => |_base_g: f64| 0.0, // Default
};
// Generate data points manually
let values: Vec<[f64; 2]> = (0..100)
.map(|i| {
let x = i as f64 * 0.1; // Adjust step size
let y = func(x);
[x, y]
})
.collect();
// Create a Line with the generated values
egui_plot::Line::new(values)
}
/// Returns a description for each plot based on its index
fn plot_description(index: usize) -> &'static str {
match index {
1 => "Sine Wave",
2 => "Cosine Wave",
3 => "Tangent Wave",
4 => "Hyperbolic Sine",
5 => "Hyperbolic Cosine",
6 => "Hyperbolic Tangent",
_ => "Unknown Plot",
}
}
In the generate_plot_data() function i want to change the x.sin and so on to be my own array for x and y coordinates
In the future these arrays will be data recieved over serial and plotted
I have tried using AI to solve the problem but every solution they gave just created more errors
Dexter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.