diff --git a/chaco/polygon_plot.py b/chaco/polygon_plot.py index 864d964c9..59edc7942 100644 --- a/chaco/polygon_plot.py +++ b/chaco/polygon_plot.py @@ -51,15 +51,15 @@ class PolygonPlot(BaseXYPlot): # Override the hittest_type trait inherited from BaseXYPlot hittest_type = Enum("poly", "point", "line") - + # The RGBA tuple for rendering edges. It is always a tuple of length 4. # It has the same RGB values as edge_color_, and its alpha value is the - # alpha value of self.edge_color multiplied by self.alpha. + # alpha value of self.edge_color multiplied by self.alpha. effective_edge_color = Property(Tuple, depends_on=['edge_color', 'alpha']) - + # The RGBA tuple for rendering the face. It is always a tuple of length 4. # It has the same RGB values as face_color_, and its alpha value is the - # alpha value of self.face_color multiplied by self.alpha. + # alpha value of self.face_color multiplied by self.alpha. effective_face_color = Property(Tuple, depends_on=['face_color', 'alpha']) #---------------------------------------------------------------------- @@ -100,9 +100,19 @@ def _render(self, gc, points): gc.set_line_dash(self.edge_style_) gc.set_fill_color(self.effective_face_color) - gc.lines(points) - gc.close_path() - gc.draw_path() + # bnds is True where polygons are separated + ensures last is True + # indx contains the list of indices that separates the polygons + # lines contains a list of lines that represents all polygons + bnds = [np.isnan(point[0]) for point in points] + if not bnds[-1]: + bnds.append(True) + indx = [-1] + [n for n, b in enumerate(bnds) if b] + lines = [points[i+1:j] for i, j in zip(indx[:-1],indx[1:])] + + for line in lines: + gc.lines(line) + gc.close_path() + gc.draw_path() def _render_icon(self, gc, x, y, width, height): @@ -134,11 +144,23 @@ def hittest(self, screen_pt, threshold=7.0, return_distance=False): data_pt = self.map_data(screen_pt, all_values=True) index = self.index.get_data() value = self.value.get_data() - poly = np.vstack((index,value)).T - if points_in_polygon([data_pt], poly)[0] == 1: - return True - else: - return False + points = zip(index,value) + + # bnds is True where polygons are separated + ensures last is True + # indx contains the list of indices that separates the polygons + # lines contains a list of lines that represents all polygons + bnds = [np.isnan(point[0]) for point in points] + if not bnds[-1]: + bnds.append(True) + indx = [-1] + [n for n, b in enumerate(bnds) if b] + lines = [points[i+1:j] for i, j in zip(indx[:-1],indx[1:])] + + for line in lines: + poly = np.vstack(line).T + if points_in_polygon([data_pt], poly)[0] == 1: + return True + + return False #------------------------------------------------------------------------ # Event handlers