From 5adb1b9a5b46708713fd7172ebf98c2fed1c4f20 Mon Sep 17 00:00:00 2001 From: 2xB <31772910+2xB@users.noreply.github.com> Date: Thu, 19 Nov 2020 19:19:32 +0100 Subject: [PATCH] Fixes for examples\customPlot.py (#1448) * Fix examples\customPlot.py: Not object-oriented Copy-paste error: I referenced a specific object instead of self. Fixed. * Fix examples\customPlot.py: Allow calling setTicks more than once This fixes a bug where TickSliderItem is expected to return ticks.keys() but while it returns ticks.items(). Also: Minor code correction. * Fix examples\customPlot.py: Avoid scaling differences Consider padding of TickSliderItem for link between ViewBox and ticks Co-authored-by: 2xB <2xB@users.noreply.github.com> --- examples/customPlot.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/examples/customPlot.py b/examples/customPlot.py index 33469ce9..d6b23ba7 100644 --- a/examples/customPlot.py +++ b/examples/customPlot.py @@ -39,29 +39,36 @@ class CustomTickSliderItem(pg.TickSliderItem): self._range = [0,1] def setTicks(self, ticks): - for tick in tickViewer.listTicks(): + for tick, pos in self.listTicks(): self.removeTick(tick) - self.visible_ticks = {} + self.visible_ticks = {} self.all_ticks = ticks self.updateRange(None, self._range) def updateRange(self, vb, viewRange): + origin = self.tickSize/2. + length = self.length + + lengthIncludingPadding = length + self.tickSize + 2 + self._range = viewRange for pos in self.all_ticks: - visible = pos >= viewRange[0] and pos <= viewRange[1] + + if pos not in self.visible_ticks: + self.visible_ticks[pos] = self.addTick(pos, movable=False, color="333333") + + tick = self.visible_ticks[pos] + + tickValueIncludingPadding = (pos - viewRange[0]) / (viewRange[1] - viewRange[0]) + tickValue = (tickValueIncludingPadding*lengthIncludingPadding - origin) / length + + visible = tickValue >= 0 and tickValue <= 1 if visible: - if pos not in self.visible_ticks: - self.visible_ticks[pos] = self.addTick(pos, movable=False, color="333333") - - tick = self.visible_ticks[pos] - - tickValue = (pos - viewRange[0]) / (viewRange[1] - viewRange[0]) self.setTickValue(tick, tickValue) - elif pos in self.visible_ticks: self.removeTick(self.visible_ticks[pos]) del self.visible_ticks[pos]