From: kyz Date: Thu, 13 Jun 2024 03:06:06 +0000 (+0800) Subject: 优化图形显示pyqtgraph代码 X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=07220285abb1ada1f907ab4382b5c842e5ebe4ae;p=yn%2Fweldingcontroller.git 优化图形显示pyqtgraph代码 --- diff --git a/tools_v2/flow_algo/flow_algo.py b/tools_v2/flow_algo/flow_algo.py index 54d9d11..f848c23 100644 --- a/tools_v2/flow_algo/flow_algo.py +++ b/tools_v2/flow_algo/flow_algo.py @@ -36,17 +36,10 @@ class App( QDialog, ui_flow_algo.Ui_Dialog ): self.label_curr.setText( str( self.vs_curr.value())) self.label_factor.setText( str(self.vs_factor.value()) ) - self.curves = {} - self.draw_image( frame =self.frame_factor, title=u"节气因子", y_name=u"流量L/min", x_name=u"电流A", curve_name=u"电流-流量", color="green") - self.draw_image( frame =self.frame_curr, title=u"焊接电流", y_name=u"流量L/min", x_name=u"节气因子", curve_name=u"节气因子-流量", color = "blue") - dump_path = 'dump.csv' - self.flow_list = [] - with open( dump_path, "r" ) as f: dump_reader = csv.reader(f) - for r in dump_reader: flow_item = {} _, curr, *flow = r @@ -54,44 +47,57 @@ class App( QDialog, ui_flow_algo.Ui_Dialog ): flow_item["flow"] = list(map( float, flow )) self.flow_list.append( flow_item ) - for i in self.flow_list: - print( i ) - self.vs_factor_2.valueChanged.connect( lambda:self.on_slider_value_changed_2( "factor")) - self.vs_curr_2.valueChanged.connect( lambda:self.on_slider_value_changed_2( "current")) - - self.vs_curr_2.setValue( 5 ) self.vs_factor_2.setValue(2) - self.label_curr_2.setText( str( self.vs_curr_2.value())) self.label_factor_2.setText( str(self.vs_factor_2.value()) ) - self.curves_2 = {} - self.draw_image( frame =self.frame_factor_2, title=u"YN节气因子", y_name=u"流量L/min", x_name=u"电流A", curve_name=u"电流-流量", color = "red" ) - self.draw_image( frame =self.frame_curr_2, title=u"YN焊接电流", y_name=u"流量L/min", x_name=u"节气因子", curve_name=u"节气因子-流量", color = "yellow" ) - self.vs_factor_2.setMinimum(1) self.vs_factor_2.setMaximum(10) self.vs_factor_2.setSingleStep(1) self.vs_factor_2.setPageStep(1) + win = pg.GraphicsLayoutWidget(self.frame_factor) + win_2 = pg.GraphicsLayoutWidget(self.frame_factor_2) + QVBoxLayout(self.frame_factor).addWidget(win) + QVBoxLayout(self.frame_factor_2).addWidget(win_2) + self.plot_factor = win.addPlot() + self.plot_curr = win.addPlot() + self.plot_factor_2 = win_2.addPlot() + self.plot_curr_2 = win_2.addPlot() + + self.draw_image( plot=self.plot_factor, title=u"节气因子", y_name=u"流量L/min", x_name=u"电流A", curve_name=u"电流-流量", color="green") + self.draw_image( plot=self.plot_curr, title=u"焊接电流", y_name=u"流量L/min", x_name=u"节气因子", curve_name=u"节气因子-流量", color = "blue") + self.draw_image( plot=self.plot_factor_2, title=u"YN节气因子", y_name=u"流量L/min", x_name=u"电流A", curve_name=u"电流-流量", color = "red" ) + self.draw_image( plot=self.plot_curr_2, title=u"YN焊接电流", y_name=u"流量L/min", x_name=u"节气因子", curve_name=u"节气因子-流量", color = "yellow" ) + + self.on_slider_value_changed( "factor" ) + self.on_slider_value_changed_2( "factor" ) + self.on_slider_value_changed( "current" ) + + def callableFunction(self, x, y): + return f"({x:.0f}, {y:.1f})" + def on_slider_value_changed( self, slider ): factor = self.vs_factor.value() curr = self.vs_curr.value() + flow = self.calc_target_flow( factor = factor, curr=curr) if slider == "factor": self.label_factor.setText( str( factor )) x = list( range( 5, 400+1, 5 )) y = list( map( lambda x: self.calc_target_flow( factor=factor, curr=x ), x )) - self.curves[u"节气因子"]["plot"].setTitle( u"节气因子-{}".format(factor) ) - self.curves[u"节气因子"]["curve"].setData( np.array( x), np.array( y)) + self.plot_factor.setTitle( u"节气因子-{}".format(factor) ) + self.plot_factor.listDataItems()[0].setData( np.array( x), np.array( y)) else: - self.on_slider_value_changed_2( slider ) - self.label_curr.setText( str( curr)) x = list(range(2,30+1, 2)) y = list( map( lambda x: self.calc_target_flow( factor=x, curr=curr ), x )) - self.curves[u"焊接电流"][u"plot"].setTitle( u"焊接电流-{}".format(self.label_curr.text())) - self.curves[u"焊接电流"][u"curve"].setData( np.array( x), np.array( y)) + self.plot_curr.setTitle( u"焊接电流-{}".format(self.label_curr.text())) + self.plot_curr.listDataItems()[0].setData( np.array( x), np.array( y)) + self.on_slider_value_changed_2( slider ) + self.groupBox.setTitle( u"客户算法 -- 节气因子 {} + 焊接电流 {} A -> 目标流量 {} L/min".format( self.label_factor.text(), self.label_curr.text(), self.calc_target_flow() )) + self.plot_factor.items[1].setPos( (curr, flow ) ) + self.plot_curr.items[1].setPos( (factor, flow ) ) def calc_target_flow( self, factor=0, curr=0 ): if factor == 0: @@ -105,85 +111,47 @@ class App( QDialog, ui_flow_algo.Ui_Dialog ): def on_slider_value_changed_2( self, slider ): factor = self.vs_factor_2.value() - #curr = self.vs_curr_2.value() curr = self.vs_curr.value() + flow = self.calc_target_flow_2( factor = factor, curr=curr) if slider == "factor": self.label_factor_2.setText( str( factor )) x = list( range( 5, 400+1, 5 )) y = list( map( lambda x: self.calc_target_flow_2( factor=factor, curr=x ), x )) - self.curves[u"YN节气因子"]["plot"].setTitle( u"节气因子-{}".format(factor) ) - self.curves[u"YN节气因子"]["curve"].setData( np.array( x), np.array( y)) + self.plot_factor_2.setTitle( u"节气因子-{}".format(factor) ) + self.plot_factor_2.listDataItems()[0].setData( np.array( x), np.array( y)) else: - self.label_curr_2.setText( str( curr)) x = list(range(1,10+1, 1)) y = list( map( lambda x: self.calc_target_flow_2( factor=x, curr=curr ), x )) - self.curves[u"YN焊接电流"][u"plot"].setTitle( u"焊接电流-{}".format(self.label_curr_2.text())) - self.curves[u"YN焊接电流"][u"curve"].setData( np.array( x), np.array( y)) - self.groupBox_2.setTitle( u"YN算法 -- 节气因子 {} + 焊接电流 {} A -> 目标流量 {} L/min".format( self.label_factor_2.text(), self.label_curr_2.text(), self.calc_target_flow_2() )) + self.plot_curr_2.setTitle( u"焊接电流-{}".format(self.label_curr.text())) + self.plot_curr_2.listDataItems()[0].setData( np.array( x), np.array( y)) + self.groupBox_2.setTitle( u"YN算法 -- 节气因子 {} + 焊接电流 {} A -> 目标流量 {} L/min".format( self.label_factor_2.text(), self.label_curr.text(), flow )) + self.plot_factor_2.items[1].setPos( (curr, flow ) ) + self.plot_curr_2.items[1].setPos( (factor, flow ) ) def calc_target_flow_2( self, factor=0, curr=0 ): if factor == 0: factor = int(self.label_factor_2.text()) if curr == 0: - curr = int(self.label_curr_2.text()) + curr = int(self.label_curr.text()) # max400A - factor 1..10 --- 10..110L/min flow_400A = 10+(factor-1)*10; # curr 0..400 -- 0.5L -- flow_400A flow = 0.5 + flow_400A *curr//400 - print( " ------ fact[{}] + curr[{}] --> flow[{}] -- [{}]".format( factor, curr, flow, 0.25*curr*0.1*factor )) + #print( " ------ fact[{}] + curr[{}] --> flow[{}] -- [{}]".format( factor, curr, flow, 0.25*curr*0.1*factor )) Flow = factor * curr *0.025 +0.5 return flow - def draw_image(self, frame, title, y_name, x_name, curve_name, color="red" ): - win = pg.GraphicsLayoutWidget(frame) - QVBoxLayout(frame).addWidget(win) - p_curr = win.addPlot(title=title) - p_curr.showGrid(x=True,y=True) - p_curr.setLabel(axis="left",text=y_name) - p_curr.setLabel(axis="bottom",text=x_name) - p_curr.setTitle( title) - p_curr.addLegend() - self.curves[title] = { "plot":p_curr, "curve":p_curr.plot(pen=color,name=curve_name) } - return - - #self.curve1 = p.plot(pen="r",name="焊接电流") - self.curve_valve_curr = p_curr.plot(pen="r",name="比例阀控制电流") - verticalLayout_flow = QVBoxLayout(self.frame_flow) - win_flow = pg.GraphicsLayoutWidget(self.frame_flow) - verticalLayout_flow.addWidget(win_flow) - p_flow = win_flow.addPlot(title="动态波形图") - p_flow.showGrid(x=True,y=True) - p_flow.setLabel(axis="left",text="气体流量 L/min") - p_flow.setLabel(axis="bottom",text="时间 (s)") - p_flow.setTitle("1.目标流量 2.实际流量") - p_flow.addLegend() - self.curve_flow_target = p_flow.plot(pen="r",name="1.目标流量") - self.curve_flow = p_flow.plot(pen="g",name="2.实际流量") - - #curr raw disp - verticalLayout_curr_raw = QVBoxLayout(self.frame_curr_raw) - win_curr_raw = pg.GraphicsLayoutWidget(self.frame_curr_raw) - verticalLayout_curr_raw.addWidget(win_curr_raw) - p_curr_raw = win_curr_raw.addPlot(title="动态波形图") - p_curr_raw.showGrid(x=True,y=True) - p_curr_raw.setLabel(axis="left",text="电流 A") - p_curr_raw.setLabel(axis="bottom",text="时间 (0.1ms/10KHz)") - p_curr_raw.setTitle("1.焊接电流") - p_curr_raw.addLegend() - self.curve_curr_raw = p_curr_raw.plot(pen="yellow",name="焊接电流") - - self.valve_curr_y = [] - self.flow_target_y = [] - self.flow_x = [] - self.flow_y = [] - self.curr_raw_x = [] - self.curr_raw_y = [] + def draw_image(self, plot, title, y_name, x_name, curve_name, color="red" ): + plot.showGrid(x=True,y=True) + plot.setLabel(axis="left",text=y_name) + plot.setLabel(axis="bottom",text=x_name) + plot.setTitle( title) + plot.addLegend() + plot.plot(pen=color,name=curve_name) + plot.addItem( pg.TargetItem(pos=(0,0), label=self.callableFunction)) if __name__ == "__main__": app = QApplication(sys.argv) # 创建一个QApplication,也就是你要开发的软件app x = App() x.show() - #App().show() sys.exit(app.exec_()) # 使用exit()或者点击关闭按钮退出QApplication - - \ No newline at end of file diff --git a/tools_v2/flow_algo/flow_algo.ui b/tools_v2/flow_algo/flow_algo.ui index 32a4c2d..b1a8afb 100644 --- a/tools_v2/flow_algo/flow_algo.ui +++ b/tools_v2/flow_algo/flow_algo.ui @@ -18,8 +18,8 @@ 10 10 - 591 - 811 + 1201 + 391 @@ -28,10 +28,10 @@ - 39 + 10 60 - 541 - 351 + 1181 + 331 @@ -44,9 +44,9 @@ - 220 + 210 40 - 361 + 371 21 @@ -72,47 +72,6 @@ 2 - - - - 210 - 420 - 371 - 21 - - - - 5 - - - 400 - - - 5 - - - Qt::Horizontal - - - QSlider::NoTicks - - - - - - 40 - 440 - 541 - 361 - - - - QFrame::StyledPanel - - - QFrame::Raised - - @@ -139,53 +98,78 @@ 2 - + - 40 - 420 - 121 - 16 + 400 + 10 + 181 + 21 - 焊接电流(5-400): + [从设备导出数据,查表显示] - + - 170 - 420 - 21 + 770 + 40 + 421 + 21 + + + + 5 + + + 400 + + + 5 + + + Qt::Horizontal + + + QSlider::NoTicks + + + + + + 600 + 40 + 121 16 - 5 + 焊接电流(5-400): - + - 400 - 10 - 181 - 21 + 730 + 40 + 21 + 16 - [从设备导出数据,查表显示] + 5 - 610 - 10 - 591 - 811 + 0 + 410 + 1211 + 411 @@ -194,9 +178,9 @@ - 39 + 9 60 - 541 + 1191 351 @@ -238,50 +222,6 @@ 2 - - - false - - - - 210 - 420 - 371 - 21 - - - - 5 - - - 400 - - - 5 - - - Qt::Horizontal - - - QSlider::NoTicks - - - - - - 40 - 440 - 541 - 361 - - - - QFrame::StyledPanel - - - QFrame::Raised - - @@ -308,38 +248,6 @@ 1 - - - false - - - - 40 - 420 - 121 - 16 - - - - 焊接电流(5-400): - - - - - false - - - - 170 - 420 - 21 - 16 - - - - 5 - -