Changeset 0e553fd in sasview


Ignore:
Timestamp:
Jan 4, 2012 10:35:21 AM (12 years ago)
Author:
Jae Cho <jhjcho@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
8542bf3
Parents:
e38e335
Message:

1)fixed y-axis unit when it includes x and x-axis has power
2)now enable to drag the legend

Location:
plottools/src/danse/common/plottools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • plottools/src/danse/common/plottools/PlotPanel.py

    r8d27cac r0e553fd  
    119119        wx.Panel.__init__(self, parent, id=id, **kwargs) 
    120120        self.parent = parent 
     121        self.gotLegend = 0  #to begin, legend is not picked.  
     122        self.legend_pos_loc = None 
     123        self.legend = None 
     124        self.line_collections_list = []  
    121125        self.figure = Figure(None, dpi, linewidth=2.0) 
    122126        self.color = '#b3b3b3'   
     
    126130        #self.SetBackgroundColour(parent.GetBackgroundColour()) 
    127131        self._resizeflag = True 
    128         self._SetSize() 
     132        self._SetInitialSize()  
    129133        self.subplot = self.figure.add_subplot(111) 
    130134        self.figure.subplots_adjust(left=0.2, bottom=.2) 
     
    178182        self.canvas.mpl_connect('motion_notify_event', self.onMouseMotion) 
    179183        self.canvas.mpl_connect('button_press_event', self.onLeftDown) 
     184        self.canvas.mpl_connect('pick_event', self.onPick) 
    180185        self.canvas.mpl_connect('button_release_event', self.onLeftUp) 
     186         
    181187        wx.EVT_RIGHT_DOWN(self, self.onLeftDown) 
    182188        # to turn axis off whenn resizing the panel 
     
    192198        self.connect = BindArtist(self.subplot.figure) 
    193199        #self.selected_plottable = None 
    194  
     200         
    195201        # new data for the fit  
    196202        self.fit_result = Data1D(x=[], y=[], dy=None) 
     
    264270        # let canvas know about axes 
    265271        self.canvas.set_panel(self) 
    266         #Bind focus to change the border color        
     272         
     273        #Bind focus to change the border color       
    267274        self.canvas.Bind(wx.EVT_SET_FOCUS, self.on_set_focus) 
    268275        self.canvas.Bind(wx.EVT_KILL_FOCUS, self.on_kill_focus) 
    269          
     276 
     277    def _SetInitialSize(self,): 
     278        """ 
     279        """ 
     280        pixels = self.parent.GetClientSize() 
     281        self.canvas.SetSize(pixels) 
     282        self.figure.set_size_inches( (pixels[0])/self.figure.get_dpi(), 
     283         (pixels[1])/self.figure.get_dpi(), forward=True )    
     284           
    270285    def On_Paint(self, event): 
    271286        """ 
     
    369384            self.mousemotion = False  
    370385            self.leftup = True 
    371          
     386             
     387        #release the legend     
     388        if self.gotLegend == 1: 
     389            self.gotLegend = 0 
     390            self.set_legend_alpha(1) 
     391             
     392    def set_legend_alpha(self, alpha=1): 
     393        """ 
     394        Set legend alpha 
     395        """ 
     396        if self.legend != None: 
     397            self.legend.legendPatch.set_alpha(alpha) 
     398         
     399    def onPick(self, event):     
     400        """ 
     401        On pick legend 
     402        """ 
     403        legend = self.legend 
     404        if event.artist == legend: 
     405            #gets the box of the legend. 
     406            bbox = self.legend.get_window_extent()   
     407            #get mouse coordinates at time of pick. 
     408            self.mouse_x = event.mouseevent.x   
     409            self.mouse_y = event.mouseevent.y 
     410            #get legend coordinates at time of pick. 
     411            self.legend_x = bbox.xmin           
     412            self.legend_y = bbox.ymin 
     413            #indicates we picked up the legend. 
     414            self.gotLegend = 1    
     415            self.set_legend_alpha(0.5) 
     416   
     417             
     418    def _on_legend_motion(self, event):  
     419        """ 
     420        On legend in motion 
     421        """ 
     422        ax = event.inaxes 
     423        if ax == None: 
     424            return 
     425        # Event occurred inside a plotting area 
     426        lo_x, hi_x = ax.get_xlim() 
     427        lo_y, hi_y = ax.get_ylim() 
     428        # How much the mouse moved. 
     429        x = mouse_diff_x = self.mouse_x - event.x   
     430        y = mouse_diff_y = self.mouse_y - event.y 
     431        # Put back inside 
     432        if x < lo_x: 
     433            x = lo_x 
     434        if x > hi_x: 
     435            x = hi_x 
     436        if y < lo_y: 
     437            y = lo_y 
     438        if y> hi_y: 
     439            y = hi_y 
     440        # Move the legend from its previous location by that same amount 
     441        loc_in_canvas = self.legend_x - mouse_diff_x, \ 
     442                        self.legend_y - mouse_diff_y 
     443        # Transform into legend coordinate system 
     444        trans_axes = self.legend.parent.transAxes.inverted() 
     445        loc_in_norm_axes = trans_axes.transform_point(loc_in_canvas) 
     446        self.legend_pos_loc = tuple(loc_in_norm_axes) 
     447        self.legend._loc = self.legend_pos_loc 
     448        self.resizing = True 
     449        self.canvas.set_resizing(self.resizing) 
     450        self.canvas.draw()  
     451            
    372452    def onMouseMotion(self, event):  
    373453        """ 
     
    377457         
    378458        """ 
     459        if self.gotLegend == 1: 
     460            self._on_legend_motion(event) 
     461            return 
    379462        if self.enable_toolbar: 
    380463            #Disable dragging without the toolbar to allow zooming with toolbar 
     
    400483            else:# no dragging is perform elsewhere 
    401484                self._dragHelper(0,0) 
     485 
    402486                 
    403487    def _offset_graph(self): 
     
    701785        """ 
    702786        if not pixels: 
    703             pixels = self.GetClientSize() 
     787            pixels = tuple(self.GetClientSize()) 
    704788        self.canvas.SetSize(pixels) 
    705         self.figure.set_size_inches(pixels[0]/self.figure.get_dpi(), 
    706         pixels[1]/self.figure.get_dpi()) 
     789        self.figure.set_size_inches( float( pixels[0] )/self.figure.get_dpi(), 
     790                                     float( pixels[1] )/self.figure.get_dpi() )  
    707791 
    708792    def draw(self): 
     
    713797        self.figure.canvas.draw_idle() 
    714798         
     799        
     800    #pick up the legend patch 
     801    def legend_picker(self, legend, event): 
     802        return self.legend.legendPatch.contains(event)       
    715803         
    716804    def get_loc_label(self): 
     
    846934                        key=operator.itemgetter(1)) 
    847935            handles2, labels2 = zip(*hl) 
    848             self.subplot.legend(handles2, labels2,  
     936            self.line_collections_list = handles2 
     937            self.legend = self.subplot.legend(handles2, labels2,  
    849938                                prop=FontProperties(size=10), numpoints=1, 
    850939                                handletextsep=.05, loc=self.legendLoc) 
     940            if self.legend != None: 
     941                self.legend.set_picker(self.legend_picker)  
     942                self.legend.set_axes(self.subplot)  
    851943         
    852944        self.subplot.figure.canvas.draw_idle()        
     
    862954         
    863955        self.legendLoc = label 
     956        self.legend_pos_loc = None 
    864957        # sort them by labels 
    865958        handles, labels = self.subplot.get_legend_handles_labels() 
     
    867960                    key=operator.itemgetter(1)) 
    868961        handles2, labels2 = zip(*hl) 
    869         self.subplot.legend(handles2, labels2,  
     962        self.line_collections_list = handles2 
     963        self.legend = self.subplot.legend(handles2, labels2,  
    870964                            prop=FontProperties(size=10),  numpoints=1, 
    871965                            handletextsep=.05, loc=self.legendLoc) 
     966        if self.legend != None: 
     967                self.legend.set_picker(self.legend_picker)  
     968                self.legend.set_axes(self.subplot)  
    872969        self.subplot.figure.canvas.draw_idle()   
    873970        #self._onEVT_FUNC_PROPERTY() 
     
    11181215                            key=operator.itemgetter(1)) 
    11191216                handles2, labels2 = zip(*hl) 
    1120                 leg = ax.legend(handles2, labels2,  numpoints=1, 
     1217                self.line_collections_list = handles2 
     1218                self.legend = ax.legend(handles2, labels2,  numpoints=1, 
    11211219                                prop=FontProperties(size=10),  
    11221220                                handletextsep=.05, loc=self.legendLoc) 
     1221                if self.legend != None: 
     1222                    self.legend.set_picker(self.legend_picker) 
     1223                    self.legend.set_axes(self.subplot)  
     1224                 
    11231225            except: 
    1124                 leg = ax.legend(prop=FontProperties(size=10),  numpoints=1, 
     1226                self.legend = ax.legend(prop=FontProperties(size=10),  numpoints=1, 
    11251227                                handletextsep=.05, loc=self.legendLoc) 
    1126                  
     1228                  
    11271229    def xaxis(self, label, units, font=None, color='black'): 
    11281230        """xaxis label and units. 
     
    16361738            if(self.yLabel == "y*x^(4)"): 
    16371739                item.transformY(transform.toYX4, transform.errToYX4) 
    1638                 xunits = convertUnit(4, xunits)  
     1740                xunits = convertUnit(4, self.xaxis_unit)  
    16391741                self.graph._yaxis_transformed("%s \ \ %s^{4}" % (yname,xname),  
    16401742                                               "%s%s" % (yunits,xunits)) 
     
    16471749                item.transformY(transform.toLogXY, transform.errToLogXY) 
    16481750                self.graph._yaxis_transformed("\ln (%s \ \ %s)" % (yname,xname), 
    1649                                                 "%s%s" % (yunits, xunits)) 
     1751                                                "%s%s" % (yunits, self.xaxis_unit)) 
    16501752            if(self.yLabel == "ln(y*x^(2))"): 
    16511753                item.transformY( transform.toLogYX2, transform.errToLogYX2)  
    1652                 xunits = convertUnit(2,xunits)  
     1754                xunits = convertUnit(2, self.xaxis_unit)  
    16531755                self.graph._yaxis_transformed("\ln (%s \ \ %s^{2})" % (yname,xname),  
    16541756                                               "%s%s" % (yunits,xunits)) 
    16551757            if(self.yLabel == "ln(y*x^(4))"): 
    16561758                item.transformY(transform.toLogYX4, transform.errToLogYX4) 
    1657                 xunits = convertUnit(4, xunits)  
     1759                xunits = convertUnit(4, self.xaxis_unit)  
    16581760                self.graph._yaxis_transformed("\ln (%s \ \ %s^{4})" % (yname,xname),  
    16591761                                               "%s%s" % (yunits,xunits)) 
    16601762            if(self.yLabel == "log10(y*x^(4))"): 
    16611763                item.transformY(transform.toYX4, transform.errToYX4) 
    1662                 xunits = convertUnit(4, xunits)  
     1764                xunits = convertUnit(4, self.xaxis_unit)  
    16631765                _yscale = 'log'  
    16641766                self.graph._yaxis_transformed("%s \ \ %s^{4}" % (yname,xname),  
  • plottools/src/danse/common/plottools/binder.py

    r82a54b8 r0e553fd  
    6868 
    6969        # Turn off picker if it hasn't already been done 
    70         try: 
    71             canvas.mpl_disconnect(canvas.button_pick_id) 
    72             canvas.mpl_disconnect(canvas.scroll_pick_id) 
    73         except:  
    74             pass 
     70        #try: 
     71        #    canvas.mpl_disconnect(canvas.button_pick_id) 
     72        #    canvas.mpl_disconnect(canvas.scroll_pick_id) 
     73        #except:  
     74        #    pass 
    7575        self._current = None 
    7676        self._actions = {} 
  • plottools/src/danse/common/plottools/canvas.py

    r82a54b8 r0e553fd  
    148148        """ 
    149149        self.panel.subplot.grid(self.panel.grid_on) 
     150        if self.panel.legend_pos_loc: 
     151            self.panel.legend._loc = self.panel.legend_pos_loc 
    150152        self.idletimer.Restart(5, *args, **kwargs)  # Delay by 5 ms 
    151153 
     
    182184        self.resizing = resizing 
    183185        self.panel.set_resizing(False) 
    184          
     186              
    185187    def draw(self, drawDC=None): 
    186188        """ 
Note: See TracChangeset for help on using the changeset viewer.