diff --git a/pyqtgraph/graphicsItems/LegendItem.py b/pyqtgraph/graphicsItems/LegendItem.py index e40c43d9..6509f979 100644 --- a/pyqtgraph/graphicsItems/LegendItem.py +++ b/pyqtgraph/graphicsItems/LegendItem.py @@ -29,7 +29,7 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor): def __init__(self, size=None, offset=None, horSpacing=25, verSpacing=0, pen=None, brush=None, labelTextColor=None, frame=True, - rowCount=1, colCount=1, **kwargs): + labelTextSize='9pt', rowCount=1, colCount=1, **kwargs): """ ============== =============================================================== **Arguments:** @@ -50,6 +50,8 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor): accepted by :func:`mkBrush ` is allowed. labelTextColor Pen to use when drawing legend text. Any single argument accepted by :func:`mkPen ` is allowed. + labelTextSize Size to use when drawing legend text. Accepts CSS style + string arguments, e.g. '9pt'. ============== =============================================================== """ @@ -75,6 +77,7 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor): 'pen': fn.mkPen(pen), 'brush': fn.mkBrush(brush), 'labelTextColor': labelTextColor, + 'labelTextSize': labelTextSize, 'offset': offset, } @@ -139,6 +142,21 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor): self.update() + def labelTextSize(self): + """Get the `labelTextSize` used for the item labels.""" + return self.opts['labelTextSize'] + + def setLabelTextSize(self, size): + """Set the `size` of the item labels. + + Accepts the CSS style string arguments, e.g. '8pt'. + """ + self.opts['labelTextSize'] = size + for _, label in self.items: + label.setAttr('size', self.opts['labelTextSize']) + + self.update() + def setParentItem(self, p): """Set the parent.""" ret = GraphicsWidget.setParentItem(self, p) @@ -164,7 +182,7 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor): ============== ======================================================== """ label = LabelItem(name, color=self.opts['labelTextColor'], - justify='left') + justify='left', size=self.opts['labelTextSize']) if isinstance(item, ItemSample): sample = item else: diff --git a/pyqtgraph/graphicsItems/tests/test_LegendItem.py b/pyqtgraph/graphicsItems/tests/test_LegendItem.py new file mode 100644 index 00000000..f9647024 --- /dev/null +++ b/pyqtgraph/graphicsItems/tests/test_LegendItem.py @@ -0,0 +1,101 @@ +import pytest +import pyqtgraph as pg + + +def test_legend_item_basics(): + app = pg.mkQApp() + + legend = pg.LegendItem() + + assert legend.opts['pen'] == pg.mkPen(None) + assert legend.opts['brush'] == pg.mkBrush(None) + assert legend.opts['labelTextColor'] is None + assert legend.opts['labelTextSize'] == '9pt' + assert legend.opts['offset'] is None + + assert legend.columnCount == 1 + assert legend.rowCount == 1 + assert legend.curRow == 0 + + assert legend.labelTextColor() is None + assert legend.labelTextSize() == '9pt' + assert legend.brush() == pg.mkBrush(None) + assert legend.pen() == pg.mkPen(None) + + # Set brush + # ---------------------------------------------------- + + brush = pg.mkBrush('b') + legend.setBrush(brush) + assert legend.brush() == brush + assert legend.opts['brush'] == brush + + # Set pen + # ---------------------------------------------------- + + pen = pg.mkPen('b') + legend.setPen(pen) + assert legend.pen() == pen + assert legend.opts['pen'] == pen + + # Set labelTextColor + # ---------------------------------------------------- + + text_color = pg.mkColor('b') + legend.setLabelTextColor(text_color) + assert legend.labelTextColor() == text_color + assert legend.opts['labelTextColor'] == text_color + + # Set labelTextSize + # ---------------------------------------------------- + + text_size = '12pt' + legend.setLabelTextSize(text_size) + assert legend.labelTextSize() == text_size + assert legend.opts['labelTextSize'] == text_size + + # Add items + # ---------------------------------------------------- + + assert len(legend.items) == 0 + plot = pg.PlotDataItem(name="Plot") + legend.addItem(plot, name="Plot") + assert len(legend.items) == 1 + + scatter = pg.PlotDataItem(name="Scatter") + legend.addItem(scatter, name="Scatter") + assert len(legend.items) == 2 + assert legend.columnCount == 1 + assert legend.rowCount == 1 + + curve = pg.PlotDataItem(name="Curve") + legend.addItem(curve, name="Curve") + assert len(legend.items) == 3 + + scrabble = pg.PlotDataItem(name="Scrabble") + legend.addItem(scrabble, name="Scrabble") + assert len(legend.items) == 4 + + assert legend.layout.rowCount() == 4 + legend.setColumnCount(2) + assert legend.rowCount == 2 + + assert legend.layout.rowCount() == 3 + + # Remove items + # ---------------------------------------------------- + + legend.removeItem(scrabble) + assert legend.rowCount == 2 + assert legend.layout.rowCount() == 2 + assert scrabble not in legend.items + assert len(legend.items) == 3 + + legend.removeItem(curve) + assert legend.rowCount == 2 + assert legend.layout.rowCount() == 1 + assert curve not in legend.items + assert len(legend.items) == 2 + + legend.clear() + assert legend.items == []