1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-23 02:38:53 -05:00

Refactored to only draw the horizontal bars, updated demo

This commit is contained in:
Adriano Parisi 2020-06-08 20:45:31 +01:00
parent 4293345c54
commit 1d11b1a1a3
3 changed files with 24 additions and 52 deletions

View File

@ -2920,29 +2920,6 @@ void PlotErrorBars(const char* label_id, const double* xs, const double* ys, con
// PLOT ERROR BARS H // 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 <typename T>
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 <typename Getter> template <typename Getter>
void PlotErrorBarsHEx(const char* label_id, Getter getter) { void PlotErrorBarsHEx(const char* label_id, Getter getter) {
IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PlotErrorBars() needs to be called between BeginPlot() and EndPlot()!"); 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 // find data extents
if (gp.FitThisFrame) { if (gp.FitThisFrame) {
for (int i = 0; i < getter.Count; ++i) { for (int i = 0; i < getter.Count; ++i) {
ImPlotPointErrorH e = getter(i); ImPlotPointError e = getter(i);
FitPoint(ImPlotPoint(e.x, e.y - e.neg_v)); FitPoint(ImPlotPoint(e.x - e.neg, e.y));
FitPoint(ImPlotPoint(e.x, e.y + e.pos_v)); FitPoint(ImPlotPoint(e.x + e.pos, e.y));
FitPoint(ImPlotPoint(e.x - e.neg_h, e.y));
FitPoint(ImPlotPoint(e.x + e.pos_h, e.y));
} }
} }
for (int i = 0; i < getter.Count; ++i) { for (int i = 0; i < getter.Count; ++i) {
ImPlotPointErrorH e = getter(i); ImPlotPointError e = getter(i);
ImVec2 p1 = PlotToPixels(e.x, e.y - e.neg_v); ImVec2 p1 = PlotToPixels(e.x - e.neg, e.y);
ImVec2 p2 = PlotToPixels(e.x, e.y + e.pos_v); ImVec2 p2 = PlotToPixels(e.x + e.pos, e.y);
ImVec2 p3 = PlotToPixels(e.x - e.neg_h, e.y);
ImVec2 p4 = PlotToPixels(e.x + e.pos_h, e.y);
DrawList.AddLine(p1, p2, col, gp.Style.ErrorBarWeight); DrawList.AddLine(p1, p2, col, gp.Style.ErrorBarWeight);
DrawList.AddLine(p3, p4, col, gp.Style.ErrorBarWeight);
if (rend_whisker) { if (rend_whisker) {
DrawList.AddLine(p1 - ImVec2(half_whisker, 0), p1 + ImVec2(half_whisker, 0), col, gp.Style.ErrorBarWeight); DrawList.AddLine(p1 - ImVec2(0, half_whisker), p1 + ImVec2(0, half_whisker), col, gp.Style.ErrorBarWeight);
DrawList.AddLine(p2 - ImVec2(half_whisker, 0), p2 + ImVec2(half_whisker, 0), col, gp.Style.ErrorBarWeight); DrawList.AddLine(p2 - ImVec2(0, half_whisker), p2 + ImVec2(0, half_whisker), 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);
} }
} }
PopPlotClipRect(); PopPlotClipRect();
@ -2993,26 +2963,26 @@ void PlotErrorBarsHEx(const char* label_id, Getter getter) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// float // 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) { void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* err, int count, int offset, int stride) {
GetterErrorH<float> getter(xs, ys, err_v, err_v, err_h, err_h, count, offset, stride); GetterError<float> getter(xs, ys, err, err, count, offset, stride);
PlotErrorBarsHEx(label_id, getter); 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) { void PlotErrorBarsH(const char* label_id, const float* xs, const float* ys, const float* neg, const float* pos, int count, int offset, int stride) {
GetterErrorH<float> getter(xs, ys, neg_v, pos_v, neg_h, pos_h, count, offset, stride); GetterError<float> getter(xs, ys, neg, pos, count, offset, stride);
PlotErrorBarsHEx(label_id, getter); PlotErrorBarsHEx(label_id, getter);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// double // 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) { void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* err, int count, int offset, int stride) {
GetterErrorH<double> getter(xs, ys, err_v, err_v, err_h, err_h, count, offset, stride); GetterError<double> getter(xs, ys, err, err, count, offset, stride);
PlotErrorBarsHEx(label_id, getter); 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) { void PlotErrorBarsH(const char* label_id, const double* xs, const double* ys, const double* neg, const double* pos, int count, int offset, int stride) {
GetterErrorH<double> getter(xs, ys, neg_v, pos_v, neg_h, pos_h, count, offset, stride); GetterError<double> getter(xs, ys, neg, pos, count, offset, stride);
PlotErrorBarsHEx(label_id, getter); PlotErrorBarsHEx(label_id, getter);
} }

View File

@ -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 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)); 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 // Plots 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 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_v, const double* err_h, int count, int offset = 0, int stride = sizeof(double)); 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_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 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_v, const double* pos_v, const double* neg_h, const double* pos_h, int count, int offset = 0, int stride = sizeof(double)); 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. // 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 float* values, int count, float x, float y, float radius, bool normalize = false, const char* label_fmt = "%.1f", float angle0 = 90);

View File

@ -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 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 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 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); ImPlot::SetNextPlotLimits(0, 6, 0, 10);
if (ImPlot::BeginPlot("##ErrorBars",NULL,NULL)) { if (ImPlot::BeginPlot("##ErrorBars",NULL,NULL)) {
ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f); 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_Marker, ImPlotMarker_Square);
ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, 3); ImPlot::PushStyleVar(ImPlotStyleVar_MarkerSize, 3);
ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImVec4(0, 1, 0, 1)); 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::PlotLine("Line2##ErrorBarH", xs, lin2, 5);
ImPlot::PopStyleVar(2); ImPlot::PopStyleVar(2);
ImPlot::PopStyleColor(); ImPlot::PopStyleColor();