areagraph.py (5229B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\graphs\areagraph.py 2 import math 3 import carbonui.const as uiconst 4 from carbonui.graphs import animations, axis 5 from carbonui.primitives.fill import Fill 6 from carbonui.primitives.polygon import Polygon 7 from carbonui.uicore import uicore 8 9 class AreaGraph(Polygon): 10 default_name = 'AreaGraph' 11 default_align = uiconst.TOPLEFT 12 default_state = uiconst.UI_NORMAL 13 default_graphColor = (1, 1, 1, 0.2) 14 default_hoverColor = (1, 1, 1, 1) 15 16 def ApplyAttributes(self, attributes): 17 Polygon.ApplyAttributes(self, attributes) 18 self._categoryAxis = attributes['categoryAxis'] 19 self._valueAxis = attributes['valueAxis'] 20 self._orientation = attributes.get('orientation', axis.AxisOrientation.VERTICAL) 21 self._categoryAxis.onChange.connect(self._AxisChanged) 22 self._valueAxis.onChange.connect(self._AxisChanged) 23 self._values = attributes['values'] 24 self._vertices = None 25 self.graphColor = attributes.get('graphColor', self.default_graphColor) 26 self.hoverColor = attributes.get('hoverColor', self.default_hoverColor) 27 self._locked = False 28 self._dirty = False 29 self._originalColor = self.color 30 self._animation = None 31 self._Build() 32 33 def GetLegendItem(self): 34 return Fill(name='legendVolume', color=self.graphColor, align=uiconst.TOLEFT) 35 36 def Animate(self, animationType, animationDynamics, duration): 37 self.CancelAnimation() 38 self._animation = animations.CreateAnimation(self, self._categoryAxis, self._valueAxis, self._orientation, animationType, animationDynamics, duration, 2) 39 40 def CancelAnimation(self, applyLastFrame = True): 41 self.StopAnimations() 42 self.opacity = 1.0 43 if self._animation: 44 self._animation.Cancel(applyLastFrame) 45 self._animation = None 46 47 def Close(self): 48 self.CancelAnimation(False) 49 super(AreaGraph, self).Close() 50 self._categoryAxis.onChange.disconnect(self._AxisChanged) 51 self._valueAxis.onChange.disconnect(self._AxisChanged) 52 53 def _Build(self): 54 positions = self._GetVertexPositions() 55 transform = self._GetTransform() 56 color = self.graphColor 57 self.AppendVertices(positions, transform, color) 58 triangles = [] 59 for i in xrange(len(positions) / 2 - 1): 60 i2 = i * 2 61 triangles.append((i2 + 1, i2 + 3, i2 + 2)) 62 triangles.append((i2, i2 + 1, i2 + 2)) 63 64 self.renderObject.AppendTriangles(triangles) 65 66 def _GetTransform(self): 67 dpiScaling = uicore.desktop.dpiScaling 68 transform = ((1, 0, 0), (0, 1, 0), (0, 0, 1)) 69 if self._orientation == axis.AxisOrientation.VERTICAL: 70 transform = self._categoryAxis.UpdateToViewportTransform(transform, 0, dpiScaling) 71 transform = self._valueAxis.UpdateToViewportTransform(transform, 1, dpiScaling) 72 else: 73 transform = self._categoryAxis.UpdateToViewportTransform(transform, 1, dpiScaling) 74 transform = self._valueAxis.UpdateToViewportTransform(transform, 0, dpiScaling) 75 return transform 76 77 def _GetVertexPositions(self): 78 if self._vertices: 79 return self._vertices 80 positions = [] 81 if self._orientation == axis.AxisOrientation.VERTICAL: 82 for x, y in zip(xrange(len(self._categoryAxis.GetDataPoints())), self._values): 83 positions.append((x, y)) 84 positions.append((x, 0)) 85 if x + 1 < len(self._values): 86 n = self._values[x + 1] 87 if n * y < 0: 88 z = float(abs(y)) / (abs(y) + abs(n)) 89 positions.append((x + z, 0)) 90 positions.append((x + z, 0)) 91 92 else: 93 for x, y in zip(xrange(len(self._categoryAxis.GetDataPoints())), self._values): 94 positions.append((y, x)) 95 positions.append((0, x)) 96 if x + 1 < len(self._values): 97 n = self._values[x + 1] 98 if n * y < 0: 99 z = float(abs(y)) / (abs(y) + abs(n)) 100 positions.append((0, x + z)) 101 positions.append((0, x + z)) 102 103 self._vertices = positions 104 return positions 105 106 def _AxisChanged(self, _): 107 if self._locked: 108 self._dirty = True 109 else: 110 self._Rescale() 111 112 def LockGraphUpdates(self): 113 self._locked = True 114 115 def UnlockGraphUpdates(self): 116 if self._locked: 117 self._locked = False 118 if self._dirty: 119 self._Rescale() 120 121 def _Rescale(self): 122 self.CancelAnimation(False) 123 self._dirty = False 124 self.renderObject.SetVertices(self._GetVertexPositions(), self._GetTransform()) 125 126 def CancelAnimations(self): 127 self.StopAnimations() 128 self.opacity = 1.0 129 130 def OnMouseEnter(self, *args): 131 self._originalColor = self.color.GetRGBA() 132 self.color = self.hoverColor 133 134 def OnMouseExit(self, *args): 135 self.color = self._originalColor