use std::time::Duration;
use eframe::egui::{self, CentralPanel, Visuals};
use egui_plotter::{Chart, MouseConfig};
use plotters::prelude::*;
fn main() {
let native_options = eframe::NativeOptions::default();
eframe::run_native(
"3d Chart Example",
native_options,
Box::new(|cc| Ok(Box::new(Chart3d::new(cc)))),
)
.unwrap();
}
struct Chart3d {
chart: Chart<()>,
}
impl Chart3d {
fn new(cc: &eframe::CreationContext<'_>) -> Self {
let context = &cc.egui_ctx;
context.set_visuals(Visuals::light());
let chart = Chart::new(())
.mouse(MouseConfig::enabled())
.pitch(0.7)
.yaw(0.7)
.builder_cb(Box::new(|area, transform, _d| {
let x_axis = (-3.0..3.0).step(0.1);
let z_axis = (-3.0..3.0).step(0.1);
let mut chart = ChartBuilder::on(area)
.caption(format!("3D Plot Test"), (FontFamily::SansSerif, 20))
.build_cartesian_3d(x_axis, -3.0..3.0, z_axis)
.unwrap();
chart.with_projection(|mut pb| {
pb.yaw = transform.yaw;
pb.pitch = transform.pitch;
pb.scale = 0.7; pb.into_matrix()
});
chart
.configure_axes()
.light_grid_style(BLACK.mix(0.15))
.max_light_lines(3)
.draw()
.unwrap();
chart
.draw_series(
SurfaceSeries::xoz(
(-30..30).map(|f| f as f64 / 10.0),
(-30..30).map(|f| f as f64 / 10.0),
|x, z| (x * x + z * z).cos(),
)
.style(BLUE.mix(0.2).filled()),
)
.unwrap()
.label("Surface")
.legend(|(x, y)| {
Rectangle::new([(x + 5, y - 5), (x + 15, y + 5)], BLUE.mix(0.5).filled())
});
chart
.draw_series(LineSeries::new(
(-100..100)
.map(|y| y as f64 / 40.0)
.map(|y| ((y * 10.0).sin(), y, (y * 10.0).cos())),
&BLACK,
))
.unwrap()
.label("Line")
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], BLACK));
chart
.configure_series_labels()
.border_style(BLACK)
.draw()
.unwrap();
}));
Self { chart }
}
}
impl eframe::App for Chart3d {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
CentralPanel::default().show(ctx, |ui| {
self.chart.draw(ui);
});
std::thread::sleep(Duration::from_millis(10));
ctx.request_repaint();
}
}