diff --git a/implot.cpp b/implot.cpp index 5567c1a..fe1b2fc 100644 --- a/implot.cpp +++ b/implot.cpp @@ -2916,6 +2916,77 @@ void PlotErrorBars(const char* label_id, const double* xs, const double* ys, con PlotErrorBarsEx(label_id, getter); } +//----------------------------------------------------------------------------- +// PLOT ERROR BARS H +//----------------------------------------------------------------------------- + +template +void PlotErrorBarsHEx(const char* label_id, Getter getter) { + IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PlotErrorBarsH() needs to be called between BeginPlot() and EndPlot()!"); + + ImGuiID id = ImGui::GetID(label_id); + ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(id); + if (item != NULL && item->Show == false) + return; + + ImDrawList& DrawList = *ImGui::GetWindowDrawList(); + + PushPlotClipRect(); + + const ImU32 col = gp.Style.Colors[ImPlotCol_ErrorBar].w == -1 ? ImGui::GetColorU32(ImGuiCol_Text) : ImGui::GetColorU32(gp.Style.Colors[ImPlotCol_ErrorBar]); + const bool rend_whisker = gp.Style.ErrorBarSize > 0; + + const float half_whisker = gp.Style.ErrorBarSize * 0.5f; + + // find data extents + if (gp.FitThisFrame) { + for (int i = 0; i < getter.Count; ++i) { + 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) { + 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); + if (rend_whisker) { + 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(); +} + +//----------------------------------------------------------------------------- +// float + +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, 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, 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, const double* pos, int count, int offset, int stride) { + GetterError getter(xs, ys, neg, pos, count, offset, stride); + PlotErrorBarsHEx(label_id, getter); +} + + //----------------------------------------------------------------------------- // PLOT PIE CHART //----------------------------------------------------------------------------- diff --git a/implot.h b/implot.h index 6f5e60e..f58b318 100644 --- a/implot.h +++ b/implot.h @@ -70,7 +70,7 @@ enum ImPlotAxisFlags_ { // Plot styling colors. enum ImPlotCol_ { - ImPlotCol_Line, // plot line/outline color (defaults to a rotating color set) + ImPlotCol_Line, // plot line/outline color (defaults to next unused color in current colormap) ImPlotCol_Fill, // plot fill color for bars (defaults to the current line color) ImPlotCol_MarkerOutline, // marker outline color (defaults to the current line color) ImPlotCol_MarkerFill, // marker fill color (defaults to the current line color) @@ -232,12 +232,18 @@ void PlotBarsH(const char* label_id, const float* xs, const float* ys, int count void PlotBarsH(const char* label_id, const double* xs, const double* ys, int count, double height, int offset = 0, int stride = sizeof(double)); void PlotBarsH(const char* label_id, ImPlotPoint (*getter)(void* data, int idx), void* data, int count, double height, int offset = 0); -// Plots vertical error bar. +// Plots vertical error bar. The label_id should match the label_id of the associated line or bar plot. void PlotErrorBars(const char* label_id, const float* xs, const float* ys, const float* err, int count, int offset = 0, int stride = sizeof(float)); void PlotErrorBars(const char* label_id, const double* xs, const double* ys, const double* err, int count, int offset = 0, int stride = sizeof(double)); 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 horizontal error bars. The label_id should match the label_id of the associated line or bar plot. +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); void PlotPieChart(const char** label_ids, const double* values, int count, double x, double y, double radius, bool normalize = false, const char* label_fmt = "%.1f", double angle0 = 90); diff --git a/implot_demo.cpp b/implot_demo.cpp index 4256b5e..d145d5b 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -164,7 +164,7 @@ void ShowDemoWindow(bool* p_open) { ImGui::Indent(); ImGui::BulletText("Software AA can be enabled per plot with ImPlotFlags_AntiAliased."); ImGui::BulletText("AA for demo plots can be enabled from the plot's context menu."); - ImGui::BulletText("If allowable, you are better off using hardware AA (e.g. MSAA)."); + ImGui::BulletText("If permitable, you are better off using hardware AA (e.g. MSAA)."); ImGui::Unindent(); #ifdef IMPLOT_DEMO_USE_DOUBLE ImGui::BulletText("The demo data precision is: double"); @@ -185,11 +185,15 @@ void ShowDemoWindow(bool* p_open) { xs2[i] = i * 0.1f; ys2[i] = xs2[i] * xs2[i]; } + static float weight = ImPlot::GetStyle().LineWeight; + ImGui::BulletText("Anti-aliasing can be enabled from the plot's context menu (see Help)."); + ImGui::DragFloat("Line Weight", &weight, 0.05f, 1.0f, 5.0f, "%.2f px"); if (ImPlot::BeginPlot("Line Plot", "x", "f(x)")) { + ImPlot::PushStyleVar(ImPlotStyleVar_LineWeight, weight); ImPlot::PlotLine("0.5 + 0.5*sin(50*x)", xs1, ys1, 1001); ImPlot::PushStyleVar(ImPlotStyleVar_Marker, ImPlotMarker_Circle); ImPlot::PlotLine("x^2", xs2, ys2, 11); - ImPlot::PopStyleVar(); + ImPlot::PopStyleVar(2); ImPlot::EndPlot(); } } @@ -280,21 +284,37 @@ void ShowDemoWindow(bool* p_open) { //------------------------------------------------------------------------- if (ImGui::CollapsingHeader("Error Bars")) { t_float xs[5] = {1,2,3,4,5}; - t_float lin[5] = {8,8,9,7,8}; t_float bar[5] = {1,2,5,3,4}; + t_float lin1[5] = {8,8,9,7,8}; + t_float lin2[5] = {6,7,6,9,6}; 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}; + static float size = ImPlot::GetStyle().ErrorBarSize; + static float weight = ImPlot::GetStyle().ErrorBarWeight; + ImGui::DragFloat("Error Bar Size", &size, 0.1f, 0, 10,"%.2f px"); + ImGui::DragFloat("Error Bar Weight",&weight,0.01f,1,3,"%.2f px"); ImPlot::SetNextPlotLimits(0, 6, 0, 10); if (ImPlot::BeginPlot("##ErrorBars",NULL,NULL)) { + ImPlot::PushStyleVar(ImPlotStyleVar_ErrorBarSize, size); + ImPlot::PushStyleVar(ImPlotStyleVar_ErrorBarWeight, weight); ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f); + // error bars should have the same label ID as the associated plot ImPlot::PlotErrorBars("Bar", xs, bar, err1, 5); ImPlot::PushStyleVar(ImPlotStyleVar_Marker, ImPlotMarker_Circle); ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, 3); - ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImVec4(1,0,0,1)); - ImPlot::PlotErrorBars("Line", xs, lin, err1, err2, 5); - ImPlot::PlotLine("Line", xs, lin, 5); - ImPlot::PopStyleVar(2); - ImPlot::PopStyleColor(); + ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImPlot::GetColormapColor(1)); + ImPlot::PlotErrorBars("Line1", xs, lin1, err1, err2, 5); + ImPlot::PlotLine("Line1", xs, lin1, 5); + ImPlot::PushStyleVar(ImPlotStyleVar_Marker, ImPlotMarker_Square); + ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, 3); + ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImPlot::GetColormapColor(2)); + ImPlot::PlotErrorBars("Line2", xs, lin2, err2, 5); + ImPlot::PlotErrorBarsH("Line2", xs, lin2, err3, err4, 5); + ImPlot::PlotLine("Line2", xs, lin2, 5); + ImPlot::PopStyleVar(6); + ImPlot::PopStyleColor(2); ImPlot::EndPlot(); } } @@ -408,15 +428,21 @@ void ShowDemoWindow(bool* p_open) { } } //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Markers and Text")) { + if (ImGui::CollapsingHeader("Colormaps, Markers, and Text")) { static ImPlotColormap map = ImPlotColormap_Default; if (ImGui::Button("Change Colormap##2")) map = (map + 1) % ImPlotColormap_COUNT; ImGui::SameLine(); ImGui::LabelText("##Colormap Index", "%s", cmap_names[map]); + static float mk_size = ImPlot::GetStyle().MarkerSize; + static float mk_weight = ImPlot::GetStyle().MarkerWeight; + ImGui::DragFloat("Marker Size",&mk_size,0.1f,2.0f,10.0f,"%.2f px"); + ImGui::DragFloat("Marker Weight", &mk_weight,0.05f,0.5f,3.0f,"%.2f px"); ImGui::PushID(map); // NB: This is merely a workaround so that the demo can cycle color maps. You wouldn't need to do this in your own code! ImPlot::SetNextPlotLimits(0, 10, 0, 12); if (ImPlot::BeginPlot("##MarkerStyles", NULL, NULL, ImVec2(-1,0), 0, 0, 0)) { + ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, mk_size); + ImPlot::PushStyleVar(ImPlotStyleVar_MarkerWeight, mk_weight); ImPlot::SetColormap(map); t_float xs[2] = {1,4}; t_float ys[2] = {10,11}; @@ -480,7 +506,7 @@ void ShowDemoWindow(bool* p_open) { ImPlot::PushStyleColor(ImPlotCol_MarkerFill, ImVec4(1,1,1,1)); ImPlot::PushStyleColor(ImPlotCol_Line, ImVec4(0,0,0,1)); ImPlot::PlotLine("Circle|Cross", xs, ys, 2); - ImPlot::PopStyleVar(4); + ImPlot::PopStyleVar(6); ImPlot::PopStyleColor(3); ImPlot::PlotText("Filled Markers", 1.5, 11.75);