5724 lines
259 KiB
C++
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;
|
||
|