Skip to content

ViewBox.setLimits() doesn't trigger update if view range not within new limits #2415

@bbc131

Description

@bbc131

Description

If you call for example ViewBox.setLimits(minXRange=limit) with limit being larger as the current visible range, the ViewBox doesn't update its view range. We are then in a state where the limits are not satisfied.
My expectation is, that the view range is updated immediatly with the call of setLimits().
Only after some other trigger, like updating the view range by ViewRange.setRange(), the view range suddenly changes and the limits are satisfied.
This can be seen as a workaround: Just call setRange with the current range.
This is behaviour seems to be the case for all eight limit parameters (xMin, ..., yMax, minXRange, ..., maxYRange)

Code to reproduce

Use the following mwe to check the current behaviour:
The width of the LinearRegionItem sets the minXRange limit of the right plot.
At the beginning it's all good, the region has a width of 10 and the right plot a range of 20.
Increasing the regions width to above 20 doesn't affect the right plot.
Now, try to pan the right plot just a little bit, this will trigger the limits and the plot will "jump" into a state where the limits are satisfied.

The expected behaviour can be seen, if the workaround is enabled (see the if 0 in the code).

import pyqtgraph as pg

app = pg.mkQApp()

win = pg.GraphicsLayoutWidget(show=True)
win.resize(1000,600)

p1 = win.addPlot(title="Region defines limit for other plot")
lr = pg.LinearRegionItem([0,10])
p1.addItem(lr)
p1.setXRange(0,50)

p2 = win.addPlot()
p2.setXRange(0,20)

def lr_updated():
    r = lr.getRegion()
    region_span = r[1] - r[0]
    print(f"p2.setLimits(minXRange={region_span})")
    p2.setLimits(minXRange=region_span)

    if 0:  # workaround
        p2.setRange(xRange=p2.viewRange()[0], padding=0.0)

lr.sigRegionChanged.connect(lr_updated)
lr_updated()

if __name__ == '__main__':
    pg.exec()

Suggested solution

I think the solution would be to extend ViewBox.updateViewRange() at line 1576.
Between for axis in [0, 1]: and if aspect is not False and ... one should compare viewRange against limits, minRng and maxRng and adjust viewRange accordingly.

That shouldn't be that much work, so if you agree with this suggestion, I might find time in the near future to come up with a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions