s464967_przetwarzanie_obraz.../backends/implot.cpp
2023-07-06 14:10:47 +02:00

5724 lines
259 KiB
C++

// MIT License
// Copyright (c) 2022 Evan Pezent
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ImPlot v0.14
/*
API BREAKING CHANGES
====================
Occasionally introducing changes that are breaking the API. We try to make the breakage minor and easy to fix.
Below is a change-log of API breaking changes only. If you are using one of the functions listed, expect to have to fix some code.
When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all implot files.
You can read releases logs https://github.com/epezent/implot/releases for more details.
- 2022/06/19 (0.14) - The signature of ColormapScale has changed to accommodate a new ImPlotColormapScaleFlags parameter
- 2022/06/17 (0.14) - **IMPORTANT** All PlotX functions now take an ImPlotX_Flags `flags` parameter. Where applicable, it is located before the existing `offset` and `stride` parameters.
If you were providing offset and stride values, you will need to update your function call to include a `flags` value. If you fail to do this, you will likely see
unexpected results or crashes without a compiler warning since these three are all default args. We apologize for the inconvenience, but this was a necessary evil.
- PlotBarsH has been removed; use PlotBars + ImPlotBarsFlags_Horizontal instead
- PlotErrorBarsH has been removed; use PlotErrorBars + ImPlotErrorBarsFlags_Horizontal
- PlotHistogram/PlotHistogram2D signatures changed; `cumulative`, `density`, and `outliers` options now specified via ImPlotHistogramFlags
- PlotPieChart signature changed; `normalize` option now specified via ImPlotPieChartFlags
- PlotText signature changes; `vertical` option now specified via `ImPlotTextFlags_Vertical`
- `PlotVLines` and `PlotHLines` replaced with `PlotInfLines` (+ ImPlotInfLinesFlags_Horizontal )
- arguments of ImPlotGetter have been reversed to be consistent with other API callbacks
- SetupAxisScale + ImPlotScale have replaced ImPlotAxisFlags_LogScale and ImPlotAxisFlags_Time flags
- ImPlotFormatters should now return an int indicating the size written
- the signature of ImPlotGetter has been reversed so that void* user_data is the last argument and consistent with other callbacks
- 2021/10/19 (0.13) - MAJOR API OVERHAUL! See #168 and #272
- TRIVIAL RENAME:
- ImPlotLimits -> ImPlotRect
- ImPlotYAxis_ -> ImAxis_
- SetPlotYAxis -> SetAxis
- BeginDragDropTarget -> BeginDragDropTargetPlot
- BeginDragDropSource -> BeginDragDropSourcePlot
- ImPlotFlags_NoMousePos -> ImPlotFlags_NoMouseText
- SetNextPlotLimits -> SetNextAxesLimits
- SetMouseTextLocation -> SetupMouseText
- SIGNATURE MODIFIED:
- PixelsToPlot/PlotToPixels -> added optional X-Axis arg
- GetPlotMousePos -> added optional X-Axis arg
- GetPlotLimits -> added optional X-Axis arg
- GetPlotSelection -> added optional X-Axis arg
- DragLineX/Y/DragPoint -> now takes int id; removed labels (render with Annotation/Tag instead)
- REPLACED:
- IsPlotXAxisHovered/IsPlotXYAxisHovered -> IsAxisHovered(ImAxis)
- BeginDragDropTargetX/BeginDragDropTargetY -> BeginDragDropTargetAxis(ImAxis)
- BeginDragDropSourceX/BeginDragDropSourceY -> BeginDragDropSourceAxis(ImAxis)
- ImPlotCol_XAxis, ImPlotCol_YAxis1, etc. -> ImPlotCol_AxisText (push/pop this around SetupAxis to style individual axes)
- ImPlotCol_XAxisGrid, ImPlotCol_Y1AxisGrid -> ImPlotCol_AxisGrid (push/pop this around SetupAxis to style individual axes)
- SetNextPlotLimitsX/Y -> SetNextAxisLimits(ImAxis)
- LinkNextPlotLimits -> SetNextAxisLinks(ImAxis)
- FitNextPlotAxes -> SetNextAxisToFit(ImAxis)/SetNextAxesToFit
- SetLegendLocation -> SetupLegend
- ImPlotFlags_NoHighlight -> ImPlotLegendFlags_NoHighlight
- ImPlotOrientation -> ImPlotLegendFlags_Horizontal
- Annotate -> Annotation
- REMOVED:
- GetPlotQuery, SetPlotQuery, IsPlotQueried -> use DragRect
- SetNextPlotTicksX, SetNextPlotTicksY -> use SetupAxisTicks
- SetNextPlotFormatX, SetNextPlotFormatY -> use SetupAxisFormat
- AnnotateClamped -> use Annotation(bool clamp = true)
- OBSOLETED:
- BeginPlot (original signature) -> use simplified signature + Setup API
- 2021/07/30 (0.12) - The offset argument of `PlotXG` functions was been removed. Implement offsetting in your getter callback instead.
- 2021/03/08 (0.9) - SetColormap and PushColormap(ImVec4*) were removed. Use AddColormap for custom colormap support. LerpColormap was changed to SampleColormap.
ShowColormapScale was changed to ColormapScale and requires additional arguments.
- 2021/03/07 (0.9) - The signature of ShowColormapScale was modified to accept a ImVec2 size.
- 2021/02/28 (0.9) - BeginLegendDragDropSource was changed to BeginDragDropSourceItem with a number of other drag and drop improvements.
- 2021/01/18 (0.9) - The default behavior for opening context menus was change from double right-click to single right-click. ImPlotInputMap and related functions were moved
to implot_internal.h due to its immaturity.
- 2020/10/16 (0.8) - ImPlotStyleVar_InfoPadding was changed to ImPlotStyleVar_MousePosPadding
- 2020/09/10 (0.8) - The single array versions of PlotLine, PlotScatter, PlotStems, and PlotShaded were given additional arguments for x-scale and x0.
- 2020/09/07 (0.8) - Plotting functions which accept a custom getter function pointer have been post-fixed with a G (e.g. PlotLineG)
- 2020/09/06 (0.7) - Several flags under ImPlotFlags and ImPlotAxisFlags were inverted (e.g. ImPlotFlags_Legend -> ImPlotFlags_NoLegend) so that the default flagset
is simply 0. This more closely matches ImGui's style and makes it easier to enable non-default but commonly used flags (e.g. ImPlotAxisFlags_Time).
- 2020/08/28 (0.5) - ImPlotMarker_ can no longer be combined with bitwise OR, |. This features caused unecessary slow-down, and almost no one used it.
- 2020/08/25 (0.5) - ImPlotAxisFlags_Scientific was removed. Logarithmic axes automatically uses scientific notation.
- 2020/08/17 (0.5) - PlotText was changed so that text is centered horizontally and vertically about the desired point.
- 2020/08/16 (0.5) - An ImPlotContext must be explicitly created and destroyed now with `CreateContext` and `DestroyContext`. Previously, the context was statically initialized in this source file.
- 2020/06/13 (0.4) - The flags `ImPlotAxisFlag_Adaptive` and `ImPlotFlags_Cull` were removed. Both are now done internally by default.
- 2020/06/03 (0.3) - The signature and behavior of PlotPieChart was changed so that data with sum less than 1 can optionally be normalized. The label format can now be specified as well.
- 2020/06/01 (0.3) - SetPalette was changed to `SetColormap` for consistency with other plotting libraries. `RestorePalette` was removed. Use `SetColormap(ImPlotColormap_Default)`.
- 2020/05/31 (0.3) - Plot functions taking custom ImVec2* getters were removed. Use the ImPlotPoint* getter versions instead.
- 2020/05/29 (0.3) - The signature of ImPlotLimits::Contains was changed to take two doubles instead of ImVec2
- 2020/05/16 (0.2) - All plotting functions were reverted to being prefixed with "Plot" to maintain a consistent VerbNoun style. `Plot` was split into `PlotLine`
and `PlotScatter` (however, `PlotLine` can still be used to plot scatter points as `Plot` did before.). `Bar` is not `PlotBars`, to indicate
that multiple bars will be plotted.
- 2020/05/13 (0.2) - `ImMarker` was change to `ImPlotMarker` and `ImAxisFlags` was changed to `ImPlotAxisFlags`.
- 2020/05/11 (0.2) - `ImPlotFlags_Selection` was changed to `ImPlotFlags_BoxSelect`
- 2020/05/11 (0.2) - The namespace ImGui:: was replaced with ImPlot::. As a result, the following additional changes were made:
- Functions that were prefixed or decorated with the word "Plot" have been truncated. E.g., `ImGui::PlotBars` is now just `ImPlot::Bar`.
It should be fairly obvious what was what.
- Some functions have been given names that would have otherwise collided with the ImGui namespace. This has been done to maintain a consistent
style with ImGui. E.g., 'ImGui::PushPlotStyleVar` is now 'ImPlot::PushStyleVar'.
- 2020/05/10 (0.2) - The following function/struct names were changes:
- ImPlotRange -> ImPlotLimits
- GetPlotRange() -> GetPlotLimits()
- SetNextPlotRange -> SetNextPlotLimits
- SetNextPlotRangeX -> SetNextPlotLimitsX
- SetNextPlotRangeY -> SetNextPlotLimitsY
- 2020/05/10 (0.2) - Plot queries are pixel based by default. Query rects that maintain relative plot position have been removed. This was done to support multi-y-axis.
*/
#include "implot.h"
#include "implot_internal.h"
#include <stdlib.h>
// Support for pre-1.82 versions. Users on 1.82+ can use 0 (default) flags to mean "all corners" but in order to support older versions we are more explicit.
#if (IMGUI_VERSION_NUM < 18102) && !defined(ImDrawFlags_RoundCornersAll)
#define ImDrawFlags_RoundCornersAll ImDrawCornerFlags_All
#endif
// Visual Studio warnings
#ifdef _MSC_VER
#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
#endif
// Clang/GCC warnings with -Weverything
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wformat-nonliteral" // warning: format string is not a string literal
#elif defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked
#endif
// Global plot context
#ifndef GImPlot
ImPlotContext* GImPlot = NULL;
#endif
//-----------------------------------------------------------------------------
// Struct Implementations
//-----------------------------------------------------------------------------
ImPlotInputMap::ImPlotInputMap() {
ImPlot::MapInputDefault(this);
}
ImPlotStyle::ImPlotStyle() {
LineWeight = 1;
Marker = ImPlotMarker_None;
MarkerSize = 4;
MarkerWeight = 1;
FillAlpha = 1;
ErrorBarSize = 5;
ErrorBarWeight = 1.5f;
DigitalBitHeight = 8;
DigitalBitGap = 4;
PlotBorderSize = 1;
MinorAlpha = 0.25f;
MajorTickLen = ImVec2(10,10);
MinorTickLen = ImVec2(5,5);
MajorTickSize = ImVec2(1,1);
MinorTickSize = ImVec2(1,1);
MajorGridSize = ImVec2(1,1);
MinorGridSize = ImVec2(1,1);
PlotPadding = ImVec2(10,10);
LabelPadding = ImVec2(5,5);
LegendPadding = ImVec2(10,10);
LegendInnerPadding = ImVec2(5,5);
LegendSpacing = ImVec2(5,0);
MousePosPadding = ImVec2(10,10);
AnnotationPadding = ImVec2(2,2);
FitPadding = ImVec2(0,0);
PlotDefaultSize = ImVec2(400,300);
PlotMinSize = ImVec2(200,150);
ImPlot::StyleColorsAuto(this);
Colormap = ImPlotColormap_Deep;
UseLocalTime = false;
Use24HourClock = false;
UseISO8601 = false;
}
//-----------------------------------------------------------------------------
// Style
//-----------------------------------------------------------------------------
namespace ImPlot {
const char* GetStyleColorName(ImPlotCol col) {
static const char* col_names[ImPlotCol_COUNT] = {
"Line",
"Fill",
"MarkerOutline",
"MarkerFill",
"ErrorBar",
"FrameBg",
"PlotBg",
"PlotBorder",
"LegendBg",
"LegendBorder",
"LegendText",
"TitleText",
"InlayText",
"AxisText",
"AxisGrid",
"AxisTick",
"AxisBg",
"AxisBgHovered",
"AxisBgActive",
"Selection",
"Crosshairs"
};
return col_names[col];
}
const char* GetMarkerName(ImPlotMarker marker) {
switch (marker) {
case ImPlotMarker_None: return "None";
case ImPlotMarker_Circle: return "Circle";
case ImPlotMarker_Square: return "Square";
case ImPlotMarker_Diamond: return "Diamond";
case ImPlotMarker_Up: return "Up";
case ImPlotMarker_Down: return "Down";
case ImPlotMarker_Left: return "Left";
case ImPlotMarker_Right: return "Right";
case ImPlotMarker_Cross: return "Cross";
case ImPlotMarker_Plus: return "Plus";
case ImPlotMarker_Asterisk: return "Asterisk";
default: return "";
}
}
ImVec4 GetAutoColor(ImPlotCol idx) {
ImVec4 col(0,0,0,1);
switch(idx) {
case ImPlotCol_Line: return col; // these are plot dependent!
case ImPlotCol_Fill: return col; // these are plot dependent!
case ImPlotCol_MarkerOutline: return col; // these are plot dependent!
case ImPlotCol_MarkerFill: return col; // these are plot dependent!
case ImPlotCol_ErrorBar: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
case ImPlotCol_FrameBg: return ImGui::GetStyleColorVec4(ImGuiCol_FrameBg);
case ImPlotCol_PlotBg: return ImGui::GetStyleColorVec4(ImGuiCol_WindowBg);
case ImPlotCol_PlotBorder: return ImGui::GetStyleColorVec4(ImGuiCol_Border);
case ImPlotCol_LegendBg: return ImGui::GetStyleColorVec4(ImGuiCol_PopupBg);
case ImPlotCol_LegendBorder: return GetStyleColorVec4(ImPlotCol_PlotBorder);
case ImPlotCol_LegendText: return GetStyleColorVec4(ImPlotCol_InlayText);
case ImPlotCol_TitleText: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
case ImPlotCol_InlayText: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
case ImPlotCol_AxisText: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
case ImPlotCol_AxisGrid: return GetStyleColorVec4(ImPlotCol_AxisText) * ImVec4(1,1,1,0.25f);
case ImPlotCol_AxisTick: return GetStyleColorVec4(ImPlotCol_AxisGrid);
case ImPlotCol_AxisBg: return ImVec4(0,0,0,0);
case ImPlotCol_AxisBgHovered: return ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered);
case ImPlotCol_AxisBgActive: return ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive);
case ImPlotCol_Selection: return ImVec4(1,1,0,1);
case ImPlotCol_Crosshairs: return GetStyleColorVec4(ImPlotCol_PlotBorder);
default: return col;
}
}
struct ImPlotStyleVarInfo {
ImGuiDataType Type;
ImU32 Count;
ImU32 Offset;
void* GetVarPtr(ImPlotStyle* style) const { return (void*)((unsigned char*)style + Offset); }
};
static const ImPlotStyleVarInfo GPlotStyleVarInfo[] =
{
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, LineWeight) }, // ImPlotStyleVar_LineWeight
{ ImGuiDataType_S32, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, Marker) }, // ImPlotStyleVar_Marker
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, MarkerSize) }, // ImPlotStyleVar_MarkerSize
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, MarkerWeight) }, // ImPlotStyleVar_MarkerWeight
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, FillAlpha) }, // ImPlotStyleVar_FillAlpha
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, ErrorBarSize) }, // ImPlotStyleVar_ErrorBarSize
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, ErrorBarWeight) }, // ImPlotStyleVar_ErrorBarWeight
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitHeight) }, // ImPlotStyleVar_DigitalBitHeight
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitGap) }, // ImPlotStyleVar_DigitalBitGap
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotBorderSize) }, // ImPlotStyleVar_PlotBorderSize
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, MinorAlpha) }, // ImPlotStyleVar_MinorAlpha
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MajorTickLen) }, // ImPlotStyleVar_MajorTickLen
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MinorTickLen) }, // ImPlotStyleVar_MinorTickLen
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MajorTickSize) }, // ImPlotStyleVar_MajorTickSize
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MinorTickSize) }, // ImPlotStyleVar_MinorTickSize
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MajorGridSize) }, // ImPlotStyleVar_MajorGridSize
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MinorGridSize) }, // ImPlotStyleVar_MinorGridSize
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotPadding) }, // ImPlotStyleVar_PlotPadding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, LabelPadding) }, // ImPlotStyleVar_LabelPaddine
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, LegendPadding) }, // ImPlotStyleVar_LegendPadding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, LegendInnerPadding) }, // ImPlotStyleVar_LegendInnerPadding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, LegendSpacing) }, // ImPlotStyleVar_LegendSpacing
{ 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
};
static const ImPlotStyleVarInfo* GetPlotStyleVarInfo(ImPlotStyleVar idx) {
IM_ASSERT(idx >= 0 && idx < ImPlotStyleVar_COUNT);
IM_ASSERT(IM_ARRAYSIZE(GPlotStyleVarInfo) == ImPlotStyleVar_COUNT);
return &GPlotStyleVarInfo[idx];
}
//-----------------------------------------------------------------------------
// Generic Helpers
//-----------------------------------------------------------------------------
void AddTextVertical(ImDrawList *DrawList, ImVec2 pos, ImU32 col, const char *text_begin, const char* text_end) {
// the code below is based loosely on ImFont::RenderText
if (!text_end)
text_end = text_begin + strlen(text_begin);
ImGuiContext& g = *GImGui;
ImFont* font = g.Font;
// Align to be pixel perfect
pos.x = IM_FLOOR(pos.x);
pos.y = IM_FLOOR(pos.y);
const float scale = g.FontSize / font->FontSize;
const char* s = text_begin;
int chars_exp = (int)(text_end - s);
int chars_rnd = 0;
const int vtx_count_max = chars_exp * 4;
const int idx_count_max = chars_exp * 6;
DrawList->PrimReserve(idx_count_max, vtx_count_max);
while (s < text_end) {
unsigned int c = (unsigned int)*s;
if (c < 0x80) {
s += 1;
}
else {
s += ImTextCharFromUtf8(&c, s, text_end);
if (c == 0) // Malformed UTF-8?
break;
}
const ImFontGlyph * glyph = font->FindGlyph((ImWchar)c);
if (glyph == NULL) {
continue;
}
DrawList->PrimQuadUV(pos + ImVec2(glyph->Y0, -glyph->X0) * scale, pos + ImVec2(glyph->Y0, -glyph->X1) * scale,
pos + ImVec2(glyph->Y1, -glyph->X1) * scale, pos + ImVec2(glyph->Y1, -glyph->X0) * scale,
ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V0),
ImVec2(glyph->U1, glyph->V1), ImVec2(glyph->U0, glyph->V1),
col);
pos.y -= glyph->AdvanceX * scale;
chars_rnd++;
}
// Give back unused vertices
int chars_skp = chars_exp-chars_rnd;
DrawList->PrimUnreserve(chars_skp*6, chars_skp*4);
}
void AddTextCentered(ImDrawList* DrawList, ImVec2 top_center, ImU32 col, const char* text_begin, const char* text_end) {
float txt_ht = ImGui::GetTextLineHeight();
const char* title_end = ImGui::FindRenderedTextEnd(text_begin, text_end);
ImVec2 text_size;
float y = 0;
while (const char* tmp = (const char*)memchr(text_begin, '\n', title_end-text_begin)) {
text_size = ImGui::CalcTextSize(text_begin,tmp,true);
DrawList->AddText(ImVec2(top_center.x - text_size.x * 0.5f, top_center.y+y),col,text_begin,tmp);
text_begin = tmp + 1;
y += txt_ht;
}
text_size = ImGui::CalcTextSize(text_begin,title_end,true);
DrawList->AddText(ImVec2(top_center.x - text_size.x * 0.5f, top_center.y+y),col,text_begin,title_end);
}
double NiceNum(double x, bool round) {
double f;
double nf;
int expv = (int)floor(ImLog10(x));
f = x / ImPow(10.0, (double)expv);
if (round)
if (f < 1.5)
nf = 1;
else if (f < 3)
nf = 2;
else if (f < 7)
nf = 5;
else
nf = 10;
else if (f <= 1)
nf = 1;
else if (f <= 2)
nf = 2;
else if (f <= 5)
nf = 5;
else
nf = 10;
return nf * ImPow(10.0, expv);
}
//-----------------------------------------------------------------------------
// Context Utils
//-----------------------------------------------------------------------------
void SetImGuiContext(ImGuiContext* ctx) {
ImGui::SetCurrentContext(ctx);
}
ImPlotContext* CreateContext() {
ImPlotContext* ctx = IM_NEW(ImPlotContext)();
Initialize(ctx);
if (GImPlot == NULL)
SetCurrentContext(ctx);
return ctx;
}
void DestroyContext(ImPlotContext* ctx) {
if (ctx == NULL)
ctx = GImPlot;
if (GImPlot == ctx)
SetCurrentContext(NULL);
IM_DELETE(ctx);
}
ImPlotContext* GetCurrentContext() {
return GImPlot;
}
void SetCurrentContext(ImPlotContext* ctx) {
GImPlot = ctx;
}
#define IMPLOT_APPEND_CMAP(name, qual) ctx->ColormapData.Append(#name, name, sizeof(name)/sizeof(ImU32), qual)
#define IM_RGB(r,g,b) IM_COL32(r,g,b,255)
void Initialize(ImPlotContext* ctx) {
ResetCtxForNextPlot(ctx);
ResetCtxForNextAlignedPlots(ctx);
ResetCtxForNextSubplot(ctx);
const ImU32 Deep[] = {4289753676, 4283598045, 4285048917, 4283584196, 4289950337, 4284512403, 4291005402, 4287401100, 4285839820, 4291671396 };
const ImU32 Dark[] = {4280031972, 4290281015, 4283084621, 4288892568, 4278222847, 4281597951, 4280833702, 4290740727, 4288256409 };
const ImU32 Pastel[] = {4289639675, 4293119411, 4291161036, 4293184478, 4289124862, 4291624959, 4290631909, 4293712637, 4294111986 };
const ImU32 Paired[] = {4293119554, 4290017311, 4287291314, 4281114675, 4288256763, 4280031971, 4285513725, 4278222847, 4292260554, 4288298346, 4288282623, 4280834481};
const ImU32 Viridis[] = {4283695428, 4285867080, 4287054913, 4287455029, 4287526954, 4287402273, 4286883874, 4285579076, 4283552122, 4280737725, 4280674301 };
const ImU32 Plasma[] = {4287039501, 4288480321, 4289200234, 4288941455, 4287638193, 4286072780, 4284638433, 4283139314, 4281771772, 4280667900, 4280416752 };
const ImU32 Hot[] = {4278190144, 4278190208, 4278190271, 4278190335, 4278206719, 4278223103, 4278239231, 4278255615, 4283826175, 4289396735, 4294967295 };
const ImU32 Cool[] = {4294967040, 4294960666, 4294954035, 4294947661, 4294941030, 4294934656, 4294928025, 4294921651, 4294915020, 4294908646, 4294902015 };
const ImU32 Pink[] = {4278190154, 4282532475, 4284308894, 4285690554, 4286879686, 4287870160, 4288794330, 4289651940, 4291685869, 4293392118, 4294967295 };
const ImU32 Jet[] = {4289331200, 4294901760, 4294923520, 4294945280, 4294967040, 4289396565, 4283826090, 4278255615, 4278233855, 4278212095, 4278190335 };
const ImU32 Twilight[] = {IM_RGB(226,217,226),IM_RGB(166,191,202),IM_RGB(109,144,192),IM_RGB(95,88,176),IM_RGB(83,30,124),IM_RGB(47,20,54),IM_RGB(100,25,75),IM_RGB(159,60,80),IM_RGB(192,117,94),IM_RGB(208,179,158),IM_RGB(226,217,226)};
const ImU32 RdBu[] = {IM_RGB(103,0,31),IM_RGB(178,24,43),IM_RGB(214,96,77),IM_RGB(244,165,130),IM_RGB(253,219,199),IM_RGB(247,247,247),IM_RGB(209,229,240),IM_RGB(146,197,222),IM_RGB(67,147,195),IM_RGB(33,102,172),IM_RGB(5,48,97)};
const ImU32 BrBG[] = {IM_RGB(84,48,5),IM_RGB(140,81,10),IM_RGB(191,129,45),IM_RGB(223,194,125),IM_RGB(246,232,195),IM_RGB(245,245,245),IM_RGB(199,234,229),IM_RGB(128,205,193),IM_RGB(53,151,143),IM_RGB(1,102,94),IM_RGB(0,60,48)};
const ImU32 PiYG[] = {IM_RGB(142,1,82),IM_RGB(197,27,125),IM_RGB(222,119,174),IM_RGB(241,182,218),IM_RGB(253,224,239),IM_RGB(247,247,247),IM_RGB(230,245,208),IM_RGB(184,225,134),IM_RGB(127,188,65),IM_RGB(77,146,33),IM_RGB(39,100,25)};
const ImU32 Spectral[] = {IM_RGB(158,1,66),IM_RGB(213,62,79),IM_RGB(244,109,67),IM_RGB(253,174,97),IM_RGB(254,224,139),IM_RGB(255,255,191),IM_RGB(230,245,152),IM_RGB(171,221,164),IM_RGB(102,194,165),IM_RGB(50,136,189),IM_RGB(94,79,162)};
const ImU32 Greys[] = {IM_COL32_WHITE, IM_COL32_BLACK };
IMPLOT_APPEND_CMAP(Deep, true);
IMPLOT_APPEND_CMAP(Dark, true);
IMPLOT_APPEND_CMAP(Pastel, true);
IMPLOT_APPEND_CMAP(Paired, true);
IMPLOT_APPEND_CMAP(Viridis, false);
IMPLOT_APPEND_CMAP(Plasma, false);
IMPLOT_APPEND_CMAP(Hot, false);
IMPLOT_APPEND_CMAP(Cool, false);
IMPLOT_APPEND_CMAP(Pink, false);
IMPLOT_APPEND_CMAP(Jet, false);
IMPLOT_APPEND_CMAP(Twilight, false);
IMPLOT_APPEND_CMAP(RdBu, false);
IMPLOT_APPEND_CMAP(BrBG, false);
IMPLOT_APPEND_CMAP(PiYG, false);
IMPLOT_APPEND_CMAP(Spectral, false);
IMPLOT_APPEND_CMAP(Greys, false);
}
void ResetCtxForNextPlot(ImPlotContext* ctx) {
// end child window if it was made
if (ctx->ChildWindowMade)
ImGui::EndChild();
ctx->ChildWindowMade = false;
// reset the next plot/item data
ctx->NextPlotData.Reset();
ctx->NextItemData.Reset();
// reset labels
ctx->Annotations.Reset();
ctx->Tags.Reset();
// reset extents/fit
ctx->OpenContextThisFrame = false;
// reset digital plot items count
ctx->DigitalPlotItemCnt = 0;
ctx->DigitalPlotOffset = 0;
// nullify plot
ctx->CurrentPlot = NULL;
ctx->CurrentItem = NULL;
ctx->PreviousItem = NULL;
}
void ResetCtxForNextAlignedPlots(ImPlotContext* ctx) {
ctx->CurrentAlignmentH = NULL;
ctx->CurrentAlignmentV = NULL;
}
void ResetCtxForNextSubplot(ImPlotContext* ctx) {
ctx->CurrentSubplot = NULL;
ctx->CurrentAlignmentH = NULL;
ctx->CurrentAlignmentV = NULL;
}
//-----------------------------------------------------------------------------
// Plot Utils
//-----------------------------------------------------------------------------
ImPlotPlot* GetPlot(const char* title) {
ImGuiWindow* Window = GImGui->CurrentWindow;
const ImGuiID ID = Window->GetID(title);
return GImPlot->Plots.GetByKey(ID);
}
ImPlotPlot* GetCurrentPlot() {
return GImPlot->CurrentPlot;
}
void BustPlotCache() {
GImPlot->Plots.Clear();
GImPlot->Subplots.Clear();
}
//-----------------------------------------------------------------------------
// Legend Utils
//-----------------------------------------------------------------------------
ImVec2 GetLocationPos(const ImRect& outer_rect, const ImVec2& inner_size, ImPlotLocation loc, const ImVec2& pad) {
ImVec2 pos;
if (ImHasFlag(loc, ImPlotLocation_West) && !ImHasFlag(loc, ImPlotLocation_East))
pos.x = outer_rect.Min.x + pad.x;
else if (!ImHasFlag(loc, ImPlotLocation_West) && ImHasFlag(loc, ImPlotLocation_East))
pos.x = outer_rect.Max.x - pad.x - inner_size.x;
else
pos.x = outer_rect.GetCenter().x - inner_size.x * 0.5f;
// legend reference point y
if (ImHasFlag(loc, ImPlotLocation_North) && !ImHasFlag(loc, ImPlotLocation_South))
pos.y = outer_rect.Min.y + pad.y;
else if (!ImHasFlag(loc, ImPlotLocation_North) && ImHasFlag(loc, ImPlotLocation_South))
pos.y = outer_rect.Max.y - pad.y - inner_size.y;
else
pos.y = outer_rect.GetCenter().y - inner_size.y * 0.5f;
pos.x = IM_ROUND(pos.x);
pos.y = IM_ROUND(pos.y);
return pos;
}
ImVec2 CalcLegendSize(ImPlotItemGroup& items, const ImVec2& pad, const ImVec2& spacing, bool vertical) {
// vars
const int nItems = items.GetLegendCount();
const float txt_ht = ImGui::GetTextLineHeight();
const float icon_size = txt_ht;
// get label max width
float max_label_width = 0;
float sum_label_width = 0;
for (int i = 0; i < nItems; ++i) {
const char* label = items.GetLegendLabel(i);
const float label_width = ImGui::CalcTextSize(label, NULL, true).x;
max_label_width = label_width > max_label_width ? label_width : max_label_width;
sum_label_width += label_width;
}
// calc legend size
const ImVec2 legend_size = vertical ?
ImVec2(pad.x * 2 + icon_size + max_label_width, pad.y * 2 + nItems * txt_ht + (nItems - 1) * spacing.y) :
ImVec2(pad.x * 2 + icon_size * nItems + sum_label_width + (nItems - 1) * spacing.x, pad.y * 2 + txt_ht);
return legend_size;
}
int LegendSortingComp(const void* _a, const void* _b) {
ImPlotItemGroup* items = GImPlot->SortItems;
const int a = *(const int*)_a;
const int b = *(const int*)_b;
const char* label_a = items->GetLegendLabel(a);
const char* label_b = items->GetLegendLabel(b);
return strcmp(label_a,label_b);
}
bool ShowLegendEntries(ImPlotItemGroup& items, const ImRect& legend_bb, bool hovered, const ImVec2& pad, const ImVec2& spacing, bool vertical, ImDrawList& DrawList) {
// vars
const float txt_ht = ImGui::GetTextLineHeight();
const float icon_size = txt_ht;
const float icon_shrink = 2;
ImU32 col_txt = GetStyleColorU32(ImPlotCol_LegendText);
ImU32 col_txt_dis = ImAlphaU32(col_txt, 0.25f);
// render each legend item
float sum_label_width = 0;
bool any_item_hovered = false;
const int num_items = items.GetLegendCount();
if (num_items < 1)
return hovered;
// build render order
ImVector<int>& indices = GImPlot->TempInt1;
indices.resize(num_items);
for (int i = 0; i < num_items; ++i)
indices[i] = i;
if (ImHasFlag(items.Legend.Flags, ImPlotLegendFlags_Sort) && num_items > 1) {
GImPlot->SortItems = &items;
qsort(indices.Data, num_items, sizeof(int), LegendSortingComp);
}
// render
for (int i = 0; i < num_items; ++i) {
const int idx = indices[i];
ImPlotItem* item = items.GetLegendItem(idx);
const char* label = items.GetLegendLabel(idx);
const float label_width = ImGui::CalcTextSize(label, NULL, true).x;
const ImVec2 top_left = vertical ?
legend_bb.Min + pad + ImVec2(0, i * (txt_ht + spacing.y)) :
legend_bb.Min + pad + ImVec2(i * (icon_size + spacing.x) + sum_label_width, 0);
sum_label_width += label_width;
ImRect icon_bb;
icon_bb.Min = top_left + ImVec2(icon_shrink,icon_shrink);
icon_bb.Max = top_left + ImVec2(icon_size - icon_shrink, icon_size - icon_shrink);
ImRect label_bb;
label_bb.Min = top_left;
label_bb.Max = top_left + ImVec2(label_width + icon_size, icon_size);
ImU32 col_txt_hl;
ImU32 col_item = ImAlphaU32(item->Color,1);
ImRect button_bb(icon_bb.Min, label_bb.Max);
ImGui::KeepAliveID(item->ID);
bool item_hov = false;
bool item_hld = false;
bool item_clk = ImHasFlag(items.Legend.Flags, ImPlotLegendFlags_NoButtons)
? false
: ImGui::ButtonBehavior(button_bb, item->ID, &item_hov, &item_hld);
if (item_clk)
item->Show = !item->Show;
const bool can_hover = (item_hov)
&& (!ImHasFlag(items.Legend.Flags, ImPlotLegendFlags_NoHighlightItem)
|| !ImHasFlag(items.Legend.Flags, ImPlotLegendFlags_NoHighlightAxis));
if (can_hover) {
item->LegendHoverRect.Min = icon_bb.Min;
item->LegendHoverRect.Max = label_bb.Max;
item->LegendHovered = true;
col_txt_hl = ImMixU32(col_txt, col_item, 64);
any_item_hovered = true;
}
else {
col_txt_hl = ImGui::GetColorU32(col_txt);
}
ImU32 col_icon;
if (item_hld)
col_icon = item->Show ? ImAlphaU32(col_item,0.5f) : ImGui::GetColorU32(ImGuiCol_TextDisabled, 0.5f);
else if (item_hov)
col_icon = item->Show ? ImAlphaU32(col_item,0.75f) : ImGui::GetColorU32(ImGuiCol_TextDisabled, 0.75f);
else
col_icon = item->Show ? col_item : col_txt_dis;
DrawList.AddRectFilled(icon_bb.Min, icon_bb.Max, col_icon);
const char* text_display_end = ImGui::FindRenderedTextEnd(label, NULL);
if (label != text_display_end)
DrawList.AddText(top_left + ImVec2(icon_size, 0), item->Show ? col_txt_hl : col_txt_dis, label, text_display_end);
}
return hovered && !any_item_hovered;
}
//-----------------------------------------------------------------------------
// Locators
//-----------------------------------------------------------------------------
static const float TICK_FILL_X = 0.8f;
static const float TICK_FILL_Y = 1.0f;
void Locator_Default(ImPlotTicker& ticker, const ImPlotRange& range, float pixels, bool vertical, ImPlotFormatter formatter, void* formatter_data) {
if (range.Min == range.Max)
return;
const int nMinor = 10;
const int nMajor = ImMax(2, (int)IM_ROUND(pixels / (vertical ? 300.0f : 400.0f)));
const double nice_range = NiceNum(range.Size() * 0.99, false);
const double interval = NiceNum(nice_range / (nMajor - 1), true);
const double graphmin = floor(range.Min / interval) * interval;
const double graphmax = ceil(range.Max / interval) * interval;
bool first_major_set = false;
int first_major_idx = 0;
const int idx0 = ticker.TickCount(); // ticker may have user custom ticks
ImVec2 total_size(0,0);
for (double major = graphmin; major < graphmax + 0.5 * interval; major += interval) {
// is this zero? combat zero formatting issues
if (major-interval < 0 && major+interval > 0)
major = 0;
if (range.Contains(major)) {
if (!first_major_set) {
first_major_idx = ticker.TickCount();
first_major_set = true;
}
total_size += ticker.AddTick(major, true, 0, true, formatter, formatter_data).LabelSize;
}
for (int i = 1; i < nMinor; ++i) {
double minor = major + i * interval / nMinor;
if (range.Contains(minor)) {
total_size += ticker.AddTick(minor, false, 0, true, formatter, formatter_data).LabelSize;
}
}
}
// prune if necessary
if ((!vertical && total_size.x > pixels*TICK_FILL_X) || (vertical && total_size.y > pixels*TICK_FILL_Y)) {
for (int i = first_major_idx-1; i >= idx0; i -= 2)
ticker.Ticks[i].ShowLabel = false;
for (int i = first_major_idx+1; i < ticker.TickCount(); i += 2)
ticker.Ticks[i].ShowLabel = false;
}
}
bool CalcLogarithmicExponents(const ImPlotRange& range, float pix, bool vertical, int& exp_min, int& exp_max, int& exp_step) {
if (range.Min * range.Max > 0) {
const int nMajor = vertical ? ImMax(2, (int)IM_ROUND(pix * 0.02f)) : ImMax(2, (int)IM_ROUND(pix * 0.01f)); // TODO: magic numbers
double log_min = ImLog10(ImAbs(range.Min));
double log_max = ImLog10(ImAbs(range.Max));
double log_a = ImMin(log_min,log_max);
double log_b = ImMax(log_min,log_max);
exp_step = ImMax(1,(int)(log_b - log_a) / nMajor);
exp_min = (int)log_a;
exp_max = (int)log_b;
if (exp_step != 1) {
while(exp_step % 3 != 0) exp_step++; // make step size multiple of three
while(exp_min % exp_step != 0) exp_min--; // decrease exp_min until exp_min + N * exp_step will be 0
}
return true;
}
return false;
}
void AddTicksLogarithmic(const ImPlotRange& range, int exp_min, int exp_max, int exp_step, ImPlotTicker& ticker, ImPlotFormatter formatter, void* data) {
const double sign = ImSign(range.Max);
for (int e = exp_min - exp_step; e < (exp_max + exp_step); e += exp_step) {
double major1 = sign*ImPow(10, (double)(e));
double major2 = sign*ImPow(10, (double)(e + 1));
double interval = (major2 - major1) / 9;
if (major1 >= (range.Min - DBL_EPSILON) && major1 <= (range.Max + DBL_EPSILON))
ticker.AddTick(major1, true, 0, true, formatter, data);
for (int j = 0; j < exp_step; ++j) {
major1 = sign*ImPow(10, (double)(e+j));
major2 = sign*ImPow(10, (double)(e+j+1));
interval = (major2 - major1) / 9;
for (int i = 1; i < (9 + (int)(j < (exp_step - 1))); ++i) {
double minor = major1 + i * interval;
if (minor >= (range.Min - DBL_EPSILON) && minor <= (range.Max + DBL_EPSILON))
ticker.AddTick(minor, false, 0, false, formatter, data);
}
}
}
}
void Locator_Log10(ImPlotTicker& ticker, const ImPlotRange& range, float pixels, bool vertical, ImPlotFormatter formatter, void* formatter_data) {
int exp_min, exp_max, exp_step;
if (CalcLogarithmicExponents(range, pixels, vertical, exp_min, exp_max, exp_step))
AddTicksLogarithmic(range, exp_min, exp_max, exp_step, ticker, formatter, formatter_data);
}
float CalcSymLogPixel(double plt, const ImPlotRange& range, float pixels) {
double scaleToPixels = pixels / range.Size();
double scaleMin = TransformForward_SymLog(range.Min,NULL);
double scaleMax = TransformForward_SymLog(range.Max,NULL);
double s = TransformForward_SymLog(plt, NULL);
double t = (s - scaleMin) / (scaleMax - scaleMin);
plt = range.Min + range.Size() * t;
return (float)(0 + scaleToPixels * (plt - range.Min));
}
void Locator_SymLog(ImPlotTicker& ticker, const ImPlotRange& range, float pixels, bool vertical, ImPlotFormatter formatter, void* formatter_data) {
if (range.Min >= -1 && range.Max <= 1) {
Locator_Default(ticker, range, pixels, vertical, formatter, formatter_data);
}
else if (range.Min * range.Max < 0) { // cross zero
const float pix_min = 0;
const float pix_max = pixels;
const float pix_p1 = CalcSymLogPixel(1, range, pixels);
const float pix_n1 = CalcSymLogPixel(-1, range, pixels);
int exp_min_p, exp_max_p, exp_step_p;
int exp_min_n, exp_max_n, exp_step_n;
CalcLogarithmicExponents(ImPlotRange(1,range.Max), ImAbs(pix_max-pix_p1),vertical,exp_min_p,exp_max_p,exp_step_p);
CalcLogarithmicExponents(ImPlotRange(range.Min,-1),ImAbs(pix_n1-pix_min),vertical,exp_min_n,exp_max_n,exp_step_n);
int exp_step = ImMax(exp_step_n, exp_step_p);
ticker.AddTick(0,true,0,true,formatter,formatter_data);
AddTicksLogarithmic(ImPlotRange(1,range.Max), exp_min_p,exp_max_p,exp_step,ticker,formatter,formatter_data);
AddTicksLogarithmic(ImPlotRange(range.Min,-1),exp_min_n,exp_max_n,exp_step,ticker,formatter,formatter_data);
}
else {
Locator_Log10(ticker, range, pixels, vertical, formatter, formatter_data);
}
}
void AddTicksCustom(const double* values, const char* const labels[], int n, ImPlotTicker& ticker, ImPlotFormatter formatter, void* data) {
for (int i = 0; i < n; ++i) {
if (labels != NULL)
ticker.AddTick(values[i], false, 0, true, labels[i]);
else
ticker.AddTick(values[i], false, 0, true, formatter, data);
}
}
//-----------------------------------------------------------------------------
// Time Ticks and Utils
//-----------------------------------------------------------------------------
// this may not be thread safe?
static const double TimeUnitSpans[ImPlotTimeUnit_COUNT] = {
0.000001,
0.001,
1,
60,
3600,
86400,
2629800,
31557600
};
inline ImPlotTimeUnit GetUnitForRange(double range) {
static double cutoffs[ImPlotTimeUnit_COUNT] = {0.001, 1, 60, 3600, 86400, 2629800, 31557600, IMPLOT_MAX_TIME};
for (int i = 0; i < ImPlotTimeUnit_COUNT; ++i) {
if (range <= cutoffs[i])
return (ImPlotTimeUnit)i;
}
return ImPlotTimeUnit_Yr;
}
inline int LowerBoundStep(int max_divs, const int* divs, const int* step, int size) {
if (max_divs < divs[0])
return 0;
for (int i = 1; i < size; ++i) {
if (max_divs < divs[i])
return step[i-1];
}
return step[size-1];
}
inline int GetTimeStep(int max_divs, ImPlotTimeUnit unit) {
if (unit == ImPlotTimeUnit_Ms || unit == ImPlotTimeUnit_Us) {
static const int step[] = {500,250,200,100,50,25,20,10,5,2,1};
static const int divs[] = {2,4,5,10,20,40,50,100,200,500,1000};
return LowerBoundStep(max_divs, divs, step, 11);
}
if (unit == ImPlotTimeUnit_S || unit == ImPlotTimeUnit_Min) {
static const int step[] = {30,15,10,5,1};
static const int divs[] = {2,4,6,12,60};
return LowerBoundStep(max_divs, divs, step, 5);
}
else if (unit == ImPlotTimeUnit_Hr) {
static const int step[] = {12,6,3,2,1};
static const int divs[] = {2,4,8,12,24};
return LowerBoundStep(max_divs, divs, step, 5);
}
else if (unit == ImPlotTimeUnit_Day) {
static const int step[] = {14,7,2,1};
static const int divs[] = {2,4,14,28};
return LowerBoundStep(max_divs, divs, step, 4);
}
else if (unit == ImPlotTimeUnit_Mo) {
static const int step[] = {6,3,2,1};
static const int divs[] = {2,4,6,12};
return LowerBoundStep(max_divs, divs, step, 4);
}
return 0;
}
ImPlotTime MkGmtTime(struct tm *ptm) {
ImPlotTime t;
#ifdef _WIN32
t.S = _mkgmtime(ptm);
#else
t.S = timegm(ptm);
#endif
if (t.S < 0)
t.S = 0;
return t;
}
tm* GetGmtTime(const ImPlotTime& t, tm* ptm)
{
#ifdef _WIN32
if (gmtime_s(ptm, &t.S) == 0)
return ptm;
else
return NULL;
#else
return gmtime_r(&t.S, ptm);
#endif
}
ImPlotTime MkLocTime(struct tm *ptm) {
ImPlotTime t;
t.S = mktime(ptm);
if (t.S < 0)
t.S = 0;
return t;
}
tm* GetLocTime(const ImPlotTime& t, tm* ptm) {
#ifdef _WIN32
if (localtime_s(ptm, &t.S) == 0)
return ptm;
else
return NULL;
#else
return localtime_r(&t.S, ptm);
#endif
}
inline ImPlotTime MkTime(struct tm *ptm) {
if (GetStyle().UseLocalTime)
return MkLocTime(ptm);
else
return MkGmtTime(ptm);
}
inline tm* GetTime(const ImPlotTime& t, tm* ptm) {
if (GetStyle().UseLocalTime)
return GetLocTime(t,ptm);
else
return GetGmtTime(t,ptm);
}
ImPlotTime MakeTime(int year, int month, int day, int hour, int min, int sec, int us) {
tm& Tm = GImPlot->Tm;
int yr = year - 1900;
if (yr < 0)
yr = 0;
sec = sec + us / 1000000;
us = us % 1000000;
Tm.tm_sec = sec;
Tm.tm_min = min;
Tm.tm_hour = hour;
Tm.tm_mday = day;
Tm.tm_mon = month;
Tm.tm_year = yr;
ImPlotTime t = MkTime(&Tm);
t.Us = us;
return t;
}
int GetYear(const ImPlotTime& t) {
tm& Tm = GImPlot->Tm;
GetTime(t, &Tm);
return Tm.tm_year + 1900;
}
ImPlotTime AddTime(const ImPlotTime& t, ImPlotTimeUnit unit, int count) {
tm& Tm = GImPlot->Tm;
ImPlotTime t_out = t;
switch(unit) {
case ImPlotTimeUnit_Us: t_out.Us += count; break;
case ImPlotTimeUnit_Ms: t_out.Us += count * 1000; break;
case ImPlotTimeUnit_S: t_out.S += count; break;
case ImPlotTimeUnit_Min: t_out.S += count * 60; break;
case ImPlotTimeUnit_Hr: t_out.S += count * 3600; break;
case ImPlotTimeUnit_Day: t_out.S += count * 86400; break;
case ImPlotTimeUnit_Mo: for (int i = 0; i < abs(count); ++i) {
GetTime(t_out, &Tm);
if (count > 0)
t_out.S += 86400 * GetDaysInMonth(Tm.tm_year + 1900, Tm.tm_mon);
else if (count < 0)
t_out.S -= 86400 * GetDaysInMonth(Tm.tm_year + 1900 - (Tm.tm_mon == 0 ? 1 : 0), Tm.tm_mon == 0 ? 11 : Tm.tm_mon - 1); // NOT WORKING
}
break;
case ImPlotTimeUnit_Yr: for (int i = 0; i < abs(count); ++i) {
if (count > 0)
t_out.S += 86400 * (365 + (int)IsLeapYear(GetYear(t_out)));
else if (count < 0)
t_out.S -= 86400 * (365 + (int)IsLeapYear(GetYear(t_out) - 1));
// this is incorrect if leap year and we are past Feb 28
}
break;
default: break;
}
t_out.RollOver();
return t_out;
}
ImPlotTime FloorTime(const ImPlotTime& t, ImPlotTimeUnit unit) {
GetTime(t, &GImPlot->Tm);
switch (unit) {
case ImPlotTimeUnit_S: return ImPlotTime(t.S, 0);
case ImPlotTimeUnit_Ms: return ImPlotTime(t.S, (t.Us / 1000) * 1000);
case ImPlotTimeUnit_Us: return t;
case ImPlotTimeUnit_Yr: GImPlot->Tm.tm_mon = 0; // fall-through
case ImPlotTimeUnit_Mo: GImPlot->Tm.tm_mday = 1; // fall-through
case ImPlotTimeUnit_Day: GImPlot->Tm.tm_hour = 0; // fall-through
case ImPlotTimeUnit_Hr: GImPlot->Tm.tm_min = 0; // fall-through
case ImPlotTimeUnit_Min: GImPlot->Tm.tm_sec = 0; break;
default: return t;
}
return MkTime(&GImPlot->Tm);
}
ImPlotTime CeilTime(const ImPlotTime& t, ImPlotTimeUnit unit) {
return AddTime(FloorTime(t, unit), unit, 1);
}
ImPlotTime RoundTime(const ImPlotTime& t, ImPlotTimeUnit unit) {
ImPlotTime t1 = FloorTime(t, unit);
ImPlotTime t2 = AddTime(t1,unit,1);
if (t1.S == t2.S)
return t.Us - t1.Us < t2.Us - t.Us ? t1 : t2;
return t.S - t1.S < t2.S - t.S ? t1 : t2;
}
ImPlotTime CombineDateTime(const ImPlotTime& date_part, const ImPlotTime& tod_part) {
tm& Tm = GImPlot->Tm;
GetTime(date_part, &GImPlot->Tm);
int y = Tm.tm_year;
int m = Tm.tm_mon;
int d = Tm.tm_mday;
GetTime(tod_part, &GImPlot->Tm);
Tm.tm_year = y;
Tm.tm_mon = m;
Tm.tm_mday = d;
ImPlotTime t = MkTime(&Tm);
t.Us = tod_part.Us;
return t;
}
// TODO: allow users to define these
static const char* MONTH_NAMES[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
static const char* WD_ABRVS[] = {"Su","Mo","Tu","We","Th","Fr","Sa"};
static const char* MONTH_ABRVS[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt, bool use_24_hr_clk) {
tm& Tm = GImPlot->Tm;
GetTime(t, &Tm);
const int us = t.Us % 1000;
const int ms = t.Us / 1000;
const int sec = Tm.tm_sec;
const int min = Tm.tm_min;
if (use_24_hr_clk) {
const int hr = Tm.tm_hour;
switch(fmt) {
case ImPlotTimeFmt_Us: return ImFormatString(buffer, size, ".%03d %03d", ms, us);
case ImPlotTimeFmt_SUs: return ImFormatString(buffer, size, ":%02d.%03d %03d", sec, ms, us);
case ImPlotTimeFmt_SMs: return ImFormatString(buffer, size, ":%02d.%03d", sec, ms);
case ImPlotTimeFmt_S: return ImFormatString(buffer, size, ":%02d", sec);
case ImPlotTimeFmt_MinSMs: return ImFormatString(buffer, size, ":%02d:%02d.%03d", min, sec, ms);
case ImPlotTimeFmt_HrMinSMs: return ImFormatString(buffer, size, "%02d:%02d:%02d.%03d", hr, min, sec, ms);
case ImPlotTimeFmt_HrMinS: return ImFormatString(buffer, size, "%02d:%02d:%02d", hr, min, sec);
case ImPlotTimeFmt_HrMin: return ImFormatString(buffer, size, "%02d:%02d", hr, min);
case ImPlotTimeFmt_Hr: return ImFormatString(buffer, size, "%02d:00", hr);
default: return 0;
}
}
else {
const char* ap = Tm.tm_hour < 12 ? "am" : "pm";
const int hr = (Tm.tm_hour == 0 || Tm.tm_hour == 12) ? 12 : Tm.tm_hour % 12;
switch(fmt) {
case ImPlotTimeFmt_Us: return ImFormatString(buffer, size, ".%03d %03d", ms, us);
case ImPlotTimeFmt_SUs: return ImFormatString(buffer, size, ":%02d.%03d %03d", sec, ms, us);
case ImPlotTimeFmt_SMs: return ImFormatString(buffer, size, ":%02d.%03d", sec, ms);
case ImPlotTimeFmt_S: return ImFormatString(buffer, size, ":%02d", sec);
case ImPlotTimeFmt_MinSMs: return ImFormatString(buffer, size, ":%02d:%02d.%03d", min, sec, ms);
case ImPlotTimeFmt_HrMinSMs: return ImFormatString(buffer, size, "%d:%02d:%02d.%03d%s", hr, min, sec, ms, ap);
case ImPlotTimeFmt_HrMinS: return ImFormatString(buffer, size, "%d:%02d:%02d%s", hr, min, sec, ap);
case ImPlotTimeFmt_HrMin: return ImFormatString(buffer, size, "%d:%02d%s", hr, min, ap);
case ImPlotTimeFmt_Hr: return ImFormatString(buffer, size, "%d%s", hr, ap);
default: return 0;
}
}
}
int FormatDate(const ImPlotTime& t, char* buffer, int size, ImPlotDateFmt fmt, bool use_iso_8601) {
tm& Tm = GImPlot->Tm;
GetTime(t, &Tm);
const int day = Tm.tm_mday;
const int mon = Tm.tm_mon + 1;
const int year = Tm.tm_year + 1900;
const int yr = year % 100;
if (use_iso_8601) {
switch (fmt) {
case ImPlotDateFmt_DayMo: return ImFormatString(buffer, size, "--%02d-%02d", mon, day);
case ImPlotDateFmt_DayMoYr: return ImFormatString(buffer, size, "%d-%02d-%02d", year, mon, day);
case ImPlotDateFmt_MoYr: return ImFormatString(buffer, size, "%d-%02d", year, mon);
case ImPlotDateFmt_Mo: return ImFormatString(buffer, size, "--%02d", mon);
case ImPlotDateFmt_Yr: return ImFormatString(buffer, size, "%d", year);
default: return 0;
}
}
else {
switch (fmt) {
case ImPlotDateFmt_DayMo: return ImFormatString(buffer, size, "%d/%d", mon, day);
case ImPlotDateFmt_DayMoYr: return ImFormatString(buffer, size, "%d/%d/%02d", mon, day, yr);
case ImPlotDateFmt_MoYr: return ImFormatString(buffer, size, "%s %d", MONTH_ABRVS[Tm.tm_mon], year);
case ImPlotDateFmt_Mo: return ImFormatString(buffer, size, "%s", MONTH_ABRVS[Tm.tm_mon]);
case ImPlotDateFmt_Yr: return ImFormatString(buffer, size, "%d", year);
default: return 0;
}
}
}
int FormatDateTime(const ImPlotTime& t, char* buffer, int size, ImPlotDateTimeSpec fmt) {
int written = 0;
if (fmt.Date != ImPlotDateFmt_None)
written += FormatDate(t, buffer, size, fmt.Date, fmt.UseISO8601);
if (fmt.Time != ImPlotTimeFmt_None) {
if (fmt.Date != ImPlotDateFmt_None)
buffer[written++] = ' ';
written += FormatTime(t, &buffer[written], size - written, fmt.Time, fmt.Use24HourClock);
}
return written;
}
inline float GetDateTimeWidth(ImPlotDateTimeSpec fmt) {
static const ImPlotTime t_max_width = MakeTime(2888, 12, 22, 12, 58, 58, 888888); // best guess at time that maximizes pixel width
char buffer[32];
FormatDateTime(t_max_width, buffer, 32, fmt);
return ImGui::CalcTextSize(buffer).x;
}
inline bool TimeLabelSame(const char* l1, const char* l2) {
size_t len1 = strlen(l1);
size_t len2 = strlen(l2);
size_t n = len1 < len2 ? len1 : len2;
return strcmp(l1 + len1 - n, l2 + len2 - n) == 0;
}
static const ImPlotDateTimeSpec TimeFormatLevel0[ImPlotTimeUnit_COUNT] = {
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_Us),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_SMs),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_S),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_HrMin),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_Hr),
ImPlotDateTimeSpec(ImPlotDateFmt_DayMo, ImPlotTimeFmt_None),
ImPlotDateTimeSpec(ImPlotDateFmt_Mo, ImPlotTimeFmt_None),
ImPlotDateTimeSpec(ImPlotDateFmt_Yr, ImPlotTimeFmt_None)
};
static const ImPlotDateTimeSpec TimeFormatLevel1[ImPlotTimeUnit_COUNT] = {
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_HrMin),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_HrMinS),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_HrMin),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_HrMin),
ImPlotDateTimeSpec(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_None),
ImPlotDateTimeSpec(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_None),
ImPlotDateTimeSpec(ImPlotDateFmt_Yr, ImPlotTimeFmt_None),
ImPlotDateTimeSpec(ImPlotDateFmt_Yr, ImPlotTimeFmt_None)
};
static const ImPlotDateTimeSpec TimeFormatLevel1First[ImPlotTimeUnit_COUNT] = {
ImPlotDateTimeSpec(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_HrMinS),
ImPlotDateTimeSpec(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_HrMinS),
ImPlotDateTimeSpec(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_HrMin),
ImPlotDateTimeSpec(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_HrMin),
ImPlotDateTimeSpec(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_None),
ImPlotDateTimeSpec(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_None),
ImPlotDateTimeSpec(ImPlotDateFmt_Yr, ImPlotTimeFmt_None),
ImPlotDateTimeSpec(ImPlotDateFmt_Yr, ImPlotTimeFmt_None)
};
static const ImPlotDateTimeSpec TimeFormatMouseCursor[ImPlotTimeUnit_COUNT] = {
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_Us),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_SUs),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_SMs),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_HrMinS),
ImPlotDateTimeSpec(ImPlotDateFmt_None, ImPlotTimeFmt_HrMin),
ImPlotDateTimeSpec(ImPlotDateFmt_DayMo, ImPlotTimeFmt_Hr),
ImPlotDateTimeSpec(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_None),
ImPlotDateTimeSpec(ImPlotDateFmt_MoYr, ImPlotTimeFmt_None)
};
inline ImPlotDateTimeSpec GetDateTimeFmt(const ImPlotDateTimeSpec* ctx, ImPlotTimeUnit idx) {
ImPlotStyle& style = GetStyle();
ImPlotDateTimeSpec fmt = ctx[idx];
fmt.UseISO8601 = style.UseISO8601;
fmt.Use24HourClock = style.Use24HourClock;
return fmt;
}
void Locator_Time(ImPlotTicker& ticker, const ImPlotRange& range, float pixels, bool vertical, ImPlotFormatter formatter, void* formatter_data) {
IM_ASSERT_USER_ERROR(vertical == false, "Cannot locate Time ticks on vertical axis!");
(void)vertical;
// get units for level 0 and level 1 labels
const ImPlotTimeUnit unit0 = GetUnitForRange(range.Size() / (pixels / 100)); // level = 0 (top)
const ImPlotTimeUnit unit1 = ImClamp(unit0 + 1, 0, ImPlotTimeUnit_COUNT-1); // level = 1 (bottom)
// get time format specs
const ImPlotDateTimeSpec fmt0 = GetDateTimeFmt(TimeFormatLevel0, unit0);
const ImPlotDateTimeSpec fmt1 = GetDateTimeFmt(TimeFormatLevel1, unit1);
const ImPlotDateTimeSpec fmtf = GetDateTimeFmt(TimeFormatLevel1First, unit1);
// min max times
const ImPlotTime t_min = ImPlotTime::FromDouble(range.Min);
const ImPlotTime t_max = ImPlotTime::FromDouble(range.Max);
// maximum allowable density of labels
const float max_density = 0.5f;
// book keeping
int last_major_offset = -1;
// formatter data
Formatter_Time_Data ftd;
ftd.UserFormatter = formatter;
ftd.UserFormatterData = formatter_data;
if (unit0 != ImPlotTimeUnit_Yr) {
// pixels per major (level 1) division
const float pix_per_major_div = pixels / (float)(range.Size() / TimeUnitSpans[unit1]);
// nominal pixels taken up by labels
const float fmt0_width = GetDateTimeWidth(fmt0);
const float fmt1_width = GetDateTimeWidth(fmt1);
const float fmtf_width = GetDateTimeWidth(fmtf);
// the maximum number of minor (level 0) labels that can fit between major (level 1) divisions
const int minor_per_major = (int)(max_density * pix_per_major_div / fmt0_width);
// the minor step size (level 0)
const int step = GetTimeStep(minor_per_major, unit0);
// generate ticks
ImPlotTime t1 = FloorTime(ImPlotTime::FromDouble(range.Min), unit1);
while (t1 < t_max) {
// get next major
const ImPlotTime t2 = AddTime(t1, unit1, 1);
// add major tick
if (t1 >= t_min && t1 <= t_max) {
// minor level 0 tick
ftd.Time = t1; ftd.Spec = fmt0;
ticker.AddTick(t1.ToDouble(), true, 0, true, Formatter_Time, &ftd);
// major level 1 tick
ftd.Time = t1; ftd.Spec = last_major_offset < 0 ? fmtf : fmt1;
ImPlotTick& tick_maj = ticker.AddTick(t1.ToDouble(), true, 1, true, Formatter_Time, &ftd);
const char* this_major = ticker.GetText(tick_maj);
if (last_major_offset >= 0 && TimeLabelSame(ticker.TextBuffer.Buf.Data + last_major_offset, this_major))
tick_maj.ShowLabel = false;
last_major_offset = tick_maj.TextOffset;
}
// add minor ticks up until next major
if (minor_per_major > 1 && (t_min <= t2 && t1 <= t_max)) {
ImPlotTime t12 = AddTime(t1, unit0, step);
while (t12 < t2) {
float px_to_t2 = (float)((t2 - t12).ToDouble()/range.Size()) * pixels;
if (t12 >= t_min && t12 <= t_max) {
ftd.Time = t12; ftd.Spec = fmt0;
ticker.AddTick(t12.ToDouble(), false, 0, px_to_t2 >= fmt0_width, Formatter_Time, &ftd);
if (last_major_offset < 0 && px_to_t2 >= fmt0_width && px_to_t2 >= (fmt1_width + fmtf_width) / 2) {
ftd.Time = t12; ftd.Spec = fmtf;
ImPlotTick& tick_maj = ticker.AddTick(t12.ToDouble(), true, 1, true, Formatter_Time, &ftd);
last_major_offset = tick_maj.TextOffset;
}
}
t12 = AddTime(t12, unit0, step);
}
}
t1 = t2;
}
}
else {
const ImPlotDateTimeSpec fmty = GetDateTimeFmt(TimeFormatLevel0, ImPlotTimeUnit_Yr);
const float label_width = GetDateTimeWidth(fmty);
const int max_labels = (int)(max_density * pixels / label_width);
const int year_min = GetYear(t_min);
const int year_max = GetYear(CeilTime(t_max, ImPlotTimeUnit_Yr));
const double nice_range = NiceNum((year_max - year_min)*0.99,false);
const double interval = NiceNum(nice_range / (max_labels - 1), true);
const int graphmin = (int)(floor(year_min / interval) * interval);
const int graphmax = (int)(ceil(year_max / interval) * interval);
const int step = (int)interval <= 0 ? 1 : (int)interval;
for (int y = graphmin; y < graphmax; y += step) {
ImPlotTime t = MakeTime(y);
if (t >= t_min && t <= t_max) {
ftd.Time = t; ftd.Spec = fmty;
ticker.AddTick(t.ToDouble(), true, 0, true, Formatter_Time, &ftd);
}
}
}
}
//-----------------------------------------------------------------------------
// Context Menu
//-----------------------------------------------------------------------------
template <typename F>
bool DragFloat(const char*, F*, float, F, F) {
return false;
}
template <>
bool DragFloat<double>(const char* label, double* v, float v_speed, double v_min, double v_max) {
return ImGui::DragScalar(label, ImGuiDataType_Double, v, v_speed, &v_min, &v_max, "%.3f", 1);
}
template <>
bool DragFloat<float>(const char* label, float* v, float v_speed, float v_min, float v_max) {
return ImGui::DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, "%.3f", 1);
}
inline void BeginDisabledControls(bool cond) {
if (cond) {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.25f);
}
}
inline void EndDisabledControls(bool cond) {
if (cond) {
ImGui::PopItemFlag();
ImGui::PopStyleVar();
}
}
void ShowAxisContextMenu(ImPlotAxis& axis, ImPlotAxis* equal_axis, bool /*time_allowed*/) {
ImGui::PushItemWidth(75);
bool always_locked = axis.IsRangeLocked() || axis.IsAutoFitting();
bool label = axis.HasLabel();
bool grid = axis.HasGridLines();
bool ticks = axis.HasTickMarks();
bool labels = axis.HasTickLabels();
double drag_speed = (axis.Range.Size() <= DBL_EPSILON) ? DBL_EPSILON * 1.0e+13 : 0.01 * axis.Range.Size(); // recover from almost equal axis limits.
if (axis.Scale == ImPlotScale_Time) {
ImPlotTime tmin = ImPlotTime::FromDouble(axis.Range.Min);
ImPlotTime tmax = ImPlotTime::FromDouble(axis.Range.Max);
BeginDisabledControls(always_locked);
ImGui::CheckboxFlags("##LockMin", (unsigned int*)&axis.Flags, ImPlotAxisFlags_LockMin);
EndDisabledControls(always_locked);
ImGui::SameLine();
BeginDisabledControls(axis.IsLockedMin() || always_locked);
if (ImGui::BeginMenu("Min Time")) {
if (ShowTimePicker("mintime", &tmin)) {
if (tmin >= tmax)
tmax = AddTime(tmin, ImPlotTimeUnit_S, 1);
axis.SetRange(tmin.ToDouble(),tmax.ToDouble());
}
ImGui::Separator();
if (ShowDatePicker("mindate",&axis.PickerLevel,&axis.PickerTimeMin,&tmin,&tmax)) {
tmin = CombineDateTime(axis.PickerTimeMin, tmin);
if (tmin >= tmax)
tmax = AddTime(tmin, ImPlotTimeUnit_S, 1);
axis.SetRange(tmin.ToDouble(), tmax.ToDouble());
}
ImGui::EndMenu();
}
EndDisabledControls(axis.IsLockedMin() || always_locked);
BeginDisabledControls(always_locked);
ImGui::CheckboxFlags("##LockMax", (unsigned int*)&axis.Flags, ImPlotAxisFlags_LockMax);
EndDisabledControls(always_locked);
ImGui::SameLine();
BeginDisabledControls(axis.IsLockedMax() || always_locked);
if (ImGui::BeginMenu("Max Time")) {
if (ShowTimePicker("maxtime", &tmax)) {
if (tmax <= tmin)
tmin = AddTime(tmax, ImPlotTimeUnit_S, -1);
axis.SetRange(tmin.ToDouble(),tmax.ToDouble());
}
ImGui::Separator();
if (ShowDatePicker("maxdate",&axis.PickerLevel,&axis.PickerTimeMax,&tmin,&tmax)) {
tmax = CombineDateTime(axis.PickerTimeMax, tmax);
if (tmax <= tmin)
tmin = AddTime(tmax, ImPlotTimeUnit_S, -1);
axis.SetRange(tmin.ToDouble(), tmax.ToDouble());
}
ImGui::EndMenu();
}
EndDisabledControls(axis.IsLockedMax() || always_locked);
}
else {
BeginDisabledControls(always_locked);
ImGui::CheckboxFlags("##LockMin", (unsigned int*)&axis.Flags, ImPlotAxisFlags_LockMin);
EndDisabledControls(always_locked);
ImGui::SameLine();
BeginDisabledControls(axis.IsLockedMin() || always_locked);
double temp_min = axis.Range.Min;
if (DragFloat("Min", &temp_min, (float)drag_speed, -HUGE_VAL, axis.Range.Max - DBL_EPSILON)) {
axis.SetMin(temp_min,true);
if (equal_axis != NULL)
equal_axis->SetAspect(axis.GetAspect());
}
EndDisabledControls(axis.IsLockedMin() || always_locked);
BeginDisabledControls(always_locked);
ImGui::CheckboxFlags("##LockMax", (unsigned int*)&axis.Flags, ImPlotAxisFlags_LockMax);
EndDisabledControls(always_locked);
ImGui::SameLine();
BeginDisabledControls(axis.IsLockedMax() || always_locked);
double temp_max = axis.Range.Max;
if (DragFloat("Max", &temp_max, (float)drag_speed, axis.Range.Min + DBL_EPSILON, HUGE_VAL)) {
axis.SetMax(temp_max,true);
if (equal_axis != NULL)
equal_axis->SetAspect(axis.GetAspect());
}
EndDisabledControls(axis.IsLockedMax() || always_locked);
}
ImGui::Separator();
ImGui::CheckboxFlags("Auto-Fit",(unsigned int*)&axis.Flags, ImPlotAxisFlags_AutoFit);
// TODO
// BeginDisabledControls(axis.IsTime() && time_allowed);
// ImGui::CheckboxFlags("Log Scale",(unsigned int*)&axis.Flags, ImPlotAxisFlags_LogScale);
// EndDisabledControls(axis.IsTime() && time_allowed);
// if (time_allowed) {
// BeginDisabledControls(axis.IsLog() || axis.IsSymLog());
// ImGui::CheckboxFlags("Time",(unsigned int*)&axis.Flags, ImPlotAxisFlags_Time);
// EndDisabledControls(axis.IsLog() || axis.IsSymLog());
// }
ImGui::Separator();
ImGui::CheckboxFlags("Invert",(unsigned int*)&axis.Flags, ImPlotAxisFlags_Invert);
ImGui::CheckboxFlags("Opposite",(unsigned int*)&axis.Flags, ImPlotAxisFlags_Opposite);
ImGui::Separator();
BeginDisabledControls(axis.LabelOffset == -1);
if (ImGui::Checkbox("Label", &label))
ImFlipFlag(axis.Flags, ImPlotAxisFlags_NoLabel);
EndDisabledControls(axis.LabelOffset == -1);
if (ImGui::Checkbox("Grid Lines", &grid))
ImFlipFlag(axis.Flags, ImPlotAxisFlags_NoGridLines);
if (ImGui::Checkbox("Tick Marks", &ticks))
ImFlipFlag(axis.Flags, ImPlotAxisFlags_NoTickMarks);
if (ImGui::Checkbox("Tick Labels", &labels))
ImFlipFlag(axis.Flags, ImPlotAxisFlags_NoTickLabels);
}
bool ShowLegendContextMenu(ImPlotLegend& legend, bool visible) {
const float s = ImGui::GetFrameHeight();
bool ret = false;
if (ImGui::Checkbox("Show",&visible))
ret = true;
if (legend.CanGoInside)
ImGui::CheckboxFlags("Outside",(unsigned int*)&legend.Flags, ImPlotLegendFlags_Outside);
if (ImGui::RadioButton("H", ImHasFlag(legend.Flags, ImPlotLegendFlags_Horizontal)))
legend.Flags |= ImPlotLegendFlags_Horizontal;
ImGui::SameLine();
if (ImGui::RadioButton("V", !ImHasFlag(legend.Flags, ImPlotLegendFlags_Horizontal)))
legend.Flags &= ~ImPlotLegendFlags_Horizontal;
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2,2));
if (ImGui::Button("NW",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_NorthWest; } ImGui::SameLine();
if (ImGui::Button("N", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_North; } ImGui::SameLine();
if (ImGui::Button("NE",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_NorthEast; }
if (ImGui::Button("W", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_West; } ImGui::SameLine();
if (ImGui::InvisibleButton("C", ImVec2(1.5f*s,s))) { } ImGui::SameLine();
if (ImGui::Button("E", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_East; }
if (ImGui::Button("SW",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_SouthWest; } ImGui::SameLine();
if (ImGui::Button("S", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_South; } ImGui::SameLine();
if (ImGui::Button("SE",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_SouthEast; }
ImGui::PopStyleVar();
return ret;
}
void ShowSubplotsContextMenu(ImPlotSubplot& subplot) {
if ((ImGui::BeginMenu("Linking"))) {
if (ImGui::MenuItem("Link Rows",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkRows)))
ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkRows);
if (ImGui::MenuItem("Link Cols",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkCols)))
ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkCols);
if (ImGui::MenuItem("Link All X",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllX)))
ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllX);
if (ImGui::MenuItem("Link All Y",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllY)))
ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllY);
ImGui::EndMenu();
}
if ((ImGui::BeginMenu("Settings"))) {
BeginDisabledControls(!subplot.HasTitle);
if (ImGui::MenuItem("Title",NULL,subplot.HasTitle && !ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoTitle)))
ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoTitle);
EndDisabledControls(!subplot.HasTitle);
if (ImGui::MenuItem("Resizable",NULL,!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoResize)))
ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoResize);
if (ImGui::MenuItem("Align",NULL,!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoAlign)))
ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoAlign);
if (ImGui::MenuItem("Share Items",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems)))
ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems);
ImGui::EndMenu();
}
}
void ShowPlotContextMenu(ImPlotPlot& plot) {
const bool owns_legend = GImPlot->CurrentItems == &plot.Items;
const bool equal = ImHasFlag(plot.Flags, ImPlotFlags_Equal);
char buf[16] = {};
for (int i = 0; i < IMPLOT_NUM_X_AXES; i++) {
ImPlotAxis& x_axis = plot.XAxis(i);
if (!x_axis.Enabled || !x_axis.HasMenus())
continue;
ImGui::PushID(i);
ImFormatString(buf, sizeof(buf) - 1, i == 0 ? "X-Axis" : "X-Axis %d", i + 1);
if (ImGui::BeginMenu(x_axis.HasLabel() ? plot.GetAxisLabel(x_axis) : buf)) {
ShowAxisContextMenu(x_axis, equal ? x_axis.OrthoAxis : NULL, false);
ImGui::EndMenu();
}
ImGui::PopID();
}
for (int i = 0; i < IMPLOT_NUM_Y_AXES; i++) {
ImPlotAxis& y_axis = plot.YAxis(i);
if (!y_axis.Enabled || !y_axis.HasMenus())
continue;
ImGui::PushID(i);
ImFormatString(buf, sizeof(buf) - 1, i == 0 ? "Y-Axis" : "Y-Axis %d", i + 1);
if (ImGui::BeginMenu(y_axis.HasLabel() ? plot.GetAxisLabel(y_axis) : buf)) {
ShowAxisContextMenu(y_axis, equal ? y_axis.OrthoAxis : NULL, false);
ImGui::EndMenu();
}
ImGui::PopID();
}
ImGui::Separator();
if (!ImHasFlag(GImPlot->CurrentItems->Legend.Flags, ImPlotLegendFlags_NoMenus)) {
if ((ImGui::BeginMenu("Legend"))) {
if (owns_legend) {
if (ShowLegendContextMenu(plot.Items.Legend, !ImHasFlag(plot.Flags, ImPlotFlags_NoLegend)))
ImFlipFlag(plot.Flags, ImPlotFlags_NoLegend);
}
else if (GImPlot->CurrentSubplot != NULL) {
if (ShowLegendContextMenu(GImPlot->CurrentSubplot->Items.Legend, !ImHasFlag(GImPlot->CurrentSubplot->Flags, ImPlotSubplotFlags_NoLegend)))
ImFlipFlag(GImPlot->CurrentSubplot->Flags, ImPlotSubplotFlags_NoLegend);
}
ImGui::EndMenu();
}
}
if ((ImGui::BeginMenu("Settings"))) {
if (ImGui::MenuItem("Equal", NULL, ImHasFlag(plot.Flags, ImPlotFlags_Equal)))
ImFlipFlag(plot.Flags, ImPlotFlags_Equal);
if (ImGui::MenuItem("Box Select",NULL,!ImHasFlag(plot.Flags, ImPlotFlags_NoBoxSelect)))
ImFlipFlag(plot.Flags, ImPlotFlags_NoBoxSelect);
BeginDisabledControls(plot.TitleOffset == -1);
if (ImGui::MenuItem("Title",NULL,plot.HasTitle()))
ImFlipFlag(plot.Flags, ImPlotFlags_NoTitle);
EndDisabledControls(plot.TitleOffset == -1);
if (ImGui::MenuItem("Mouse Position",NULL,!ImHasFlag(plot.Flags, ImPlotFlags_NoMouseText)))
ImFlipFlag(plot.Flags, ImPlotFlags_NoMouseText);
if (ImGui::MenuItem("Crosshairs",NULL,ImHasFlag(plot.Flags, ImPlotFlags_Crosshairs)))
ImFlipFlag(plot.Flags, ImPlotFlags_Crosshairs);
ImGui::EndMenu();
}
if (GImPlot->CurrentSubplot != NULL && !ImHasFlag(GImPlot->CurrentPlot->Flags, ImPlotSubplotFlags_NoMenus)) {
ImGui::Separator();
if ((ImGui::BeginMenu("Subplots"))) {
ShowSubplotsContextMenu(*GImPlot->CurrentSubplot);
ImGui::EndMenu();
}
}
}
//-----------------------------------------------------------------------------
// Axis Utils
//-----------------------------------------------------------------------------
static inline int AxisPrecision(const ImPlotAxis& axis) {
const double range = axis.Ticker.TickCount() > 1 ? (axis.Ticker.Ticks[1].PlotPos - axis.Ticker.Ticks[0].PlotPos) : axis.Range.Size();
return Precision(range);
}
static inline double RoundAxisValue(const ImPlotAxis& axis, double value) {
return RoundTo(value, AxisPrecision(axis));
}
void LabelAxisValue(const ImPlotAxis& axis, double value, char* buff, int size, bool round) {
ImPlotContext& gp = *GImPlot;
// TODO: We shouldn't explicitly check that the axis is Time here. Ideally,
// Formatter_Time would handle the formatting for us, but the code below
// needs additional arguments which are not currently available in ImPlotFormatter
if (axis.Locator == Locator_Time) {
ImPlotTimeUnit unit = axis.Vertical
? GetUnitForRange(axis.Range.Size() / (gp.CurrentPlot->PlotRect.GetHeight() / 100)) // TODO: magic value!
: GetUnitForRange(axis.Range.Size() / (gp.CurrentPlot->PlotRect.GetWidth() / 100)); // TODO: magic value!
FormatDateTime(ImPlotTime::FromDouble(value), buff, size, GetDateTimeFmt(TimeFormatMouseCursor, unit));
}
else {
if (round)
value = RoundAxisValue(axis, value);
axis.Formatter(value, buff, size, axis.FormatterData);
}
}
void UpdateAxisColors(ImPlotAxis& axis) {
const ImVec4 col_grid = GetStyleColorVec4(ImPlotCol_AxisGrid);
axis.ColorMaj = ImGui::GetColorU32(col_grid);
axis.ColorMin = ImGui::GetColorU32(col_grid*ImVec4(1,1,1,GImPlot->Style.MinorAlpha));
axis.ColorTick = GetStyleColorU32(ImPlotCol_AxisTick);
axis.ColorTxt = GetStyleColorU32(ImPlotCol_AxisText);
axis.ColorBg = GetStyleColorU32(ImPlotCol_AxisBg);
axis.ColorHov = GetStyleColorU32(ImPlotCol_AxisBgHovered);
axis.ColorAct = GetStyleColorU32(ImPlotCol_AxisBgActive);
// axis.ColorHiLi = IM_COL32_BLACK_TRANS;
}
void PadAndDatumAxesX(ImPlotPlot& plot, float& pad_T, float& pad_B, ImPlotAlignmentData* align) {
ImPlotContext& gp = *GImPlot;
const float T = ImGui::GetTextLineHeight();
const float P = gp.Style.LabelPadding.y;
const float K = gp.Style.MinorTickLen.x;
int count_T = 0;
int count_B = 0;
float last_T = plot.AxesRect.Min.y;
float last_B = plot.AxesRect.Max.y;
for (int i = IMPLOT_NUM_X_AXES; i-- > 0;) { // FYI: can iterate forward
ImPlotAxis& axis = plot.XAxis(i);
if (!axis.Enabled)
continue;
const bool label = axis.HasLabel();
const bool ticks = axis.HasTickLabels();
const bool opp = axis.IsOpposite();
const bool time = axis.Scale == ImPlotScale_Time;
if (opp) {
if (count_T++ > 0)
pad_T += K + P;
if (label)
pad_T += T + P;
if (ticks)
pad_T += ImMax(T, axis.Ticker.MaxSize.y) + P + (time ? T + P : 0);
axis.Datum1 = plot.CanvasRect.Min.y + pad_T;
axis.Datum2 = last_T;
last_T = axis.Datum1;
}
else {
if (count_B++ > 0)
pad_B += K + P;
if (label)
pad_B += T + P;
if (ticks)
pad_B += ImMax(T, axis.Ticker.MaxSize.y) + P + (time ? T + P : 0);
axis.Datum1 = plot.CanvasRect.Max.y - pad_B;
axis.Datum2 = last_B;
last_B = axis.Datum1;
}
}
if (align) {
count_T = count_B = 0;
float delta_T, delta_B;
align->Update(pad_T,pad_B,delta_T,delta_B);
for (int i = IMPLOT_NUM_X_AXES; i-- > 0;) {
ImPlotAxis& axis = plot.XAxis(i);
if (!axis.Enabled)
continue;
if (axis.IsOpposite()) {
axis.Datum1 += delta_T;
axis.Datum2 += count_T++ > 1 ? delta_T : 0;
}
else {
axis.Datum1 -= delta_B;
axis.Datum2 -= count_B++ > 1 ? delta_B : 0;
}
}
}
}
void PadAndDatumAxesY(ImPlotPlot& plot, float& pad_L, float& pad_R, ImPlotAlignmentData* align) {
// [ pad_L ] [ pad_R ]
// .................CanvasRect................
// :TPWPK.PTPWP _____PlotRect____ PWPTP.KPWPT:
// :A # |- A # |- -| # A -| # A:
// :X | X | | X | x:
// :I # |- I # |- -| # I -| # I:
// :S | S | | S | S:
// :3 # |- 0 # |-_______________-| # 1 -| # 2:
// :.........................................:
//
// T = text height
// P = label padding
// K = minor tick length
// W = label width
ImPlotContext& gp = *GImPlot;
const float T = ImGui::GetTextLineHeight();
const float P = gp.Style.LabelPadding.x;
const float K = gp.Style.MinorTickLen.y;
int count_L = 0;
int count_R = 0;
float last_L = plot.AxesRect.Min.x;
float last_R = plot.AxesRect.Max.x;
for (int i = IMPLOT_NUM_Y_AXES; i-- > 0;) { // FYI: can iterate forward
ImPlotAxis& axis = plot.YAxis(i);
if (!axis.Enabled)
continue;
const bool label = axis.HasLabel();
const bool ticks = axis.HasTickLabels();
const bool opp = axis.IsOpposite();
if (opp) {
if (count_R++ > 0)
pad_R += K + P;
if (label)
pad_R += T + P;
if (ticks)
pad_R += axis.Ticker.MaxSize.x + P;
axis.Datum1 = plot.CanvasRect.Max.x - pad_R;
axis.Datum2 = last_R;
last_R = axis.Datum1;
}
else {
if (count_L++ > 0)
pad_L += K + P;
if (label)
pad_L += T + P;
if (ticks)
pad_L += axis.Ticker.MaxSize.x + P;
axis.Datum1 = plot.CanvasRect.Min.x + pad_L;
axis.Datum2 = last_L;
last_L = axis.Datum1;
}
}
plot.PlotRect.Min.x = plot.CanvasRect.Min.x + pad_L;
plot.PlotRect.Max.x = plot.CanvasRect.Max.x - pad_R;
if (align) {
count_L = count_R = 0;
float delta_L, delta_R;
align->Update(pad_L,pad_R,delta_L,delta_R);
for (int i = IMPLOT_NUM_Y_AXES; i-- > 0;) {
ImPlotAxis& axis = plot.YAxis(i);
if (!axis.Enabled)
continue;
if (axis.IsOpposite()) {
axis.Datum1 -= delta_R;
axis.Datum2 -= count_R++ > 1 ? delta_R : 0;
}
else {
axis.Datum1 += delta_L;
axis.Datum2 += count_L++ > 1 ? delta_L : 0;
}
}
}
}
//-----------------------------------------------------------------------------
// RENDERING
//-----------------------------------------------------------------------------
static inline void RenderGridLinesX(ImDrawList& DrawList, const ImPlotTicker& ticker, const ImRect& rect, ImU32 col_maj, ImU32 col_min, float size_maj, float size_min) {
const float density = ticker.TickCount() / rect.GetWidth();
ImVec4 col_min4 = ImGui::ColorConvertU32ToFloat4(col_min);
col_min4.w *= ImClamp(ImRemap(density, 0.1f, 0.2f, 1.0f, 0.0f), 0.0f, 1.0f);
col_min = ImGui::ColorConvertFloat4ToU32(col_min4);
for (int t = 0; t < ticker.TickCount(); t++) {
const ImPlotTick& xt = ticker.Ticks[t];
if (xt.PixelPos < rect.Min.x || xt.PixelPos > rect.Max.x)
continue;
if (xt.Level == 0) {
if (xt.Major)
DrawList.AddLine(ImVec2(xt.PixelPos, rect.Min.y), ImVec2(xt.PixelPos, rect.Max.y), col_maj, size_maj);
else if (density < 0.2f)
DrawList.AddLine(ImVec2(xt.PixelPos, rect.Min.y), ImVec2(xt.PixelPos, rect.Max.y), col_min, size_min);
}
}
}
static inline void RenderGridLinesY(ImDrawList& DrawList, const ImPlotTicker& ticker, const ImRect& rect, ImU32 col_maj, ImU32 col_min, float size_maj, float size_min) {
const float density = ticker.TickCount() / rect.GetHeight();
ImVec4 col_min4 = ImGui::ColorConvertU32ToFloat4(col_min);
col_min4.w *= ImClamp(ImRemap(density, 0.1f, 0.2f, 1.0f, 0.0f), 0.0f, 1.0f);
col_min = ImGui::ColorConvertFloat4ToU32(col_min4);
for (int t = 0; t < ticker.TickCount(); t++) {
const ImPlotTick& yt = ticker.Ticks[t];
if (yt.PixelPos < rect.Min.y || yt.PixelPos > rect.Max.y)
continue;
if (yt.Major)
DrawList.AddLine(ImVec2(rect.Min.x, yt.PixelPos), ImVec2(rect.Max.x, yt.PixelPos), col_maj, size_maj);
else if (density < 0.2f)
DrawList.AddLine(ImVec2(rect.Min.x, yt.PixelPos), ImVec2(rect.Max.x, yt.PixelPos), col_min, size_min);
}
}
static inline void RenderSelectionRect(ImDrawList& DrawList, const ImVec2& p_min, const ImVec2& p_max, const ImVec4& col) {
const ImU32 col_bg = ImGui::GetColorU32(col * ImVec4(1,1,1,0.25f));
const ImU32 col_bd = ImGui::GetColorU32(col);
DrawList.AddRectFilled(p_min, p_max, col_bg);
DrawList.AddRect(p_min, p_max, col_bd);
}
//-----------------------------------------------------------------------------
// Input Handling
//-----------------------------------------------------------------------------
static const float MOUSE_CURSOR_DRAG_THRESHOLD = 5.0f;
static const float BOX_SELECT_DRAG_THRESHOLD = 4.0f;
bool UpdateInput(ImPlotPlot& plot) {
bool changed = false;
ImPlotContext& gp = *GImPlot;
ImGuiIO& IO = ImGui::GetIO();
// BUTTON STATE -----------------------------------------------------------
const ImGuiButtonFlags plot_button_flags = ImGuiButtonFlags_AllowItemOverlap
| ImGuiButtonFlags_PressedOnClick
| ImGuiButtonFlags_PressedOnDoubleClick
| ImGuiButtonFlags_MouseButtonLeft
| ImGuiButtonFlags_MouseButtonRight
| ImGuiButtonFlags_MouseButtonMiddle;
const ImGuiButtonFlags axis_button_flags = ImGuiButtonFlags_FlattenChildren
| plot_button_flags;
const bool plot_clicked = ImGui::ButtonBehavior(plot.PlotRect,plot.ID,&plot.Hovered,&plot.Held,plot_button_flags);
ImGui::SetItemAllowOverlap();
if (plot_clicked) {
if (!ImHasFlag(plot.Flags, ImPlotFlags_NoBoxSelect) && IO.MouseClicked[gp.InputMap.Select] && ImHasFlag(IO.KeyMods, gp.InputMap.SelectMod)) {
plot.Selecting = true;
plot.SelectStart = IO.MousePos;
plot.SelectRect = ImRect(0,0,0,0);
}
if (IO.MouseDoubleClicked[gp.InputMap.Fit]) {
plot.FitThisFrame = true;
for (int i = 0; i < ImAxis_COUNT; ++i)
plot.Axes[i].FitThisFrame = true;
}
}
const bool can_pan = IO.MouseDown[gp.InputMap.Pan] && ImHasFlag(IO.KeyMods, gp.InputMap.PanMod);
plot.Held = plot.Held && can_pan;
bool x_click[IMPLOT_NUM_X_AXES] = {false};
bool x_held[IMPLOT_NUM_X_AXES] = {false};
bool x_hov[IMPLOT_NUM_X_AXES] = {false};
bool y_click[IMPLOT_NUM_Y_AXES] = {false};
bool y_held[IMPLOT_NUM_Y_AXES] = {false};
bool y_hov[IMPLOT_NUM_Y_AXES] = {false};
for (int i = 0; i < IMPLOT_NUM_X_AXES; ++i) {
ImPlotAxis& xax = plot.XAxis(i);
if (xax.Enabled) {
ImGui::KeepAliveID(xax.ID);
x_click[i] = ImGui::ButtonBehavior(xax.HoverRect,xax.ID,&xax.Hovered,&xax.Held,axis_button_flags);
if (x_click[i] && IO.MouseDoubleClicked[gp.InputMap.Fit])
plot.FitThisFrame = xax.FitThisFrame = true;
xax.Held = xax.Held && can_pan;
x_hov[i] = xax.Hovered || plot.Hovered;
x_held[i] = xax.Held || plot.Held;
}
}
for (int i = 0; i < IMPLOT_NUM_Y_AXES; ++i) {
ImPlotAxis& yax = plot.YAxis(i);
if (yax.Enabled) {
ImGui::KeepAliveID(yax.ID);
y_click[i] = ImGui::ButtonBehavior(yax.HoverRect,yax.ID,&yax.Hovered,&yax.Held,axis_button_flags);
if (y_click[i] && IO.MouseDoubleClicked[gp.InputMap.Fit])
plot.FitThisFrame = yax.FitThisFrame = true;
yax.Held = yax.Held && can_pan;
y_hov[i] = yax.Hovered || plot.Hovered;
y_held[i] = yax.Held || plot.Held;
}
}
// cancel due to DND activity
if (GImGui->DragDropActive || (IO.KeyMods == gp.InputMap.OverrideMod && gp.InputMap.OverrideMod != 0))
return false;
// STATE -------------------------------------------------------------------
const bool axis_equal = ImHasFlag(plot.Flags, ImPlotFlags_Equal);
const bool any_x_hov = plot.Hovered || AnyAxesHovered(&plot.Axes[ImAxis_X1], IMPLOT_NUM_X_AXES);
const bool any_x_held = plot.Held || AnyAxesHeld(&plot.Axes[ImAxis_X1], IMPLOT_NUM_X_AXES);
const bool any_y_hov = plot.Hovered || AnyAxesHovered(&plot.Axes[ImAxis_Y1], IMPLOT_NUM_Y_AXES);
const bool any_y_held = plot.Held || AnyAxesHeld(&plot.Axes[ImAxis_Y1], IMPLOT_NUM_Y_AXES);
const bool any_hov = any_x_hov || any_y_hov;
const bool any_held = any_x_held || any_y_held;
const ImVec2 select_drag = ImGui::GetMouseDragDelta(gp.InputMap.Select);
const ImVec2 pan_drag = ImGui::GetMouseDragDelta(gp.InputMap.Pan);
const float select_drag_sq = ImLengthSqr(select_drag);
const float pan_drag_sq = ImLengthSqr(pan_drag);
const bool selecting = plot.Selecting && select_drag_sq > MOUSE_CURSOR_DRAG_THRESHOLD;
const bool panning = any_held && pan_drag_sq > MOUSE_CURSOR_DRAG_THRESHOLD;
// CONTEXT MENU -----------------------------------------------------------
if (IO.MouseReleased[gp.InputMap.Menu] && !plot.ContextLocked)
gp.OpenContextThisFrame = true;
if (selecting || panning)
plot.ContextLocked = true;
else if (!(IO.MouseDown[gp.InputMap.Menu] || IO.MouseReleased[gp.InputMap.Menu]))
plot.ContextLocked = false;
// DRAG INPUT -------------------------------------------------------------
if (any_held && !plot.Selecting) {
int drag_direction = 0;
for (int i = 0; i < IMPLOT_NUM_X_AXES; i++) {
ImPlotAxis& x_axis = plot.XAxis(i);
if (x_held[i] && !x_axis.IsInputLocked()) {
drag_direction |= (1 << 1);
bool increasing = x_axis.IsInverted() ? IO.MouseDelta.x > 0 : IO.MouseDelta.x < 0;
if (IO.MouseDelta.x != 0 && !x_axis.IsPanLocked(increasing)) {
const double plot_l = x_axis.PixelsToPlot(plot.PlotRect.Min.x - IO.MouseDelta.x);
const double plot_r = x_axis.PixelsToPlot(plot.PlotRect.Max.x - IO.MouseDelta.x);
x_axis.SetMin(x_axis.IsInverted() ? plot_r : plot_l);
x_axis.SetMax(x_axis.IsInverted() ? plot_l : plot_r);
if (axis_equal && x_axis.OrthoAxis != NULL)
x_axis.OrthoAxis->SetAspect(x_axis.GetAspect());
changed = true;
}
}
}
for (int i = 0; i < IMPLOT_NUM_Y_AXES; i++) {
ImPlotAxis& y_axis = plot.YAxis(i);
if (y_held[i] && !y_axis.IsInputLocked()) {
drag_direction |= (1 << 2);
bool increasing = y_axis.IsInverted() ? IO.MouseDelta.y < 0 : IO.MouseDelta.y > 0;
if (IO.MouseDelta.y != 0 && !y_axis.IsPanLocked(increasing)) {
const double plot_t = y_axis.PixelsToPlot(plot.PlotRect.Min.y - IO.MouseDelta.y);
const double plot_b = y_axis.PixelsToPlot(plot.PlotRect.Max.y - IO.MouseDelta.y);
y_axis.SetMin(y_axis.IsInverted() ? plot_t : plot_b);
y_axis.SetMax(y_axis.IsInverted() ? plot_b : plot_t);
if (axis_equal && y_axis.OrthoAxis != NULL)
y_axis.OrthoAxis->SetAspect(y_axis.GetAspect());
changed = true;
}
}
}
if (IO.MouseDragMaxDistanceSqr[gp.InputMap.Pan] > MOUSE_CURSOR_DRAG_THRESHOLD) {
switch (drag_direction) {
case 0 : ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed); break;
case (1 << 1) : ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW); break;
case (1 << 2) : ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS); break;
default : ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll); break;
}
}
}
// SCROLL INPUT -----------------------------------------------------------
if (any_hov && IO.MouseWheel != 0 && ImHasFlag(IO.KeyMods, gp.InputMap.ZoomMod)) {
float zoom_rate = gp.InputMap.ZoomRate;
if (IO.MouseWheel > 0)
zoom_rate = (-zoom_rate) / (1.0f + (2.0f * zoom_rate));
ImVec2 rect_size = plot.PlotRect.GetSize();
float tx = ImRemap(IO.MousePos.x, plot.PlotRect.Min.x, plot.PlotRect.Max.x, 0.0f, 1.0f);
float ty = ImRemap(IO.MousePos.y, plot.PlotRect.Min.y, plot.PlotRect.Max.y, 0.0f, 1.0f);
for (int i = 0; i < IMPLOT_NUM_X_AXES; i++) {
ImPlotAxis& x_axis = plot.XAxis(i);
const bool equal_zoom = axis_equal && x_axis.OrthoAxis != NULL;
const bool equal_locked = (equal_zoom != false) && x_axis.OrthoAxis->IsInputLocked();
if (x_hov[i] && !x_axis.IsInputLocked() && !equal_locked) {
float correction = (plot.Hovered && equal_zoom) ? 0.5f : 1.0f;
const double plot_l = x_axis.PixelsToPlot(plot.PlotRect.Min.x - rect_size.x * tx * zoom_rate * correction);
const double plot_r = x_axis.PixelsToPlot(plot.PlotRect.Max.x + rect_size.x * (1 - tx) * zoom_rate * correction);
x_axis.SetMin(x_axis.IsInverted() ? plot_r : plot_l);
x_axis.SetMax(x_axis.IsInverted() ? plot_l : plot_r);
if (axis_equal && x_axis.OrthoAxis != NULL)
x_axis.OrthoAxis->SetAspect(x_axis.GetAspect());
changed = true;
}
}
for (int i = 0; i < IMPLOT_NUM_Y_AXES; i++) {
ImPlotAxis& y_axis = plot.YAxis(i);
const bool equal_zoom = axis_equal && y_axis.OrthoAxis != NULL;
const bool equal_locked = equal_zoom && y_axis.OrthoAxis->IsInputLocked();
if (y_hov[i] && !y_axis.IsInputLocked() && !equal_locked) {
float correction = (plot.Hovered && equal_zoom) ? 0.5f : 1.0f;
const double plot_t = y_axis.PixelsToPlot(plot.PlotRect.Min.y - rect_size.y * ty * zoom_rate * correction);
const double plot_b = y_axis.PixelsToPlot(plot.PlotRect.Max.y + rect_size.y * (1 - ty) * zoom_rate * correction);
y_axis.SetMin(y_axis.IsInverted() ? plot_t : plot_b);
y_axis.SetMax(y_axis.IsInverted() ? plot_b : plot_t);
if (axis_equal && y_axis.OrthoAxis != NULL)
y_axis.OrthoAxis->SetAspect(y_axis.GetAspect());
changed = true;
}
}
}
// BOX-SELECTION ----------------------------------------------------------
if (plot.Selecting) {
const ImVec2 d = plot.SelectStart - IO.MousePos;
const bool x_can_change = !ImHasFlag(IO.KeyMods,gp.InputMap.SelectHorzMod) && ImFabs(d.x) > 2;
const bool y_can_change = !ImHasFlag(IO.KeyMods,gp.InputMap.SelectVertMod) && ImFabs(d.y) > 2;
// confirm
if (IO.MouseReleased[gp.InputMap.Select]) {
for (int i = 0; i < IMPLOT_NUM_X_AXES; i++) {
ImPlotAxis& x_axis = plot.XAxis(i);
if (!x_axis.IsInputLocked() && x_can_change) {
const double p1 = x_axis.PixelsToPlot(plot.SelectStart.x);
const double p2 = x_axis.PixelsToPlot(IO.MousePos.x);
x_axis.SetMin(ImMin(p1, p2));
x_axis.SetMax(ImMax(p1, p2));
changed = true;
}
}
for (int i = 0; i < IMPLOT_NUM_Y_AXES; i++) {
ImPlotAxis& y_axis = plot.YAxis(i);
if (!y_axis.IsInputLocked() && y_can_change) {
const double p1 = y_axis.PixelsToPlot(plot.SelectStart.y);
const double p2 = y_axis.PixelsToPlot(IO.MousePos.y);
y_axis.SetMin(ImMin(p1, p2));
y_axis.SetMax(ImMax(p1, p2));
changed = true;
}
}
if (x_can_change || y_can_change || (ImHasFlag(IO.KeyMods,gp.InputMap.SelectHorzMod) && ImHasFlag(IO.KeyMods,gp.InputMap.SelectVertMod)))
gp.OpenContextThisFrame = false;
plot.Selected = plot.Selecting = false;
}
// cancel
else if (IO.MouseReleased[gp.InputMap.SelectCancel]) {
plot.Selected = plot.Selecting = false;
gp.OpenContextThisFrame = false;
}
else if (ImLengthSqr(d) > BOX_SELECT_DRAG_THRESHOLD) {
// bad selection
if (plot.IsInputLocked()) {
ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed);
gp.OpenContextThisFrame = false;
plot.Selected = false;
}
else {
// TODO: Handle only min or max locked cases
const bool full_width = ImHasFlag(IO.KeyMods, gp.InputMap.SelectHorzMod) || AllAxesInputLocked(&plot.Axes[ImAxis_X1], IMPLOT_NUM_X_AXES);
const bool full_height = ImHasFlag(IO.KeyMods, gp.InputMap.SelectVertMod) || AllAxesInputLocked(&plot.Axes[ImAxis_Y1], IMPLOT_NUM_Y_AXES);
plot.SelectRect.Min.x = full_width ? plot.PlotRect.Min.x : ImMin(plot.SelectStart.x, IO.MousePos.x);
plot.SelectRect.Max.x = full_width ? plot.PlotRect.Max.x : ImMax(plot.SelectStart.x, IO.MousePos.x);
plot.SelectRect.Min.y = full_height ? plot.PlotRect.Min.y : ImMin(plot.SelectStart.y, IO.MousePos.y);
plot.SelectRect.Max.y = full_height ? plot.PlotRect.Max.y : ImMax(plot.SelectStart.y, IO.MousePos.y);
plot.SelectRect.Min -= plot.PlotRect.Min;
plot.SelectRect.Max -= plot.PlotRect.Min;
plot.Selected = true;
}
}
else {
plot.Selected = false;
}
}
return changed;
}
//-----------------------------------------------------------------------------
// Next Plot Data (Legacy)
//-----------------------------------------------------------------------------
void ApplyNextPlotData(ImAxis idx) {
ImPlotContext& gp = *GImPlot;
ImPlotPlot& plot = *GImPlot->CurrentPlot;
ImPlotAxis& axis = plot.Axes[idx];
if (!axis.Enabled)
return;
double* npd_lmin = gp.NextPlotData.LinkedMin[idx];
double* npd_lmax = gp.NextPlotData.LinkedMax[idx];
bool npd_rngh = gp.NextPlotData.HasRange[idx];
ImPlotCond npd_rngc = gp.NextPlotData.RangeCond[idx];
ImPlotRange npd_rngv = gp.NextPlotData.Range[idx];
axis.LinkedMin = npd_lmin;
axis.LinkedMax = npd_lmax;
axis.PullLinks();
if (npd_rngh) {
if (!plot.Initialized || npd_rngc == ImPlotCond_Always)
axis.SetRange(npd_rngv);
}
axis.HasRange = npd_rngh;
axis.RangeCond = npd_rngc;
}
//-----------------------------------------------------------------------------
// Setup
//-----------------------------------------------------------------------------
void SetupAxis(ImAxis idx, const char* label, ImPlotAxisFlags flags) {
IM_ASSERT_USER_ERROR(GImPlot->CurrentPlot != NULL && !GImPlot->CurrentPlot->SetupLocked,
"Setup needs to be called after BeginPlot and before any setup locking functions (e.g. PlotX)!");
// get plot and axis
ImPlotPlot& plot = *GImPlot->CurrentPlot;
ImPlotAxis& axis = plot.Axes[idx];
// set ID
axis.ID = plot.ID + idx + 1;
// check and set flags
if (plot.JustCreated || flags != axis.PreviousFlags)
axis.Flags = flags;
axis.PreviousFlags = flags;
// enable axis
axis.Enabled = true;
// set label
plot.SetAxisLabel(axis,label);
// cache colors
UpdateAxisColors(axis);
}
void SetupAxisLimits(ImAxis idx, double min_lim, double max_lim, ImPlotCond cond) {
IM_ASSERT_USER_ERROR(GImPlot->CurrentPlot != NULL && !GImPlot->CurrentPlot->SetupLocked,
"Setup needs to be called after BeginPlot and before any setup locking functions (e.g. PlotX)!"); // get plot and axis
ImPlotPlot& plot = *GImPlot->CurrentPlot;
ImPlotAxis& axis = plot.Axes[idx];
IM_ASSERT_USER_ERROR(axis.Enabled, "Axis is not enabled! Did you forget to call SetupAxis()?");
if (!plot.Initialized || cond == ImPlotCond_Always)
axis.SetRange(min_lim, max_lim);
axis.HasRange = true;
axis.RangeCond = cond;
}
void SetupAxisFormat(ImAxis idx, const char* fmt) {
IM_ASSERT_USER_ERROR(GImPlot->CurrentPlot != NULL && !GImPlot->CurrentPlot->SetupLocked,
"Setup needs to be called after BeginPlot and before any setup locking functions (e.g. PlotX)!");
ImPlotPlot& plot = *GImPlot->CurrentPlot;
ImPlotAxis& axis = plot.Axes[idx];
IM_ASSERT_USER_ERROR(axis.Enabled, "Axis is not enabled! Did you forget to call SetupAxis()?");
axis.HasFormatSpec = fmt != NULL;
if (fmt != NULL)
ImStrncpy(axis.FormatSpec,fmt,sizeof(axis.FormatSpec));
}
void SetupAxisLinks(ImAxis idx, double* min_lnk, double* max_lnk) {
IM_ASSERT_USER_ERROR(GImPlot->CurrentPlot != NULL && !GImPlot->CurrentPlot->SetupLocked,
"Setup needs to be called after BeginPlot and before any setup locking functions (e.g. PlotX)!");
ImPlotPlot& plot = *GImPlot->CurrentPlot;
ImPlotAxis& axis = plot.Axes[idx];
IM_ASSERT_USER_ERROR(axis.Enabled, "Axis is not enabled! Did you forget to call SetupAxis()?");
axis.LinkedMin = min_lnk;
axis.LinkedMax = max_lnk;
axis.PullLinks();
}
void SetupAxisFormat(ImAxis idx, ImPlotFormatter formatter, void* data) {
IM_ASSERT_USER_ERROR(GImPlot->CurrentPlot != NULL && !GImPlot->CurrentPlot->SetupLocked,
"Setup needs to be called after BeginPlot and before any setup locking functions (e.g. PlotX)!");
ImPlotPlot& plot = *GImPlot->CurrentPlot;
ImPlotAxis& axis = plot.Axes[idx];
IM_ASSERT_USER_ERROR(axis.Enabled, "Axis is not enabled! Did you forget to call SetupAxis()?");
axis.Formatter = formatter;
axis.FormatterData = data;
}
void SetupAxisTicks(ImAxis idx, const double* values, int n_ticks, const char* const labels[], bool show_default) {
IM_ASSERT_USER_ERROR(GImPlot->CurrentPlot != NULL && !GImPlot->