Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AlignedPlots: Y/X axis padding over multiple plots #144

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions implot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1443,10 +1443,20 @@ bool BeginPlot(const char* title, const char* x_label, const char* y1_label, con

const bool show_x_label = x_label && !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoLabel);

const float pad_top = title_size.x > 0.0f ? txt_height + gp.Style.LabelPadding.y : 0;
const float pad_bot = (plot.XAxis.IsLabeled() ? txt_height + gp.Style.LabelPadding.y + (plot.XAxis.IsTime() ? txt_height + gp.Style.LabelPadding.y : 0) : 0)
float pad_top = title_size.x > 0.0f ? txt_height + gp.Style.LabelPadding.y : 0;
float pad_bot = (plot.XAxis.IsLabeled() ? txt_height + gp.Style.LabelPadding.y + (plot.XAxis.IsTime() ? txt_height + gp.Style.LabelPadding.y : 0) : 0)
+ (show_x_label ? txt_height + gp.Style.LabelPadding.y : 0);

// (1*) align plots group
if (gp.CurrentAlignPlotGroup) {
ImAlignPlotGroupData &alignedPlotGroup = *gp.CurrentAlignPlotGroup;
if (ImHasFlag(alignedPlotGroup.orientation, ImPlotOrientation_Horizontal)) {
if (alignedPlotGroup.pad_top_max < pad_top) alignedPlotGroup.pad_top_max = pad_top;
if (pad_top < alignedPlotGroup.pad_top) pad_top = alignedPlotGroup.pad_top;
if (alignedPlotGroup.pad_bot_max < pad_bot) alignedPlotGroup.pad_bot_max = pad_bot;
if (pad_bot < alignedPlotGroup.pad_bot) pad_bot = alignedPlotGroup.pad_bot;
}
}
const float plot_height = plot.CanvasRect.GetHeight() - pad_top - pad_bot;

