Skip to content

feat: add IsPlotChanging() #591

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ The list below represents a combination of high-priority work, nice-to-have feat

## Tools / Misc.

- add `IsPlotChanging` to detect change in limits
- add ability to extend plot/axis context menus
- add LTTB downsampling for lines
- add box selection to axes
Expand Down Expand Up @@ -79,6 +78,7 @@ Ideally every `PlotX` function should use our faster rendering pipeline when it
|PlotDummy|-|-|-|-|

## Completed
- add `IsAxisRangeChanging` to detect change in limits
- add exploding to `PlotPieChart` (on legend hover)
- make BeginPlot take fewer args:
- make query a tool -> `DragRect`
Expand Down
15 changes: 15 additions & 0 deletions implot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,11 @@ void SetupFinish() {
}
}

// cache previous ranges
for(int i = 0; i < ImAxis_COUNT; i++) {
plot.Axes[i].PreviousRange = ImPlotRange(plot.Axes[i].Range.Min, plot.Axes[i].Range.Max);
}

// INPUT ------------------------------------------------------------------
if (!ImHasFlag(plot.Flags, ImPlotFlags_NoInputs))
UpdateInput(plot);
Expand Down Expand Up @@ -3734,6 +3739,16 @@ bool IsPlotHovered() {
return gp.CurrentPlot->Hovered;
}

bool IsAxisRangeChanging(ImAxis axis) {
ImPlotContext& gp = *GImPlot;
IM_ASSERT_USER_ERROR(gp.CurrentPlot != nullptr, "IsAxisRangeChanging() needs to be called between BeginPlot() and EndPlot()!");
SetupLock();
ImPlotPlot& plot = *gp.CurrentPlot;
bool isMinDifferent = plot.Axes[axis].Range.Min != plot.Axes[axis].PreviousRange.Min;
bool isMaxDifferent = plot.Axes[axis].Range.Max != plot.Axes[axis].PreviousRange.Max;
return isMinDifferent || isMaxDifferent;
}

bool IsAxisHovered(ImAxis axis) {
ImPlotContext& gp = *GImPlot;
IM_ASSERT_USER_ERROR(gp.CurrentPlot != nullptr, "IsPlotXAxisHovered() needs to be called between BeginPlot() and EndPlot()!");
Expand Down
2 changes: 2 additions & 0 deletions implot.h
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,8 @@ IMPLOT_API ImPlotRect GetPlotLimits(ImAxis x_axis = IMPLOT_AUTO, ImAxis y_axis =

// Returns true if the plot area in the current plot is hovered.
IMPLOT_API bool IsPlotHovered();
// Returns true if the limits of the selected axis are changing.
IMPLOT_API bool IsAxisRangeChanging(ImAxis axis);
// Returns true if the axis label area in the current plot is hovered.
IMPLOT_API bool IsAxisHovered(ImAxis axis);
// Returns true if the bounding frame of a subplot is hovered.
Expand Down
18 changes: 18 additions & 0 deletions implot_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,23 @@ void Demo_ColormapWidgets() {

//-----------------------------------------------------------------------------

void Demo_PlotLimitsChanging() {
unsigned int lin[10] = {8,8,9,7,8,8,8,9,7,8};
unsigned int bar[10] = {1,2,5,3,4,1,2,5,3,4};

if (ImPlot::BeginPlot("##LimitsChanging")) {
ImPlot::SetupAxes( "x-axis", "y-axis");
ImPlot::SetupAxesLimits(-0.5f, 9.5f, 0, 10);
ImPlot::PlotBars("Bars", bar, 10, 0.5f);
ImPlot::PlotLine("Line", lin, 10);
bool is_changing = ImPlot::IsAxisRangeChanging(ImAxis_X1);
ImGui::Text("Axis X1 is changing: %s", (is_changing ? "true" : "false"));
ImPlot::EndPlot();
}
}

//-----------------------------------------------------------------------------

void Demo_CustomPlottersAndTooltips() {
ImGui::BulletText("You can create custom plotters or extend ImPlot using implot_internal.h.");
double dates[] = {1546300800,1546387200,1546473600,1546560000,1546819200,1546905600,1546992000,1547078400,1547164800,1547424000,1547510400,1547596800,1547683200,1547769600,1547942400,1548028800,1548115200,1548201600,1548288000,1548374400,1548633600,1548720000,1548806400,1548892800,1548979200,1549238400,1549324800,1549411200,1549497600,1549584000,1549843200,1549929600,1550016000,1550102400,1550188800,1550361600,1550448000,1550534400,1550620800,1550707200,1550793600,1551052800,1551139200,1551225600,1551312000,1551398400,1551657600,1551744000,1551830400,1551916800,1552003200,1552262400,1552348800,1552435200,1552521600,1552608000,1552867200,1552953600,1553040000,1553126400,1553212800,1553472000,1553558400,1553644800,1553731200,1553817600,1554076800,1554163200,1554249600,1554336000,1554422400,1554681600,1554768000,1554854400,1554940800,1555027200,1555286400,1555372800,1555459200,1555545600,1555632000,1555891200,1555977600,1556064000,1556150400,1556236800,1556496000,1556582400,1556668800,1556755200,1556841600,1557100800,1557187200,1557273600,1557360000,1557446400,1557705600,1557792000,1557878400,1557964800,1558051200,1558310400,1558396800,1558483200,1558569600,1558656000,1558828800,1558915200,1559001600,1559088000,1559174400,1559260800,1559520000,1559606400,1559692800,1559779200,1559865600,1560124800,1560211200,1560297600,1560384000,1560470400,1560729600,1560816000,1560902400,1560988800,1561075200,1561334400,1561420800,1561507200,1561593600,1561680000,1561939200,1562025600,1562112000,1562198400,1562284800,1562544000,1562630400,1562716800,1562803200,1562889600,1563148800,1563235200,1563321600,1563408000,1563494400,1563753600,1563840000,1563926400,1564012800,1564099200,1564358400,1564444800,1564531200,1564617600,1564704000,1564963200,1565049600,1565136000,1565222400,1565308800,1565568000,1565654400,1565740800,1565827200,1565913600,1566172800,1566259200,1566345600,1566432000,1566518400,1566777600,1566864000,1566950400,1567036800,1567123200,1567296000,1567382400,1567468800,1567555200,1567641600,1567728000,1567987200,1568073600,1568160000,1568246400,1568332800,1568592000,1568678400,1568764800,1568851200,1568937600,1569196800,1569283200,1569369600,1569456000,1569542400,1569801600,1569888000,1569974400,1570060800,1570147200,1570406400,1570492800,1570579200,1570665600,1570752000,1571011200,1571097600,1571184000,1571270400,1571356800,1571616000,1571702400,1571788800,1571875200,1571961600};
Expand Down Expand Up @@ -2281,6 +2298,7 @@ void ShowDemoWindow(bool* p_open) {
DemoHeader("Legend Options", Demo_LegendOptions);
DemoHeader("Legend Popups", Demo_LegendPopups);
DemoHeader("Colormap Widgets", Demo_ColormapWidgets);
DemoHeader("Plot limits changing", Demo_PlotLimitsChanging);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Custom")) {
Expand Down
1 change: 1 addition & 0 deletions implot_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ struct ImPlotAxis
ImPlotAxisFlags Flags;
ImPlotAxisFlags PreviousFlags;
ImPlotRange Range;
ImPlotRange PreviousRange;
ImPlotCond RangeCond;
ImPlotScale Scale;
ImPlotRange FitExtents;
Expand Down