Package pyforms :: Package gui :: Package Controls :: Package ControlEventTimeline :: Module TimelineChart |
[hide private]
[frames] | no frames]

# Source Code for Module pyforms.gui.Controls.ControlEventTimeline.TimelineChart

     1  #!/usr/bin/python
     2  # -*- coding: utf-8 -*-
     3
     4  """ pyforms.gui.Controls.ControlEventTimeline.TimelineChart
     5
     6  """
     7
     8  from PyQt4 import QtGui
     9
    10  __author__ = ["Ricardo Ribeiro", "Hugo Cachitas"]
    11  __credits__ = ["Ricardo Ribeiro", "Hugo Cachitas"]
    12  __license__ = "MIT"
    13  __version__ = "0.0"
    14  __maintainer__ = "Ricardo Ribeiro"
    15  __email__ = "ricardojvr@gmail.com"
    16  __status__ = "Development"
    17
    18
    19 -class TimelineChart(object):
    20
    21 -    def __init__(self, timeLineWidget, csvfileobject, color=QtGui.QColor(255, 0, 0)):
    22          self._data = []
    23          self._firstFrame = None
    24          self._graphMax = None
    25          self._graphMin = None
    26          self._widget = timeLineWidget
    27          self._color = color
    28
    29          data = [map(float, row) for row in csvfileobject]
    30
    31          self._graphMax = 0
    32          self._graphMin = 100000000000
    33          self._data = []
    34          lastX = 0
    35          for x, y in data:
    36              if y > self._graphMax:
    37                  self._graphMax = y
    38              if y < self._graphMin:
    39                  self._graphMin = y
    40
    41              if int(x - lastX) > 1:
    42                  for i in range(0, int(x - lastX) + 1):
    43                      self._data.append((lastX + i, y))
    44                      # self._data.append( None )
    45              self._data.append((x, y))
    46              lastX = x
    47
    48          if self._graphMin < 0:
    49              min2Zero = 0 - self._graphMin
    50              self._graphMin += min2Zero
    51              self._graphMax += min2Zero
    52              newData = []
    53              for f, y in self._data:
    54                  newData.append((f, y + min2Zero))
    55
    56              self._data = newData
    57
    58 -    def draw(self,  painter, left, right, top, bottom):
    59          painter.setPen(self._color)
    60          painter.setOpacity(0.7)
    61
    62          maxPixelsHeight = (bottom - top)
    63          start = self._widget.x2frame(left)
    64          end = self._widget.x2frame(right)
    65          end = len(self._data) if end > len(self._data) else end
    66
    67          diffMinMaxValue = self._graphMax - self._graphMin
    68          if diffMinMaxValue <= 0:
    69              diffMinMaxValue = 1
    70
    71          for pos1, pos2 in zip(self._data[start + 1:end], self._data[start:end]):
    72              if pos1 and pos2:
    73                  x, y = pos1
    74                  xx, yy = pos2
    75
    76                  # y -= self._graphMin
    77                  # yy -= self._graphMin
    78
    79                  y = (y * maxPixelsHeight) // diffMinMaxValue  # + yMiddle
    80                  yy = (yy * maxPixelsHeight) // diffMinMaxValue  # + yMiddle
    81
    82                  painter.drawLine(
    83                      self._widget.frame2x(xx), yy, self._widget.frame2x(x), y)
    84
    85          painter.setOpacity(1.0)
    86