Add AxisItem.setTickSpacing()

This commit is contained in:
Luke Campagnola 2014-07-25 07:52:58 -04:00
parent 8268ccfa65
commit 74b5ba6f7e

View File

@ -55,6 +55,8 @@ class AxisItem(GraphicsWidget):
], ],
'showValues': showValues, 'showValues': showValues,
'tickLength': maxTickLength, 'tickLength': maxTickLength,
'maxTickLevel': 2,
'maxTextLevel': 2,
} }
self.textWidth = 30 ## Keeps track of maximum width / height of tick text self.textWidth = 30 ## Keeps track of maximum width / height of tick text
@ -68,6 +70,7 @@ class AxisItem(GraphicsWidget):
self.tickFont = None self.tickFont = None
self._tickLevels = None ## used to override the automatic ticking system with explicit ticks self._tickLevels = None ## used to override the automatic ticking system with explicit ticks
self._tickSpacing = None # used to override default tickSpacing method
self.scale = 1.0 self.scale = 1.0
self.autoSIPrefix = True self.autoSIPrefix = True
self.autoSIPrefixScale = 1.0 self.autoSIPrefixScale = 1.0
@ -517,6 +520,37 @@ class AxisItem(GraphicsWidget):
self.picture = None self.picture = None
self.update() self.update()
def setTickSpacing(self, major=None, minor=None, levels=None):
"""
Explicitly determine the spacing of major and minor ticks. This
overrides the default behavior of the tickSpacing method, and disables
the effect of setTicks(). Arguments may be either *major* and *minor*,
or *levels* which is a list of (spacing, offset) tuples for each
tick level desired.
If no arguments are given, then the default behavior of tickSpacing
is enabled.
Examples::
# two levels, all offsets = 0
axis.setTickSpacing(5, 1)
# three levels, all offsets = 0
axis.setTickSpacing([(3, 0), (1, 0), (0.25, 0)])
# reset to default
axis.setTickSpacing()
"""
if levels is None:
if major is None:
levels = None
else:
levels = [(major, 0), (minor, 0)]
self._tickSpacing = levels
self.picture = None
self.update()
def tickSpacing(self, minVal, maxVal, size): def tickSpacing(self, minVal, maxVal, size):
"""Return values describing the desired spacing and offset of ticks. """Return values describing the desired spacing and offset of ticks.
@ -532,6 +566,10 @@ class AxisItem(GraphicsWidget):
... ...
] ]
""" """
# First check for override tick spacing
if self._tickSpacing is not None:
return self._tickSpacing
dif = abs(maxVal - minVal) dif = abs(maxVal - minVal)
if dif == 0: if dif == 0:
return [] return []
@ -557,12 +595,13 @@ class AxisItem(GraphicsWidget):
#(intervals[minorIndex], 0) ## Pretty, but eats up CPU #(intervals[minorIndex], 0) ## Pretty, but eats up CPU
] ]
## decide whether to include the last level of ticks if self.style['maxTickLevel'] >= 2:
minSpacing = min(size / 20., 30.) ## decide whether to include the last level of ticks
maxTickCount = size / minSpacing minSpacing = min(size / 20., 30.)
if dif / intervals[minorIndex] <= maxTickCount: maxTickCount = size / minSpacing
levels.append((intervals[minorIndex], 0)) if dif / intervals[minorIndex] <= maxTickCount:
return levels levels.append((intervals[minorIndex], 0))
return levels
@ -588,8 +627,6 @@ class AxisItem(GraphicsWidget):
#(intervals[intIndexes[0]], 0) #(intervals[intIndexes[0]], 0)
#] #]
def tickValues(self, minVal, maxVal, size): def tickValues(self, minVal, maxVal, size):
""" """
Return the values and spacing of ticks to draw:: Return the values and spacing of ticks to draw::
@ -763,8 +800,6 @@ class AxisItem(GraphicsWidget):
values.append(val) values.append(val)
strings.append(strn) strings.append(strn)
textLevel = 1 ## draw text at this scale level
## determine mapping between tick values and local coordinates ## determine mapping between tick values and local coordinates
dif = self.range[1] - self.range[0] dif = self.range[1] - self.range[0]
if dif == 0: if dif == 0:
@ -853,7 +888,7 @@ class AxisItem(GraphicsWidget):
if not self.style['showValues']: if not self.style['showValues']:
return (axisSpec, tickSpecs, textSpecs) return (axisSpec, tickSpecs, textSpecs)
for i in range(len(tickLevels)): for i in range(min(len(tickLevels), self.style['maxTextLevel']+1)):
## Get the list of strings to display for this level ## Get the list of strings to display for this level
if tickStrings is None: if tickStrings is None:
spacing, values = tickLevels[i] spacing, values = tickLevels[i]