2017-10-17 117 views
0

我發現this示例來自pyqtgraph github回購。pyqtgraph獲取節點的文本並在MouseClick上更改顏色

我想與pyqtgraph具有以下功能創建一個交互式(網絡)圖:

  • 當用戶點擊一個節點上,他/她將獲得的 的「text',信息是節點。
  • 當一個節點被點擊節點的顏色,以及它的所有鄰居(邊緣和節點)的 將改變(例如黃色)
  • 允許多個選擇

代碼:

# -*- coding: utf-8 -*- 
""" 
Simple example of subclassing GraphItem. 
""" 

import initExample ## Add path to library (just for examples; you do not need this) 

import pyqtgraph as pg 
from pyqtgraph.Qt import QtCore, QtGui 
import numpy as np 

# Enable antialiasing for prettier plots 
pg.setConfigOptions(antialias=True) 

w = pg.GraphicsWindow() 
w.setWindowTitle('pyqtgraph example: CustomGraphItem') 
v = w.addViewBox() 
v.setAspectLocked() 

class Graph(pg.GraphItem): 
    def __init__(self): 
     self.dragPoint = None 
     self.dragOffset = None 
     self.textItems = [] 
     pg.GraphItem.__init__(self) 
     self.scatter.sigClicked.connect(self.clicked) 

    def setData(self, **kwds): 
     self.text = kwds.pop('text', []) 
     self.data = kwds 
     if 'pos' in self.data: 
      npts = self.data['pos'].shape[0] 
      self.data['data'] = np.empty(npts, dtype=[('index', int)]) 
      self.data['data']['index'] = np.arange(npts) 
     self.setTexts(self.text) 
     self.updateGraph() 

    def setTexts(self, text): 
     for i in self.textItems: 
      i.scene().removeItem(i) 
     self.textItems = [] 
     for t in text: 
      item = pg.TextItem(t) 
      self.textItems.append(item) 
      item.setParentItem(self) 

    def updateGraph(self): 
     pg.GraphItem.setData(self, **self.data) 
     for i,item in enumerate(self.textItems): 
      item.setPos(*self.data['pos'][i]) 


    def mouseDragEvent(self, ev): 
     if ev.button() != QtCore.Qt.LeftButton: 
      ev.ignore() 
      return 

     if ev.isStart(): 
      # We are already one step into the drag. 
      # Find the point(s) at the mouse cursor when the button was first 
      # pressed: 
      pos = ev.buttonDownPos() 
      pts = self.scatter.pointsAt(pos) 
      if len(pts) == 0: 
       ev.ignore() 
       return 
      self.dragPoint = pts[0] 
      ind = pts[0].data()[0] 
      self.dragOffset = self.data['pos'][ind] - pos 
     elif ev.isFinish(): 
      self.dragPoint = None 
      return 
     else: 
      if self.dragPoint is None: 
       ev.ignore() 
       return 

     ind = self.dragPoint.data()[0] 
     self.data['pos'][ind] = ev.pos() + self.dragOffset 
     self.updateGraph() 
     ev.accept() 

    def clicked(self, pts): 
     print("clicked: %s" % pts) 


g = Graph() 
v.addItem(g) 

## Define positions of nodes 
pos = np.array([ 
    [0,0], 
    [10,0], 
    [0,10], 
    [10,10], 
    [5,5], 
    [15,5] 
    ], dtype=float) 

## Define the set of connections in the graph 
adj = np.array([ 
    [0,1], 
    [1,3], 
    [3,2], 
    [2,0], 
    [1,5], 
    [3,5], 
    ]) 

## Define the symbol to use for each node (this is optional) 
symbols = ['o','o','o','o','t','+'] 

## Define the line style for each connection (this is optional) 
lines = np.array([ 
    (255,0,0,255,1), 
    (255,0,255,255,2), 
    (255,0,255,255,3), 
    (255,255,0,255,2), 
    (255,0,0,255,1), 
    (255,255,255,255,4), 
    ], dtype=[('red',np.ubyte),('green',np.ubyte),('blue',np.ubyte),('alpha',np.ubyte),('width',float)]) 

## Define text to show next to each symbol 
texts = ["Point %d" % i for i in range(6)] 

## Update the graph 
g.setData(pos=pos, adj=adj, pen=lines, size=1, symbol=symbols, pxMode=False, text=texts) 




## Start Qt event loop unless running in interactive mode or using pyside. 
if __name__ == '__main__': 
    import sys 
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): 
     QtGui.QApplication.instance().exec_() 

上述代碼返回以下信息,被點擊的節點時:

<pyqtgraph.graphicsItems.ScatterPlotItem.ScatterPlotItem object at 0x7f1e51657558>

這實際上是整個圖形項目(因爲點擊信號在整個圖形上被調用)。

現在:我怎樣才能在每個節點上調用MouseClicking功能,獲取該節點的文本並在點擊事件中更改它的(及其鄰居)顏色?

Another example顯示在另一個繪圖中更改點擊曲線的顏色。這裏,點擊信號在每條曲線上被調用。我試圖用這個作爲一個起點,在下面第一個提到的代碼示例中實現類似的東西,但老實說,我甚至不知道如何到達pyqtgraph圖形對象中的單個節點([我可能在這裏是錯的],節點只能通過它們的位置來定義)。

一如既往,在這裏的任何幫助將非常感激。

編輯:感謝kesumu的回答,我是能夠得到一個點擊的節點像這樣的文本內容:

def clicked(self, scatter, pts): 
    data_list = scatter.data.tolist() 
    mypoint = [tup for tup in data_list if pts[0] in tup][0] 
    mypoint_index = data_list.index(mypoint) 
    mypoint_text = self.text[mypoint_index] 

編輯II

的一個更復雜的例子同樣的問題可以發現here

回答

1

問題是您的clicked函數是錯誤的。

它應該是這樣的:

def clicked(self, scatter, pts): 
    print(scatter) 
    print(pts[0]) 
    print("clicked: %s" % pts) 

因爲sigClicked有兩個參數:自我,點。這是它的定義:

sigClicked = QtCore.Signal(object, object) ## self, points 

固定後,就可以獲取單擊點是這樣的:

​​

希望這對您有所幫助。

+0

kesumu,非常感謝您的幫助,這確實非常有用!我現在可以獲取點擊節點以及它們的文本(將編輯問題如何獲取文本)。但現在我堅持如何改變點擊節點的顏色以及它的鄰居......我會把這個問題放在另一個問題上,因爲這個問題的主要關注點實際上是獲取點擊節點及其文本內容。再次謝謝你! – dliv