diff --git a/examples/Legend.py b/examples/Legend.py index 3759c2e9..9239f1ae 100644 --- a/examples/Legend.py +++ b/examples/Legend.py @@ -13,7 +13,7 @@ win = pg.plot() win.setWindowTitle('pyqtgraph example: BarGraphItem') # # option1: only for .plot(), following c1,c2 for example----------------------- -# win.addLegend() +# win.addLegend(frame=False, rowCount=1, colCount=2) # bar graph x = np.arange(10) diff --git a/pyqtgraph/graphicsItems/LegendItem.py b/pyqtgraph/graphicsItems/LegendItem.py index 6afaed4d..67604c45 100644 --- a/pyqtgraph/graphicsItems/LegendItem.py +++ b/pyqtgraph/graphicsItems/LegendItem.py @@ -26,7 +26,7 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor): """ def __init__(self, size=None, offset=None, horSpacing=25, verSpacing=0, pen=None, - brush=None, labelTextColor=None, **kwargs): + brush=None, labelTextColor=None, frame=True, rowCount=1, colCount=1, **kwargs): """ ============== =============================================================== **Arguments:** @@ -60,6 +60,11 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor): self.setLayout(self.layout) self.items = [] self.size = size + self.offset = offset + self.frame = frame + self.columnCount = colCount + self.rowCount = rowCount + self.curRow = 0 if size is not None: self.setGeometry(QtCore.QRectF(0, 0, self.size[0], self.size[1])) @@ -159,14 +164,52 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor): if isinstance(item, ItemSample): sample = item else: - sample = ItemSample(item) - - row = self.layout.rowCount() + sample = ItemSample(item) self.items.append((sample, label)) - self.layout.addItem(sample, row, 0) - self.layout.addItem(label, row, 1) + self._addItemToLayout(sample, label) self.updateSize() + def _addItemToLayout(self, sample, label): + col = self.layout.columnCount() + row = self.layout.rowCount() + if row: + row -= 1 + nCol = self.columnCount*2 + #FIRST ROW FULL + if col == nCol: + for col in range(0,nCol,2): + #FIND RIGHT COLUMN + if not self.layout.itemAt(row, col): + break + if col+2 == nCol: + #MAKE NEW ROW + col = 0 + row += 1 + self.layout.addItem(sample, row, col) + self.layout.addItem(label, row, col+1) + + def setColumnCount(self, columnCount): + ''' + change the orientation of all items of the legend + ''' + if columnCount != self.columnCount: + self.columnCount = columnCount + self.rowCount = int(len(self.items)/columnCount) + for i in range(self.layout.count()-1,-1,-1): + self.layout.removeAt(i) #clear layout + for sample, label in self.items: + self._addItemToLayout(sample, label) + self.updateSize() + + def getLabel(self, plotItem): + """ + return the labelItem inside the legend for a given plotItem + the label-text can be changed via labenItem.setText + """ + out = [(it, lab) for it, lab in self.items if it.item==plotItem] + try: return out[0][1] + except IndexError: return None + def removeItem(self, item): """ Removes one item from the legend. @@ -198,16 +241,29 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor): def updateSize(self): if self.size is not None: return - - self.setGeometry(0, 0, 0, 0) + height = 0 + width = 0 + for row in range(self.layout.rowCount()): + row_height = 0 + col_witdh = 0 + for col in range(self.layout.columnCount()): + item = self.layout.itemAt(row, col) + if item: + col_witdh += item.width() + 3 + row_height = max(row_height, item.height()) + width = max(width, col_witdh) + height += row_height + self.setGeometry(0, 0, width, height) + return def boundingRect(self): return QtCore.QRectF(0, 0, self.width(), self.height()) def paint(self, p, *args): - p.setPen(self.opts['pen']) - p.setBrush(self.opts['brush']) - p.drawRect(self.boundingRect()) + if self.frame: + p.setPen(self.opts['pen']) + p.setBrush(self.opts['brush']) + p.drawRect(self.boundingRect()) def hoverEvent(self, ev): ev.acceptDrags(QtCore.Qt.LeftButton) diff --git a/pyqtgraph/graphicsItems/PlotItem/PlotItem.py b/pyqtgraph/graphicsItems/PlotItem/PlotItem.py index 79d59235..38a9ba5c 100644 --- a/pyqtgraph/graphicsItems/PlotItem/PlotItem.py +++ b/pyqtgraph/graphicsItems/PlotItem/PlotItem.py @@ -660,6 +660,7 @@ class PlotItem(GraphicsWidget): Accepts the same arguments as :meth:`~pyqtgraph.LegendItem`. """ + if self.legend is None: self.legend = LegendItem(offset=offset, **kwargs) self.legend.setParentItem(self.vb)