Skip to main content

dear_implot/
lib.rs

1//! # Dear ImPlot - Rust Bindings with Dear ImGui Compatibility
2//!
3//! High-level Rust bindings for ImPlot, the immediate mode plotting library.
4//! This crate provides safe, idiomatic Rust bindings designed to work seamlessly
5//! with dear-imgui (C++ bindgen) rather than imgui-rs (cimgui).
6//!
7//! ## Features
8//!
9//! - Safe, idiomatic Rust API
10//! - Full compatibility with dear-imgui
11//! - Builder pattern for plots and plot elements
12//! - Memory-safe string handling
13//! - Support for all major plot types
14//!
15//! ## Quick Start
16//!
17//! ```no_run
18//! use dear_imgui_rs::*;
19//! use dear_implot::*;
20//!
21//! let mut ctx = Context::create();
22//! let mut plot_ctx = PlotContext::create(&ctx);
23//!
24//! let ui = ctx.frame();
25//! let plot_ui = plot_ctx.get_plot_ui(&ui);
26//!
27//! if let Some(token) = plot_ui.begin_plot("My Plot") {
28//!     plot_ui.plot_line("Line", &[1.0, 2.0, 3.0, 4.0], &[1.0, 4.0, 2.0, 3.0]);
29//!     token.end();
30//! }
31//! ```
32//!
33//! ## Integration with Dear ImGui
34//!
35//! This crate is designed to work with the `dear-imgui-rs` ecosystem:
36//! - Uses the same context management patterns
37//! - Compatible with dear-imgui's UI tokens and lifetime management
38//! - Shares the same underlying Dear ImGui context
39
40use dear_implot_sys as sys;
41
42// Bindgen output for `dear-implot-sys` can fluctuate between historical
43// out-parameter signatures and the newer return-by-value signatures depending
44// on which generated `OUT_DIR` file rust-analyzer happens to index.
45//
46// Keep the wrapper crate stable by calling a local extern declaration for the
47// specific APIs we expose.
48#[allow(non_snake_case)]
49pub(crate) mod compat_ffi {
50    use super::sys;
51    use std::os::raw::c_char;
52
53    unsafe extern "C" {
54        pub fn ImPlot_GetPlotPos() -> sys::ImVec2;
55        pub fn ImPlot_GetPlotSize() -> sys::ImVec2;
56    }
57
58    // Some targets (notably import-style wasm) cannot call C variadic (`...`) functions.
59    // Declare the `*_Str0` convenience wrappers here to keep the safe layer independent
60    // of bindgen fluctuations / pregenerated bindings.
61    //
62    // On wasm32, these must be provided by the `imgui-sys-v0` provider module.
63    #[cfg(target_arch = "wasm32")]
64    #[link(wasm_import_module = "imgui-sys-v0")]
65    unsafe extern "C" {
66        pub fn ImPlot_Annotation_Str0(
67            x: f64,
68            y: f64,
69            col: sys::ImVec4_c,
70            pix_offset: sys::ImVec2_c,
71            clamp: bool,
72            fmt: *const c_char,
73        );
74        pub fn ImPlot_TagX_Str0(x: f64, col: sys::ImVec4_c, fmt: *const c_char);
75        pub fn ImPlot_TagY_Str0(y: f64, col: sys::ImVec4_c, fmt: *const c_char);
76    }
77
78    #[cfg(not(target_arch = "wasm32"))]
79    unsafe extern "C" {
80        pub fn ImPlot_Annotation_Str0(
81            x: f64,
82            y: f64,
83            col: sys::ImVec4_c,
84            pix_offset: sys::ImVec2_c,
85            clamp: bool,
86            fmt: *const c_char,
87        );
88        pub fn ImPlot_TagX_Str0(x: f64, col: sys::ImVec4_c, fmt: *const c_char);
89        pub fn ImPlot_TagY_Str0(y: f64, col: sys::ImVec4_c, fmt: *const c_char);
90    }
91}
92
93// Re-export essential types
94pub use dear_imgui_rs::{Context, Ui};
95pub use sys::{ImPlotPoint, ImPlotRange, ImPlotRect};
96pub use sys::{ImTextureID, ImVec2, ImVec4};
97
98mod advanced;
99mod axis_types;
100mod colormap;
101mod colors;
102mod context;
103mod flags;
104mod histogram_bins;
105mod markers;
106mod plot_types;
107mod style;
108mod ui_ext;
109mod utils;
110
111// New modular plot types
112pub mod plots;
113
114pub use axis_types::{Axis, XAxis, YAxis, YAxisChoice};
115pub(crate) use axis_types::{IMPLOT_AUTO, y_axis_choice_option_to_i32};
116pub use colormap::Colormap;
117pub use colors::PlotColorElement;
118pub use context::*;
119pub use flags::*;
120pub use histogram_bins::{BinMethod, HistogramBins};
121pub use markers::Marker;
122pub use plot_types::{PlotCond, PlotLocation, PlotOrientation};
123pub use style::*;
124pub use ui_ext::ImPlotExt;
125pub use utils::*;
126
127// Re-export new modular plot types for convenience
128pub use plots::{
129    Plot, PlotData, PlotDataLayout, PlotDataOffset, PlotDataStride, PlotError,
130    bar::{BarPlot, PositionalBarPlot},
131    error_bars::{AsymmetricErrorBarsPlot, ErrorBarsPlot, SimpleErrorBarsPlot},
132    heatmap::{HeatmapPlot, HeatmapPlotF32},
133    histogram::{Histogram2DPlot, HistogramPlot},
134    line::{LinePlot, SimpleLinePlot},
135    pie::{PieChartPlot, PieChartPlotF32},
136    polygon::PolygonPlot,
137    scatter::{ScatterPlot, SimpleScatterPlot},
138    shaded::{ShadedBetweenPlot, ShadedPlot, SimpleShadedPlot},
139    stems::{SimpleStemPlot, StemPlot},
140};
141
142// Re-export all plot types for convenience
143pub use plots::*;
144
145// Re-export advanced features (explicit to avoid AxisFlags name clash)
146pub use advanced::{
147    LegendFlags, LegendLocation, LegendManager, LegendToken, MultiAxisPlot, MultiAxisToken,
148    SubplotFlags, SubplotGrid, SubplotToken, YAxisConfig,
149};