Example #1
0
class SmallPanel(PlotPanel):
    """
    PlotPanel for Quick plot and masking plot
    """
    def __init__(self, parent, id=-1, is_number=False, content='?', **kwargs):
        """
        """
        PlotPanel.__init__(self, parent, id=id, **kwargs)
        self.is_number = is_number
        self.content = content
        self.point = None
        self.position = (0.4, 0.5)
        self.scale = 'linear'
        self.prevXtrans = "x"
        self.prevYtrans = "y"
        self.viewModel = "--"
        self.subplot.set_xticks([])
        self.subplot.set_yticks([])
        self.add_text()
        self.figure.subplots_adjust(left=0.1, bottom=0.1)

    def set_content(self, content=''):
        """
        Set text content
        """
        self.content = str(content)

    def add_toolbar(self):
        """
        Add toolbar
        """
        # Not implemented
        pass

    def on_set_focus(self, event):
        """
        send to the parenet the current panel on focus
        """
        pass

    def add_image(self, plot):
        """
        Add Image
        """
        self.content = ''
        self.textList = []
        self.plots = {}
        self.clear()
        self.point = plot
        try:
            self.figure.delaxes(self.figure.axes[0])
            self.subplot = self.figure.add_subplot(111)
            #self.figure.delaxes(self.figure.axes[1])
        except:
            pass
        try:
            name = plot.name
        except:
            name = plot.filename
        self.plots[name] = plot

        #init graph
        self.graph = Graph()

        #add plot
        self.graph.add(plot)
        #draw
        self.graph.render(self)

        try:
            self.figure.delaxes(self.figure.axes[1])
        except:
            pass
        self.subplot.figure.canvas.resizing = False
        self.subplot.tick_params(axis='both', labelsize=9)
        # Draw zero axis lines
        self.subplot.axhline(linewidth=1, color='r')
        self.subplot.axvline(linewidth=1, color='r')

        self.erase_legend()
        try:
            # mpl >= 1.1.0
            self.figure.tight_layout()
        except:
            self.figure.subplots_adjust(left=0.1, bottom=0.1)
        self.subplot.figure.canvas.draw()

    def add_text(self):
        """
        Text in the plot
        """
        if not self.is_number:
            return

        self.clear()
        try:
            self.figure.delaxes(self.figure.axes[0])
            self.subplot = self.figure.add_subplot(111)
            self.figure.delaxes(self.figure.axes[1])
        except:
            pass
        self.subplot.set_xticks([])
        self.subplot.set_yticks([])
        label = self.content
        FONT = FontProperties()
        xpos, ypos = (0.4, 0.5)
        font = FONT.copy()
        font.set_size(14)

        self.textList = []
        self.subplot.set_xlim((0, 1))
        self.subplot.set_ylim((0, 1))

        try:
            if self.content != '?':
                float(label)
        except:
            self.subplot.set_frame_on(False)
        try:
            # mpl >= 1.1.0
            self.figure.tight_layout()
        except:
            self.figure.subplots_adjust(left=0.1, bottom=0.1)
        if len(label) > 0 and xpos > 0 and ypos > 0:
            new_text = self.subplot.text(str(xpos),
                                         str(ypos),
                                         str(label),
                                         fontproperties=font)
            self.textList.append(new_text)

    def erase_legend(self):
        """
        Remove Legend
        """
        #for ax in self.axes:
        self.remove_legend(self.subplot)

    def onMouseMotion(self, event):
        """
        Disable dragging 2D image
        """

    def onWheel(self, event):
        """
        """

    def onLeftDown(self, event):
        """
        Disables LeftDown
        """

    def onPick(self, event):
        """
        Remove Legend
        """
        for ax in self.axes:
            self.remove_legend(ax)

    def draw(self):
        """
        Draw
        """
        if self.dimension == 3:
            pass
        else:
            self.subplot.figure.canvas.resizing = False
            self.subplot.tick_params(axis='both', labelsize=9)
            self.erase_legend()
            self.subplot.figure.canvas.draw_idle()
            try:
                self.figure.delaxes(self.figure.axes[1])
            except:
                pass

    def onContextMenu(self, event):
        """
        Default context menu for a plot panel
        """
        id = wx.NewId()
        slicerpop = wx.Menu()
        data = self.point
        if issubclass(data.__class__, Data1D):
            slicerpop.Append(id, '&Change Scale')
            wx.EVT_MENU(self, id, self._onProperties)
        else:
            slicerpop.Append(id, '&Toggle Linear/Log Scale')
            wx.EVT_MENU(self, id, self.ontogglescale)
        try:
            # mouse event
            pos_evt = event.GetPosition()
            pos = self.ScreenToClient(pos_evt)
        except:
            # toolbar event
            pos_x, pos_y = self.toolbar.GetPositionTuple()
            pos = (pos_x, pos_y + 5)
        self.PopupMenu(slicerpop, pos)

    def ontogglescale(self, event):
        """
        On toggle 2d scale
        """
        self._onToggleScale(event)
        try:
            # mpl >= 1.1.0
            self.figure.tight_layout()
        except:
            self.figure.subplots_adjust(left=0.1, bottom=0.1)
        try:
            self.figure.delaxes(self.figure.axes[1])
        except:
            pass

    def _onProperties(self, event):
        """
        when clicking on Properties on context menu ,
        The Property dialog is displayed
        The user selects a transformation for x or y value and
        a new plot is displayed
        """
        list = []
        list = self.graph.returnPlottable()
        if len(list.keys()) > 0:
            first_item = list.keys()[0]
            if first_item.x != []:
                from sas.plottools.PropertyDialog import Properties
                dial = Properties(self, -1, 'Change Scale')
                # type of view or model used
                dial.xvalue.Clear()
                dial.yvalue.Clear()
                dial.view.Clear()
                dial.xvalue.Insert("x", 0)
                dial.xvalue.Insert("log10(x)", 1)
                dial.yvalue.Insert("y", 0)
                dial.yvalue.Insert("log10(y)", 1)
                dial.view.Insert("--", 0)
                dial.view.Insert("Linear y vs x", 1)
                dial.setValues(self.prevXtrans, self.prevYtrans,
                               self.viewModel)
                dial.Update()
                if dial.ShowModal() == wx.ID_OK:
                    self.xLabel, self.yLabel, self.viewModel = dial.getValues()
                    if self.viewModel == "Linear y vs x":
                        self.xLabel = "x"
                        self.yLabel = "y"
                        self.viewModel = "--"
                        dial.setValues(self.xLabel, self.yLabel,
                                       self.viewModel)
                    self._onEVT_FUNC_PROPERTY()
                dial.Destroy()

    def _onEVT_FUNC_PROPERTY(self, remove_fit=True):
        """
        Receive the x and y transformation from myDialog,
        Transforms x and y in View
        and set the scale
        """
        list = []
        list = self.graph.returnPlottable()
        # Changing the scale might be incompatible with
        # currently displayed data (for instance, going
        # from ln to log when all plotted values have
        # negative natural logs).
        # Go linear and only change the scale at the end.
        self.set_xscale("linear")
        self.set_yscale("linear")
        _xscale = 'linear'
        _yscale = 'linear'
        for item in list:
            item.setLabel(self.xLabel, self.yLabel)
            # control axis labels from the panel itself
            yname, yunits = item.get_yaxis()
            xname, xunits = item.get_xaxis()
            # Goes through all possible scales
            # Goes through all possible scales
            if (self.xLabel == "x"):
                item.transformX(transform.toX, transform.errToX)
                self.graph._xaxis_transformed("%s" % xname, "%s" % xunits)
            if (self.xLabel == "log10(x)"):
                item.transformX(transform.toX_pos, transform.errToX_pos)
                _xscale = 'log'
                self.graph._xaxis_transformed("%s" % xname, "%s" % xunits)
            if (self.yLabel == "y"):
                item.transformY(transform.toX, transform.errToX)
                self.graph._yaxis_transformed("%s" % yname, "%s" % yunits)
            if (self.yLabel == "log10(y)"):
                item.transformY(transform.toX_pos, transform.errToX_pos)
                _yscale = 'log'
                self.graph._yaxis_transformed("%s" % yname, "%s" % yunits)
            item.transformView()
        self.prevXtrans = self.xLabel
        self.prevYtrans = self.yLabel
        self.set_xscale(_xscale)
        self.set_yscale(_yscale)
        self.draw()
