Expand description
Plotting library for the Bevy game engine. To quickly get started, run a Bevy App
, add the
PlotPlugin
to the App
, instantiate a Plot
struct, and either use the
Plot::plot
(my_data: impl
Plotable
)
method for a regular graph, thePlot::plotm
(my_data: impl
Plotable
)
method for a scatter plot (or plot with markers), or thePlot::plot_func
(my_function: fn(f32, f32) -> 32)
method that supports plotting of explicit functions.
The my_data
argument of either of the first two methods has to implement the Plotable
trait
(e.g. Vec<Vec2>
, Vec<(f32, f32)>
, Vec<f32>
, etc.). In the third option,
my_function
is an explicit function that takes two arguments (x and time) and returns a f32
.
The following code can be found in examples/minimal.rs:
use bevy::prelude::*;
use bevy_plot::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(PlotPlugin)
.add_startup_system(setup)
.run();
}
fn setup(mut commands: Commands, mut plots: ResMut<Assets<Plot>>) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
let mut plot = Plot::default();
let xs = (0..30).map(|i| i as f32 / 30.0).collect::<Vec<f32>>();
let ys = xs
.iter()
.map(|x| Vec2::new(*x, 0.5 * x))
.collect::<Vec<Vec2>>();
plot.plot(ys);
let plot_handle = plots.add(plot.clone());
commands.spawn().insert(plot_handle);
}
For customizing the look of the curves and markers, see the Opt
enum for the
available options together with the Plot::plotopt
and
Plot::plotopt_func
methods. For customizing the canvas (grid, colors, etc…), see the Plot
fields.
Setting the range of the x and y axes is done with the Plot::set_bounds
(lo, up)
method, but bevy_plot
panics if lo.x > up.x or lo.y > up.y
.
Note that the library allows the user to
- zoom in and out with the mousewheel,
- move the origin with the mouse by pressing and dragging,
- spawn a target and the corresponding coordinates by pressing the middle mouse button, and
- change the Plot fields at runtime (see examples/runtime_setter.rs).
Modules
Structs
Component that serves as identification for the nth curve group of the bezier_groups
field
of PlotData
.
Struct containing the data to be plotted and metaparameters of any explicit function plot.
It can be found in the data.bezier_groups
sub-field of a Plot
. The reason for its name is
that bevy_plot interpolates between samples of the function using quadratic bezier curves.
Struct containing the data to be plotted and metaparameters of a marker (or scatter) plot.
It can be found in the data.marker_groups
sub-field of a Plot
.
Contains all relevant information to both the look of the canvas and the data to be plotted.
The data for each type of plot has to be accessed though this struct first. Each element of a Vec
corresponds to a particular curve on the graph.
Main plugin for bevy_plot
Upon modifying any of the plot fields, use this event to update the the view (shaders). For updating a scatter plot (markers) or a regular plot (segments), send the RespawnAllEvent event. Bevy Plot will then despawn all the entities and respawn them with the updated information.
Struct containing the data to be plotted and metaparameters of a segment (or regular) plot.
It can be found in the data.segment_groups
sub-field of a Plot
.
See the animate.rs example, where UpdateBezierShaderEvent
is used to tell bevy_plot that
the view for an explicit function needs to be updated.
Enums
The None
variant can be used to avoid spawning the
segments of a regular plot when calling plotopt(), leaving only the markers.
Type of markers for a given marker plot.
Options for customizing the appearance of the plot.
Traits
Functions
To get a particular color, get the color from the hashmap with a key of the PlotColor enum. Then get the shade of this color from the Vec of colors, the higher the index the darker the shade.