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

Merge pull request #25 from jpieper/20200510-multi_y_axis

Support multiple Y axes simultaneously
This commit is contained in:
Evan Pezent 2020-05-10 21:04:38 -05:00 committed by GitHub
commit bece676929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 795 additions and 465 deletions

1091
implot.cpp

File diff suppressed because it is too large Load Diff

View File

@ -37,16 +37,17 @@ typedef int ImMarker;
// Options for plots
enum ImPlotFlags_ {
ImPlotFlags_MousePos = 1 << 0, // the mouse position, in plot coordinates, will be displayed in the bottom-right
ImPlotFlags_Legend = 1 << 1, // a legend will be displayed in the top-left
ImPlotFlags_Highlight = 1 << 2, // plot items will be highlighted when their legend entry is hovered
ImPlotFlags_Selection = 1 << 3, // the user will be able to box-select with right-mouse
ImPlotFlags_PixelQuery = 1 << 4, // query ranges will not change their pixel position if the plot is scrolled/zoomed
ImPlotFlags_ContextMenu = 1 << 5, // the user will be able to open a context menu with double-right click
ImPlotFlags_Crosshairs = 1 << 6, // the default mouse cursor will be replaced with a crosshair when hovered
ImPlotFlags_CullData = 1 << 7, // plot data outside the plot area will be culled from rendering
ImPlotFlags_AntiAliased = 1 << 8, // lines and fills will be anti-aliased (not recommended)
ImPlotFlags_NoChild = 1 << 9, // a child window region will not be used to capture mouse scroll (can boost performance for single ImGui window applications)
ImPlotFlags_MousePos = 1 << 0, // the mouse position, in plot coordinates, will be displayed in the bottom-right
ImPlotFlags_Legend = 1 << 1, // a legend will be displayed in the top-left
ImPlotFlags_Highlight = 1 << 2, // plot items will be highlighted when their legend entry is hovered
ImPlotFlags_Selection = 1 << 3, // the user will be able to box-select with right-mouse
ImPlotFlags_ContextMenu = 1 << 4, // the user will be able to open a context menu with double-right click
ImPlotFlags_Crosshairs = 1 << 5, // the default mouse cursor will be replaced with a crosshair when hovered
ImPlotFlags_CullData = 1 << 6, // plot data outside the plot area will be culled from rendering
ImPlotFlags_AntiAliased = 1 << 7, // lines and fills will be anti-aliased (not recommended)
ImPlotFlags_NoChild = 1 << 8, // a child window region will not be used to capture mouse scroll (can boost performance for single ImGui window applications)
ImPlotFlags_Y2Axis = 1 << 9, // enable a second y axis
ImPlotFlags_Y3Axis = 1 << 10, // enable a third y axis
ImPlotFlags_Default = ImPlotFlags_MousePos | ImPlotFlags_Legend | ImPlotFlags_Highlight | ImPlotFlags_Selection | ImPlotFlags_ContextMenu | ImPlotFlags_CullData
};
@ -61,7 +62,8 @@ enum ImAxisFlags_ {
ImAxisFlags_Adaptive = 1 << 6, // grid divisions will adapt to the current pixel size the axis
ImAxisFlags_LogScale = 1 << 7, // a logartithmic (base 10) axis scale will be used
ImAxisFlags_Scientific = 1 << 8, // scientific notation will be used for tick labels if displayed (WIP, not very good yet)
ImAxisFlags_Default = ImAxisFlags_GridLines | ImAxisFlags_TickMarks | ImAxisFlags_TickLabels | ImAxisFlags_Adaptive
ImAxisFlags_Default = ImAxisFlags_GridLines | ImAxisFlags_TickMarks | ImAxisFlags_TickLabels | ImAxisFlags_Adaptive,
ImAxisFlags_Auxiliary_Default = ImAxisFlags_Default & ~ImAxisFlags_GridLines,
};
// Plot styling colors
@ -75,7 +77,9 @@ enum ImPlotCol_ {
ImPlotCol_PlotBg, // plot area background color (defaults to ImGuiCol_WindowBg)
ImPlotCol_PlotBorder, // plot area border color (defaults to ImGuiCol_Text)
ImPlotCol_XAxis, // x-axis grid/label color (defaults to ImGuiCol_Text)
ImPlotCol_YAxis, // x-axis grid/label color (defaults to ImGuiCol_Text)
ImPlotCol_YAxis, // y-axis grid/label color (defaults to ImGuiCol_Text)
ImPlotCol_Y2Axis, // y2-axis grid/label color (defaults to ImGuiCol_Text)
ImPlotCol_Y3Axis, // y3-axis grid/label color (defaults to ImGuiCol_Text)
ImPlotCol_Selection, // box-selection color (defaults to yellow)
ImPlotCol_Query, // box-query color (defaults to green)
ImPlotCol_COUNT
@ -107,11 +111,18 @@ enum ImMarker_ {
ImMarker_Asterisk = 1 << 10, // a asterisk marker will be rendered at each point (not filled)
};
/// Plot range utility struct
struct ImPlotRange {
float XMin, XMax, YMin, YMax;
float Min, Max;
ImPlotRange();
bool Contains(const ImVec2& p);
bool Contains(float) const;
float Size() const;
};
/// Plot range utility struct
struct ImPlotBounds {
ImPlotRange X, Y;
ImPlotBounds();
bool Contains(const ImVec2& p) const;
};
// Plot style structure
@ -137,15 +148,17 @@ namespace ImGui {
// be called, e.g. "if (BeginPlot(...)) { ... EndPlot(); }"". #title_id must
// be unique. If you need to avoid ID collisions or don't want to display a
// title in the plot, use double hashes (e.g. "MyPlot##Hidden"). If #x_label
// and/or #y_label are provided, axes labels will be displayed. Flags are only
// set ONCE during the first call to BeginPlot.
// and/or #y_label are provided, axes labels will be displayed. Axis flags are
// only set ONCE during the first call to BeginPlot.
bool BeginPlot(const char* title_id,
const char* x_label = NULL,
const char* y_label = NULL,
const ImVec2& size = ImVec2(-1,-1),
ImPlotFlags flags = ImPlotFlags_Default,
ImAxisFlags x_flags = ImAxisFlags_Default,
ImAxisFlags y_flags = ImAxisFlags_Default);
ImAxisFlags y_flags = ImAxisFlags_Default,
ImAxisFlags y2_flags = ImAxisFlags_Auxiliary_Default,
ImAxisFlags y3_flags = ImAxisFlags_Auxiliary_Default);
// Only call EndPlot() if BeginPlot() returns true! Typically called at the end
// of an if statement conditioned on BeginPlot().
void EndPlot();
@ -185,14 +198,14 @@ void PlotDigital(const char* label_id, ImVec2 (*getter)(void* data, int idx), vo
/// Returns true if the plot area in the current or most recent plot is hovered.
bool IsPlotHovered();
/// Returns the mouse position in x,y coordinates of the current or most recent plot.
ImVec2 GetPlotMousePos();
/// Returns the current or most recent plot axis range.
ImPlotRange GetPlotRange();
/// Returns the mouse position in x,y coordinates of the current or most recent plot. A negative y_axis uses the current value of SetPlotYAxis (0 initially).
ImVec2 GetPlotMousePos(int y_axis = -1);
/// Returns the current or most recent plot axis range. A negative y_axis uses the current value of SetPlotYAxis (0 initially).
ImPlotBounds GetPlotBounds(int y_axis = -1);
/// Returns true if the current or most recent plot is being queried.
bool IsPlotQueried();
/// Returns the current or most recent plot querey range.
ImPlotRange GetPlotQuery();
/// Returns the current or most recent plot query bounds.
ImPlotBounds GetPlotQuery(int y_axis = -1);
//-----------------------------------------------------------------------------
// Plot Styling
@ -225,21 +238,24 @@ void PopPlotStyleVar(int count = 1);
//-----------------------------------------------------------------------------
/// Set the axes ranges of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axes will be locked.
void SetNextPlotRange(float x_min, float x_max, float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once);
void SetNextPlotBounds(float x_min, float x_max, float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once);
/// Set the X axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked.
void SetNextPlotRangeX(float x_min, float x_max, ImGuiCond cond = ImGuiCond_Once);
void SetNextPlotBoundsX(float x_min, float x_max, ImGuiCond cond = ImGuiCond_Once);
/// Set the Y axis range of the next plot. Call right before BeginPlot(). If ImGuiCond_Always is used, the axis will be locked.
void SetNextPlotRangeY(float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once);
void SetNextPlotBoundsY(float y_min, float y_max, ImGuiCond cond = ImGuiCond_Once, int y_axis = 0);
/// Select which Y axis will be used for subsequent plot elements. The default is '0', or the first Y axis.
void SetPlotYAxis(int);
// Get the current Plot position (top-left) in pixels.
ImVec2 GetPlotPos();
// Get the curent Plot size in pixels.
ImVec2 GetPlotSize();
// Convert pixels to a position in the current plot's coordinate system.
ImVec2 PixelsToPlot(const ImVec2& pix);
// Convert a position in the current plot's coordinate system to pixels.
ImVec2 PlotToPixels(const ImVec2& plt);
// Convert pixels to a position in the current plot's coordinate system. A negative y_axis uses the current value of SetPlotYAxis (0 initially).
ImVec2 PixelsToPlot(const ImVec2& pix, int y_axis = -1);
// Convert a position in the current plot's coordinate system to pixels. A negative y_axis uses the current value of SetPlotYAxis (0 initially).
ImVec2 PlotToPixels(const ImVec2& plt, int y_axis = -1);
// Push clip rect for rendering to current plot area
void PushPlotClipRect();

View File

@ -176,9 +176,9 @@ void ShowImPlotDemoWindow(bool* p_open) {
static bool horz = false;
ImGui::Checkbox("Horizontal",&horz);
if (horz)
ImGui::SetNextPlotRange(0, 110, -0.5f, 9.5f, ImGuiCond_Always);
ImGui::SetNextPlotBounds(0, 110, -0.5f, 9.5f, ImGuiCond_Always);
else
ImGui::SetNextPlotRange(-0.5f, 9.5f, 0, 110, ImGuiCond_Always);
ImGui::SetNextPlotBounds(-0.5f, 9.5f, 0, 110, ImGuiCond_Always);
if (ImGui::BeginPlot("Bar Plot", horz ? "Score": "Student", horz ? "Student" : "Score", {-1, 300})) {
static float midtm[10] = {83, 67, 23, 89, 83, 78, 91, 82, 85, 90};
static float final[10] = {80, 62, 56, 99, 55, 78, 88, 78, 90, 100};
@ -203,7 +203,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
float bar[5] = {1,2,5,3,4};
float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f};
float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f};
ImGui::SetNextPlotRange(0, 6, 0, 10);
ImGui::SetNextPlotBounds(0, 6, 0, 10);
if (ImGui::BeginPlot("##ErrorBars",NULL,NULL,ImVec2(-1,300))) {
ImGui::PlotBar("Bar", xs, bar, 5, 0.5f);
@ -227,7 +227,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
ImVec2 center(0.5f,0.5f); // in plot units, not pixels
float radius = 0.4f; // in plot units, not pixels
SetNextPlotRange(0,1,0,1,ImGuiCond_Always);
SetNextPlotBounds(0,1,0,1,ImGuiCond_Always);
if (ImGui::BeginPlot("##Pie1", NULL, NULL, ImVec2(250,250), ImPlotFlags_Legend, 0, 0)) {
ImGui::PlotPieChart(labels1, pre_normalized, 4, center, radius);
ImGui::EndPlot();
@ -242,7 +242,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
{0.7412f, 0.0f, 0.1490f, 1.0f},
};
ImGui::SetPlotPalette(YlOrRd, 5);
SetNextPlotRange(0,1,0,1,ImGuiCond_Always);
SetNextPlotBounds(0,1,0,1,ImGuiCond_Always);
static const char* labels2[] = {"One","Two","Three","Four","Five"};
static float not_normalized[] = {1,2,3,4,5};
if (ImGui::BeginPlot("##Pie2", NULL, NULL, ImVec2(250,250), ImPlotFlags_Legend, 0, 0)) {
@ -268,14 +268,14 @@ void ShowImPlotDemoWindow(bool* p_open) {
sdata2.AddPoint(t, mouse.y * 0.0005f);
rdata2.AddPoint(t, mouse.y * 0.0005f);
}
ImGui::SetNextPlotRangeX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
ImGui::SetNextPlotBoundsX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
static int rt_axis = ImAxisFlags_Default & ~ImAxisFlags_TickLabels;
if (ImGui::BeginPlot("##Scrolling", NULL, NULL, {-1,150}, ImPlotFlags_Default, rt_axis, rt_axis)) {
ImGui::Plot("Data 1", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), sdata1.Offset, 2 * sizeof(float));
ImGui::Plot("Data 2", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), sdata2.Offset, 2 * sizeof(float));
ImGui::EndPlot();
}
ImGui::SetNextPlotRangeX(0, 10, ImGuiCond_Always);
ImGui::SetNextPlotBoundsX(0, 10, ImGuiCond_Always);
if (ImGui::BeginPlot("##Rolling", NULL, NULL, {-1,150}, ImPlotFlags_Default, rt_axis, rt_axis)) {
ImGui::Plot("Data 1", &rdata1.Data[0].x, &rdata1.Data[0].y, rdata1.Data.size(), 0, 2 * sizeof(float));
ImGui::Plot("Data 2", &rdata2.Data[0].x, &rdata2.Data[0].y, rdata2.Data.size(), 0, 2 * sizeof(float));
@ -285,7 +285,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Markers and Labels")) {
ImGui::SetNextPlotRange(0, 10, 0, 12);
ImGui::SetNextPlotBounds(0, 10, 0, 12);
if (ImGui::BeginPlot("##MarkerStyles", NULL, NULL, ImVec2(-1,300), 0, 0, 0)) {
float xs[2] = {1,4};
float ys[2] = {10,11};
@ -369,7 +369,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
ys2[i] = log(xs[i]);
ys3[i] = pow(10.0f, xs[i]);
}
ImGui::SetNextPlotRange(0.1f, 100, 0, 10);
ImGui::SetNextPlotBounds(0.1f, 100, 0, 10);
if (ImGui::BeginPlot("Log Plot", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default, ImAxisFlags_Default | ImAxisFlags_LogScale )) {
ImGui::Plot("f(x) = x", xs, xs, 1001);
ImGui::Plot("f(x) = sin(x)+1", xs, ys1, 1001);
@ -379,12 +379,51 @@ void ShowImPlotDemoWindow(bool* p_open) {
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Multiple Y Axes")) {
static float xs[1001], xs2[1001], ys1[1001], ys2[1001], ys3[1001];
static bool y2_axis = true;
static bool y3_axis = false;
for (int i = 0; i < 1001; ++i) {
xs[i] = (float)(i*0.1f);
ys1[i] = sin(xs[i]) * 3 + 1;
ys2[i] = cos(xs[i]) * 0.2 + 0.5;
ys3[i] = sin(xs[i]+.5) * 100 + 200;
xs2[i] = xs[i] + 10.0;
}
ImGui::SetNextPlotBounds(0.1f, 100, 0, 10);
ImGui::SetNextPlotBoundsY(0, 1, ImGuiCond_Once, 1);
ImGui::SetNextPlotBoundsY(0, 300, ImGuiCond_Once, 2);
if (ImGui::BeginPlot("Multi-Axis Plot", NULL, NULL, ImVec2(-1,300),
ImPlotFlags_Default |
(y2_axis ? ImPlotFlags_Y2Axis : 0) |
(y3_axis ? ImPlotFlags_Y3Axis : 0))) {
ImGui::Plot("f(x) = x", xs, xs, 1001);
ImGui::Plot("f(x) = sin(x)*3+1", xs, ys1, 1001);
if (y2_axis) {
ImGui::SetPlotYAxis(1);
ImGui::Plot("f(x) = cos(x)*.2+.5 (Y2)", xs, ys2, 1001);
}
if (y3_axis) {
ImGui::SetPlotYAxis(2);
ImGui::Plot("f(x) = sin(x+.5)*100+200 (Y3)", xs2, ys3, 1001);
}
ImGui::EndPlot();
}
ImGui::Checkbox("Y2 Axis", &y2_axis);
ImGui::SameLine();
ImGui::Checkbox("Y3 Axis", &y3_axis);
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Querying")) {
ImGui::BulletText("Ctrl + click in the plot area to draw points.");
ImGui::BulletText("Middle click (or Ctrl + right click) and drag to query points.");
ImGui::BulletText("Hold the Alt and/or Shift keys to expand the query range.");
static ImVector<ImVec2> data;
ImPlotRange range, query;
ImPlotBounds range, query;
if (ImGui::BeginPlot("##Drawing", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default, ImAxisFlags_GridLines, ImAxisFlags_GridLines)) {
if (ImGui::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl)
data.push_back(ImGui::GetPlotMousePos());
@ -393,7 +432,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
if (data.size() > 0)
ImGui::Plot("Points", &data[0].x, &data[0].y, data.size(), 0, 2 * sizeof(float));
if (ImGui::IsPlotQueried() && data.size() > 0) {
ImPlotRange range = ImGui::GetPlotQuery();
ImPlotBounds range = ImGui::GetPlotQuery();
int cnt = 0;
ImVec2 avg;
for (int i = 0; i < data.size(); ++i) {
@ -410,12 +449,12 @@ void ShowImPlotDemoWindow(bool* p_open) {
}
}
ImGui::PopPlotStyleVar(2);
range = ImGui::GetPlotRange();
range = ImGui::GetPlotBounds();
query = ImGui::GetPlotQuery();
ImGui::EndPlot();
}
ImGui::Text("The current plot range is: [%g,%g,%g,%g]", range.XMin, range.XMax, range.YMin, range.YMax);
ImGui::Text("The current query range is: [%g,%g,%g,%g]", query.XMin, query.XMax, query.YMin, query.YMax);
ImGui::Text("The current plot range is: [%g,%g,%g,%g]", range.X.Min, range.X.Max, range.Y.Min, range.Y.Max);
ImGui::Text("The current query range is: [%g,%g,%g,%g]", query.X.Min, query.X.Max, query.Y.Min, query.Y.Max);
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Views")) {
@ -436,9 +475,9 @@ void ShowImPlotDemoWindow(bool* p_open) {
}
ImGui::BulletText("Query the first plot to render a subview in the second plot.");
ImGui::BulletText("Toggle \"Pixel Query\" in the context menu and then pan the plot.");
ImGui::SetNextPlotRange(0,0.01f,-1,1);
ImGui::SetNextPlotBounds(0,0.01f,-1,1);
ImAxisFlags flgs = ImAxisFlags_Default & ~ImAxisFlags_TickLabels;
ImPlotRange query;
ImPlotBounds query;
if (ImGui::BeginPlot("##View1",NULL,NULL,ImVec2(-1,150), ImPlotFlags_Default, flgs, flgs)) {
ImGui::Plot("Signal 1", x_data, y_data1, 512);
ImGui::Plot("Signal 2", x_data, y_data2, 512);
@ -446,7 +485,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
query = ImGui::GetPlotQuery();
ImGui::EndPlot();
}
ImGui::SetNextPlotRange(query.XMin, query.XMax, query.YMin, query.YMax, ImGuiCond_Always);
ImGui::SetNextPlotBounds(query.X.Min, query.X.Max, query.Y.Min, query.Y.Max, ImGuiCond_Always);
if (ImGui::BeginPlot("##View2",NULL,NULL,ImVec2(-1,150), 0, 0, 0)) {
ImGui::Plot("Signal 1", x_data, y_data1, 512);
ImGui::Plot("Signal 2", x_data, y_data2, 512);
@ -504,7 +543,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
data[i].Data.back().y + (0.005f + 0.0002f * (float)rand() / float(RAND_MAX)) * (-1 + 2 * (float)rand() / float(RAND_MAX)));
}
}
ImGui::SetNextPlotRangeX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
ImGui::SetNextPlotBoundsX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
if (ImGui::BeginPlot("##DND", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default)) {
for (int i = 0; i < 10; ++i) {
if (show[i]) {
@ -605,8 +644,8 @@ void ShowImPlotDemoWindow(bool* p_open) {
if (showAnalog[i])
dataAnalog[i].AddPoint(t, sin(2*t) - cos(2*t));
}
ImGui::SetNextPlotRangeY(-1, 1);
ImGui::SetNextPlotRangeX(t - 10.0f, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
ImGui::SetNextPlotBoundsY(-1, 1);
ImGui::SetNextPlotBoundsX(t - 10.0f, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
if (ImGui::BeginPlot("##Digital", NULL, NULL, ImVec2(-1,300), ImPlotFlags_Default)) {
for (int i = 0; i < K_PLOT_DIGITAL_CH_COUNT; ++i) {
if (showDigital[i]) {
@ -653,7 +692,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
ImGui::PushPlotColor(ImPlotCol_XAxis, IM_COL32(192, 192, 192, 192));
ImGui::PushPlotColor(ImPlotCol_YAxis, IM_COL32(192, 192, 192, 192));
ImGui::PushPlotStyleVar(ImPlotStyleVar_LineWeight, 2);
ImGui::SetNextPlotRange(-0.5f, 9.5f, -0.5f, 9.5f);
ImGui::SetNextPlotBounds(-0.5f, 9.5f, -0.5f, 9.5f);
if (ImGui::BeginPlot("##Custom", NULL, NULL, {-1,300}, ImPlotFlags_Default & ~ImPlotFlags_Legend, 0)) {
float lin[10] = {8,8,9,7,8,8,8,9,7,8};
float bar[10] = {1,2,5,3,4,1,2,5,3,4};
@ -688,7 +727,7 @@ void ShowImPlotDemoWindow(bool* p_open) {
static BenchmarkItem items[n_items];
ImGui::BulletText("Make sure VSync is disabled.");
ImGui::BulletText("%d lines with %d points each @ %.3f FPS.",n_items,1000,ImGui::GetIO().Framerate);
SetNextPlotRange(0,1,0,1, ImGuiCond_Always);
SetNextPlotBounds(0,1,0,1, ImGuiCond_Always);
if (ImGui::BeginPlot("##Bench",NULL,NULL,{-1,300},ImPlotFlags_Default | ImPlotFlags_NoChild)) {
char buff[16];
for (int i = 0; i < 100; ++i) {