Changeset 8274:14fbb033e243 in orange


Ignore:
Timestamp:
05/25/11 19:42:17 (3 years ago)
Author:
Noughmad <Noughmad@…>
Branch:
default
Convert:
d02bb638d4e691b1717c15453909180e72c4cbc8
Message:

Ability to draw basic curves, and simple coordinate mapping from data to graph

Location:
orange/OrangeWidgets
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/Graph/axis.py

    r8271 r8274  
    1818 
    1919class Axis(QGraphicsItemGroup): 
    20     def __init__(self, line_style, size, label, line = None, parent=None): 
     20    def __init__(self, size, label, line = None, style=None, parent=None): 
    2121        QGraphicsItemGroup.__init__(self, parent) 
    2222        self.size = size 
     
    3232        self.label_item = QGraphicsTextItem(self) 
    3333        self.label = label 
     34        if style: 
     35            self.style = style 
     36        else: 
     37            self.style = palette.shared_palette().axis_style 
    3438        self.update() 
    3539 
  • orange/OrangeWidgets/Graph/curve.py

    r8273 r8274  
    22from OWBaseWidget import * 
    33 
    4 from PyQt4.QtGui import QGraphicsItemGroup, QGraphicsEllipseItem 
     4from PyQt4.QtGui import QGraphicsItemGroup, QGraphicsEllipseItem, QGraphicsLineItem 
    55from PyQt4.QtGui import QBrush, QPen 
    66 
     
    2222 
    2323class Curve(QGraphicsItemGroup): 
    24     def __init__(self, parent=None, scene=None): 
    25         QGraphicsItemGroup.__init__(self,  parent, scene) 
     24    def __init__(self, data, style, graph, parent=None): 
     25        QGraphicsItemGroup.__init__(self,  parent) 
     26        self.graph = graph 
     27        self.items = [] 
     28        for i in range(len(data)-1): 
     29            (x, y) = self.graph.map_to_graph(data[i]) 
     30            (x1, y1) = self.graph.map_to_graph(data[i+1]) 
     31            item = QGraphicsLineItem( x, y, x1, y1, self ) 
     32            item.setPen(style.pen()) 
     33            self.items.append(item) 
    2634     
    2735    def __setattr__(self, name, value): 
  • orange/OrangeWidgets/Graph/palette.py

    r8273 r8274  
    1212            Returns a QPen matching this line style 
    1313         
    14     .. class:: PointStyle 
     14        .. attribute:: point_size 
    1515         
    16         .. attribute:: color 
    17          
    18         .. attribute:: size 
    19          
    20         .. attribute:: shape 
     16        .. attribute:: point_shape 
    2117 
    2218    .. class:: Palette 
     
    4238from PyQt4.QtCore import Qt 
    4339 
     40NoShape = 0 
     41CircleShape = 1 
     42SquareShape = 2 
     43CrossShape = 3 
     44PlusShape = 4 
     45EllipseShape = 5 
     46 
     47_shared_palette = None 
     48 
    4449class LineStyle: 
    45     def __init__(self,  color=Qt.black,  width=1,  type=Qt.SolidLine): 
     50    def __init__(self,  color=Qt.black,  width=1,  type=Qt.SolidLine,  point_shape=CircleShape, point_size=5): 
    4651        self.color = color 
    4752        self.width = width 
    4853        self.type = type 
     54        self.point_shape = point_shape 
     55        self.point_size = point_size 
    4956         
    5057    def pen(self): 
     
    5562        return p 
    5663 
    57 NoShape = 0 
    58 CircleShape = 1 
    59 SquareShape = 2 
    60 CrossShape = 3 
    61 PlusShape = 4 
    62 EllipseShape = 5 
    63  
    64 class PointStyle: 
    65     def __init__(self,  color=Qt.black, size=5, shape=CircleShape): 
    66         self.color = color 
    67         self.size = size 
    68         self.shape = shape 
    69  
    70 _shared_palette = None 
    71  
    7264class Palette: 
    7365    def __init__(self): 
    7466        self.grid_style = LineStyle(Qt.gray,  1,  Qt.SolidLine) 
    75         self.line_styles = [] 
     67        self.line_styles = [ LineStyle(Qt.blue, 1, Qt.SolidLine) ] 
    7668        self.point_styles = [] 
    7769        self.axis_style = LineStyle(Qt.black, 1, Qt.SolidLine) 
  • orange/OrangeWidgets/QtGraph.py

    r8273 r8274  
    1313        Chooses which palette is used by this graph. By default, this is `shared_palette`.  
    1414         
    15     .. method mapToGraph(point) 
    16         Maps the ``point`` in scene coordinates to graph (data) coordinates 
    17          
    18     .. method mapFromGraph(point) 
    19         Maps the ``point`` from data coordinates to graph coordinates 
     15    .. method map_to_graph(axis_ids, point) 
     16        Maps the ``point`` in data coordinates to graph (scene) coordinates 
     17        This method has to be reimplemented in graphs with special axes (RadViz, PolyViz) 
     18         
     19    .. method map_from_graph(axis_ids, point) 
     20        Maps the ``point`` from scene coordinates to data coordinates 
     21        This method has to be reimplemented in graphs with special axes (RadViz, PolyViz) 
    2022         
    2123    .. method activateZooming() 
    2224        Activates zoom 
     25         
     26    .. method clear() 
     27        Removes all curves from the graph 
    2328""" 
    2429 
     
    7378         
    7479        self.selectionCurveList = [] 
     80        self.curves = [] 
     81        self.data_range = {xBottom : (0, 1), yLeft : (0, 1)} 
     82         
     83        self.map_to_graph = self.map_to_graph_cart 
     84        self.map_from_graph = self.map_from_graph_cart 
    7585 
    7686        self.update() 
     
    8191         
    8292    def update(self): 
    83         size = self.childrenRect().size() 
    84          
    8593        if self.show_legend and not self.legend: 
    8694            self.legend = legend.Legend(self.canvas) 
     
    9098            self.legend = None 
    9199         
    92     def mapToGraph(self, point): 
    93         # TODO 
    94         return point 
    95          
    96     def mapFromGraph(self, point): 
    97         # TODO 
    98         return point 
     100    def map_to_graph_cart(self, point, axes=None): 
     101        px, py = point 
     102        if not axes: 
     103            axes = [xBottom, yLeft] 
     104        min_x, max_x = self.data_range[axes[0]] 
     105        min_y, max_y = self.data_range[axes[1]] 
     106        # TODO: Adjust the childrenRect for axis, labels and legends 
     107        rx = (px - min_x) * self.childrenRect().width() / (max_x - min_x) 
     108        ry = -(py - min_y) * self.childrenRect().height() / (max_y - min_y) 
     109        return (rx, ry) 
     110         
     111    def map_from_graph_cart(self, point, axes = None): 
     112        px, py = point 
     113        if not axes: 
     114            axes = [xBottom, yLeft] 
     115        min_x, max_x = self.data_range[axes[0]] 
     116        min_y, max_y = self.data_range[axes[1]] 
     117        # TODO: Adjust the childrenRect for axis, labels and legends 
     118        rect = self.childrenRect() 
     119        rx = (px - rect.left()) / rect().width() * (max_x - min_x) 
     120        ry = -(py - rect.bottom()) / rect.height() * (max_y - min_y) 
     121        return (rx, ry) 
    99122         
    100123    def saveToFile(self, extraButtons = []): 
     
    231254                 symbol = palette.EllipseShape, enableLegend = 0, xData = [], yData = [], showFilledSymbols = None, 
    232255                 lineWidth = 1, pen = None, autoScale = 0, antiAlias = None, penAlpha = 255, brushAlpha = 255): 
    233         # TODO: Should return the Curve object 
    234         c = curve.Curve() 
     256        self.data_range[xBottom] = ( min(xData), max(xData) ) 
     257        self.data_range[yLeft] = ( min(yData), max(yData) ) 
     258        data = [] 
     259        for i in range(len(xData)): 
     260            data.append( (xData[i], yData[i]) ) 
     261            c = curve.Curve(data, self.palette.line_styles[0], self) 
     262            self.canvas.addItem(c) 
     263            self.curves.append(c) 
    235264        return c 
     265         
     266    def addAxis(self, axis_id, line, text): 
     267        a = axis.Axis() 
     268        self.axis[id] = a 
     269         
     270    def removeAllSelections(self): 
     271        pass 
     272         
     273    def clear(self): 
     274        for c in self.curves: 
     275            self.canvas.removeItem(c) 
     276        del self.curves[:] 
  • orange/OrangeWidgets/Visualize/OWScatterPlotGraph.py

    r8273 r8274  
    5858    def updateData(self, xAttr, yAttr, colorAttr, shapeAttr = "", sizeShapeAttr = "", labelAttr = None, **args): 
    5959#        if not self.haveData: 
    60 #            self.clear() 
     60        self.clear() 
    6161#            self.oldLegendKeys = {} 
    6262#            return 
Note: See TracChangeset for help on using the changeset viewer.