Skip to content

Commit 8c1bbf4

Browse files
epezentozlb
andauthored
Subplots (#203)
* SubPlots: Y axis padding over multiple plots * Align Plots Group new signature ID, ImPool to store padding data, demo * Align plots orientation (vertically, horizontally) vertical will align Y axis, horizontal will align X axis. *signature changed * ImPlotOrientation used as flag for 2D grids of aligned plots #144 (comment) * AlignPlots updates to merge with v.0.9 * Sync to v0.9 20210127 * subplots proto * make link flags work * stuff * add multi-line centered titles * subplots work * flag ideas * better subplot positioning * resizable subplots * subplot shared items * subplot ratios * some cleanup and refactor * some cleanup and refactor * refactors and demo reorganization * context menus...almost done! * context menus, bug fixes * active id * make implot use ButtonBehavior throughout * bug fixes * more bug fixes * tweaks * fix id issue * finish work on subplots Co-authored-by: ozlb <ozlb@users.noreply.github.com>
1 parent 65aa2c8 commit 8c1bbf4

File tree

6 files changed

+3132
-2102
lines changed

6 files changed

+3132
-2102
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ImPlot is an immediate mode, GPU accelerated plotting library for [Dear ImGui](h
3030
- and more likely to come
3131
- mix/match multiple plot items on a single plot
3232
- configurable axes ranges and scaling (linear/log)
33+
- subplots
3334
- time formatted x-axes (US formatted or ISO 8601)
3435
- reversible and lockable axes
3536
- up to three independent y-axes

implot.cpp

Lines changed: 1317 additions & 744 deletions
Large diffs are not rendered by default.

implot.h

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,19 @@
5252
struct ImPlotContext; // ImPlot context (opaque struct, see implot_internal.h)
5353

5454
// Enums/Flags
55-
typedef int ImPlotFlags; // -> enum ImPlotFlags_
56-
typedef int ImPlotAxisFlags; // -> enum ImPlotAxisFlags_
57-
typedef int ImPlotCol; // -> enum ImPlotCol_
58-
typedef int ImPlotStyleVar; // -> enum ImPlotStyleVar_
59-
typedef int ImPlotMarker; // -> enum ImPlotMarker_
60-
typedef int ImPlotColormap; // -> enum ImPlotColormap_
61-
typedef int ImPlotLocation; // -> enum ImPlotLocation_
62-
typedef int ImPlotOrientation; // -> enum ImPlotOrientation_
63-
typedef int ImPlotYAxis; // -> enum ImPlotYAxis_;
64-
typedef int ImPlotBin; // -> enum ImPlotBin_
65-
66-
// Options for plots.
55+
typedef int ImPlotFlags; // -> enum ImPlotFlags_
56+
typedef int ImPlotAxisFlags; // -> enum ImPlotAxisFlags_
57+
typedef int ImPlotSubplotFlags; // -> enum ImPlotSubplotFlags_
58+
typedef int ImPlotCol; // -> enum ImPlotCol_
59+
typedef int ImPlotStyleVar; // -> enum ImPlotStyleVar_
60+
typedef int ImPlotMarker; // -> enum ImPlotMarker_
61+
typedef int ImPlotColormap; // -> enum ImPlotColormap_
62+
typedef int ImPlotLocation; // -> enum ImPlotLocation_
63+
typedef int ImPlotOrientation; // -> enum ImPlotOrientation_
64+
typedef int ImPlotYAxis; // -> enum ImPlotYAxis_;
65+
typedef int ImPlotBin; // -> enum ImPlotBin_
66+
67+
// Options for plots (see BeginPlot).
6768
enum ImPlotFlags_ {
6869
ImPlotFlags_None = 0, // default
6970
ImPlotFlags_NoTitle = 1 << 0, // the plot title will not be displayed (titles are also hidden if preceeded by double hashes, e.g. "##MyPlot")
@@ -82,7 +83,7 @@ enum ImPlotFlags_ {
8283
ImPlotFlags_CanvasOnly = ImPlotFlags_NoTitle | ImPlotFlags_NoLegend | ImPlotFlags_NoMenus | ImPlotFlags_NoBoxSelect | ImPlotFlags_NoMousePos
8384
};
8485

85-
// Options for plot axes (X and Y).
86+
// Options for plot axes (see BeginPlot).
8687
enum ImPlotAxisFlags_ {
8788
ImPlotAxisFlags_None = 0, // default
8889
ImPlotAxisFlags_NoLabel = 1 << 0, // the axis label will not be displayed (axis labels also hidden if the supplied string name is NULL)
@@ -102,6 +103,22 @@ enum ImPlotAxisFlags_ {
102103
ImPlotAxisFlags_NoDecorations = ImPlotAxisFlags_NoLabel | ImPlotAxisFlags_NoGridLines | ImPlotAxisFlags_NoTickMarks | ImPlotAxisFlags_NoTickLabels
103104
};
104105

106+
// Options for subplots (see BeginSubplot).
107+
enum ImPlotSubplotFlags_ {
108+
ImPlotSubplotFlags_None = 0, // default
109+
ImPlotSubplotFlags_NoTitle = 1 << 0, // the subplot title will not be displayed (titles are also hidden if preceeded by double hashes, e.g. "##MySubplot")
110+
ImPlotSubplotFlags_NoLegend = 1 << 1, // the legend will not be displayed (only applicable if ImPlotSubplotFlags_ShareItems is enabled)
111+
ImPlotSubplotFlags_NoMenus = 1 << 2, // the user will not be able to open context menus with right-click
112+
ImPlotSubplotFlags_NoResize = 1 << 3, // resize splitters between subplot cells will be not be provided
113+
ImPlotSubplotFlags_NoAlign = 1 << 4, // subplot edges will not be aligned vertically or horizontally
114+
ImPlotSubplotFlags_ShareItems = 1 << 5, // items across all subplots will be shared and rendered into a single legend entry
115+
ImPlotSubplotFlags_LinkRows = 1 << 6, // link the y-axis limits of all plots in each row (does not apply auxiliary y-axes)
116+
ImPlotSubplotFlags_LinkCols = 1 << 7, // link the x-axis limits of all plots in each column
117+
ImPlotSubplotFlags_LinkAllX = 1 << 8, // link the x-axis limits in every plot in the subplot
118+
ImPlotSubplotFlags_LinkAllY = 1 << 9 , // link the y-axis limits in every plot in the subplot (does not apply to auxiliary y-axes)
119+
ImPlotSubplotFlags_ColMajor = 1 << 10 // subplots are added in column major order instead of the default row major order
120+
};
121+
105122
// Plot styling colors.
106123
enum ImPlotCol_ {
107124
// item styling colors
@@ -301,7 +318,7 @@ struct ImPlotStyle {
301318
ImVec2 AnnotationPadding; // = 2,2 text padding around annotation labels
302319
ImVec2 FitPadding; // = 0,0 additional fit padding as a percentage of the fit extents (e.g. ImVec2(0.1f,0.1f) adds 10% to the fit extents of X and Y)
303320
ImVec2 PlotDefaultSize; // = 400,300 default size used when ImVec2(0,0) is passed to BeginPlot
304-
ImVec2 PlotMinSize; // = 300,225 minimum size plot frame can be when shrunk
321+
ImVec2 PlotMinSize; // = 200,150 minimum size plot frame can be when shrunk
305322
// style colors
306323
ImVec4 Colors[ImPlotCol_COUNT]; // Array of styling colors. Indexable with ImPlotCol_ enums.
307324
// colormap
@@ -380,6 +397,67 @@ IMPLOT_API bool BeginPlot(const char* title_id,
380397
// of an if statement conditioned on BeginPlot(). See example above.
381398
IMPLOT_API void EndPlot();
382399

400+
//-----------------------------------------------------------------------------
401+
// Begin/EndSubplots
402+
//-----------------------------------------------------------------------------
403+
404+
// Starts a subdivided plotting context. If the function returns true,
405+
// EndSubplots() MUST be called! Call BeginPlot/EndPlot AT MOST [rows*cols]
406+
// times in between the begining and end of the subplot context. Plots are
407+
// added in row major order.
408+
//
409+
// Example:
410+
//
411+
// if (BeginSubplots("My Subplot",2,3,ImVec2(800,400)) {
412+
// for (int i = 0; i < 6; ++i) {
413+
// if (BeginPlot(...)) {
414+
// ImPlot::PlotLine(...);
415+
// ...
416+
// EndPlot();
417+
// }
418+
// }
419+
// EndSubplots();
420+
// }
421+
//
422+
// Procudes:
423+
//
424+
// [0][1][2]
425+
// [3][4][5]
426+
//
427+
// Important notes:
428+
//
429+
// - #title_id must be unique to the current ImGui ID scope. If you need to avoid ID
430+
// collisions or don't want to display a title in the plot, use double hashes
431+
// (e.g. "MyPlot##HiddenIdText" or "##NoTitle").
432+
// - #rows and #cols must be greater than 0.
433+
// - #size is the size of the entire grid of subplots, not the individual plots
434+
// - #row_ratios and #col_ratios must have AT LEAST #rows and #cols elements,
435+
// respectively. These are the sizes of the rows and columns expressed in ratios.
436+
// If the user adjusts the dimensions, the arrays are updated with new ratios.
437+
//
438+
// Important notes regarding BeginPlot from inside of BeginSubplots:
439+
//
440+
// - The #title_id parameter of _BeginPlot_ (see above) does NOT have to be
441+
// unique when called inside of a subplot context. Subplot IDs are hashed
442+
// for your convenience so you don't have call PushID or generate unique title
443+
// strings. Simply pass an empty string to BeginPlot unless you want to title
444+
// each subplot.
445+
// - The #size parameter of _BeginPlot_ (see above) is ignored when inside of a
446+
// subplot context. The actual size of the subplot will be based on the
447+
// #size value you pass to _BeginSubplots_ and #row/#col_ratios if provided.
448+
449+
IMPLOT_API bool BeginSubplots(const char* title_id,
450+
int rows,
451+
int cols,
452+
const ImVec2& size,
453+
ImPlotSubplotFlags flags = ImPlotSubplotFlags_None,
454+
float* row_ratios = NULL,
455+
float* col_ratios = NULL);
456+
457+
// Only call EndSubplots() if BeginSubplots() returns true! Typically called at the end
458+
// of an if statement conditioned on BeginSublots(). See example above.
459+
IMPLOT_API void EndSubplots();
460+
383461
//-----------------------------------------------------------------------------
384462
// Plot Items
385463
//-----------------------------------------------------------------------------
@@ -573,6 +651,19 @@ IMPLOT_API ImPlotLimits GetPlotQuery(ImPlotYAxis y_axis = IMPLOT_AUTO);
573651
// Set the current plot query bounds. Query must be enabled with ImPlotFlags_Query.
574652
IMPLOT_API void SetPlotQuery(const ImPlotLimits& query, ImPlotYAxis y_axis = IMPLOT_AUTO);
575653

654+
//-----------------------------------------------------------------------------
655+
// Algined Plots
656+
//-----------------------------------------------------------------------------
657+
658+
// Consider using Begin/EndSubplots first. They are more feature rich and
659+
// accomplish the same behaviour by default. The functions below offer lower
660+
// level control of plot alignment.
661+
662+
// Align axis padding over multiple plots in a single row or column. If this function returns true, EndAlignedPlots() must be called. #group_id must be unique.
663+
IMPLOT_API bool BeginAlignedPlots(const char* group_id, ImPlotOrientation orientation = ImPlotOrientation_Vertical);
664+
// Only call EndAlignedPlots() if BeginAlignedPlots() returns true!
665+
IMPLOT_API void EndAlignedPlots();
666+
576667
//-----------------------------------------------------------------------------
577668
// Plot Tools
578669
//-----------------------------------------------------------------------------
@@ -604,7 +695,7 @@ IMPLOT_API bool DragPoint(const char* id, double* x, double* y, bool show_label
604695

605696
// The following functions MUST be called BETWEEN Begin/EndPlot!
606697

607-
// Set the location of the current plot's legend (default = North|West).
698+
// Set the location of the current plot's (or subplot's) legend.
608699
IMPLOT_API void SetLegendLocation(ImPlotLocation location, ImPlotOrientation orientation = ImPlotOrientation_Vertical, bool outside = false);
609700
// Set the location of the current plot's mouse position text (default = South|East).
610701
IMPLOT_API void SetMousePosLocation(ImPlotLocation location);

0 commit comments

Comments
 (0)