Skip to content

Commit 9c44711

Browse files
committed
feat: add IsPlotChanging()
As pointed out in the `TODO.md`
1 parent f156599 commit 9c44711

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ The list below represents a combination of high-priority work, nice-to-have feat
3737

3838
## Tools / Misc.
3939

40-
- add `IsPlotChanging` to detect change in limits
4140
- add ability to extend plot/axis context menus
4241
- add LTTB downsampling for lines
4342
- add box selection to axes
@@ -80,6 +79,7 @@ Ideally every `PlotX` function should use our faster rendering pipeline when it
8079
|PlotDummy|-|-|-|-|
8180

8281
## Completed
82+
- add `IsPlotChanging` to detect change in limits
8383
- make BeginPlot take fewer args:
8484
- make query a tool -> `DragRect`
8585
- rework DragLine/Point to use ButtonBehavior

implot.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,8 @@ void SetupAxis(ImAxis idx, const char* label, ImPlotAxisFlags flags) {
21352135
if (plot.JustCreated || flags != axis.PreviousFlags)
21362136
axis.Flags = flags;
21372137
axis.PreviousFlags = flags;
2138+
// cache previous range
2139+
axis.PreviousRange = ImPlotRange(axis.Range.Min, axis.Range.Max);
21382140
// enable axis
21392141
axis.Enabled = true;
21402142
// set label
@@ -3739,6 +3741,16 @@ bool IsPlotHovered() {
37393741
return gp.CurrentPlot->Hovered;
37403742
}
37413743

3744+
bool IsPlotChanging() {
3745+
ImPlotContext& gp = *GImPlot;
3746+
IM_ASSERT_USER_ERROR(gp.CurrentPlot != nullptr, "IsPlotChanging() needs to be called between BeginPlot() and EndPlot()!");
3747+
SetupLock();
3748+
ImPlotPlot& plot = *gp.CurrentPlot;
3749+
bool differentX = plot.Axes[plot.CurrentX].Range.Min != plot.Axes[plot.CurrentX].PreviousRange.Min || plot.Axes[plot.CurrentX].Range.Max != plot.Axes[plot.CurrentX].PreviousRange.Max;
3750+
bool differentY = plot.Axes[plot.CurrentY].Range.Min != plot.Axes[plot.CurrentY].PreviousRange.Min || plot.Axes[plot.CurrentY].Range.Max != plot.Axes[plot.CurrentY].PreviousRange.Max;
3751+
return differentX || differentY;
3752+
}
3753+
37423754
bool IsAxisHovered(ImAxis axis) {
37433755
ImPlotContext& gp = *GImPlot;
37443756
IM_ASSERT_USER_ERROR(gp.CurrentPlot != nullptr, "IsPlotXAxisHovered() needs to be called between BeginPlot() and EndPlot()!");

implot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,8 @@ IMPLOT_API ImPlotRect GetPlotLimits(ImAxis x_axis = IMPLOT_AUTO, ImAxis y_axis =
982982

983983
// Returns true if the plot area in the current plot is hovered.
984984
IMPLOT_API bool IsPlotHovered();
985+
// Returns true if the limits of the current plot are changing.
986+
IMPLOT_API bool IsPlotChanging();
985987
// Returns true if the axis label area in the current plot is hovered.
986988
IMPLOT_API bool IsAxisHovered(ImAxis axis);
987989
// Returns true if the bounding frame of a subplot is hovered.

implot_demo.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,24 @@ void Demo_ColormapWidgets() {
21282128

21292129
//-----------------------------------------------------------------------------
21302130

2131+
void Demo_PlotLimitsChanging() {
2132+
unsigned int lin[10] = {8,8,9,7,8,8,8,9,7,8};
2133+
unsigned int bar[10] = {1,2,5,3,4,1,2,5,3,4};
2134+
unsigned int dot[10] = {7,6,6,7,8,5,6,5,8,7};
2135+
2136+
if (ImPlot::BeginPlot("##LimitsChanging")) {
2137+
ImPlot::SetupAxes( "x-axis", "y-axis");
2138+
ImPlot::SetupAxesLimits(-0.5f, 9.5f, 0, 10);
2139+
ImPlot::PlotBars("Bars", bar, 10, 0.5f);
2140+
ImPlot::PlotLine("Line", lin, 10);
2141+
bool is_changing = ImPlot::IsPlotChanging();
2142+
ImGui::Text("Axes are changing: %s", (is_changing ? "true" : "false"));
2143+
ImPlot::EndPlot();
2144+
}
2145+
}
2146+
2147+
//-----------------------------------------------------------------------------
2148+
21312149
void Demo_CustomPlottersAndTooltips() {
21322150
ImGui::BulletText("You can create custom plotters or extend ImPlot using implot_internal.h.");
21332151
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};
@@ -2277,6 +2295,7 @@ void ShowDemoWindow(bool* p_open) {
22772295
DemoHeader("Legend Options", Demo_LegendOptions);
22782296
DemoHeader("Legend Popups", Demo_LegendPopups);
22792297
DemoHeader("Colormap Widgets", Demo_ColormapWidgets);
2298+
DemoHeader("Plot limits changing", Demo_PlotLimitsChanging);
22802299
ImGui::EndTabItem();
22812300
}
22822301
if (ImGui::BeginTabItem("Custom")) {

implot_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ struct ImPlotAxis
627627
ImPlotAxisFlags Flags;
628628
ImPlotAxisFlags PreviousFlags;
629629
ImPlotRange Range;
630+
ImPlotRange PreviousRange;
630631
ImPlotCond RangeCond;
631632
ImPlotScale Scale;
632633
ImPlotRange FitExtents;

0 commit comments

Comments
 (0)