Skip to content

Commit

Permalink
Change implementation of dashed lines with 2 colors
Browse files Browse the repository at this point in the history
  • Loading branch information
t20100 committed Jul 12, 2023
1 parent 95d0d9d commit 4c5a1ba
Showing 1 changed file with 54 additions and 48 deletions.
102 changes: 54 additions & 48 deletions src/silx/gui/plot/backends/BackendMatplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,42 +393,47 @@ def contains(self, mouseevent):
return self.line.contains(mouseevent)


class _DoubleColoredLinePatch(matplotlib.patches.Patch):
"""Matplotlib patch to display any patch using double color."""
class SecondEdgeColorPatchMixIn:
"""Mix-in class to add a second color for patches with dashed lines"""

def __init__(self, patch):
super(_DoubleColoredLinePatch, self).__init__()
self.__patch = patch
self.linebgcolor = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._second_edgecolor = None

def set_second_edgecolor(self, color):
"""Set the second color used to fill dashed edges"""
self._second_edgecolor = color

def __getattr__(self, name):
return getattr(self.__patch, name)
def get_second_edgecolor(self):
"""Returns the second color used to fill dashed edges"""
return self._second_edgecolor

def draw(self, renderer):
oldLineStype = self.__patch.get_linestyle()
if self.linebgcolor is not None and oldLineStype != "solid":
oldLineColor = self.__patch.get_edgecolor()
oldHatch = self.__patch.get_hatch()
self.__patch.set_linestyle("solid")
self.__patch.set_edgecolor(self.linebgcolor)
self.__patch.set_hatch(None)
self.__patch.draw(renderer)
self.__patch.set_linestyle(oldLineStype)
self.__patch.set_edgecolor(oldLineColor)
self.__patch.set_hatch(oldHatch)
self.__patch.draw(renderer)
linestyle = self.get_linestyle()
if linestyle == "solid" or self.get_second_edgecolor() is None:
super().draw(renderer)
return

edgecolor = self.get_edgecolor()
hatch = self.get_hatch()

self.set_linestyle("solid")
self.set_edgecolor(self.get_second_edgecolor())
self.set_hatch(None)
super().draw(renderer)

def set_transform(self, transform):
self.__patch.set_transform(transform)
self.set_linestyle(linestyle)
self.set_edgecolor(edgecolor)
self.set_hatch(hatch)
super().draw(renderer)

def get_path(self):
return self.__patch.get_path()

def contains(self, mouseevent, radius=None):
return self.__patch.contains(mouseevent, radius)
class Rectangle2EdgeColor(SecondEdgeColorPatchMixIn, Rectangle):
"""Rectangle patch with a second edge color for dashed line"""

def contains_point(self, point, radius=None):
return self.__patch.contains_point(point, radius)

class Polygon2EdgeColor(SecondEdgeColorPatchMixIn, Polygon):
"""Polygon patch with a second edge color for dashed line"""


class Image(AxesImage):
Expand Down Expand Up @@ -805,19 +810,20 @@ def addShape(self, x, y, shape, color, fill, overlay,
yMax = numpy.nanmax(yView)
w = xMax - xMin
h = yMax - yMin
item = Rectangle(xy=(xMin, yMin),
width=w,
height=h,
fill=False,
color=color,
linestyle=linestyle,
linewidth=linewidth)
item = Rectangle2EdgeColor(
xy=(xMin, yMin),
width=w,
height=h,
fill=False,
color=color,
linestyle=linestyle,
linewidth=linewidth,
)
item.set_second_edgecolor(linebgcolor)

if fill:
item.set_hatch('.')

if linestyle != "solid" and linebgcolor is not None:
item = _DoubleColoredLinePatch(item)
item.linebgcolor = linebgcolor

self.ax.add_patch(item)

Expand All @@ -827,19 +833,19 @@ def addShape(self, x, y, shape, color, fill, overlay,
closed = True
else: # shape == 'polylines'
closed = numpy.all(numpy.equal(points[0], points[-1]))
item = Polygon(points,
closed=closed,
fill=False,
color=color,
linestyle=linestyle,
linewidth=linewidth)
item = Polygon2EdgeColor(
points,
closed=closed,
fill=False,
color=color,
linestyle=linestyle,
linewidth=linewidth,
)
item.set_second_edgecolor(linebgcolor)

if fill and shape == 'polygon':
item.set_hatch('/')

if linestyle != "solid" and linebgcolor is not None:
item = _DoubleColoredLinePatch(item)
item.linebgcolor = linebgcolor

self.ax.add_patch(item)

else:
Expand Down

0 comments on commit 4c5a1ba

Please sign in to comment.