// (2) get y tick labels (needed for left/right pad)
Expand All @@ -1464,14 +1474,25 @@ bool BeginPlot(const char* title, const char* x_label, const char* y1_label, con
const bool show_y2_label = y2_label && !ImHasFlag(plot.YAxis[1].Flags, ImPlotAxisFlags_NoLabel);
const bool show_y3_label = y3_label && !ImHasFlag(plot.YAxis[2].Flags, ImPlotAxisFlags_NoLabel);

const float pad_left = (show_y1_label ? txt_height + gp.Style.LabelPadding.x : 0)
float pad_left = (show_y1_label ? txt_height + gp.Style.LabelPadding.x : 0)
+ (plot.YAxis[0].IsLabeled() ? gp.YTicks[0].MaxWidth + gp.Style.LabelPadding.x : 0);
const float pad_right = ((plot.YAxis[1].Present && plot.YAxis[1].IsLabeled()) ? gp.YTicks[1].MaxWidth + gp.Style.LabelPadding.x : 0)
float pad_right = ((plot.YAxis[1].Present && plot.YAxis[1].IsLabeled()) ? gp.YTicks[1].MaxWidth + gp.Style.LabelPadding.x : 0)
+ ((plot.YAxis[1].Present && show_y2_label) ? txt_height + gp.Style.LabelPadding.x : 0)
+ ((plot.YAxis[1].Present && plot.YAxis[2].Present) ? gp.Style.LabelPadding.x + gp.Style.MinorTickLen.y : 0)
+ ((plot.YAxis[2].Present && plot.YAxis[2].IsLabeled()) ? gp.YTicks[2].MaxWidth + gp.Style.LabelPadding.x : 0)
+ ((plot.YAxis[2].Present && show_y3_label) ? txt_height + gp.Style.LabelPadding.x : 0);

// (3*) align plots group
if (gp.CurrentAlignPlotGroup) {
ImAlignPlotGroupData &alignedPlotGroup = *gp.CurrentAlignPlotGroup;
if (ImHasFlag(alignedPlotGroup.orientation, ImPlotOrientation_Vertical)) {
if (alignedPlotGroup.pad_left_max < pad_left) alignedPlotGroup.pad_left_max = pad_left;
if (pad_left < alignedPlotGroup.pad_left) pad_left = alignedPlotGroup.pad_left;
if (alignedPlotGroup.pad_right_max < pad_right) alignedPlotGroup.pad_right_max = pad_right;
if (pad_right < alignedPlotGroup.pad_right) pad_right = alignedPlotGroup.pad_right;
}
}

const float plot_width = plot.CanvasRect.GetWidth() - pad_left - pad_right;

// (4) get x ticks
Expand Down Expand Up @@ -1943,6 +1964,27 @@ bool BeginPlot(const char* title, const char* x_label, const char* y1_label, con
return true;
}

bool BeginAlignedPlots(const char* group_id, ImPlotOrientation orientation) {
IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?");

ImPlotContext& gp = *GImPlot;
ImGuiContext &G = *GImGui;
ImGuiWindow * Window = G.CurrentWindow;
if (Window->SkipItems)
return false;

const ImGuiID ID = Window->GetID(group_id);
gp.CurrentAlignPlotGroup = gp.AlignPlotGroup.GetOrAddByKey(ID);
ImAlignPlotGroupData &alignedPlotGroup = *gp.CurrentAlignPlotGroup;

alignedPlotGroup.orientation = orientation;
alignedPlotGroup.pad_top_max = 0.0f;
alignedPlotGroup.pad_bot_max = 0.0f;
alignedPlotGroup.pad_left_max = 0.0f;
alignedPlotGroup.pad_right_max = 0.0f;
return true;
}

//-----------------------------------------------------------------------------
// Context Menu
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -2553,6 +2595,20 @@ void EndPlot() {
Reset(GImPlot);
}

void EndAlignedPlots() {
IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?");
ImPlotContext& gp = *GImPlot;
if (gp.CurrentAlignPlotGroup) {
ImAlignPlotGroupData &alignedPlotGroup = *gp.CurrentAlignPlotGroup;
alignedPlotGroup.pad_top = alignedPlotGroup.pad_top_max;
alignedPlotGroup.pad_bot = alignedPlotGroup.pad_bot_max;
alignedPlotGroup.pad_left = alignedPlotGroup.pad_left_max;
alignedPlotGroup.pad_right = alignedPlotGroup.pad_right_max;
}
// nullify align plot group
gp.CurrentAlignPlotGroup = NULL;
}

//-----------------------------------------------------------------------------
// MISC API
//-----------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions implot.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ IMPLOT_API bool BeginPlot(const char* title_id,
// of an if statement conditioned on BeginPlot().
IMPLOT_API void EndPlot();

// Align axis padding over multiple plots. If this function returns true, EndAlignedPlots() must
// be called, e.g. "if (BeginAlignedPlots(...)) { ... EndAlignedPlots(); }". #group_id must
// be unique.
IMPLOT_API bool BeginAlignedPlots(const char* group_id, ImPlotOrientation orientation = ImPlotOrientation_Vertical);
// Only call EndAlignedPlots() if BeginAlignedPlots() returns true! Typically called at the end
// of an if statement conditioned on BeginAlignedPlots().
IMPLOT_API void EndAlignedPlots();

//-----------------------------------------------------------------------------
// Plot Items
//-----------------------------------------------------------------------------
Expand Down
47 changes: 47 additions & 0 deletions implot_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,53 @@ void ShowDemoWindow(bool* p_open) {
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Align plot group")) {
static double xmin = 0, xmax = 1;
int data[2] = {0,1};
static int orientation = 0;
static float hzWidth = (ImGui::GetWindowSize().x / 3) - 20;
ImGui::Combo("Orientation", &orientation, "Vertical\0Horizontal\0");
if (ImPlot::BeginAlignedPlots("Align_Demo", (orientation == 0) ? ImPlotOrientation_Vertical : ImPlotOrientation_Horizontal)) {
static bool showA = true;
ImGui::Checkbox("A", &showA); ImGui::SameLine();
static bool showB = true;
ImGui::Checkbox("B", &showB); ImGui::SameLine();
static bool showC = false;
ImGui::Checkbox("C", &showC);
if (showA) {
ImPlot::SetNextPlotLimitsY(-1000, 1000, ImGuiCond_Once, 0);
ImPlot::LinkNextPlotLimits(&xmin, &xmax, NULL, NULL);
if (ImPlot::BeginPlot("Plot A", NULL, NULL, ImVec2((orientation == 0) ? -1 : hzWidth, 200))) {
ImPlot::PlotLine("Line",data,2);
ImPlot::EndPlot();
}
}
if (showB) {
if (orientation != 0) ImGui::SameLine();
ImPlot::LinkNextPlotLimits(&xmin, &xmax, NULL, NULL);
if (ImPlot::BeginPlot((orientation == 0) ? "Plot B" : "##Plot B", NULL, NULL, ImVec2((orientation == 0) ? -1 : hzWidth, 200), ImPlotFlags_YAxis2)) {
ImPlot::PlotLine("Line",data,2);
ImPlot::EndPlot();
}
}
if (showC) {
if (orientation != 0) ImGui::SameLine();
static int yAxis = 0;
ImPlot::LinkNextPlotLimits(&xmin, &xmax, NULL, NULL);
ImPlot::SetNextPlotLimitsY(-100000, 100000, ImGuiCond_Once, 0);
ImPlot::SetNextPlotLimitsY(-100000, 100000, ImGuiCond_Once, 1);
ImPlot::SetNextPlotLimitsY(-100000, 100000, ImGuiCond_Once, 2);
if (ImPlot::BeginPlot("Plot C", (orientation == 0) ? NULL : "X axis label", "Y axis label", ImVec2((orientation == 0) ? -1 : hzWidth, 200), ((yAxis > 0 ) ? ImPlotFlags_YAxis2 : 0) | ((yAxis > 1 ) ? ImPlotFlags_YAxis3 : 0))) {
ImPlot::PlotLine("Line",data,2);
ImPlot::EndPlot();
}
ImGui::SetNextItemWidth(150);
ImGui::Combo("Y axis", &yAxis, "Y1\0Y1, Y2\0Y1, Y2, Y3\0");
}
ImPlot::EndAlignedPlots();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Querying")) {
static ImVector<ImPlotPoint> data;
static ImPlotLimits range, query;
Expand Down
33 changes: 33 additions & 0 deletions implot_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,35 @@ struct ImPlotNextItemData {
}
};

// Align plots group data
struct ImAlignPlotGroupData {
ImPlotOrientation orientation;

float pad_top;
float pad_bot;
float pad_top_max;
float pad_bot_max;

float pad_left;
float pad_right;
float pad_left_max;
float pad_right_max;

ImAlignPlotGroupData() {
orientation = ImPlotOrientation_Vertical;

pad_top = 0;
pad_bot = 0;
pad_top_max = 0;
pad_bot_max = 0;

pad_left = 0;
pad_right = 0;
pad_left_max = 0;
pad_right_max = 0;
}
};

// Holds state information that must persist between calls to BeginPlot()/EndPlot()
struct ImPlotContext {
// Plot States
Expand Down Expand Up @@ -714,6 +743,10 @@ struct ImPlotContext {
ImPlotNextItemData NextItemData;
ImPlotInputMap InputMap;
ImPlotPoint MousePos[IMPLOT_Y_AXES];

// Align plots
ImPool<ImAlignPlotGroupData> AlignPlotGroup;
ImAlignPlotGroupData* CurrentAlignPlotGroup;
};

//-----------------------------------------------------------------------------
Expand Down