优化图形显示pyqtgraph代码
authorkyz <kongyuzhong@google.com>
Thu, 13 Jun 2024 03:06:06 +0000 (11:06 +0800)
committerkyz <kongyuzhong@google.com>
Thu, 13 Jun 2024 06:54:24 +0000 (14:54 +0800)
tools_v2/flow_algo/flow_algo.py
tools_v2/flow_algo/flow_algo.ui

index 54d9d117bb223f75accaf0fbc369d9dd31170699..f848c239f86cae4586c823944bc6371813c140a4 100644 (file)
@@ -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
index 32a4c2d82e0324d3a88de003dc9c1515aa8530a3..b1a8afba3fd278a86ddec80745c6ef62178dba9b 100644 (file)
@@ -18,8 +18,8 @@
     <rect>
      <x>10</x>
      <y>10</y>
-     <width>591</width>
-     <height>811</height>
+     <width>1201</width>
+     <height>391</height>
     </rect>
    </property>
    <property name="title">
    <widget class="QFrame" name="frame_factor">
     <property name="geometry">
      <rect>
-      <x>39</x>
+      <x>10</x>
       <y>60</y>
-      <width>541</width>
-      <height>351</height>
+      <width>1181</width>
+      <height>331</height>
      </rect>
     </property>
     <property name="frameShape">
@@ -44,9 +44,9 @@
    <widget class="QSlider" name="vs_factor">
     <property name="geometry">
      <rect>
-      <x>220</x>
+      <x>210</x>
       <y>40</y>
-      <width>361</width>
+      <width>371</width>
       <height>21</height>
      </rect>
     </property>
      <number>2</number>
     </property>
    </widget>
-   <widget class="QSlider" name="vs_curr">
-    <property name="geometry">
-     <rect>
-      <x>210</x>
-      <y>420</y>
-      <width>371</width>
-      <height>21</height>
-     </rect>
-    </property>
-    <property name="minimum">
-     <number>5</number>
-    </property>
-    <property name="maximum">
-     <number>400</number>
-    </property>
-    <property name="singleStep">
-     <number>5</number>
-    </property>
-    <property name="orientation">
-     <enum>Qt::Horizontal</enum>
-    </property>
-    <property name="tickPosition">
-     <enum>QSlider::NoTicks</enum>
-    </property>
-   </widget>
-   <widget class="QFrame" name="frame_curr">
-    <property name="geometry">
-     <rect>
-      <x>40</x>
-      <y>440</y>
-      <width>541</width>
-      <height>361</height>
-     </rect>
-    </property>
-    <property name="frameShape">
-     <enum>QFrame::StyledPanel</enum>
-    </property>
-    <property name="frameShadow">
-     <enum>QFrame::Raised</enum>
-    </property>
-   </widget>
    <widget class="QLabel" name="label">
     <property name="geometry">
      <rect>
      <string>2</string>
     </property>
    </widget>
-   <widget class="QLabel" name="label_3">
+   <widget class="QLabel" name="label_6">
     <property name="geometry">
      <rect>
-      <x>40</x>
-      <y>420</y>
-      <width>121</width>
-      <height>16</height>
+      <x>400</x>
+      <y>10</y>
+      <width>181</width>
+      <height>21</height>
      </rect>
     </property>
     <property name="text">
-     <string>焊接电流(5-400):</string>
+     <string> [从设备导出数据,查表显示]</string>
     </property>
    </widget>
-   <widget class="QLabel" name="label_curr">
+   <widget class="QSlider" name="vs_curr">
     <property name="geometry">
      <rect>
