使用pyqt 显示控件实现曲线的绘制
代码中brain_node_disk_session 为我项目中需要显示的数据源,自行替换。
class XchartView(QWidget): def __init__(self,brain_node_disk_session): super(XchartView, self).__init__() self.brain_node_disk_session = brain_node_disk_session main_layout =QVBoxLayout() comb_box =QHBoxLayout() self.combox1 = QComboBox() self.combox2 = QComboBox() self.combox1.addItems(utils_ui_list_sig) self.combox2.addItems(utils_ui_list_sig) self.combox1.setStyleSheet("QWidget{color:red}")#color self.combox2.setStyleSheet("QWidget{color:green}") comb_box.addWidget(self.combox1) comb_box.addWidget(self.combox2) self.win_charts_sensor = pyqtgraph.GraphicsLayoutWidget(show=True) self.label = pyqtgraph.LabelItem(justify='right') self.win_charts_sensor.addItem(self.label) self.win_charts_sensor.nextRow() # self.win_charts_sensor = pyqtgraph.PlotWidget() self.p1 = self.win_charts_sensor.addPlot()#(title="value") # self.p1 = self.win_charts_sensor.plotItem #self.p1.setLabels(left='axis 1') self.p2 = pyqtgraph.ViewBox() self.p1.showAxis('right') self.p1.scene().addItem(self.p2) self.p1.getAxis('right').linkToView(self.p2) self.p2.setXLink(self.p1) #self.p1.getAxis('right').setLabel('axis2', color='#0000ff') # self.label.setGeometry(QRectF(0, 0, 100, 100)) self.vLine = pyqtgraph.InfiniteLine(angle=90, movable=False) self.hLine = pyqtgraph.InfiniteLine(angle=0, movable=False) #self.curpose_Line = pyqtgraph.InfiniteLine(angle=90, movable=False)#current slide position self.p1.addItem(self.vLine, ignoreBounds=True) self.p1.addItem(self.hLine, ignoreBounds=True) #self.p1.addItem(self.curpose_Line, ignoreBounds=True) self.vb = self.p1.vb main_layout.addLayout(comb_box) main_layout.addWidget(self.win_charts_sensor) main_layout.setContentsMargins(0, 0, 0, 0) self.setLayout(main_layout) self.combox1_data = [] self.combox2_data = [] self.curve1 = self.p1.plot(self.combox1_data, pen="r") self.p2.addItem(pyqtgraph.PlotCurveItem(self.combox2_data, pen='g')) self.combox1.currentIndexChanged[int].connect(self.update_comb1) self.combox2.currentIndexChanged[int].connect(self.update_comb2) self.ts = 0 self.timer_init() self.index1_choose = None self.index2_choose = None def timer_init(self): #使用QTimer,2秒触发一次,更新数据 self.timer = QTimer(self) self.timer.timeout.connect(self.drawLine) self.timer.start(200) def drawLine(self): # print("self.index1_choose,self.index2_choose:",self.index1_choose,self.index2_choose) if self.index1_choose != None and self.index2_choose != None: self.combox1_data = np.array(self.get_val_from_str(utils_ui_list_sig[self.index1_choose])) self.combox2_data = np.array(self.get_val_from_str(utils_ui_list_sig[self.index2_choose])) elif self.index1_choose == None and self.index2_choose != None: self.combox1_data = [] self.combox2_data = np.array(self.get_val_from_str(utils_ui_list_sig[self.index2_choose])) elif self.index1_choose != None and self.index2_choose == None: self.combox1_data = np.array(self.get_val_from_str(utils_ui_list_sig[self.index1_choose])) self.combox2_data = [] else: self.combox1_data = [] self.combox2_data = [] # print("self.combox1_data's length:",len(self.combox1_data)) self.curve1.setData(self.combox1_data) self.p2.addItem(pyqtgraph.PlotCurveItem(self.combox2_data, pen='g')) def update_comb1(self, index): self.index1_choose = index self.combox1_data = [] self.curve1.setData(self.combox1_data) self.combox1_data = np.array(self.get_val_from_str(utils_ui_list_sig[index])) if np.any(self.combox1_data) : self.curve1.setData(self.combox1_data) else: print("self.combox2_data is null") self.proxy = pyqtgraph.SignalProxy(self.p1.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved) def update_comb2(self, index): self.index2_choose = index self.p2.setGeometry(self.p1.vb.sceneBoundingRect()) self.p2.linkedViewChanged(self.p1.vb, self.p2.XAxis) self.combox2_data = [] self.p2.clear() self.combox2_data = np.array(self.get_val_from_str(utils_ui_list_sig[index])) # self.curve2 = self.plot_view_v1.plot(self.combox2_data, pen="g") if np.any(self.combox2_data): self.p2.addItem(pyqtgraph.PlotCurveItem(self.combox2_data, pen='g')) else: print("self.combox2_data is null") self.proxy = pyqtgraph.SignalProxy(self.p1.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved) def mouseMoved(self,evt): pos = evt[0] ## using signal proxy turns original arguments into a tuple if self.p1.sceneBoundingRect().contains(pos): mousePoint = self.vb.mapSceneToView(pos) mouse_index = int(mousePoint.x()) combox1_data_str, combox2_data_str = self.get_cur_combox_value(mouse_index) try: self.ts = self.brain_node_disk_session.data_lidar.timestamps[mouse_index]/1000 except: self.ts = self.brain_node_disk_session.data_lidar.timestamps[-1] / 1000 # print("ts:",self.ts) self.label.setText( "<span style='font-size: 10pt'>ts=%s, <span style='font-size: 10pt'>x=%0.3f, <span style='color: red'>y1=%s</span>, <span style='color: green'>y2=%s</span>" % ( time_convert(self.ts), mouse_index, combox1_data_str, combox2_data_str)) self.vLine.setPos(mousePoint.x()) self.hLine.setPos(mousePoint.y()) def set_p(self,pos_index): mouse_index = pos_index self.vLine.setPos(pos_index) combox1_data_str, combox2_data_str=self.get_cur_combox_value(mouse_index) self.ts = self.brain_node_disk_session.data_imu.timestamps[pos_index]/1000 self.label.setText( "<span style='font-size: 10pt'>ts=%s, <span style='font-size: 10pt'>x=%0.3f, <span style='color: red'>y1=%s</span>, <span style='color: green'>y2=%s</span>" % ( time_convert(self.ts),mouse_index, combox1_data_str, combox2_data_str)) def get_cur_combox_value(self,mouse_index): len_comb1 = len(self.combox1_data) len_comb2 = len(self.combox2_data) if len_comb1 > 0 and mouse_index > 0 and mouse_index < len_comb1: combox1_data_str = '{:.3f}'.format(self.combox1_data[mouse_index]) else: combox1_data_str = '_' if len_comb2 > 0 and mouse_index > 0 and mouse_index < len_comb2: combox2_data_str = '{:.3f}'.format(self.combox2_data[mouse_index]) else: combox2_data_str = '_' return combox1_data_str,combox2_data_str def set_y_range(self, up, down): self.p1.setRange(yRange=(down,up))