Skip to content

Repository files navigation

fltk-plot

License: MITBuild

An interactive plotting library for Rust, similar to Matlab plotting.

The fltk-plot crate uses the fltk-rs crate and adds simple, interactive 2D plotting widgets.

Usage

Add the following to your project's Cargo.toml file:

[dependencies]
fltk-plot = { git = "https://github.com/GuzTech/fltk-plot", branch = "main" }
fltk = { version = "^1.3", features = ["fltk-bundled"] }

An example hello world application:

use fltk_plot::windows::figure_window::FigureWindow;use fltk::{app::*, draw::*, enums::*, prelude::*};fnlinspace(arr:&mutVec<f64>,l:f64,r:f64,len:usize){let dx = (r - l) / (len - 1)asf64;
arr.reserve(len);for i in0..len {
arr.push(l + (i asf64* dx));}}fnarrayfun(input:&[f64],output:&mutVec<f64>,fun:&dynFn(f64) -> f64){
output.reserve(input.len());for i in input.iter(){
output.push(fun(*i));}}fnmain(){let app = App::default();constN:usize = 100;letmut t:Vec<f64> = Vec::with_capacity(N);letmut x:Vec<f64> = Vec::with_capacity(N);letmut y:Vec<f64> = Vec::with_capacity(N);linspace(&mut t,0.0,2.0* std::f64::consts::PI,N);arrayfun(&t,&mut x,&f64::sin);//arrayfun(&t, &mut x, &|x| 10.0 * f64::sin(x));arrayfun(&t,&mut y,&|x:f64| 0.5 - 0.5* f64::exp(-x));letmut fig = FigureWindow::new(640,480,"Figure 1",1,2);
fig.plot(&t,&x,LineStyle::Solid,2,Color::Red,0);
fig.set_hold(true,0);
fig.plot(&t,&y,LineStyle::Dash,2,Color::Blue,0);
fig.set_hold(false,0);
fig.plot(&x,&y,LineStyle::Solid,2,Color::Red,1);
fig.set_x_label("Time [s]",0);
fig.set_y_label("Voltage [V]",0);
fig.set_caption("Votage vs. time",0);
fig.set_grid(true,0);
fig.set_x_label("Voltage [V]",1);
fig.set_y_label("Voltage [V]",1);
fig.set_caption("Signal 1 vs Signal 2",1);
fig.set_grid(true,1);
fig.end();
fig.show();letmut fig2 = FigureWindow::new(640,480,"Figure 2",1,1);
fig2.stem(&t,&x,LineStyle::Solid,2,Color::Green,0);
fig2.set_grid(true,0);
fig2.set_x_label("X label",0);
fig2.set_y_label("Y label",0);
fig2.set_caption("Caption",0);
fig2.end();
fig2.show();
app.run().unwrap();}

This will display two figure windows which can be interacted with separately:

The mouse interactions are as follows:

  • Panning: click and hold middle mouse button
  • Resetting back to the initial view: click right mouse button
  • Adding a data point: click left mouse button on the plot
  • Moving a data point: click left mouse button and move to desired position
  • Moving data point label: click middle mouse button on the data point marker and drag to desired position
  • Remove a data point: click right mouse button on the data point marker
  • Zoom in/out: mouse wheel
  • Zoom area: click left mouse button and drag to select the desired area

About

An interactive plotting library for Rust

Resources

Stars

18 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages