Revision: 7119
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7119&view=rev
Author:   leejjoon
Date:     2009-05-18 17:25:46 +0000 (Mon, 18 May 2009)

Log Message:
-----------
Add *annotation_clip* attr. for text.Annotation class

Modified Paths:
--------------
    trunk/matplotlib/CHANGELOG
    trunk/matplotlib/lib/matplotlib/axes.py
    trunk/matplotlib/lib/matplotlib/patches.py
    trunk/matplotlib/lib/matplotlib/text.py

Modified: trunk/matplotlib/CHANGELOG
===================================================================
--- trunk/matplotlib/CHANGELOG  2009-05-18 01:11:49 UTC (rev 7118)
+++ trunk/matplotlib/CHANGELOG  2009-05-18 17:25:46 UTC (rev 7119)
@@ -1,3 +1,7 @@
+2009-05-18 Add *annotation_clip* attr. for text.Annotation class.
+           If True, annotation is only drawn when the annotated point is
+           inside the axes area. -JJL
+
 2009-05-17 Fix bug(#2749174) that some properties of minor ticks are
            not conserved -JJL
 

Modified: trunk/matplotlib/lib/matplotlib/axes.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/axes.py     2009-05-18 01:11:49 UTC (rev 
7118)
+++ trunk/matplotlib/lib/matplotlib/axes.py     2009-05-18 17:25:46 UTC (rev 
7119)
@@ -2657,6 +2657,15 @@
 
         return self.patch.contains(mouseevent)
 
+    def contains_point(self, point):
+        """
+        Returns True if the point (tuple of x,y) is inside the axes
+        (the area defined by the its patch). A pixel coordinate is
+        required.
+
+        """
+        return self.patch.contains_point(point)
+
     def pick(self, *args):
         """
         call signature::

Modified: trunk/matplotlib/lib/matplotlib/patches.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/patches.py  2009-05-18 01:11:49 UTC (rev 
7118)
+++ trunk/matplotlib/lib/matplotlib/patches.py  2009-05-18 17:25:46 UTC (rev 
7119)
@@ -81,6 +81,14 @@
             (mouseevent.x, mouseevent.y), self.get_transform())
         return inside, {}
 
+    def contains_point(self, point):
+        """
+        Returns *True* if the given point is inside the path
+        (transformed with its transform attribute).
+        """
+        return self.get_path().contains_point(point,
+                                              self.get_transform())
+
     def update_from(self, other):
         """
         Updates this :class:`Patch` from the properties of *other*.

Modified: trunk/matplotlib/lib/matplotlib/text.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/text.py     2009-05-18 01:11:49 UTC (rev 
7118)
+++ trunk/matplotlib/lib/matplotlib/text.py     2009-05-18 17:25:46 UTC (rev 
7119)
@@ -1408,6 +1408,8 @@
         else:
             self.arrow_patch = None
 
+        # if True, draw annotation only if self.xy is inside the axes
+        self._annotation_clip = None
 
     __init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
 
@@ -1525,15 +1527,44 @@
             trans = self.axes.transAxes
             return trans.transform_point((x, y))
 
+    def set_annotation_clip(self, b):
+        """
+        set *annotation_clip* attribute.
 
+          * True : the annotation will only be drawn when self.xy is inside 
the axes.
+          * False : the annotation will always be drawn regardless of its 
position.
+          * None : the self.xy will be checked only if *xycoords* is "data"
+        """
+        self._annotation_clip = b
+
+    def get_annotation_clip(self):
+        """
+        Return *annotation_clip* attribute.
+        See :meth:`set_annotation_clip` for the meaning of return values.
+        """
+        return self._annotation_clip
+        
+
     def update_positions(self, renderer):
+        "Update the pixel positions of the annotated point and the text."
+        xy_pixel = self._get_position_xy(renderer)
+        self._update_position_xytext(renderer, xy_pixel)
 
+
+    def _get_position_xy(self, renderer):
+        "Return the pixel position of the the annotated point."
+        x, y = self.xy
+        return self._get_xy(x, y, self.xycoords)
+
+
+    def _update_position_xytext(self, renderer, xy_pixel):
+        "Update the pixel positions of the annotation text and the arrow 
patch."
+
         x, y = self.xytext
         self._x, self._y = self._get_xy(x, y, self.textcoords)
 
 
-        x, y = self.xy
-        x, y = self._get_xy(x, y, self.xycoords)
+        x, y = xy_pixel
 
         ox0, oy0 = self._x, self._y
         ox1, oy1 = x, y
@@ -1609,6 +1640,22 @@
 
                 self.arrow.set_clip_box(self.get_clip_box())
 
+
+    def _check_xy(self, renderer, xy_pixel):
+        """
+        given the xy pixel coordinate, check if the annotation need to
+        be drawn.
+        """
+
+        b = self.get_annotation_clip()
+        if b or (b is None and self.xycoords == "data"):
+            # check if self.xy is inside the axes.
+            if not self.axes.contains_point(xy_pixel):
+                return False
+
+        return True
+
+
     def draw(self, renderer):
         """
         Draw the :class:`Annotation` object to the given *renderer*.
@@ -1618,7 +1665,13 @@
             self._renderer = renderer
         if not self.get_visible(): return
 
-        self.update_positions(renderer)
+        xy_pixel = self._get_position_xy(renderer)
+
+        if not self._check_xy(renderer, xy_pixel):
+            return
+
+        self._update_position_xytext(renderer, xy_pixel)
+
         self.update_bbox_position_size(renderer)
 
         if self.arrow is not None:


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables 
unlimited royalty-free distribution of the report engine 
for externally facing server and web deployment. 
http://p.sf.net/sfu/businessobjects
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to