1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-23 02:38:53 -05:00
This commit is contained in:
Evan Pezent 2020-05-11 00:56:24 -05:00
commit 703a72850a
3 changed files with 894 additions and 506 deletions

1129
implot.cpp

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
// ImPlot v0.1 WIP // ImPlot v0.2 WIP
#pragma once #pragma once
#include <imgui.h> #include <imgui.h>
@ -41,12 +41,14 @@ enum ImPlotFlags_ {
ImPlotFlags_Legend = 1 << 1, // a legend will be displayed in the top-left ImPlotFlags_Legend = 1 << 1, // a legend will be displayed in the top-left
ImPlotFlags_Highlight = 1 << 2, // plot items will be highlighted when their legend entry is hovered ImPlotFlags_Highlight = 1 << 2, // plot items will be highlighted when their legend entry is hovered
ImPlotFlags_Selection = 1 << 3, // the user will be able to box-select with right-mouse ImPlotFlags_Selection = 1 << 3, // the user will be able to box-select with right-mouse
ImPlotFlags_PixelQuery = 1 << 4, // query ranges will not change their pixel position if the plot is scrolled/zoomed ImPlotFlags_Query = 1 << 4, // the user will be able to draw query rects with middle-mouse
ImPlotFlags_ContextMenu = 1 << 5, // the user will be able to open a context menu with double-right click ImPlotFlags_ContextMenu = 1 << 5, // the user will be able to open a context menu with double-right click
ImPlotFlags_Crosshairs = 1 << 6, // the default mouse cursor will be replaced with a crosshair when hovered ImPlotFlags_Crosshairs = 1 << 6, // the default mouse cursor will be replaced with a crosshair when hovered
ImPlotFlags_CullData = 1 << 7, // plot data outside the plot area will be culled from rendering ImPlotFlags_CullData = 1 << 7, // plot data outside the plot area will be culled from rendering
ImPlotFlags_AntiAliased = 1 << 8, // lines and fills will be anti-aliased (not recommended) ImPlotFlags_AntiAliased = 1 << 8, // lines and fills will be anti-aliased (not recommended)
ImPlotFlags_NoChild = 1 << 9, // a child window region will not be used to capture mouse scroll (can boost performance for single ImGui window applications) ImPlotFlags_NoChild = 1 << 9, // a child window region will not be used to capture mouse scroll (can boost performance for single ImGui window applications)
ImPlotFlags_YAxis2 = 1 << 10, // enable a 2nd y axis
ImPlotFlags_YAxis3 = 1 << 11, // enable a 3rd y axis
ImPlotFlags_Default = ImPlotFlags_MousePos | ImPlotFlags_Legend | ImPlotFlags_Highlight | ImPlotFlags_Selection | ImPlotFlags_ContextMenu | ImPlotFlags_CullData ImPlotFlags_Default = ImPlotFlags_MousePos | ImPlotFlags_Legend | ImPlotFlags_Highlight | ImPlotFlags_Selection | ImPlotFlags_ContextMenu | ImPlotFlags_CullData
}; };
@ -61,7 +63,8 @@ enum ImAxisFlags_ {
ImAxisFlags_Adaptive = 1 << 6, // grid divisions will adapt to the current pixel size the axis ImAxisFlags_Adaptive = 1 << 6, // grid divisions will adapt to the current pixel size the axis
ImAxisFlags_LogScale = 1 << 7, // a logartithmic (base 10) axis scale will be used ImAxisFlags_LogScale = 1 << 7, // a logartithmic (base 10) axis scale will be used
ImAxisFlags_Scientific = 1 << 8, // scientific notation will be used for tick labels if displayed (WIP, not very good yet) ImAxisFlags_Scientific = 1 << 8, // scientific notation will be used for tick labels if displayed (WIP, not very good yet)
ImAxisFlags_Default = ImAxisFlags_GridLines | ImAxisFlags_TickMarks | ImAxisFlags_TickLabels | ImAxisFlags_Adaptive ImAxisFlags_Default = ImAxisFlags_GridLines | ImAxisFlags_TickMarks | ImAxisFlags_TickLabels | ImAxisFlags_Adaptive,
ImAxisFlags_Auxiliary = ImAxisFlags_Default & ~ImAxisFlags_GridLines,
}; };
// Plot styling colors // Plot styling colors
@ -74,13 +77,16 @@ enum ImPlotCol_ {
ImPlotCol_FrameBg, // plot frame background color (defaults to ImGuiCol_FrameBg) ImPlotCol_FrameBg, // plot frame background color (defaults to ImGuiCol_FrameBg)
ImPlotCol_PlotBg, // plot area background color (defaults to ImGuiCol_WindowBg) ImPlotCol_PlotBg, // plot area background color (defaults to ImGuiCol_WindowBg)
ImPlotCol_PlotBorder, // plot area border color (defaults to ImGuiCol_Text) ImPlotCol_PlotBorder, // plot area border color (defaults to ImGuiCol_Text)
ImPlotCol_XAxis, // x-axis grid/label color (defaults to ImGuiCol_Text) ImPlotCol_XAxis, // x-axis grid/label color (defaults to 25% ImGuiCol_Text)
ImPlotCol_YAxis, // x-axis grid/label color (defaults to ImGuiCol_Text) ImPlotCol_YAxis, // y-axis grid/label color (defaults to 25% ImGuiCol_Text)
ImPlotCol_YAxis2, // 2nd y-axis grid/label color (defaults to 25% ImGuiCol_Text)
ImPlotCol_YAxis3, // 3rd y-axis grid/label color (defaults to 25% ImGuiCol_Text)
ImPlotCol_Selection, // box-selection color (defaults to yellow) ImPlotCol_Selection, // box-selection color (defaults to yellow)
ImPlotCol_Query, // box-query color (defaults to green) ImPlotCol_Query, // box-query color (defaults to green)
ImPlotCol_COUNT ImPlotCol_COUNT
}; };
// Plot styling variables
enum ImPlotStyleVar_ { enum ImPlotStyleVar_ {
ImPlotStyleVar_LineWeight, // float, line weight in pixels ImPlotStyleVar_LineWeight, // float, line weight in pixels
ImPlotStyleVar_Marker, // int, marker specification ImPlotStyleVar_Marker, // int, marker specification
@ -107,11 +113,19 @@ enum ImMarker_ {
ImMarker_Asterisk = 1 << 10, // a asterisk marker will be rendered at each point (not filled) ImMarker_Asterisk = 1 << 10, // a asterisk marker will be rendered at each point (not filled)
}; };
/// Plot range utility struct // A range defined by a min/max value. Used for plot axes ranges.
struct ImPlotRange { struct ImPlotRange {
float XMin, XMax, YMin, YMax; float Min, Max;
ImPlotRange(); ImPlotRange();
bool Contains(const ImVec2& p); bool Contains(float value) const;
float Size() const;
};
// Combination of two ranges for X and Y axes.
struct ImPlotLimits {
ImPlotRange X, Y;
ImPlotLimits();
bool Contains(const ImVec2& p) const;
}; };
// Plot style structure // Plot style structure
@ -137,15 +151,16 @@ namespace ImGui {
// be called, e.g. "if (BeginPlot(...)) { ... EndPlot(); }"". #title_id must // be called, e.g. "if (BeginPlot(...)) { ... EndPlot(); }"". #title_id must
// be unique. If you need to avoid ID collisions or don't want to display a // be unique. If you need to avoid ID collisions or don't want to display a
// title in the plot, use double hashes (e.g. "MyPlot##Hidden"). If #x_label // title in the plot, use double hashes (e.g. "MyPlot##Hidden"). If #x_label
// and/or #y_label are provided, axes labels will be displayed. Flags are only // and/or #y_label are provided, axes labels will be displayed.
// set ONCE during the first call to BeginPlot.
bool BeginPlot(const char* title_id, bool BeginPlot(const char* title_id,
const char* x_label = NULL, const char* x_label = NULL,
const char* y_label = NULL, const char* y_label = NULL,
const ImVec2& size = ImVec2(-1,-1), const ImVec2& size = ImVec2(-1,-1),
ImPlotFlags flags = ImPlotFlags_Default, ImPlotFlags flags = ImPlotFlags_Default,
ImAxisFlags x_flags = ImAxisFlags_Default, ImAxisFlags x_flags = ImAxisFlags_Default,
ImAxisFlags y_flags = ImAxisFlags_Default); ImAxisFlags y_flags = ImAxisFlags_Default,
ImAxisFlags y2_flags = ImAxisFlags_Auxiliary,
ImAxisFlags y3_flags = ImAxisFlags_Auxiliary);
// Only call EndPlot() if BeginPlot() returns true! Typically called at the end // Only call EndPlot() if BeginPlot() returns true! Typically called at the end
// of an if statement conditioned on BeginPlot(). // of an if statement conditioned on BeginPlot().
void EndPlot(); void EndPlot();
@ -154,7 +169,7 @@ void EndPlot();
// Plot Items // Plot Items
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Plots a standard 2D line and/or scatter plot . // Plots a standard 2D line and/or scatter plot.
void Plot(const char* label_id, const float* values, int count, int offset = 0, int stride = sizeof(float)); void Plot(const char* label_id, const float* values, int count, int offset = 0, int stride = sizeof(float));
void Plot(const char* label_id, const float* xs, const float* ys, int count, int offset = 0, int stride = sizeof(float)); void Plot(const char* label_id, const float* xs, const float* ys, int count, int offset = 0, int stride = sizeof(float));
void Plot(const char* label_id, const ImVec2* data, int count, int offset = 0); void Plot(const char* label_id, const ImVec2* data, int count, int offset = 0);
@ -185,14 +200,14 @@ void PlotDigital(const char* label_id, ImVec2 (*getter)(void* data, int idx), vo
/// Returns true if the plot area in the current or most recent plot is hovered. /// Returns true if the plot area in the current or most recent plot is hovered.
bool IsPlotHovered(); bool IsPlotHovered();
/// Returns the mouse position in x,y coordinates of the current or most recent plot. /// Returns the mouse position in x,y coordinates of the current or most recent plot. A negative y_axis uses the current value of SetPlotYAxis (0 initially).
ImVec2 GetPlotMousePos(); ImVec2 GetPlotMousePos(int y_axis = -1);
/// Returns the current or most recent plot axis range. /// Returns the current or most recent plot axis range. A negative y_axis uses the current value of SetPlotYAxis (0 initially).
ImPlotRange GetPlotRange(); ImPlotLimits GetPlotLimits(int y_axis = -1);
/// Returns true if the current or most recent plot is being queried. /// Returns true if the current or most recent plot is being queried.
bool IsPlotQueried(); bool IsPlotQueried();
/// Returns the current or most recent plot querey range. /// Returns the current or most recent plot query bounds.
ImPlotRange GetPlotQuery(); ImPlotLimits GetPlotQuery(int y_axis = -1);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Plot Styling // Plot Styling
@ -224,22 +239,25 @@ void PopPlotStyleVar(int count = 1);
// Plot Utils // Plot Utils
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/// Set the axes ranges of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axes will be locked. /// Set the axes range limits of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axes limits will be locked.
void SetNextPlotRange(float x_min, float x_max, float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once); void SetNextPlotLimits(float x_min, float x_max, float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once);
/// Set the X axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked. /// Set the X axis range limits of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis limits will be locked.
void SetNextPlotRangeX(float x_min, float x_max, ImGuiCond cond = ImGuiCond_Once); void SetNextPlotLimitsX(float x_min, float x_max, ImGuiCond cond = ImGuiCond_Once);
/// Set the Y axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked. /// Set the Y axis range limits of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis limits will be locked.
void SetNextPlotRangeY(float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once); void SetNextPlotLimitsY(float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once, int y_axis = 0);
/// Select which Y axis will be used for subsequent plot elements. The default is '0', or the first Y axis.
void SetPlotYAxis(int y_axis);
// Get the current Plot position (top-left) in pixels. // Get the current Plot position (top-left) in pixels.
ImVec2 GetPlotPos(); ImVec2 GetPlotPos();
// Get the curent Plot size in pixels. // Get the curent Plot size in pixels.
ImVec2 GetPlotSize(); ImVec2 GetPlotSize();
// Convert pixels to a position in the current plot's coordinate system. // Convert pixels to a position in the current plot's coordinate system. A negative y_axis uses the current value of SetPlotYAxis (0 initially).
ImVec2 PixelsToPlot(const ImVec2& pix); ImVec2 PixelsToPlot(const ImVec2& pix, int y_axis = -1);
// Convert a position in the current plot's coordinate system to pixels. // Convert a position in the current plot's coordinate system to pixels. A negative y_axis uses the current value of SetPlotYAxis (0 initially).
ImVec2 PlotToPixels(const ImVec2& plt); ImVec2 PlotToPixels(const ImVec2& plt, int y_axis = -1);
// Push clip rect for rendering to current plot area // Push clip rect for rendering to current plot area
void PushPlotClipRect(); void PushPlotClipRect();

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
// ImPlot v0.1 WIP // ImPlot v0.2 WIP
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
@ -33,6 +33,7 @@
#include <implot.h> #include <implot.h>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
namespace { namespace {
@ -99,7 +100,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
ImGui::SetNextWindowPos(ImVec2(50, 50), ImGuiCond_FirstUseEver); ImGui::SetNextWindowPos(ImVec2(50, 50), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(520, 750), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(520, 750), ImGuiCond_FirstUseEver);
ImGui::Begin("ImPlot Demo", p_open); ImGui::Begin("ImPlot Demo", p_open);
ImGui::Text("ImPlot says hello. (0.1 WIP)"); ImGui::Text("ImPlot says hello. (0.2 WIP)");
if (ImGui::CollapsingHeader("Help")) { if (ImGui::CollapsingHeader("Help")) {
ImGui::Text("USER GUIDE:"); ImGui::Text("USER GUIDE:");
ImGui::BulletText("Left click and drag within the plot area to pan X and Y axes."); ImGui::BulletText("Left click and drag within the plot area to pan X and Y axes.");
@ -116,11 +117,6 @@ void ShowImPlotDemoWindow(bool* p_open) {
ImGui::BulletText("Hold Shift to expand box selection vertically."); ImGui::BulletText("Hold Shift to expand box selection vertically.");
ImGui::BulletText("Left click while box selecting to cancel the selection."); ImGui::BulletText("Left click while box selecting to cancel the selection.");
ImGui::Unindent(); ImGui::Unindent();
ImGui::BulletText("Middle click (or Ctrl + right click) and drag to create a query range.");
ImGui::Indent();
ImGui::BulletText("Hold Alt to expand query horizontally.");
ImGui::BulletText("Hold Shift to expand query vertically.");
ImGui::Unindent();
ImGui::BulletText("Double left click to fit all visible data."); ImGui::BulletText("Double left click to fit all visible data.");
ImGui::Indent(); ImGui::Indent();
ImGui::BulletText("Double left click on an axis to fit the individual axis."); ImGui::BulletText("Double left click on an axis to fit the individual axis.");
@ -180,9 +176,9 @@ void ShowImPlotDemoWindow(bool* p_open) {
static bool horz = false; static bool horz = false;
ImGui::Checkbox("Horizontal",&horz); ImGui::Checkbox("Horizontal",&horz);
if (horz) if (horz)
ImGui::SetNextPlotRange(0, 110, -0.5f, 9.5f, ImGuiCond_Always); ImGui::SetNextPlotLimits(0, 110, -0.5f, 9.5f, ImGuiCond_Always);
else else
ImGui::SetNextPlotRange(-0.5f, 9.5f, 0, 110, ImGuiCond_Always); ImGui::SetNextPlotLimits(-0.5f, 9.5f, 0, 110, ImGuiCond_Always);
if (ImGui::BeginPlot("Bar Plot", horz ? "Score": "Student", horz ? "Student" : "Score", {-1, 300})) { if (ImGui::BeginPlot("Bar Plot", horz ? "Score": "Student", horz ? "Student" : "Score", {-1, 300})) {
static float midtm[10] = {83, 67, 23, 89, 83, 78, 91, 82, 85, 90}; static float midtm[10] = {83, 67, 23, 89, 83, 78, 91, 82, 85, 90};
static float final[10] = {80, 62, 56, 99, 55, 78, 88, 78, 90, 100}; static float final[10] = {80, 62, 56, 99, 55, 78, 88, 78, 90, 100};
@ -207,7 +203,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
float bar[5] = {1,2,5,3,4}; float bar[5] = {1,2,5,3,4};
float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f}; float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f};
float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f}; float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f};
ImGui::SetNextPlotRange(0, 6, 0, 10); ImGui::SetNextPlotLimits(0, 6, 0, 10);
if (ImGui::BeginPlot("##ErrorBars",NULL,NULL,ImVec2(-1,300))) { if (ImGui::BeginPlot("##ErrorBars",NULL,NULL,ImVec2(-1,300))) {
ImGui::PlotBar("Bar", xs, bar, 5, 0.5f); ImGui::PlotBar("Bar", xs, bar, 5, 0.5f);
@ -231,7 +227,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
ImVec2 center(0.5f,0.5f); // in plot units, not pixels ImVec2 center(0.5f,0.5f); // in plot units, not pixels
float radius = 0.4f; // in plot units, not pixels float radius = 0.4f; // in plot units, not pixels
SetNextPlotRange(0,1,0,1,ImGuiCond_Always); SetNextPlotLimits(0,1,0,1,ImGuiCond_Always);
if (ImGui::BeginPlot("##Pie1", NULL, NULL, ImVec2(250,250), ImPlotFlags_Legend, 0, 0)) { if (ImGui::BeginPlot("##Pie1", NULL, NULL, ImVec2(250,250), ImPlotFlags_Legend, 0, 0)) {
ImGui::PlotPieChart(labels1, pre_normalized, 4, center, radius); ImGui::PlotPieChart(labels1, pre_normalized, 4, center, radius);
ImGui::EndPlot(); ImGui::EndPlot();
@ -246,7 +242,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
{0.7412f, 0.0f, 0.1490f, 1.0f}, {0.7412f, 0.0f, 0.1490f, 1.0f},
}; };
ImGui::SetPlotPalette(YlOrRd, 5); ImGui::SetPlotPalette(YlOrRd, 5);
SetNextPlotRange(0,1,0,1,ImGuiCond_Always); SetNextPlotLimits(0,1,0,1,ImGuiCond_Always);
static const char* labels2[] = {"One","Two","Three","Four","Five"}; static const char* labels2[] = {"One","Two","Three","Four","Five"};
static float not_normalized[] = {1,2,3,4,5}; static float not_normalized[] = {1,2,3,4,5};
if (ImGui::BeginPlot("##Pie2", NULL, NULL, ImVec2(250,250), ImPlotFlags_Legend, 0, 0)) { if (ImGui::BeginPlot("##Pie2", NULL, NULL, ImVec2(250,250), ImPlotFlags_Legend, 0, 0)) {
@ -272,14 +268,14 @@ void ShowImPlotDemoWindow(bool* p_open) {
sdata2.AddPoint(t, mouse.y * 0.0005f); sdata2.AddPoint(t, mouse.y * 0.0005f);
rdata2.AddPoint(t, mouse.y * 0.0005f); rdata2.AddPoint(t, mouse.y * 0.0005f);
} }
ImGui::SetNextPlotRangeX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always); ImGui::SetNextPlotLimitsX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
static int rt_axis = ImAxisFlags_Default & ~ImAxisFlags_TickLabels; static int rt_axis = ImAxisFlags_Default & ~ImAxisFlags_TickLabels;
if (ImGui::BeginPlot("##Scrolling", NULL, NULL, {-1,150}, ImPlotFlags_Default, rt_axis, rt_axis)) { if (ImGui::BeginPlot("##Scrolling", NULL, NULL, {-1,150}, ImPlotFlags_Default, rt_axis, rt_axis)) {
ImGui::Plot("Data 1", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), sdata1.Offset, 2 * sizeof(float)); ImGui::Plot("Data 1", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), sdata1.Offset, 2 * sizeof(float));
ImGui::Plot("Data 2", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), sdata2.Offset, 2 * sizeof(float)); ImGui::Plot("Data 2", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), sdata2.Offset, 2 * sizeof(float));
ImGui::EndPlot(); ImGui::EndPlot();
} }
ImGui::SetNextPlotRangeX(0, 10, ImGuiCond_Always); ImGui::SetNextPlotLimitsX(0, 10, ImGuiCond_Always);
if (ImGui::BeginPlot("##Rolling", NULL, NULL, {-1,150}, ImPlotFlags_Default, rt_axis, rt_axis)) { if (ImGui::BeginPlot("##Rolling", NULL, NULL, {-1,150}, ImPlotFlags_Default, rt_axis, rt_axis)) {
ImGui::Plot("Data 1", &rdata1.Data[0].x, &rdata1.Data[0].y, rdata1.Data.size(), 0, 2 * sizeof(float)); ImGui::Plot("Data 1", &rdata1.Data[0].x, &rdata1.Data[0].y, rdata1.Data.size(), 0, 2 * sizeof(float));
ImGui::Plot("Data 2", &rdata2.Data[0].x, &rdata2.Data[0].y, rdata2.Data.size(), 0, 2 * sizeof(float)); ImGui::Plot("Data 2", &rdata2.Data[0].x, &rdata2.Data[0].y, rdata2.Data.size(), 0, 2 * sizeof(float));
@ -289,7 +285,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Markers and Labels")) { if (ImGui::CollapsingHeader("Markers and Labels")) {
ImGui::SetNextPlotRange(0, 10, 0, 12); ImGui::SetNextPlotLimits(0, 10, 0, 12);
if (ImGui::BeginPlot("##MarkerStyles", NULL, NULL, ImVec2(-1,300), 0, 0, 0)) { if (ImGui::BeginPlot("##MarkerStyles", NULL, NULL, ImVec2(-1,300), 0, 0, 0)) {
float xs[2] = {1,4}; float xs[2] = {1,4};
float ys[2] = {10,11}; float ys[2] = {10,11};
@ -373,7 +369,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
ys2[i] = log(xs[i]); ys2[i] = log(xs[i]);
ys3[i] = pow(10.0f, xs[i]); ys3[i] = pow(10.0f, xs[i]);
} }
ImGui::SetNextPlotRange(0.1f, 100, 0, 10); ImGui::SetNextPlotLimits(0.1f, 100, 0, 10);
if (ImGui::BeginPlot("Log Plot", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default, ImAxisFlags_Default | ImAxisFlags_LogScale )) { if (ImGui::BeginPlot("Log Plot", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default, ImAxisFlags_Default | ImAxisFlags_LogScale )) {
ImGui::Plot("f(x) = x", xs, xs, 1001); ImGui::Plot("f(x) = x", xs, xs, 1001);
ImGui::Plot("f(x) = sin(x)+1", xs, ys1, 1001); ImGui::Plot("f(x) = sin(x)+1", xs, ys1, 1001);
@ -383,13 +379,72 @@ void ShowImPlotDemoWindow(bool* p_open) {
} }
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Multiple Y-Axes")) {
static ImVec4 txt_col = ImGui::GetStyle().Colors[ImGuiCol_Text];
txt_col.w = 0.25f;
static ImVec4 y1_col = txt_col;
static ImVec4 y2_col = txt_col;
static ImVec4 y3_col = txt_col;
static float xs[1001], xs2[1001], ys1[1001], ys2[1001], ys3[1001];
static bool y2_axis = true;
static bool y3_axis = false;
ImGui::Checkbox("Y-Axis 2", &y2_axis);
ImGui::SameLine();
ImGui::Checkbox("Y-Axis 3", &y3_axis);
ImGui::SameLine();
ImGui::ColorEdit4("##Col1", &y1_col.x, ImGuiColorEditFlags_NoInputs);
ImGui::SameLine();
ImGui::ColorEdit4("##Col2", &y2_col.x, ImGuiColorEditFlags_NoInputs);
ImGui::SameLine();
ImGui::ColorEdit4("##Col3", &y3_col.x, ImGuiColorEditFlags_NoInputs);
for (int i = 0; i < 1001; ++i) {
xs[i] = (float)(i*0.1f);
ys1[i] = sin(xs[i]) * 3 + 1;
ys2[i] = cos(xs[i]) * 0.2f + 0.5f;
ys3[i] = sin(xs[i]+0.5f) * 100 + 200;
xs2[i] = xs[i] + 10.0f;
}
ImGui::SetNextPlotLimits(0.1f, 100, 0, 10);
ImGui::SetNextPlotLimitsY(0, 1, ImGuiCond_Once, 1);
ImGui::SetNextPlotLimitsY(0, 300, ImGuiCond_Once, 2);
ImGui::PushPlotColor(ImPlotCol_YAxis, y1_col);
ImGui::PushPlotColor(ImPlotCol_YAxis2, y2_col);
ImGui::PushPlotColor(ImPlotCol_YAxis3, y3_col);
if (ImGui::BeginPlot("Multi-Axis Plot", NULL, NULL, ImVec2(-1,300),
ImPlotFlags_Default |
(y2_axis ? ImPlotFlags_YAxis2 : 0) |
(y3_axis ? ImPlotFlags_YAxis3 : 0))) {
ImGui::Plot("f(x) = x", xs, xs, 1001);
ImGui::Plot("f(x) = sin(x)*3+1", xs, ys1, 1001);
if (y2_axis) {
ImGui::SetPlotYAxis(1);
ImGui::Plot("f(x) = cos(x)*.2+.5 (Y2)", xs, ys2, 1001);
}
if (y3_axis) {
ImGui::SetPlotYAxis(2);
ImGui::Plot("f(x) = sin(x+.5)*100+200 (Y3)", xs2, ys3, 1001);
}
ImGui::EndPlot();
}
ImGui::PopPlotColor(3);
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Querying")) { if (ImGui::CollapsingHeader("Querying")) {
ImGui::BulletText("Ctrl + click in the plot area to draw points."); ImGui::BulletText("Ctrl + click in the plot area to draw points.");
ImGui::BulletText("Middle click (or Ctrl + right click) and drag to query points."); ImGui::BulletText("Middle click (or Ctrl + right click) and drag to create a query rect.");
ImGui::BulletText("Hold the Alt and/or Shift keys to expand the query range."); ImGui::Indent();
ImGui::BulletText("Hold Alt to expand query horizontally.");
ImGui::BulletText("Hold Shift to expand query vertically.");
ImGui::BulletText("The query rect can be dragged after it's created.");
ImGui::Unindent();
static ImVector<ImVec2> data; static ImVector<ImVec2> data;
ImPlotRange range, query; ImPlotLimits range, query;
if (ImGui::BeginPlot("##Drawing", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default, ImAxisFlags_GridLines, ImAxisFlags_GridLines)) { if (ImGui::BeginPlot("##Drawing", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default | ImPlotFlags_Query, ImAxisFlags_GridLines, ImAxisFlags_GridLines)) {
if (ImGui::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl) if (ImGui::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl)
data.push_back(ImGui::GetPlotMousePos()); data.push_back(ImGui::GetPlotMousePos());
ImGui::PushPlotStyleVar(ImPlotStyleVar_LineWeight, 0); ImGui::PushPlotStyleVar(ImPlotStyleVar_LineWeight, 0);
@ -397,7 +452,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
if (data.size() > 0) if (data.size() > 0)
ImGui::Plot("Points", &data[0].x, &data[0].y, data.size(), 0, 2 * sizeof(float)); ImGui::Plot("Points", &data[0].x, &data[0].y, data.size(), 0, 2 * sizeof(float));
if (ImGui::IsPlotQueried() && data.size() > 0) { if (ImGui::IsPlotQueried() && data.size() > 0) {
ImPlotRange range = ImGui::GetPlotQuery(); ImPlotLimits range = ImGui::GetPlotQuery();
int cnt = 0; int cnt = 0;
ImVec2 avg; ImVec2 avg;
for (int i = 0; i < data.size(); ++i) { for (int i = 0; i < data.size(); ++i) {
@ -414,12 +469,12 @@ void ShowImPlotDemoWindow(bool* p_open) {
} }
} }
ImGui::PopPlotStyleVar(2); ImGui::PopPlotStyleVar(2);
range = ImGui::GetPlotRange(); range = ImGui::GetPlotLimits();
query = ImGui::GetPlotQuery(); query = ImGui::GetPlotQuery();
ImGui::EndPlot(); ImGui::EndPlot();
} }
ImGui::Text("The current plot range is: [%g,%g,%g,%g]", range.XMin, range.XMax, range.YMin, range.YMax); ImGui::Text("The current plot limits are: [%g,%g,%g,%g]", range.X.Min, range.X.Max, range.Y.Min, range.Y.Max);
ImGui::Text("The current query range is: [%g,%g,%g,%g]", query.XMin, query.XMax, query.YMin, query.YMax); ImGui::Text("The current query limits are: [%g,%g,%g,%g]", query.X.Min, query.X.Max, query.Y.Min, query.Y.Max);
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Views")) { if (ImGui::CollapsingHeader("Views")) {
@ -433,24 +488,23 @@ void ShowImPlotDemoWindow(bool* p_open) {
for (size_t i = 0; i < 512; ++i) { for (size_t i = 0; i < 512; ++i) {
const float t = i / sampling_freq; const float t = i / sampling_freq;
x_data[i] = t; x_data[i] = t;
const float arg = 2 * 3.14 * freq * t; const float arg = 2 * 3.14f * freq * t;
y_data1[i] = sin(arg); y_data1[i] = sin(arg);
y_data2[i] = y_data1[i] * -0.6 + sin(2 * arg) * 0.4; y_data2[i] = y_data1[i] * -0.6f + sin(2 * arg) * 0.4f;
y_data3[i] = y_data2[i] * -0.6 + sin(3 * arg) * 0.4; y_data3[i] = y_data2[i] * -0.6f + sin(3 * arg) * 0.4f;
} }
ImGui::BulletText("Query the first plot to render a subview in the second plot."); ImGui::BulletText("Query the first plot to render a subview in the second plot (see above for controls).");
ImGui::BulletText("Toggle \"Pixel Query\" in the context menu and then pan the plot."); ImGui::SetNextPlotLimits(0,0.01f,-1,1);
ImGui::SetNextPlotRange(0,0.01f,-1,1);
ImAxisFlags flgs = ImAxisFlags_Default & ~ImAxisFlags_TickLabels; ImAxisFlags flgs = ImAxisFlags_Default & ~ImAxisFlags_TickLabels;
ImPlotRange query; ImPlotLimits query;
if (ImGui::BeginPlot("##View1",NULL,NULL,ImVec2(-1,150), ImPlotFlags_Default, flgs, flgs)) { if (ImGui::BeginPlot("##View1",NULL,NULL,ImVec2(-1,150), ImPlotFlags_Default | ImPlotFlags_Query, flgs, flgs)) {
ImGui::Plot("Signal 1", x_data, y_data1, 512); ImGui::Plot("Signal 1", x_data, y_data1, 512);
ImGui::Plot("Signal 2", x_data, y_data2, 512); ImGui::Plot("Signal 2", x_data, y_data2, 512);
ImGui::Plot("Signal 3", x_data, y_data3, 512); ImGui::Plot("Signal 3", x_data, y_data3, 512);
query = ImGui::GetPlotQuery(); query = ImGui::GetPlotQuery();
ImGui::EndPlot(); ImGui::EndPlot();
} }
ImGui::SetNextPlotRange(query.XMin, query.XMax, query.YMin, query.YMax, ImGuiCond_Always); ImGui::SetNextPlotLimits(query.X.Min, query.X.Max, query.Y.Min, query.Y.Max, ImGuiCond_Always);
if (ImGui::BeginPlot("##View2",NULL,NULL,ImVec2(-1,150), 0, 0, 0)) { if (ImGui::BeginPlot("##View2",NULL,NULL,ImVec2(-1,150), 0, 0, 0)) {
ImGui::Plot("Signal 1", x_data, y_data1, 512); ImGui::Plot("Signal 1", x_data, y_data1, 512);
ImGui::Plot("Signal 2", x_data, y_data2, 512); ImGui::Plot("Signal 2", x_data, y_data2, 512);
@ -458,12 +512,9 @@ void ShowImPlotDemoWindow(bool* p_open) {
ImGui::EndPlot(); ImGui::EndPlot();
} }
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Drag and Drop")) { if (ImGui::CollapsingHeader("Drag and Drop")) {
srand(10000000 * ImGui::GetTime()); srand((int)(10000000 * ImGui::GetTime()));
static bool paused = false; static bool paused = false;
static bool init = true; static bool init = true;
static ScrollingData data[10]; static ScrollingData data[10];
@ -508,7 +559,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
data[i].Data.back().y + (0.005f + 0.0002f * (float)rand() / float(RAND_MAX)) * (-1 + 2 * (float)rand() / float(RAND_MAX))); data[i].Data.back().y + (0.005f + 0.0002f * (float)rand() / float(RAND_MAX)) * (-1 + 2 * (float)rand() / float(RAND_MAX)));
} }
} }
ImGui::SetNextPlotRangeX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always); ImGui::SetNextPlotLimitsX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
if (ImGui::BeginPlot("##DND", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default)) { if (ImGui::BeginPlot("##DND", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default)) {
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
if (show[i]) { if (show[i]) {
@ -609,8 +660,8 @@ void ShowImPlotDemoWindow(bool* p_open) {
if (showAnalog[i]) if (showAnalog[i])
dataAnalog[i].AddPoint(t, sin(2*t) - cos(2*t)); dataAnalog[i].AddPoint(t, sin(2*t) - cos(2*t));
} }
ImGui::SetNextPlotRangeY(-1, 1); ImGui::SetNextPlotLimitsY(-1, 1);
ImGui::SetNextPlotRangeX(t - 10.0f, t, paused ? ImGuiCond_Once : ImGuiCond_Always); ImGui::SetNextPlotLimitsX(t - 10.0f, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
if (ImGui::BeginPlot("##Digital", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default)) { if (ImGui::BeginPlot("##Digital", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default)) {
for (int i = 0; i < K_PLOT_DIGITAL_CH_COUNT; ++i) { for (int i = 0; i < K_PLOT_DIGITAL_CH_COUNT; ++i) {
if (showDigital[i]) { if (showDigital[i]) {
@ -657,7 +708,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
ImGui::PushPlotColor(ImPlotCol_XAxis, IM_COL32(192, 192, 192, 192)); ImGui::PushPlotColor(ImPlotCol_XAxis, IM_COL32(192, 192, 192, 192));
ImGui::PushPlotColor(ImPlotCol_YAxis, IM_COL32(192, 192, 192, 192)); ImGui::PushPlotColor(ImPlotCol_YAxis, IM_COL32(192, 192, 192, 192));
ImGui::PushPlotStyleVar(ImPlotStyleVar_LineWeight, 2); ImGui::PushPlotStyleVar(ImPlotStyleVar_LineWeight, 2);
ImGui::SetNextPlotRange(-0.5f, 9.5f, -0.5f, 9.5f); ImGui::SetNextPlotLimits(-0.5f, 9.5f, -0.5f, 9.5f);
if (ImGui::BeginPlot("##Custom", NULL, NULL, {-1,300}, ImPlotFlags_Default & ~ImPlotFlags_Legend, 0)) { if (ImGui::BeginPlot("##Custom", NULL, NULL, {-1,300}, ImPlotFlags_Default & ~ImPlotFlags_Legend, 0)) {
float lin[10] = {8,8,9,7,8,8,8,9,7,8}; float lin[10] = {8,8,9,7,8,8,8,9,7,8};
float bar[10] = {1,2,5,3,4,1,2,5,3,4}; float bar[10] = {1,2,5,3,4,1,2,5,3,4};
@ -692,7 +743,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
static BenchmarkItem items[n_items]; static BenchmarkItem items[n_items];
ImGui::BulletText("Make sure VSync is disabled."); ImGui::BulletText("Make sure VSync is disabled.");
ImGui::BulletText("%d lines with %d points each @ %.3f FPS.",n_items,1000,ImGui::GetIO().Framerate); ImGui::BulletText("%d lines with %d points each @ %.3f FPS.",n_items,1000,ImGui::GetIO().Framerate);
SetNextPlotRange(0,1,0,1, ImGuiCond_Always); SetNextPlotLimits(0,1,0,1, ImGuiCond_Always);
if (ImGui::BeginPlot("##Bench",NULL,NULL,{-1,300},ImPlotFlags_Default | ImPlotFlags_NoChild)) { if (ImGui::BeginPlot("##Bench",NULL,NULL,{-1,300},ImPlotFlags_Default | ImPlotFlags_NoChild)) {
char buff[16]; char buff[16];
for (int i = 0; i < 100; ++i) { for (int i = 0; i < 100; ++i) {