Make gui.panel.plot's toolbar optional & start generalizing axes selection
authorW. Trevor King <wking@drexel.edu>
Sun, 1 Aug 2010 22:43:07 +0000 (18:43 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 1 Aug 2010 22:43:07 +0000 (18:43 -0400)
hooke/ui/gui/panel/plot.py

index e46c8190aad9dd8566aac4e1b3999cc1b8989426..ab81c6029b5387cd337f33a97ec7769e0a969333 100644 (file)
@@ -83,18 +83,19 @@ class PlotPanel (Panel, wx.Panel):
         self.display_coordinates = False\r
         self.style = 'line'\r
         self._curve = None\r
+        self._x_column = None\r
+        self._y_column = None\r
         super(PlotPanel, self).__init__(\r
             name='plot', callbacks=callbacks, **kwargs)\r
         self._c = {}\r
         self._c['figure'] = Figure()\r
         self._c['canvas'] = FigureCanvas(\r
             parent=self, id=wx.ID_ANY, figure=self._c['figure'])\r
-        self._c['toolbar'] = NavToolbar(self._c['canvas'])\r
 \r
         self._set_color(wx.NamedColor('WHITE'))\r
         sizer = wx.BoxSizer(wx.VERTICAL)\r
         sizer.Add(self._c['canvas'], 1, wx.LEFT | wx.TOP | wx.GROW)\r
-        self._setup_toolbar(toolbar=self._c['toolbar'], sizer=sizer)\r
+        self._setup_toolbar(sizer=sizer)  # comment out to remove plot toolbar.\r
         self.SetSizer(sizer)\r
         self.Fit()\r
 \r
@@ -108,13 +109,14 @@ class PlotPanel (Panel, wx.Panel):
         self._c['figure'].canvas.mpl_connect(\r
             'motion_notify_event', self._on_mouse_move)\r
 \r
-    def _setup_toolbar(self, toolbar, sizer):\r
+    def _setup_toolbar(self, sizer):\r
+        self._c['toolbar'] = NavToolbar(self._c['canvas'])\r
         self._c['toolbar'].Realize()  # call after putting items in the toolbar\r
         if wx.Platform == '__WXMAC__':\r
             # Mac platform (OSX 10.3, MacPython) does not seem to cope with\r
             # having a toolbar in a sizer. This work-around gets the buttons\r
             # back, but at the expense of having the toolbar at the top\r
-            self.SetToolBar(toolbar)\r
+            self.SetToolBar(self._c['toolbar'])\r
         elif wx.Platform == '__WXMSW__':\r
             # On Windows platform, default window size is incorrect, so set\r
             # toolbar width to figure width.\r
@@ -123,10 +125,10 @@ class PlotPanel (Panel, wx.Panel):
             # By adding toolbar in sizer, we are able to put it at the bottom\r
             # of the frame - so appearance is closer to GTK version.\r
             # As noted above, doesn't work for Mac.\r
-            toolbar.SetSize(wx.Size(fw, th))\r
-            sizer.Add(toolbar, 0 , wx.LEFT | wx.EXPAND)\r
+            self._c['toolbar'].SetSize(wx.Size(fw, th))\r
+            sizer.Add(self._c['toolbar'], 0 , wx.LEFT | wx.EXPAND)\r
         else:\r
-            sizer.Add(toolbar, 0 , wx.LEFT | wx.EXPAND)\r
+            sizer.Add(self._c['toolbar'], 0 , wx.LEFT | wx.EXPAND)\r
         self._c['toolbar'].update()  # update the axes menu on the toolbar\r
 \r
     def _set_color(self, rgbtuple=None):\r
@@ -159,19 +161,29 @@ class PlotPanel (Panel, wx.Panel):
         #self.SetStatusText('')\r
 \r
     def _on_mouse_move(self, event):\r
-        if event.guiEvent.m_shiftDown:\r
-            self._c['toolbar'].set_cursor(wx.CURSOR_RIGHT_ARROW)\r
-        else:\r
-            self._c['toolbar'].set_cursor(wx.CURSOR_ARROW)\r
+        if 'toolbar' in self._c:\r
+            if event.guiEvent.m_shiftDown:\r
+                self._c['toolbar'].set_cursor(wx.CURSOR_RIGHT_ARROW)\r
+            else:\r
+                self._c['toolbar'].set_cursor(wx.CURSOR_ARROW)\r
         if self.display_coordinates:\r
             coordinateString = ''.join(\r
                 ['x: ', str(event.xdata), ' y: ', str(event.ydata)])\r
             #TODO: pretty format\r
             #self.SetStatusText(coordinateString)\r
 \r
+    def _on_x_column(self, event):\r
+        pass\r
+\r
+    def _on_y_column(self, event):\r
+        pass\r
+\r
     def _resize_canvas(self):\r
         w,h = self.GetClientSize()\r
-        tw,th = self._c['toolbar'].GetSizeTuple()\r
+        if 'toolbar' in self._c:\r
+            tw,th = self._c['toolbar'].GetSizeTuple()\r
+        else:\r
+            th = 0\r
         dpi = float(self._c['figure'].get_dpi())\r
         self._c['figure'].set_figwidth(w/dpi)\r
         self._c['figure'].set_figheight((h-th)/dpi)\r
@@ -185,12 +197,17 @@ class PlotPanel (Panel, wx.Panel):
 \r
     def set_curve(self, curve, config={}):\r
         self._curve = curve\r
+        columns = set()\r
+        for data in curve.data:\r
+            columns = columns.union(set(data.info['columns']))\r
+        self._columns = sorted(columns)\r
+        if self._x_column not in self._columns:\r
+            self._x_column = self._columns[0]\r
+        if self._y_column not in self._columns:\r
+            self._y_column = self._columns[-1]\r
         self.update(config=config)\r
 \r
     def update(self, config={}):\r
-        x_name = 'z piezo (m)'\r
-        y_name = 'deflection (m)'\r
-\r
         self._c['figure'].clear()\r
         self._c['figure'].suptitle(\r
             self._hooke_frame._file_name(self._curve.name),\r
@@ -199,26 +216,24 @@ class PlotPanel (Panel, wx.Panel):
 \r
         if config['plot SI format'] == 'True':  # TODO: config should convert\r
             d = int(config['plot decimals'])  # TODO: config should convert\r
-            x_n, x_unit = split_data_label(x_name)\r
-            y_n, y_unit = split_data_label(y_name)\r
-            fx = HookeFormatter(decimals=d)#, unit=x_unit)\r
+            x_n, x_unit = split_data_label(self._x_column)\r
+            y_n, y_unit = split_data_label(self._y_column)\r
+            fx = HookeFormatter(decimals=d, unit=x_unit)\r
             axes.xaxis.set_major_formatter(fx)\r
             fy = HookeFormatter(decimals=d, unit=y_unit)\r
             axes.yaxis.set_major_formatter(fy)\r
             axes.set_xlabel(x_n)\r
             axes.set_ylabel(y_n)\r
         else:\r
-            axes.set_xlabel(x_name)\r
-            axes.set_ylabel(y_name)\r
+            axes.set_xlabel(self._x_column)\r
+            axes.set_ylabel(self._y_column)\r
 \r
         self._c['figure'].hold(True)\r
         for i,data in enumerate(self._curve.data):\r
-            axes.plot(data[:,data.info['columns'].index(x_name)],\r
-                      data[:,data.info['columns'].index(y_name)],\r
+            axes.plot(data[:,data.info['columns'].index(self._x_column)],\r
+                      data[:,data.info['columns'].index(self._y_column)],\r
                       '.',\r
                       label=data.info['name'])\r
         if config['plot legend'] == 'True':  # HACK: config should convert\r
             axes.legend(loc='best')\r
         self._c['canvas'].draw()\r
-\r
-#  LocalWords:  matplotlib\r