1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2025-01-30 10:58:51 -05:00

Merge branch 'master' into tooltips

This commit is contained in:
epezent 2020-12-06 22:46:15 -06:00
commit fc815348c2
4 changed files with 56 additions and 35 deletions

View File

@ -1,5 +1,5 @@
# ImPlot
ImPlot is an immediate mode, GPU accelerated plotting library for [Dear ImGui](https://github.com/ocornut/imgui). It aims to provide a first-class API that ImGui fans will love. ImPlot is well suited for visualizing program data in real-time or creating interactive plots, and requires minimal code to integrate. Just like ImGui, it does not burden the end user with GUI state management, avoids STL containers and C++ headers, and has no external dependencies except for ImGui itself.
ImPlot is an immediate mode, GPU accelerated plotting library for [Dear ImGui](https://github.com/ocornut/imgui). It aims to provide a first-class API that ImGui fans will love. ImPlot is well suited for visualizing program data in real-time or creating interactive plots, and requires minimal code to integrate. Just like ImGui, it does not burden the end user with GUI state management, avoids STL containers and C++ headers, and has no external dependencies except for ImGui itself.
<img src="https://raw.githubusercontent.com/wiki/epezent/implot/screenshots3/controls.gif" width="270"> <img src="https://raw.githubusercontent.com/wiki/epezent/implot/screenshots3/dnd.gif" width="270"> <img src="https://raw.githubusercontent.com/wiki/epezent/implot/screenshots3/pie.gif" width="270">
@ -29,7 +29,7 @@ ImPlot is an immediate mode, GPU accelerated plotting library for [Dear ImGui](h
- and more likely to come
- mix/match multiple plot items on a single plot
- configurable axes ranges and scaling (linear/log)
- time formatted x-axes
- time formatted x-axes (US formatted or ISO 8601)
- reversible and lockable axes
- up to three independent y-axes
- controls for zooming, panning, box selection, and auto-fitting data
@ -38,7 +38,7 @@ ImPlot is an immediate mode, GPU accelerated plotting library for [Dear ImGui](h
- several plot styling options: 10 marker types, adjustable marker sizes, line weights, outline colors, fill colors, etc.
- 10 built-in and user definable colormaps
- optional plot titles, axis labels, and grid labels
- optional legend with toggle buttons to quickly show/hide items
- optional and configurable legends with toggle buttons to quickly show/hide plot items
- default styling based on current ImGui theme, but most elements can be customized independently
- customizable data getters and data striding (just like ImGui:PlotLine)
- accepts data as float, double, and 8, 16, 32, and 64-bit signed/unsigned integral types
@ -46,7 +46,7 @@ ImPlot is an immediate mode, GPU accelerated plotting library for [Dear ImGui](h
## Usage
The API is used just like any other ImGui `BeginX`/`EndX` pair. First, start a new plot with `ImPlot::BeginPlot()`. Next, plot as many items as you want with the provided `PlotX` functions (e.g. `PlotLine()`, `PlotBars()`, `PlotErrorBars()`, etc). Finally, wrap things up with a call to `ImPlot::EndPlot()`. That's it!
The API is used just like any other ImGui `BeginX`/`EndX` pair. First, start a new plot with `ImPlot::BeginPlot()`. Next, plot as many items as you want with the provided `PlotX` functions (e.g. `PlotLine()`, `PlotBars()`, `PlotScatter()`, etc). Finally, wrap things up with a call to `ImPlot::EndPlot()`. That's it!
```cpp
int bar_data[11] = ...;
@ -74,8 +74,8 @@ An online version of the demo is hosted [here](https://traineq.org/implot_demo/s
## Integration
0) Set up an [ImGui](https://github.com/ocornut/imgui) environment if you don't already have one.
1) Add `implot.h`, `implot_internal.h`, `implot.cpp`, `implot_items.cpp` and optionally `implot_demo.cpp` to your sources. Alternatively, you can get ImPlot using [vcpkg](https://github.com/microsoft/vcpkg/tree/master/ports/implot).
0) Set up an [ImGui](https://github.com/ocornut/imgui) environment if you don't already have one.
1) Add `implot.h`, `implot_internal.h`, `implot.cpp`, `implot_items.cpp` and optionally `implot_demo.cpp` to your sources. Alternatively, you can get ImPlot using [vcpkg](https://github.com/microsoft/vcpkg/tree/master/ports/implot).
2) Create and destroy an `ImPlotContext` wherever you do so for your `ImGuiContext`:
```cpp
@ -86,7 +86,7 @@ ImPlot::DestroyContext();
ImGui::DestroyContext();
```
You should be good to go!
You should be good to go!
If you want to test ImPlot quickly, consider trying [mahi-gui](https://github.com/mahilab/mahi-gui), which bundles ImGui, ImPlot, and several other packages for you.
@ -96,6 +96,7 @@ If you want to test ImPlot quickly, consider trying [mahi-gui](https://github.co
1) Handle the `ImGuiBackendFlags_RendererHasVtxOffset` flag in your renderer when using 16-bit indices (the official OpenGL3 renderer supports this) and use an ImGui version with patch [imgui@f6120f8](https://github.com/ocornut/imgui/commit/f6120f8e16eefcdb37b63974e6915a3dd35414be), OR...
2) Enable 32-bit indices by uncommenting `#define ImDrawIdx unsigned int` in your ImGui `imconfig.h` file.
- By default, no anti-aliasing is done on line plots for performance gains. If you use 4x MSAA, then you likely won't even notice. However, you can enable software AA per-plot with the `ImPlotFlags_AntiAliased` flag, or globally with `ImPlot::GetStyle().AntiAliasedLines = true;`.
- Like ImGui, it is recommended that you compile and link ImPlot as a *static* library or directly as a part of your sources. However, if you are compiling ImPlot and ImGui as separate DLLs, make sure you set the current *ImGui* context with `ImPlot::SetImGuiContext(ImGuiContext* ctx)`. This ensures that global ImGui variables are correctly shared across the DLL boundary.
## FAQ
@ -107,13 +108,17 @@ A: ImGui is an incredibly powerful tool for rapid prototyping and development, b
A: If you're looking to generate publication quality plots and/or export plots to a file, ImPlot is NOT the library for you. ImPlot is geared toward plotting application data at realtime speeds. ImPlot does its best to create pretty plots (indeed, there are quite a few styling options available), but it will always favor function over form.
**Q: Where is the documentation?**
A: The API is thoroughly commented in `implot.h`, and the demo in `implot_demo.cpp` should be more than enough to get you started.
**Q: Is ImPlot suitable for plotting large datasets?**
A: Yes, within reason. You can plot tens to hundreds of thousands of points without issue, but don't expect millions to be a buttery smooth experience. However, you can downsample extremely large datasets by telling ImPlot to stride your data at larger intervals if needed.
A: Yes, within reason. You can plot tens to hundreds of thousands of points without issue, but don't expect millions to be a buttery smooth experience. That said, you can always downsample extremely large datasets by telling ImPlot to stride your data at larger intervals if needed.
**Q: Can plot styles be modified?**
A: Yes. Plot colors, palettes, and various styling variables can be pushed/popped or modified permanently on startup. Three default styles are available, as well as an automatic style that attempts to match you ImGui style.
A: Yes. Data colormaps and various styling colors and variables can be pushed/popped or modified permanently on startup. Three default styles are available, as well as an automatic style that attempts to match you ImGui style.
**Q: Does ImPlot support logarithmic scaling or time formatting?**
@ -121,7 +126,7 @@ A: Yep! Both logscale and timescale are supported.
**Q: Does ImPlot support multiple y-axes? x-axes?**
A: Yes. Up to three y-axes can be enabled. Multiple x-axes are not supported.
A: Yes. Up to three y-axes can be enabled. Multiple x-axes are not supported.
**Q: Does ImPlot support [insert plot type]?**
@ -145,9 +150,9 @@ A: Not currently. Use your OS's screen capturing mechanisms if you need to captu
**Q: What data types can I plot?**
A: ImPlot plotting functions accept most scalar types:
`float`, `double`, `int8`, `uint8`, `int16`, `uint16`, `int32`, `uint32`, `int64`, `uint64`.
A: ImPlot plotting functions accept most scalar types:
`float`, `double`, `int8`, `uint8`, `int16`, `uint16`, `int32`, `uint32`, `int64`, `uint64`. Arrays of custom structs or classes (e.g. `Vector2f` or similar) are easily passed to ImPlot functions using the built in striding features (see `implot.h` for documentation).
**Q: Can ImPlot be used with other languages/bindings?**
A: Yes, you can use the generated C binding, [cimplot](https://github.com/cimgui/cimplot) with most high level languages. [DearPyGui](https://github.com/hoffstadt/DearPyGui) provides a Python wrapper, among other things. A Rust binding, [implot-rs](https://github.com/4bb4/implot-rs), is currently in the works. An example using Emscripten can be found [here](https://github.com/pthom/implot_demo).
A: Yes, you can use the generated C binding, [cimplot](https://github.com/cimgui/cimplot) with most high level languages. [DearPyGui](https://github.com/hoffstadt/DearPyGui) provides a Python wrapper, among other things. A Rust binding, [implot-rs](https://github.com/4bb4/implot-rs), is currently in the works. An example using Emscripten can be found [here](https://github.com/pthom/implot_demo).

View File

@ -121,6 +121,7 @@ ImPlotStyle::ImPlotStyle() {
LegendSpacing = ImVec2(0,0);
MousePosPadding = ImVec2(10,10);
AnnotationPadding = ImVec2(2,2);
FitPadding = ImVec2(0,0);
PlotDefaultSize = ImVec2(400,300);
PlotMinSize = ImVec2(300,225);
@ -263,6 +264,7 @@ static const ImPlotStyleVarInfo GPlotStyleVarInfo[] =
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MousePosPadding) }, // ImPlotStyleVar_MousePosPadding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, AnnotationPadding) }, // ImPlotStyleVar_AnnotationPadding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, FitPadding) }, // ImPlotStyleVar_FitPadding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotDefaultSize) }, // ImPlotStyleVar_PlotDefaultSize
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotMinSize) } // ImPlotStyleVar_PlotMinSize
};
@ -1019,7 +1021,7 @@ inline float GetDateTimeWidth(ImPlotDateTimeFmt fmt) {
return ImGui::CalcTextSize(buffer).x;
}
inline void LabelTickTime(ImPlotTick& tick, ImGuiTextBuffer& buffer, const ImPlotTime& t, ImPlotDateTimeFmt fmt) {
void LabelTickTime(ImPlotTick& tick, ImGuiTextBuffer& buffer, const ImPlotTime& t, ImPlotDateTimeFmt fmt) {
char temp[32];
if (tick.ShowLabel) {
tick.TextOffset = buffer.size();
@ -2083,15 +2085,15 @@ void ShowPlotContextMenu(ImPlotPlot& plot) {
plot.LegendOrientation = ImPlotOrientation_Vertical;
ImGui::Checkbox("Outside", &plot.LegendOutside);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(1,1));
if (ImGui::Button("##NW",ImVec2(1.5f*s,s))) plot.LegendLocation = ImPlotLocation_NorthWest; ImGui::SameLine();
if (ImGui::Button("##N", ImVec2(1.5f*s,s))) plot.LegendLocation = ImPlotLocation_North; ImGui::SameLine();
if (ImGui::Button("##NE",ImVec2(1.5f*s,s))) plot.LegendLocation = ImPlotLocation_NorthEast;
if (ImGui::Button("##W", ImVec2(1.5f*s,s))) plot.LegendLocation = ImPlotLocation_West; ImGui::SameLine();
if (ImGui::Button("##C", ImVec2(1.5f*s,s))) plot.LegendLocation = ImPlotLocation_Center; ImGui::SameLine();
if (ImGui::Button("##E", ImVec2(1.5f*s,s))) plot.LegendLocation = ImPlotLocation_East;
if (ImGui::Button("##SW",ImVec2(1.5f*s,s))) plot.LegendLocation = ImPlotLocation_SouthWest; ImGui::SameLine();
if (ImGui::Button("##S", ImVec2(1.5f*s,s))) plot.LegendLocation = ImPlotLocation_South; ImGui::SameLine();
if (ImGui::Button("##SE",ImVec2(1.5f*s,s))) plot.LegendLocation = ImPlotLocation_SouthEast;
if (ImGui::Button("##NW",ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_NorthWest; } ImGui::SameLine();
if (ImGui::Button("##N", ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_North; } ImGui::SameLine();
if (ImGui::Button("##NE",ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_NorthEast; }
if (ImGui::Button("##W", ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_West; } ImGui::SameLine();
if (ImGui::Button("##C", ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_Center; } ImGui::SameLine();
if (ImGui::Button("##E", ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_East; }
if (ImGui::Button("##SW",ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_SouthWest; } ImGui::SameLine();
if (ImGui::Button("##S", ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_South; } ImGui::SameLine();
if (ImGui::Button("##SE",ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_SouthEast; }
ImGui::PopStyleVar();
ImGui::EndMenu();
}
@ -2383,13 +2385,16 @@ void EndPlot() {
const bool axis_equal = ImHasFlag(plot.Flags, ImPlotFlags_Equal);
if (gp.FitThisFrame && (gp.VisibleItemCount > 0 || plot.Queried)) {
if (gp.FitX) {
const double ext_size = gp.ExtentsX.Size() * 0.5;
gp.ExtentsX.Min -= ext_size * gp.Style.FitPadding.x;
gp.ExtentsX.Max += ext_size * gp.Style.FitPadding.x;
if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LockMin) && !ImNanOrInf(gp.ExtentsX.Min))
plot.XAxis.Range.Min = (gp.ExtentsX.Min);
if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LockMax) && !ImNanOrInf(gp.ExtentsX.Max))
plot.XAxis.Range.Max = (gp.ExtentsX.Max);
if (ImAlmostEqual(plot.XAxis.Range.Max, plot.XAxis.Range.Min)) {
plot.XAxis.Range.Max += plot.XAxis.Range.Max * 1.01;
plot.XAxis.Range.Min -= plot.XAxis.Range.Max * 1.01;
plot.XAxis.Range.Max += 0.5;
plot.XAxis.Range.Min -= 0.5;
}
plot.XAxis.Constrain();
if (axis_equal && !gp.FitY[0])
@ -2397,13 +2402,16 @@ void EndPlot() {
}
for (int i = 0; i < IMPLOT_Y_AXES; i++) {
if (gp.FitY[i]) {
const double ext_size = gp.ExtentsY[i].Size() * 0.5;
gp.ExtentsY[i].Min -= ext_size * gp.Style.FitPadding.y;
gp.ExtentsY[i].Max += ext_size * gp.Style.FitPadding.y;
if (!ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LockMin) && !ImNanOrInf(gp.ExtentsY[i].Min))
plot.YAxis[i].Range.Min = (gp.ExtentsY[i].Min);
if (!ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LockMax) && !ImNanOrInf(gp.ExtentsY[i].Max))
plot.YAxis[i].Range.Max = (gp.ExtentsY[i].Max);
if (ImAlmostEqual(plot.YAxis[i].Range.Max, plot.YAxis[i].Range.Min)) {
plot.YAxis[i].Range.Max += plot.YAxis[i].Range.Max * 1.01;
plot.YAxis[i].Range.Min -= plot.YAxis[i].Range.Max * 1.01;
plot.YAxis[i].Range.Max += 0.5;
plot.YAxis[i].Range.Min -= 0.5;
}
plot.YAxis[i].Constrain();
if (i == 0 && axis_equal && !gp.FitX)
@ -3566,6 +3574,8 @@ void ShowStyleEditor(ImPlotStyle* ref) {
ImGui::SliderFloat2("LegendSpacing", (float*)&style.LegendSpacing, 0.0f, 5.0f, "%.0f");
ImGui::SliderFloat2("MousePosPadding", (float*)&style.MousePosPadding, 0.0f, 20.0f, "%.0f");
ImGui::SliderFloat2("AnnotationPadding", (float*)&style.AnnotationPadding, 0.0f, 5.0f, "%.0f");
ImGui::SliderFloat2("FitPadding", (float*)&style.FitPadding, 0, 0.2f, "%.2f");
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Colors")) {

View File

@ -155,6 +155,7 @@ enum ImPlotStyleVar_ {
ImPlotStyleVar_LegendSpacing, // ImVec2, spacing between legend entries
ImPlotStyleVar_MousePosPadding, // ImVec2, padding between plot edge and interior info text
ImPlotStyleVar_AnnotationPadding, // ImVec2, text padding around annotation labels
ImPlotStyleVar_FitPadding, // ImVec2, 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)
ImPlotStyleVar_PlotDefaultSize, // ImVec2, default size used when ImVec2(0,0) is passed to BeginPlot
ImPlotStyleVar_PlotMinSize, // ImVec2, minimum size plot frame can be when shrunk
ImPlotStyleVar_COUNT
@ -276,6 +277,7 @@ struct ImPlotStyle {
ImVec2 LegendSpacing; // = 0,0 spacing between legend entries
ImVec2 MousePosPadding; // = 10,10 padding between plot edge and interior mouse location text
ImVec2 AnnotationPadding; // = 2,2 text padding around annotation labels
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)
ImVec2 PlotDefaultSize; // = 400,300 default size used when ImVec2(0,0) is passed to BeginPlot
ImVec2 PlotMinSize; // = 300,225 minimum size plot frame can be when shrunk
// colors
@ -383,7 +385,7 @@ IMPLOT_API void EndPlot();
// MyData my_data;
// ImPlot::PlotScatterG("scatter", MyDataGetter, &my_data, my_data.Size());
//
// NB: All types are converted to double before plotting. You may loose information
// NB: All types are converted to double before plotting. You may lose information
// if you try plotting extremely large 64-bit integral types. Proceed with caution!
// Plots a standard 2D line plot.
@ -695,7 +697,7 @@ IMPLOT_API void SetImGuiContext(ImGuiContext* ctx);
// Demo (add implot_demo.cpp to your sources!)
//-----------------------------------------------------------------------------
// Shows the ImPlot demo. Pass the current ImGui context if ImPlot is a DLL.
// Shows the ImPlot demo.
IMPLOT_API void ShowDemoWindow(bool* p_open = NULL);
} // namespace ImPlot

View File

@ -135,12 +135,16 @@ struct ImBufferWriter
}
void Write(const char* fmt, ...) {
va_list argp;
va_start(argp, fmt);
const int written = ::vsnprintf(&Buffer[Pos], Size - Pos - 1, fmt, argp);
va_list args;
va_start(args, fmt);
WriteV(fmt, args);
va_end(args);
}
void WriteV(const char* fmt, va_list args) {
const int written = ::vsnprintf(&Buffer[Pos], Size - Pos - 1, fmt, args);
if (written > 0)
Pos += ImMin(written, Size-Pos-1);
va_end(argp);
}
};
@ -749,7 +753,7 @@ IMPLOT_API void BustItemCache();
// Gets the current y-axis for the current plot
inline int GetCurrentYAxis() { return GImPlot->CurrentPlot->CurrentYAxis; }
// Updates axis ticks, lins, and label colors
IMPLOT_API void UpdateAxisColors(int axis_flag, ImPlotAxisColor* col);
IMPLOT_API void UpdateAxisColors(int axis_flag, ImPlotAxis* axis);
// Updates plot-to-pixel space transformation variables for the current plot.
IMPLOT_API void UpdateTransformCache();
@ -771,7 +775,7 @@ IMPLOT_API void PushLinkedAxis(ImPlotAxis& axis);
IMPLOT_API void PullLinkedAxis(ImPlotAxis& axis);
// Shows an axis's context menu.
IMPLOT_API void ShowAxisContextMenu(ImPlotAxisState& state, bool time_allowed = false);
IMPLOT_API void ShowAxisContextMenu(ImPlotAxis& axis, ImPlotAxis* equal_axis, bool time_allowed = false);
//-----------------------------------------------------------------------------
// [SECTION] Legend Utils