text location algorithm simplification

This commit is contained in:
lesauxvi 2016-02-05 13:57:51 +01:00
parent 0be3615c88
commit e7b27c2726
2 changed files with 18 additions and 24 deletions

View File

@ -17,14 +17,14 @@ win.resize(1000,600)
pg.setConfigOptions(antialias=True) pg.setConfigOptions(antialias=True)
p1 = win.addPlot(title="Plot Items example", y=np.random.normal(size=100)) p1 = win.addPlot(title="Plot Items example", y=np.random.normal(size=100))
inf1 = pg.InfiniteLine(movable=True, angle=90, label=False, textColor=(200,200,100), textFill=(200,200,200,50)) inf1 = pg.InfiniteLine(movable=True, angle=90, label=True, textShift=0.2, textColor=(200,200,100), textFill=(200,200,200,50))
inf2 = pg.InfiniteLine(movable=True, angle=0, label=True, pen=(0, 0, 200), textColor=(200,0,0), bounds = [-2, 2], suffix="mm", hoverPen=(0,200,0)) inf2 = pg.InfiniteLine(movable=True, angle=0, label=True, pen=(0, 0, 200), textColor=(200,0,0), bounds = [-2, 2], suffix="mm", hoverPen=(0,200,0))
#inf3 = pg.InfiniteLine(movable=True, angle=45) inf3 = pg.InfiniteLine(movable=True, angle=45)
inf1.setPos([2,2]) inf1.setPos([2,2])
##inf1.setTextLocation([0.25, 0.9]) ##inf1.setTextLocation([0.25, 0.9])
p1.addItem(inf1) p1.addItem(inf1)
p1.addItem(inf2) p1.addItem(inf2)
#p1.addItem(inf3) p1.addItem(inf3)
lr = pg.LinearRegionItem(values=[0, 10]) lr = pg.LinearRegionItem(values=[0, 10])
p1.addItem(lr) p1.addItem(lr)

View File

@ -32,7 +32,7 @@ class InfiniteLine(GraphicsObject):
def __init__(self, pos=None, angle=90, pen=None, movable=False, bounds=None, def __init__(self, pos=None, angle=90, pen=None, movable=False, bounds=None,
hoverPen=None, label=False, textColor=None, textFill=None, hoverPen=None, label=False, textColor=None, textFill=None,
textLocation=[0.05,0.5], textFormat="{:.3f}", textShift=0.5, textFormat="{:.3f}",
suffix=None, name='InfiniteLine'): suffix=None, name='InfiniteLine'):
""" """
=============== ================================================================== =============== ==================================================================
@ -53,11 +53,8 @@ class InfiniteLine(GraphicsObject):
location in data coordinates location in data coordinates
textColor color of the label. Can be any argument fn.mkColor can understand. textColor color of the label. Can be any argument fn.mkColor can understand.
textFill A brush to use when filling within the border of the text. textFill A brush to use when filling within the border of the text.
textLocation list where list[0] defines the location of the text (if textShift float (0-1) that defines when the text shifts from one side to
vertical, a 0 value means that the textItem is on the bottom the other side of the line.
axis, and a 1 value means that thet TextItem is on the top
axis, same thing if horizontal) and list[1] defines when the
text shifts from one side to the other side of the line.
textFormat Any new python 3 str.format() format. textFormat Any new python 3 str.format() format.
suffix If not None, corresponds to the unit to show next to the label suffix If not None, corresponds to the unit to show next to the label
name name of the item name name of the item
@ -80,7 +77,7 @@ class InfiniteLine(GraphicsObject):
textColor = (200, 200, 100) textColor = (200, 200, 100)
self.textColor = textColor self.textColor = textColor
self.textFill = textFill self.textFill = textFill
self.textLocation = textLocation self.textShift = textShift
self.suffix = suffix self.suffix = suffix
if (self.angle == 0 or self.angle == 90) and label: if (self.angle == 0 or self.angle == 90) and label:
@ -206,8 +203,7 @@ class InfiniteLine(GraphicsObject):
ymin, ymax = rangeY ymin, ymax = rangeY
if self.angle == 90: # vertical line if self.angle == 90: # vertical line
diffMin = self.value()-xmin diffMin = self.value()-xmin
limInf = self.textLocation[1]*(xmax-xmin) limInf = self.textShift*(xmax-xmin)
ypos = ymin+self.textLocation[0]*(ymax-ymin)
if diffMin < limInf: if diffMin < limInf:
self.textItem.anchor = Point(self.anchorRight) self.textItem.anchor = Point(self.anchorRight)
else: else:
@ -218,8 +214,7 @@ class InfiniteLine(GraphicsObject):
self.textItem.setText(fmt.format(self.value()), color=self.textColor) self.textItem.setText(fmt.format(self.value()), color=self.textColor)
elif self.angle == 0: # horizontal line elif self.angle == 0: # horizontal line
diffMin = self.value()-ymin diffMin = self.value()-ymin
limInf = self.textLocation[1]*(ymax-ymin) limInf = self.textShift*(ymax-ymin)
xpos = xmin+self.textLocation[0]*(xmax-xmin)
if diffMin < limInf: if diffMin < limInf:
self.textItem.anchor = Point(self.anchorUp) self.textItem.anchor = Point(self.anchorUp)
else: else:
@ -364,18 +359,17 @@ class InfiniteLine(GraphicsObject):
else: else:
self.textItem = None self.textItem = None
def setTextShift(self, shift):
"""
Set the parameter that defines the location when the label shifts from
one side of the infiniteLine to the other.
def setTextLocation(self, loc): ============== ======================================================
**Arguments:**
shift float (range of value = [0-1]).
============== ======================================================
""" """
Set the parameters that defines the location of the textItem with respect self.textShift = np.clip(shift, 0, 1)
to a specific axis. If the line is vertical, the location is based on the
normalized range of the yaxis. Otherwise, it is based on the normalized
range of the xaxis.
loc[0] defines the location of the text along the infiniteLine
loc[1] defines the location when the label shifts from one side of then
infiniteLine to the other.
"""
self.textLocation = [np.clip(loc[0], 0, 1), np.clip(loc[1], 0, 1)]
self.update() self.update()
def setName(self, name): def setName(self, name):