1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-10-09 23:57:26 -04:00

add FitNextPlotAxes

This commit is contained in:
epezent 2020-08-19 23:50:12 -05:00
parent ff29f1ebcc
commit 7da6ed69f3
4 changed files with 35 additions and 8 deletions

View File

@ -227,7 +227,8 @@ void Reset(ImPlotContext* ctx) {
ctx->YTicks[i].shrink(0); ctx->YTicks[i].shrink(0);
ctx->YTickLabels[i].Buf.shrink(0); ctx->YTickLabels[i].Buf.shrink(0);
} }
// reset extents // reset extents/fit
ctx->FitThisFrame = false;
ctx->FitX = false; ctx->FitX = false;
ctx->ExtentsX.Min = HUGE_VAL; ctx->ExtentsX.Min = HUGE_VAL;
ctx->ExtentsX.Max = -HUGE_VAL; ctx->ExtentsX.Max = -HUGE_VAL;
@ -971,19 +972,25 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons
plot.QueryRect = ImRect(0,0,0,0); plot.QueryRect = ImRect(0,0,0,0);
} }
// DOUBLE CLICK ----------------------------------------------------------- // FIT -----------------------------------------------------------
if ( IO.MouseDoubleClicked[gp.InputMap.FitButton] && gp.Hov_Frame && (plot.XAxis.HoveredTot || any_hov_y_axis_region) && !hov_legend && !hov_query) { // fit from double click
if ( IO.MouseDoubleClicked[gp.InputMap.FitButton] && gp.Hov_Frame && (plot.XAxis.HoveredTot || any_hov_y_axis_region) && !hov_legend && !hov_query ) {
gp.FitThisFrame = true; gp.FitThisFrame = true;
gp.FitX = plot.XAxis.HoveredTot; gp.FitX = plot.XAxis.HoveredTot;
for (int i = 0; i < IMPLOT_Y_AXES; i++) for (int i = 0; i < IMPLOT_Y_AXES; i++)
gp.FitY[i] = plot.YAxis[i].HoveredTot; gp.FitY[i] = plot.YAxis[i].HoveredTot;
} }
else { // fit from FitNextPlotAxes
gp.FitThisFrame = false; if (gp.NextPlotData.FitX) {
gp.FitX = false; gp.FitThisFrame = true;
for (int i = 0; i < IMPLOT_Y_AXES; i++) gp.FitX = true;
gp.FitY[i] = false; }
for (int i = 0; i < IMPLOT_Y_AXES; ++i) {
if (gp.NextPlotData.FitY[i]) {
gp.FitThisFrame = true;
gp.FitY[i] = true;
}
} }
// FOCUS ------------------------------------------------------------------ // FOCUS ------------------------------------------------------------------
@ -1554,6 +1561,15 @@ void SetNextPlotLimitsY(double y_min, double y_max, ImGuiCond cond, int y_axis)
gp.NextPlotData.Y[y_axis].Max = y_max; gp.NextPlotData.Y[y_axis].Max = y_max;
} }
void FitNextPlotAxes(bool x, bool y, bool y2, bool y3) {
ImPlotContext& gp = *GImPlot;
IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "FitNextPlotAxes() needs to be called before BeginPlot()!");
gp.NextPlotData.FitX = x;
gp.NextPlotData.FitY[0] = y;
gp.NextPlotData.FitY[1] = y2;
gp.NextPlotData.FitY[2] = y3;
}
void SetNextPlotTicksX(const double* values, int n_ticks, const char** labels, bool show_default) { void SetNextPlotTicksX(const double* values, int n_ticks, const char** labels, bool show_default) {
ImPlotContext& gp = *GImPlot; ImPlotContext& gp = *GImPlot;
IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "SetNextPlotTicksX() needs to be called before BeginPlot()!"); IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "SetNextPlotTicksX() needs to be called before BeginPlot()!");

View File

@ -382,6 +382,8 @@ void SetNextPlotLimits(double x_min, double x_max, double y_min, double y_max, I
void SetNextPlotLimitsX(double x_min, double x_max, ImGuiCond cond = ImGuiCond_Once); void SetNextPlotLimitsX(double x_min, double x_max, ImGuiCond cond = ImGuiCond_Once);
// Set the Y axis range limits of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the Y axis limits will be locked. // Set the Y axis range limits of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the Y axis limits will be locked.
void SetNextPlotLimitsY(double y_min, double y_max, ImGuiCond cond = ImGuiCond_Once, int y_axis = 0); void SetNextPlotLimitsY(double y_min, double y_max, ImGuiCond cond = ImGuiCond_Once, int y_axis = 0);
// Fits the next plot axes if they are unlocked.
void FitNextPlotAxes(bool x = true, bool y = true, bool y2 = true, bool y3 = true);
// Set the X axis ticks and optionally the labels for the next plot. // Set the X axis ticks and optionally the labels for the next plot.
void SetNextPlotTicksX(const double* values, int n_ticks, const char** labels = NULL, bool show_default = false); void SetNextPlotTicksX(const double* values, int n_ticks, const char** labels = NULL, bool show_default = false);

View File

@ -600,6 +600,11 @@ void ShowDemoWindow(bool* p_open) {
ImGui::ColorEdit4("##Col2", &y2_col.x, ImGuiColorEditFlags_NoInputs); ImGui::ColorEdit4("##Col2", &y2_col.x, ImGuiColorEditFlags_NoInputs);
ImGui::SameLine(); ImGui::SameLine();
ImGui::ColorEdit4("##Col3", &y3_col.x, ImGuiColorEditFlags_NoInputs); ImGui::ColorEdit4("##Col3", &y3_col.x, ImGuiColorEditFlags_NoInputs);
// you can fit axes programatically
ImGui::SameLine(); if (ImGui::Button("Fit X")) ImPlot::FitNextPlotAxes(true, false, false, false);
ImGui::SameLine(); if (ImGui::Button("Fit Y")) ImPlot::FitNextPlotAxes(false, true, false, false);
ImGui::SameLine(); if (ImGui::Button("Fit Y2")) ImPlot::FitNextPlotAxes(false, false, true, false);
ImGui::SameLine(); if (ImGui::Button("Fit Y3")) ImPlot::FitNextPlotAxes(false, false, false, true);
for (int i = 0; i < 1001; ++i) { for (int i = 0; i < 1001; ++i) {
xs[i] = (i*0.1f); xs[i] = (i*0.1f);
ys1[i] = Sin(xs[i]) * 3 + 1; ys1[i] = Sin(xs[i]) * 3 + 1;

View File

@ -299,13 +299,17 @@ struct ImPlotNextPlotData
bool HasYRange[IMPLOT_Y_AXES]; bool HasYRange[IMPLOT_Y_AXES];
bool ShowDefaultTicksX; bool ShowDefaultTicksX;
bool ShowDefaultTicksY[IMPLOT_Y_AXES]; bool ShowDefaultTicksY[IMPLOT_Y_AXES];
bool FitX;
bool FitY[IMPLOT_Y_AXES];
ImPlotNextPlotData() { ImPlotNextPlotData() {
HasXRange = false; HasXRange = false;
ShowDefaultTicksX = true; ShowDefaultTicksX = true;
FitX = false;
for (int i = 0; i < IMPLOT_Y_AXES; ++i) { for (int i = 0; i < IMPLOT_Y_AXES; ++i) {
HasYRange[i] = false; HasYRange[i] = false;
ShowDefaultTicksY[i] = true; ShowDefaultTicksY[i] = true;
FitY[i] = false;
} }
} }
}; };