python-igraph manual

For using igraph from Python

   Home       Trees       Indices       Help   
Package igraph :: Package drawing :: Module coord
[hide private]

Source Code for Module igraph.drawing.coord

  1  """ 
  2  Coordinate systems and related plotting routines 
  3  """ 
  4   
  5  from igraph.compat import property 
  6  from igraph.drawing.baseclasses import AbstractCairoDrawer 
  7  from igraph.drawing.utils import BoundingBox 
  8   
  9  __license__ = "GPL" 
10 11 ##################################################################### 12 13 # pylint: disable-msg=R0922 14 # R0922: Abstract class is only referenced 1 times 15 -class CoordinateSystem(AbstractCairoDrawer):
16 """Class implementing a coordinate system object. 17 18 Coordinate system objects are used when drawing plots which 19 2D or 3D coordinate system axes. This is an abstract class 20 which must be extended in order to use it. In general, you'll 21 only need the documentation of this class if you intend to 22 implement an own coordinate system not present in igraph yet. 23 """ 24
25 - def __init__(self, context, bbox):
26 """Initializes the coordinate system. 27 28 @param context: the context on which the coordinate system will 29 be drawn. 30 @param bbox: the bounding box that will contain the coordinate 31 system. 32 """ 33 AbstractCairoDrawer.__init__(self, context, bbox) 34
35 - def draw(self):
36 """Draws the coordinate system. 37 38 This method must be overridden in derived classes. 39 """ 40 raise NotImplementedError("abstract class") 41
42 - def local_to_context(self, x, y):
43 """Converts local coordinates to the context coordinate system (given 44 by the bounding box). 45 46 This method must be overridden in derived classes.""" 47 raise NotImplementedError("abstract class")
48
49 50 -class DescartesCoordinateSystem(CoordinateSystem):
51 """Class implementing a 2D Descartes coordinate system object.""" 52
53 - def __init__(self, context, bbox, bounds):
54 """Initializes the coordinate system. 55 56 @param context: the context on which the coordinate system will 57 be drawn. 58 @param bbox: the bounding box that will contain the coordinate 59 system. 60 @param bounds: minimum and maximum X and Y values in a 4-tuple. 61 """ 62 self._bounds, self._bbox = None, None 63 self._sx, self._sy = None, None 64 self._ox, self._oy, self._ox2, self._oy2 = None, None, None, None 65 66 CoordinateSystem.__init__(self, context, bbox) 67 68 self.bbox = bbox 69 self.bounds = bounds 70 71 @property
72 - def bbox(self):
73 """Returns the bounding box of the coordinate system""" 74 return BoundingBox(self._bbox.coords) 75 76 @bbox.setter
77 - def bbox(self, bbox):
78 """Sets the bounding box of the coordinate system""" 79 self._bbox = bbox 80 self._recalc_scale_factors() 81 82 @property
83 - def bounds(self):
84 """Returns the lower and upper bounds of the X and Y values""" 85 return self._bounds.coords 86 87 @bounds.setter
88 - def bounds(self, bounds):
89 """Sets the lower and upper bounds of the X and Y values""" 90 self._bounds = BoundingBox(bounds) 91 self._recalc_scale_factors() 92
93 - def _recalc_scale_factors(self):
94 """Recalculates some cached scale factors used within the class""" 95 if self._bounds is None: 96 return 97 self._sx = self._bbox.width / self._bounds.width 98 self._sy = self._bbox.height / self._bounds.height 99 self._ox = self._bounds.left 100 self._oy = self._bounds.top 101 self._ox2 = self._bbox.left 102 self._oy2 = self._bbox.bottom 103
104 - def draw(self):
105 """Draws the coordinate system.""" 106 # Draw the frame 107 coords = self.bbox.coords 108 self.context.set_source_rgb(0., 0., 0.) 109 self.context.set_line_width(1) 110 self.context.rectangle(coords[0], coords[1], \ 111 coords[2]-coords[0], coords[3]-coords[1]) 112 self.context.stroke() 113
114 - def local_to_context(self, x, y):
115 """Converts local coordinates to the context coordinate system (given 116 by the bounding box). 117 """ 118 return (x-self._ox)*self._sx+self._ox2, self._oy2-(y-self._oy)*self._sy
119

   Home       Trees       Indices       Help