Example #2
0
class SmallPanel(PlotPanel):
    """
    PlotPanel for Quick plot and masking plot
    """
    def __init__(self, parent, id= -1, is_number=False, content='?', **kwargs):
        """
        """
        PlotPanel.__init__(self, parent, id=id, **kwargs)
        self.is_number = is_number
        self.content = content
        self.point = None
        self.position = (0.4, 0.5)
        self.scale = 'linear'
        self.prevXtrans = "x"
        self.prevYtrans = "y"
        self.viewModel = "--"
        self.subplot.set_xticks([])
        self.subplot.set_yticks([])
        self.add_text()
        self.figure.subplots_adjust(left=0.1, bottom=0.1)

    def set_content(self, content=''):
        """
        Set text content
        """
        self.content = str(content)

    def add_toolbar(self):
        """
        Add toolbar
        """
        # Not implemented
        pass

    def on_set_focus(self, event):
        """
        send to the parenet the current panel on focus
        """
        pass

    def add_image(self, plot):
        """
        Add Image
        """
        self.content = ''
        self.textList = []
        self.plots = {}
        self.clear()
        self.point = plot
        try:
            self.figure.delaxes(self.figure.axes[0])
            self.subplot = self.figure.add_subplot(111)
            #self.figure.delaxes(self.figure.axes[1])
        except:
            pass
        try:
            name = plot.name
        except:
            name = plot.filename
        self.plots[name] = plot

        #init graph
        self.graph = Graph()

        #add plot
        self.graph.add(plot)
        #draw        
        self.graph.render(self)

        try:
            self.figure.delaxes(self.figure.axes[1])
        except:
            pass
        self.subplot.figure.canvas.resizing = False
        self.subplot.tick_params(axis='both', labelsize=9)
        # Draw zero axis lines
        self.subplot.axhline(linewidth=1, color='r')
        self.subplot.axvline(linewidth=1, color='r')

        self.erase_legend()
        try:
            # mpl >= 1.1.0
            self.figure.tight_layout()
        except:
            self.figure.subplots_adjust(left=0.1, bottom=0.1)
        self.subplot.figure.canvas.draw()

    def add_text(self):
        """
        Text in the plot
        """
        if not self.is_number:
            return

        self.clear()
        try:
            self.figure.delaxes(self.figure.axes[0])
            self.subplot = self.figure.add_subplot(111)
            self.figure.delaxes(self.figure.axes[1])
        except:
            pass
        self.subplot.set_xticks([])
        self.subplot.set_yticks([])
        label = self.content
        FONT = FontProperties()
        xpos, ypos = (0.4, 0.5)
        font = FONT.copy()
        font.set_size(14)

        self.textList = []
        self.subplot.set_xlim((0, 1))
        self.subplot.set_ylim((0, 1))

        try:
            if self.content != '?':
                float(label)
        except:
            self.subplot.set_frame_on(False)
        try:
            # mpl >= 1.1.0
            self.figure.tight_layout()
        except:
            self.figure.subplots_adjust(left=0.1, bottom=0.1)
        if len(label) > 0 and xpos > 0 and ypos > 0:
            new_text = self.subplot.text(str(xpos), str(ypos), str(label),
                                           fontproperties=font)
            self.textList.append(new_text)

    def erase_legend(self):
        """
        Remove Legend
        """
        #for ax in self.axes:
        self.remove_legend(self.subplot)

    def onMouseMotion(self, event):
        """
        Disable dragging 2D image
        """

    def onWheel(self, event):
        """
        """

    def onLeftDown(self, event):
        """
        Disables LeftDown
        """

    def onPick(self, event):
        """
        Remove Legend
        """
        for ax in self.axes:
            self.remove_legend(ax)


    def draw(self):
        """
        Draw
        """
        if self.dimension == 3:
            pass
        else:
            self.subplot.figure.canvas.resizing = False
            self.subplot.tick_params(axis='both', labelsize=9)
            self.erase_legend()
            self.subplot.figure.canvas.draw_idle()
            try:
                self.figure.delaxes(self.figure.axes[1])
            except:
                pass


    def onContextMenu(self, event):
        """
        Default context menu for a plot panel
        """
        id = wx.NewId()
        slicerpop = wx.Menu()
        data = self.point
        if issubclass(data.__class__, Data1D):
            slicerpop.Append(id, '&Change Scale')
            wx.EVT_MENU(self, id, self._onProperties)
        else:
            slicerpop.Append(id, '&Toggle Linear/Log Scale')
            wx.EVT_MENU(self, id, self.ontogglescale)
        try:
            # mouse event
            pos_evt = event.GetPosition()
            pos = self.ScreenToClient(pos_evt)
        except:
            # toolbar event
            pos_x, pos_y = self.toolbar.GetPositionTuple()
            pos = (pos_x, pos_y + 5)
        self.PopupMenu(slicerpop, pos)

    def ontogglescale(self, event):
        """
        On toggle 2d scale
        """
        self._onToggleScale(event)
        try:
            # mpl >= 1.1.0
            self.figure.tight_layout()
        except:
            self.figure.subplots_adjust(left=0.1, bottom=0.1)
        try:
            self.figure.delaxes(self.figure.axes[1])
        except:
            pass

    def _onProperties(self, event):
        """
        when clicking on Properties on context menu ,
        The Property dialog is displayed
        The user selects a transformation for x or y value and
        a new plot is displayed
        """
        list = []
        list = self.graph.returnPlottable()
        if len(list.keys()) > 0:
            first_item = list.keys()[0]
            if first_item.x != []:
                from sas.plottools.PropertyDialog import Properties
                dial = Properties(self, -1, 'Change Scale')
                # type of view or model used
                dial.xvalue.Clear()
                dial.yvalue.Clear()
                dial.view.Clear()
                dial.xvalue.Insert("x", 0)
                dial.xvalue.Insert("log10(x)", 1)
                dial.yvalue.Insert("y", 0)
                dial.yvalue.Insert("log10(y)", 1)
                dial.view.Insert("--", 0)
                dial.view.Insert("Linear y vs x", 1)
                dial.setValues(self.prevXtrans, self.prevYtrans, self.viewModel)
                dial.Update()
                if dial.ShowModal() == wx.ID_OK:
                    self.xLabel, self.yLabel, self.viewModel = dial.getValues()
                    if self.viewModel == "Linear y vs x":
                        self.xLabel = "x"
                        self.yLabel = "y"
                        self.viewModel = "--"
                        dial.setValues(self.xLabel, self.yLabel, self.viewModel)
                    self._onEVT_FUNC_PROPERTY()
                dial.Destroy()

    def _onEVT_FUNC_PROPERTY(self, remove_fit=True):
        """
        Receive the x and y transformation from myDialog,
        Transforms x and y in View
        and set the scale
        """
        list = []
        list = self.graph.returnPlottable()
        # Changing the scale might be incompatible with
        # currently displayed data (for instance, going
        # from ln to log when all plotted values have
        # negative natural logs).
        # Go linear and only change the scale at the end.
        self.set_xscale("linear")
        self.set_yscale("linear")
        _xscale = 'linear'
        _yscale = 'linear'
        for item in list:
            item.setLabel(self.xLabel, self.yLabel)
            # control axis labels from the panel itself
            yname, yunits = item.get_yaxis()
            xname, xunits = item.get_xaxis()
            # Goes through all possible scales
            # Goes through all possible scales
            if(self.xLabel == "x"):
                item.transformX(transform.toX, transform.errToX)
                self.graph._xaxis_transformed("%s" % xname, "%s" % xunits)
            if(self.xLabel == "log10(x)"):
                item.transformX(transform.toX_pos, transform.errToX_pos)
                _xscale = 'log'
                self.graph._xaxis_transformed("%s" % xname, "%s" % xunits)
            if(self.yLabel == "y"):
                item.transformY(transform.toX, transform.errToX)
                self.graph._yaxis_transformed("%s" % yname, "%s" % yunits)
            if(self.yLabel == "log10(y)"):
                item.transformY(transform.toX_pos, transform.errToX_pos)
                _yscale = 'log'
                self.graph._yaxis_transformed("%s" % yname, "%s" % yunits)
            item.transformView()
        self.prevXtrans = self.xLabel
        self.prevYtrans = self.yLabel
        self.set_xscale(_xscale)
        self.set_yscale(_yscale)
        self.draw()