From 1d11b1a1a39af395a3f8ba2b069359970e1b7a10 Mon Sep 17 00:00:00 2001 From: Adriano Parisi Date: Mon, 8 Jun 2020 20:45:31 +0100 Subject: [PATCH] Refactored to only draw the horizontal bars, updated demo --- implot.cpp | 62 +++++++++++++------------------------------------ implot.h | 10 ++++---- implot_demo.cpp | 4 +++- 3 files changed, 24 insertions(+), 52 deletions(-) diff --git a/implot.cpp b/implot.cpp index c40253d..730ff8a 100644 --- a/implot.cpp +++ b/implot.cpp @@ -2920,29 +2920,6 @@ void PlotErrorBars(const char* label_id, const double* xs, const double* ys, con // PLOT ERROR BARS H //----------------------------------------------------------------------------- -struct ImPlotPointErrorH { - ImPlotPointErrorH(double _x, double _y, double _neg_v, double _pos_v, double _neg_h, double _pos_h) { - x = _x; y = _y; neg_v = _neg_v; pos_v = _pos_v; neg_h = _neg_h; pos_h = _pos_h; - } - double x, y, neg_v, pos_v, neg_h, pos_h; -}; - -template -struct GetterErrorH { - const T* Xs; const T* Ys; const T* Neg_v; const T* Pos_v; const T* Neg_h; const T* Pos_h; int Count; int Offset; int Stride; - GetterErrorH(const T* xs, const T* ys, const T* neg_v, const T* pos_v, const T* neg_h, const T* pos_h, int count, int offset, int stride) { - Xs = xs; Ys = ys; Neg_v = neg_v; Pos_v = pos_v; Neg_h = neg_h; Pos_h = pos_h; Count = count; Offset = offset; Stride = stride; - } - ImPlotPointErrorH operator()(int idx) { - return ImPlotPointErrorH(OffsetAndStride(Xs, idx, Count, Offset, Stride), - OffsetAndStride(Ys, idx, Count, Offset, Stride), - OffsetAndStride(Neg_v, idx, Count, Offset, Stride), - OffsetAndStride(Pos_v, idx, Count, Offset, Stride), - OffsetAndStride(Neg_h, idx, Count, Offset, Stride), - OffsetAndStride(Pos_h, idx, Count, Offset, Stride)); - } -}; - template void PlotErrorBarsHEx(const char* label_id, Getter getter) { IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PlotErrorBars() needs to be called between BeginPlot() and EndPlot()!"); @@ -2964,27 +2941,20 @@ void PlotErrorBarsHEx(const char* label_id, Getter getter) { // find data extents if (gp.FitThisFrame) { for (int i = 0; i < getter.Count; ++i) { - ImPlotPointErrorH e = getter(i); - FitPoint(ImPlotPoint(e.x, e.y - e.neg_v)); - FitPoint(ImPlotPoint(e.x, e.y + e.pos_v)); - FitPoint(ImPlotPoint(e.x - e.neg_h, e.y)); - FitPoint(ImPlotPoint(e.x + e.pos_h, e.y)); + ImPlotPointError e = getter(i); + FitPoint(ImPlotPoint(e.x - e.neg, e.y)); + FitPoint(ImPlotPoint(e.x + e.pos, e.y)); } } for (int i = 0; i < getter.Count; ++i) { - ImPlotPointErrorH e = getter(i); - ImVec2 p1 = PlotToPixels(e.x, e.y - e.neg_v); - ImVec2 p2 = PlotToPixels(e.x, e.y + e.pos_v); - ImVec2 p3 = PlotToPixels(e.x - e.neg_h, e.y); - ImVec2 p4 = PlotToPixels(e.x + e.pos_h, e.y); + ImPlotPointError e = getter(i); + ImVec2 p1 = PlotToPixels(e.x - e.neg, e.y); + ImVec2 p2 = PlotToPixels(e.x + e.pos, e.y); DrawList.AddLine(p1, p2, col, gp.Style.ErrorBarWeight); - DrawList.AddLine(p3, p4, col, gp.Style.ErrorBarWeight); if (rend_whisker) { - DrawList.AddLine(p1 - ImVec2(half_whisker, 0), p1 + ImVec2(half_whisker, 0), col, gp.Style.ErrorBarWeight); - DrawList.AddLine(p2 - ImVec2(half_whisker, 0), p2 + ImVec2(half_whisker, 0), col, gp.Style.ErrorBarWeight); - DrawList.AddLine(p3 - ImVec2(0, half_whisker), p3 + ImVec2(0, half_whisker), col, gp.Style.ErrorBarWeight); - DrawList.AddLine(p4 - ImVec2(0, half_whisker), p4 + ImVec2(0, half_whisker), col, gp.Style.ErrorBarWeight); + DrawList.AddLine(p1 - ImVec2(0, half_whisker), p1 + ImVec2(0, half_whisker), col, gp.Style.ErrorBarWeight); + DrawList.AddLine(p2 - ImVec2(0, half_whisker), p2 + ImVec2(0, half_whisker), col, gp.Style.ErrorBarWeight); } } PopPlotClipRect(); @@ -2993,26 +2963,26 @@ void PlotErrorBarsHEx(const char* label_id, Getter getter) { //----------------------------------------------------------------------------- // float -void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* err_v, const float* err_h, int count, int offset, int stride) { - GetterErrorH getter(xs, ys, err_v, err_v, err_h, err_h, count, offset, stride); +void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* err, int count, int offset, int stride) { + GetterError getter(xs, ys, err, err, count, offset, stride); PlotErrorBarsHEx(label_id, getter); } -void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* neg_v, const float* pos_v, const float* neg_h, const float* pos_h, int count, int offset, int stride) { - GetterErrorH getter(xs, ys, neg_v, pos_v, neg_h, pos_h, count, offset, stride); +void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* neg, const float* pos, int count, int offset, int stride) { + GetterError getter(xs, ys, neg, pos, count, offset, stride); PlotErrorBarsHEx(label_id, getter); } //----------------------------------------------------------------------------- // double -void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* err_v, const double* err_h, int count, int offset, int stride) { - GetterErrorH getter(xs, ys, err_v, err_v, err_h, err_h, count, offset, stride); +void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* err, int count, int offset, int stride) { + GetterError getter(xs, ys, err, err, count, offset, stride); PlotErrorBarsHEx(label_id, getter); } -void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* neg_v, const double* pos_v, const double* neg_h, const double* pos_h, int count, int offset, int stride) { - GetterErrorH getter(xs, ys, neg_v, pos_v, neg_h, pos_h, count, offset, stride); +void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* neg, const double* pos, int count, int offset, int stride) { + GetterError getter(xs, ys, neg, pos, count, offset, stride); PlotErrorBarsHEx(label_id, getter); } diff --git a/implot.h b/implot.h index 9f2706d..6011bc2 100644 --- a/implot.h +++ b/implot.h @@ -238,11 +238,11 @@ void PlotErrorBars(const char* label_id, const double* xs, const double* ys, con 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, const double* xs, const double* ys, const double* neg, const double* pos, int count, int offset = 0, int stride = sizeof(double)); -// Plots both vertical and horizontal error bars -void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* err_v, const float* err_h, int count, int offset = 0, int stride = sizeof(float)); -void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* err_v, const double* err_h, int count, int offset = 0, int stride = sizeof(double)); -void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* neg_v, const float* pos_v, const float* neg_h, const float* pos_h, int count, int offset = 0, int stride = sizeof(float)); -void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* neg_v, const double* pos_v, const double* neg_h, const double* pos_h, int count, int offset = 0, int stride = sizeof(double)); +// Plots horizontal error bars +void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* err, int count, int offset = 0, int stride = sizeof(float)); +void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* err, int count, int offset = 0, int stride = sizeof(double)); +void PlotErrorBarsH(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 PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* neg, const double* pos, int count, int offset = 0, int stride = sizeof(double)); // Plots a pie chart. If the sum of values > 1 or normalize is true, each value will be normalized. Center and radius are in plot coordinates. void PlotPieChart(const char** label_ids, const float* values, int count, float x, float y, float radius, bool normalize = false, const char* label_fmt = "%.1f", float angle0 = 90); diff --git a/implot_demo.cpp b/implot_demo.cpp index e5f1ab9..913a34f 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -286,6 +286,7 @@ void ShowDemoWindow(bool* p_open) { t_float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f}; t_float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f}; t_float err3[5] = {0.09f, 0.14f, 0.09f, 0.12f, 0.16f}; + t_float err4[5] = {0.02f, 0.08f, 0.15f, 0.05f, 0.2f}; ImPlot::SetNextPlotLimits(0, 6, 0, 10); if (ImPlot::BeginPlot("##ErrorBars",NULL,NULL)) { ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f); @@ -300,7 +301,8 @@ void ShowDemoWindow(bool* p_open) { ImPlot::PushStyleVar(ImPlotStyleVar_Marker, ImPlotMarker_Square); ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, 3); ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImVec4(0, 1, 0, 1)); - ImPlot::PlotErrorBarsH("Line2##ErrorBarH", xs, lin2, err1, err3, 5); + ImPlot::PlotErrorBars("Line2##ErrorBarH", xs, lin2, err2, 5); + ImPlot::PlotErrorBarsH("Line2##ErrorBarH", xs, lin2, err3, err4, 5); ImPlot::PlotLine("Line2##ErrorBarH", xs, lin2, 5); ImPlot::PopStyleVar(2); ImPlot::PopStyleColor();