Guest User

Untitled

a guest
Oct 22nd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Simple example of subclassing GraphItem.
  4. """
  5.  
  6. import initExample ## Add path to library (just for examples; you do not need this)
  7.  
  8. import pyqtgraph as pg
  9. from pyqtgraph.Qt import QtCore, QtGui
  10. import numpy as np
  11.  
  12. # Enable antialiasing for prettier plots
  13. pg.setConfigOptions(antialias=True)
  14.  
  15. w = pg.GraphicsWindow()
  16. w.setWindowTitle('pyqtgraph example: CustomGraphItem')
  17. v = w.addViewBox()
  18. v.setAspectLocked()
  19.  
  20. class Graph(pg.GraphItem):
  21. def __init__(self):
  22. self.dragPoint = None
  23. self.dragOffset = None
  24. self.textItems = []
  25. pg.GraphItem.__init__(self)
  26. self.scatter.sigClicked.connect(self.clicked)
  27.  
  28. def setData(self, **kwds):
  29. self.text = kwds.pop('text', [])
  30. self.data = kwds
  31. if 'pos' in self.data:
  32. npts = self.data['pos'].shape[0]
  33. self.data['data'] = np.empty(npts, dtype=[('index', int)])
  34. self.data['data']['index'] = np.arange(npts)
  35. self.setTexts(self.text)
  36. self.updateGraph()
  37.  
  38. def setTexts(self, text):
  39. for i in self.textItems:
  40. i.scene().removeItem(i)
  41. self.textItems = []
  42. for t in text:
  43. item = pg.TextItem(t)
  44. self.textItems.append(item)
  45. item.setParentItem(self)
  46.  
  47. def updateGraph(self):
  48. pg.GraphItem.setData(self, **self.data)
  49. for i,item in enumerate(self.textItems):
  50. item.setPos(*self.data['pos'][i])
  51.  
  52.  
  53. def mouseDragEvent(self, ev):
  54. if ev.button() != QtCore.Qt.LeftButton:
  55. ev.ignore()
  56. return
  57.  
  58. if ev.isStart():
  59. # We are already one step into the drag.
  60. # Find the point(s) at the mouse cursor when the button was first
  61. # pressed:
  62. pos = ev.buttonDownPos()
  63. pts = self.scatter.pointsAt(pos)
  64. if len(pts) == 0:
  65. ev.ignore()
  66. return
  67. self.dragPoint = pts[0]
  68. ind = pts[0].data()[0]
  69. self.dragOffset = self.data['pos'][ind] - pos
  70. elif ev.isFinish():
  71. self.dragPoint = None
  72. return
  73. else:
  74. if self.dragPoint is None:
  75. ev.ignore()
  76. return
  77.  
  78. ind = self.dragPoint.data()[0]
  79. self.data['pos'][ind] = ev.pos() + self.dragOffset
  80. self.updateGraph()
  81. ev.accept()
  82.  
  83. def clicked(self, pts):
  84. print("clicked: %s" % pts)
  85.  
  86.  
  87. g = Graph()
  88. v.addItem(g)
  89.  
  90. ## Define positions of nodes
  91. pos = np.array([
  92. [0,0],
  93. [10,0],
  94. [0,10],
  95. [10,10],
  96. [5,5],
  97. [15,5]
  98. ], dtype=float)
  99.  
  100. ## Define the set of connections in the graph
  101. adj = np.array([
  102. [0,1],
  103. [1,3],
  104. [3,2],
  105. [2,0],
  106. [1,5],
  107. [3,5],
  108. ])
  109.  
  110. ## Define the symbol to use for each node (this is optional)
  111. symbols = ['o','o','o','o','t','+']
  112.  
  113. ## Define the line style for each connection (this is optional)
  114. lines = np.array([
  115. (255,0,0,255,1),
  116. (255,0,255,255,2),
  117. (255,0,255,255,3),
  118. (255,255,0,255,2),
  119. (255,0,0,255,1),
  120. (255,255,255,255,4),
  121. ], dtype=[('red',np.ubyte),('green',np.ubyte),('blue',np.ubyte),('alpha',np.ubyte),('width',float)])
  122.  
  123. ## Define text to show next to each symbol
  124. texts = ["Point %d" % i for i in range(6)]
  125.  
  126. ## Update the graph
  127. g.setData(pos=pos, adj=adj, pen=lines, size=1, symbol=symbols, pxMode=False, text=texts)
  128.  
  129.  
  130.  
  131.  
  132. ## Start Qt event loop unless running in interactive mode or using pyside.
  133. if __name__ == '__main__':
  134. import sys
  135. if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
  136. QtGui.QApplication.instance().exec_()
  137.  
  138. def clicked(self, scatter, pts):
  139. data_list = scatter.data.tolist()
  140. mypoint = [tup for tup in data_list if pts[0] in tup][0]
  141. mypoint_index = data_list.index(mypoint)
  142. mypoint_text = self.text[mypoint_index]
  143.  
  144. def clicked(self, scatter, pts):
  145. print(scatter)
  146. print(pts[0])
  147. print("clicked: %s" % pts)
  148.  
  149. sigClicked = QtCore.Signal(object, object) ## self, points
Add Comment
Please, Sign In to add comment