-      <x>170</x>
-      <y>420</y>
-      <width>21</width>
+      <x>770</x>
+      <y>40</y>
+      <width>421</width>
+      <height>21</height>
+     </rect>
+    </property>
+    <property name="minimum">
+     <number>5</number>
+    </property>
+    <property name="maximum">
+     <number>400</number>
+    </property>
+    <property name="singleStep">
+     <number>5</number>
+    </property>
+    <property name="orientation">
+     <enum>Qt::Horizontal</enum>
+    </property>
+    <property name="tickPosition">
+     <enum>QSlider::NoTicks</enum>
+    </property>
+   </widget>
+   <widget class="QLabel" name="label_3">
+    <property name="geometry">
+     <rect>
+      <x>600</x>
+      <y>40</y>
+      <width>121</width>
       <height>16</height>
      </rect>
     </property>
     <property name="text">
-     <string>5</string>
+     <string>焊接电流(5-400):</string>
     </property>
    </widget>
-   <widget class="QLabel" name="label_6">
+   <widget class="QLabel" name="label_curr">
     <property name="geometry">
      <rect>
-      <x>400</x>
-      <y>10</y>
-      <width>181</width>
-      <height>21</height>
+      <x>730</x>
+      <y>40</y>
+      <width>21</width>
+      <height>16</height>
      </rect>
     </property>
     <property name="text">
-     <string> [从设备导出数据,查表显示]</string>
+     <string>5</string>
     </property>
    </widget>
   </widget>
   <widget class="QGroupBox" name="groupBox_2">
    <property name="geometry">
     <rect>
-     <x>610</x>
-     <y>10</y>
-     <width>591</width>
-     <height>811</height>
+     <x>0</x>
+     <y>410</y>
+     <width>1211</width>
+     <height>411</height>
     </rect>
    </property>
    <property name="title">
    <widget class="QFrame" name="frame_factor_2">
     <property name="geometry">
      <rect>
-      <x>39</x>
+      <x>9</x>
       <y>60</y>
-      <width>541</width>
+      <width>1191</width>
       <height>351</height>
      </rect>
     </property>
      <number>2</number>
     </property>
    </widget>
-   <widget class="QSlider" name="vs_curr_2">
-    <property name="enabled">
-     <bool>false</bool>
-    </property>
-    <property name="geometry">
-     <rect>
-      <x>210</x>
-      <y>420</y>
-      <width>371</width>
-      <height>21</height>
-     </rect>
-    </property>
-    <property name="minimum">
-     <number>5</number>
-    </property>
-    <property name="maximum">
-     <number>400</number>
-    </property>
-    <property name="singleStep">
-     <number>5</number>
-    </property>
-    <property name="orientation">
-     <enum>Qt::Horizontal</enum>
-    </property>
-    <property name="tickPosition">
-     <enum>QSlider::NoTicks</enum>
-    </property>
-   </widget>
-   <widget class="QFrame" name="frame_curr_2">
-    <property name="geometry">
-     <rect>
-      <x>40</x>
-      <y>440</y>
-      <width>541</width>
-      <height>361</height>
-     </rect>
-    </property>
-    <property name="frameShape">
-     <enum>QFrame::StyledPanel</enum>
-    </property>
-    <property name="frameShadow">
-     <enum>QFrame::Raised</enum>
-    </property>
-   </widget>
    <widget class="QLabel" name="label_2">
     <property name="geometry">
      <rect>
      <string>1</string>
     </property>
    </widget>
-   <widget class="QLabel" name="label_4">
-    <property name="enabled">
-     <bool>false</bool>
-    </property>
-    <property name="geometry">
-     <rect>
-      <x>40</x>
-      <y>420</y>
-      <width>121</width>
-      <height>16</height>
-     </rect>
-    </property>
-    <property name="text">
-     <string>焊接电流(5-400):</string>
-    </property>
-   </widget>
-   <widget class="QLabel" name="label_curr_2">
-    <property name="enabled">
-     <bool>false</bool>
-    </property>
-    <property name="geometry">
-     <rect>
-      <x>170</x>
-      <y>420</y>
-      <width>21</width>
-      <height>16</height>
-     </rect>
-    </property>
-    <property name="text">
-     <string>5</string>
-    </property>
-   </widget>
    <widget class="QLabel" name="label_5">
     <property name="geometry">
      <rect>