From 3bc08a946c47af6af7cc565b34b10dac448fe2fb Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Mon, 4 May 2020 08:47:39 -0500 Subject: [PATCH 1/4] make pie chart labels const, remove unused vars --- implot.cpp | 4 +--- implot.h | 2 +- implot_demo.cpp | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/implot.cpp b/implot.cpp index 24e613b..f9b3fe4 100644 --- a/implot.cpp +++ b/implot.cpp @@ -1171,12 +1171,10 @@ void EndPlot() { // AXIS STATES ------------------------------------------------------------ - const bool flip_x = HasFlag(plot.XAxis.Flags, ImAxisFlags_Invert); const bool lock_x_min = HasFlag(plot.XAxis.Flags, ImAxisFlags_LockMin); const bool lock_x_max = HasFlag(plot.XAxis.Flags, ImAxisFlags_LockMax); const bool lock_x = (lock_x_min && lock_x_max) || (gp.NextPlotData.HasXRange && gp.NextPlotData.XRangeCond == ImGuiCond_Always); - const bool flip_y = HasFlag(plot.YAxis.Flags, ImAxisFlags_Invert); const bool lock_y_min = HasFlag(plot.YAxis.Flags, ImAxisFlags_LockMin); const bool lock_y_max = HasFlag(plot.YAxis.Flags, ImAxisFlags_LockMax); const bool lock_y = (lock_y_min && lock_y_max) || (gp.NextPlotData.HasYRange && gp.NextPlotData.YRangeCond == ImGuiCond_Always); @@ -2151,7 +2149,7 @@ inline void DrawPieSlice(ImDrawList& DrawList, const ImVec2& center, float radiu } -void PlotPieChart(char** label_ids, float* values, int count, const ImVec2& center, float radius, bool show_percents, float angle0) { +void PlotPieChart(const char** label_ids, float* values, int count, const ImVec2& center, float radius, bool show_percents, float angle0) { IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PlotPieChart() Needs to be called between BeginPlot() and EndPlot()!"); ImDrawList & DrawList = *GetWindowDrawList(); diff --git a/implot.h b/implot.h index 83af945..b5ceeb5 100644 --- a/implot.h +++ b/implot.h @@ -170,7 +170,7 @@ void PlotErrorBars(const char* label_id, const float* xs, const float* ys, const void PlotErrorBars(const char* label_id, const float* xs, const float* ys, const float* neg, const float* pos, int count, int offset = 0, int stride = sizeof(float)); void PlotErrorBars(const char* label_id, ImVec4 (*getter)(void* data, int idx), void* data, int count, int offset = 0); // Plots a pie chart. If the sum of values > 1, each value will be normalized. Center and radius are in plot coordinates. -void PlotPieChart(char** label_ids, float* values, int count, const ImVec2& center, float radius, bool show_percents = true, float angle0 = 90); +void PlotPieChart(const char** label_ids, float* values, int count, const ImVec2& center, float radius, bool show_percents = true, float angle0 = 90); // Plots a text label at point x,y. void PlotLabel(const char* text, float x, float y, bool vertical = false, const ImVec2& pixel_offset = ImVec2(0,0)); diff --git a/implot_demo.cpp b/implot_demo.cpp index c200541..6ea85b3 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -212,7 +212,7 @@ void ShowImPlotDemoWindow(bool* p_open) { } //------------------------------------------------------------------------- if (ImGui::CollapsingHeader("Pie Charts")) { - static char* labels1[] = {"Frogs","Hogs","Dogs","Logs"}; + static const char* labels1[] = {"Frogs","Hogs","Dogs","Logs"}; static float pre_normalized[] = {0.15f, 0.30f, 0.45f, 0.10f}; ImVec2 center(0.5f,0.5f); // in plot units, not pixels float radius = 0.4f; // in plot units, not pixels @@ -233,7 +233,7 @@ void ShowImPlotDemoWindow(bool* p_open) { }; ImGui::SetPlotPalette(YlOrRd, 5); SetNextPlotRange(0,1,0,1,ImGuiCond_Always); - static 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}; if (ImGui::BeginPlot("##Pie2", NULL, NULL, ImVec2(250,250), ImPlotFlags_Legend, 0, 0)) { ImGui::PlotPieChart(labels2, not_normalized, 5, center, radius); From 22cf50cc44e9a4473e32360132756c0b708b99bb Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Tue, 5 May 2020 13:41:18 -0500 Subject: [PATCH 2/4] hard code color of pie labels --- implot.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/implot.cpp b/implot.cpp index f9b3fe4..3bdfebe 100644 --- a/implot.cpp +++ b/implot.cpp @@ -2181,8 +2181,8 @@ void PlotPieChart(const char** label_ids, float* values, int count, const ImVec2 ImVec2 size = CalcTextSize(buffer); float angle = a0 + (a1 - a0) * 0.5f; ImVec2 pos = PlotToPixels(center.x + 0.5f * radius * cos(angle), center.y + 0.5f * radius * sin(angle)); - DrawList.AddText(pos - size * 0.5f + ImVec2(1,1), gp.Col_Bg, buffer); - DrawList.AddText(pos - size * 0.5f, GetColorU32(ImGuiCol_Text), buffer); + DrawList.AddText(pos - size * 0.5f + ImVec2(1,1), IM_COL32(0,0,0,255), buffer); + DrawList.AddText(pos - size * 0.5f, IM_COL32(255,255,255,255), buffer); } } a0 = a1; From 762660ddcccb22d218f8ea9375f7a5634aad233d Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Tue, 5 May 2020 14:24:19 -0500 Subject: [PATCH 3/4] digital signals cleanup --- implot.cpp | 4 +-- implot.h | 4 +-- implot_demo.cpp | 82 +++++++++++++++++-------------------------------- 3 files changed, 33 insertions(+), 57 deletions(-) diff --git a/implot.cpp b/implot.cpp index 282744a..e6d24a2 100644 --- a/implot.cpp +++ b/implot.cpp @@ -49,7 +49,7 @@ ImPlotStyle::ImPlotStyle() { MarkerWeight = 1; ErrorBarSize = 5; ErrorBarWeight = 1.5; - DigitalBitHeight = 7; + DigitalBitHeight = 8; Colors[ImPlotCol_Line] = IM_COL_AUTO; Colors[ImPlotCol_Fill] = IM_COL_AUTO; @@ -1469,7 +1469,7 @@ static const ImPlotStyleVarInfo GPlotStyleVarInfo[] = { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, MarkerWeight) }, // ImPlotStyleVar_MarkerWeight { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, ErrorBarSize) }, // ImPlotStyleVar_ErrorBarSize { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, ErrorBarWeight) }, // ImPlotStyleVar_ErrorBarWeight - { ImGuiDataType_S32, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitHeight) } // ImPlotStyleVar_DigitalBitHeight + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitHeight) } // ImPlotStyleVar_DigitalBitHeight }; static const ImPlotStyleVarInfo* GetPlotStyleVarInfo(ImPlotStyleVar idx) diff --git a/implot.h b/implot.h index 6eb67c9..778e8ad 100644 --- a/implot.h +++ b/implot.h @@ -88,7 +88,7 @@ enum ImPlotStyleVar_ { ImPlotStyleVar_MarkerWeight, // float, outline weight of markers in pixels ImPlotStyleVar_ErrorBarSize, // float, error bar whisker width in pixels ImPlotStyleVar_ErrorBarWeight, // float, error bar whisker weight in pixels - ImPlotStyleVar_DigitalBitHeight, // int, digital channels bit height (at 1) + ImPlotStyleVar_DigitalBitHeight, // float, digital channels bit height (at 1) ImPlotStyleVar_COUNT }; @@ -122,7 +122,7 @@ struct ImPlotStyle { float MarkerWeight; // = 1, outline weight of markers in pixels float ErrorBarSize; // = 5, error bar whisker width in pixels float ErrorBarWeight; // = 1.5, error bar whisker weight in pixels - int DigitalBitHeight; // = 7, digital channels bit height (at 1) + float DigitalBitHeight; // = 8, digital channels bit height (at y = 1.0f) ImVec4 Colors[ImPlotCol_COUNT]; // array of plot specific colors ImPlotStyle(); }; diff --git a/implot_demo.cpp b/implot_demo.cpp index 2b91691..758d5de 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -48,7 +48,7 @@ struct ScrollingData { } void Erase() { if (Data.size() > 0) { - Data.clear(); + Data.shrink(0); Offset = 0; } } @@ -520,22 +520,23 @@ void ShowImPlotDemoWindow(bool* p_open) { } } - if (ImGui::CollapsingHeader("Digital")) { + if (ImGui::CollapsingHeader("Digital and Analog Signals")) { + - static int bitHeight = 7; - ImGui::SetNextItemWidth(100); - ImGui::DragInt("Bit Hieght", &bitHeight, 0.2, 5, 50); - /// Set the X axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked. - //void SetNextPlotRangeX(float x_min, float x_max, ImGuiCond cond = ImGuiCond_Once); static bool paused = false; - #define K_PLOT_DIGITAL_CH_COUNT 8 - #define K_PLOT_ANALOG_CH_COUNT 8 + #define K_PLOT_DIGITAL_CH_COUNT 4 + #define K_PLOT_ANALOG_CH_COUNT 4 static ScrollingData dataDigital[K_PLOT_DIGITAL_CH_COUNT]; static ScrollingData dataAnalog[K_PLOT_ANALOG_CH_COUNT]; static bool showDigital[K_PLOT_DIGITAL_CH_COUNT]; static bool showAnalog[K_PLOT_ANALOG_CH_COUNT]; - ImGui::BulletText("Drag data items from the left column onto the plot."); + ImGui::BulletText("You can plot digital and analog signals on the same plot."); + ImGui::BulletText("Digital signals do not respond to Y drag and zoom, so that"); + ImGui::Indent(); + ImGui::Text("you can drag analog signals over the rising/falling digital edge."); + ImGui::Unindent(); + static float bitHeight = 8; ImGui::BeginGroup(); if (ImGui::Button("Clear", {100, 0})) { for (int i = 0; i < K_PLOT_DIGITAL_CH_COUNT; ++i) @@ -545,10 +546,12 @@ void ShowImPlotDemoWindow(bool* p_open) { } if (ImGui::Button(paused ? "Resume" : "Pause", {100,0})) paused = !paused; + ImGui::SetNextItemWidth(100); + ImGui::DragFloat("##Bit Height", &bitHeight, 1, 5, 25, "%.0f px"); ImGui::Separator(); for (int i = 0; i < K_PLOT_DIGITAL_CH_COUNT; ++i) { char label[32]; - sprintf(label, "digital_data_%d", i); + sprintf(label, "digital_%d", i); ImGui::Checkbox(label, &showDigital[i]); if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) { ImGui::SetDragDropPayload("DND_DIGITAL_PLOT", &i, sizeof(int)); @@ -558,7 +561,7 @@ void ShowImPlotDemoWindow(bool* p_open) { } for (int i = 0; i < K_PLOT_ANALOG_CH_COUNT; ++i) { char label[32]; - sprintf(label, "analog_data_%d", i); + sprintf(label, "analog_%d", i); ImGui::Checkbox(label, &showAnalog[i]); if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) { ImGui::SetDragDropPayload("DND_ANALOG_PLOT", &i, sizeof(int)); @@ -569,69 +572,42 @@ void ShowImPlotDemoWindow(bool* p_open) { ImGui::EndGroup(); ImGui::SameLine(); static float t = 0; - if (true) { + if (!paused) { t += ImGui::GetIO().DeltaTime; - //digital signal values int i = 0; if (showDigital[i]) - dataDigital[i].AddPoint(t, sin(t) > 0.45); + dataDigital[i].AddPoint(t, sin(2*t) > 0.45); i++; if (showDigital[i]) - dataDigital[i].AddPoint(t, sin(t) < 0.45); + dataDigital[i].AddPoint(t, sin(2*t) < 0.45); i++; if (showDigital[i]) - dataDigital[i].AddPoint(t, sin(t) > 0.83); + dataDigital[i].AddPoint(t, sin(2*t) > 0.83); i++; if (showDigital[i]) - dataDigital[i].AddPoint(t, sin(t) < 0.17); - i++; - if (showDigital[i]) - dataDigital[i].AddPoint(t, cos(t) > 0.45); - i++; - if (showDigital[i]) - dataDigital[i].AddPoint(t, cos(t) < 0.45); - i++; - if (showDigital[i]) - dataDigital[i].AddPoint(t, cos(t) > 0.83); - i++; - if (showDigital[i]) - dataDigital[i].AddPoint(t, cos(t) < 0.17); - + dataDigital[i].AddPoint(t, sin(2*t) < 0.17); //Analog signal values i = 0; if (showAnalog[i]) - dataAnalog[i].AddPoint(t, sin(t)); + dataAnalog[i].AddPoint(t, sin(2*t)); i++; if (showAnalog[i]) - dataAnalog[i].AddPoint(t, cos(t)); + dataAnalog[i].AddPoint(t, cos(2*t)); i++; if (showAnalog[i]) - dataAnalog[i].AddPoint(t, sin(t) * cos(t)); + dataAnalog[i].AddPoint(t, sin(2*t) * cos(2*t)); i++; if (showAnalog[i]) - dataAnalog[i].AddPoint(t, sin(t) - cos(t)); - i++; - if (showAnalog[i]) - dataAnalog[i].AddPoint(t, cos(t) - sin(t)); - i++; - if (showAnalog[i]) - dataAnalog[i].AddPoint(t, sin(t) + cos(t)); - i++; - if (showAnalog[i]) - dataAnalog[i].AddPoint(t, (sin(t) + cos(t)) / (sin(t) * cos(t))); - i++; - if (showAnalog[i]) - dataAnalog[i].AddPoint(t, (cos(t) + cos(t)) / (cos(t) * cos(t))); - i++; - + dataAnalog[i].AddPoint(t, sin(2*t) - cos(2*t)); } - ImGui::SetNextPlotRangeX(t - 60.0f, t, paused ? ImGuiCond_Once : ImGuiCond_Always); - if (ImGui::BeginPlot("##DND", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default)) { + ImGui::SetNextPlotRangeY(-1, 1); + ImGui::SetNextPlotRangeX(t - 10.0f, t, paused ? ImGuiCond_Once : ImGuiCond_Always); + if (ImGui::BeginPlot("##Digital", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default)) { for (int i = 0; i < K_PLOT_DIGITAL_CH_COUNT; ++i) { if (showDigital[i]) { char label[32]; - sprintf(label, "digital_data_%d", i); + sprintf(label, "digital_%d", i); ImGui::PushPlotStyleVar(ImPlotStyleVar_DigitalBitHeight, bitHeight); ImGui::PlotDigital(label, &dataDigital[i].Data[0].x, &dataDigital[i].Data[0].y, dataDigital[i].Data.size(), dataDigital[i].Offset, 2 * sizeof(float)); ImGui::PopPlotStyleVar(); @@ -640,7 +616,7 @@ void ShowImPlotDemoWindow(bool* p_open) { for (int i = 0; i < K_PLOT_ANALOG_CH_COUNT; ++i) { if (showAnalog[i]) { char label[32]; - sprintf(label, "analog_data_%d", i); + sprintf(label, "analog_%d", i); if (dataAnalog[i].Data.size() > 0) ImGui::Plot(label, &dataAnalog[i].Data[0].x, &dataAnalog[i].Data[0].y, dataAnalog[i].Data.size(), dataAnalog[i].Offset, 2 * sizeof(float)); } From 997bf15584b708a2e5df200587691c9d191806e3 Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Tue, 5 May 2020 14:40:04 -0500 Subject: [PATCH 4/4] Update implot.h --- implot.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/implot.h b/implot.h index 778e8ad..0ffbeb7 100644 --- a/implot.h +++ b/implot.h @@ -176,7 +176,7 @@ void PlotPieChart(const char** label_ids, float* values, int count, const ImVec2 // Plots a text label at point x,y. void PlotLabel(const char* text, float x, float y, bool vertical = false, const ImVec2& pixel_offset = ImVec2(0,0)); // Plots digital channels. -void PlotDigital(const char* label_id, const float* xs, const float* ys, int count, int offset = 0, int stride = sizeof(float) + sizeof(bool)); +void PlotDigital(const char* label_id, const float* xs, const float* ys, int count, int offset = 0, int stride = sizeof(float)); void PlotDigital(const char* label_id, ImVec2 (*getter)(void* data, int idx), void* data, int count, int offset = 0); //-----------------------------------------------------------------------------