Skip to content

BarGraphItem: implement boundingRect with pen width#2561

Closed
pijyoi wants to merge 1 commit intopyqtgraph:masterfrom
pijyoi:bargraph-databounds
Closed

BarGraphItem: implement boundingRect with pen width#2561
pijyoi wants to merge 1 commit intopyqtgraph:masterfrom
pijyoi:bargraph-databounds

Conversation

@pijyoi
Copy link
Copy Markdown
Contributor

@pijyoi pijyoi commented Dec 25, 2022

Fixes #2550
Related to #2554, #2560

After playing around with thicker pen widths, it turns out that QPicture.boundingRect() assumes that pens are non-cosmetic; when in fact pyqtgraph usually uses cosmetic pens. This means that QPicture.boundingRect() adds the pen widths to the bounding rectangle in data sizes rather than in device pixel sizes.

A sample script below demonstrates the issue. When a thick pen is used, it results in a lot of empty space.

Not being familiar with the very code that this PR changes, this PR is created as a draft.

import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui, QtWidgets

class ObjectBounds(QtWidgets.QGraphicsItem):
    def paint(self, painter, *args):
        pen = QtGui.QPen(QtCore.Qt.GlobalColor.red, 0, QtCore.Qt.PenStyle.DashLine)
        rect = self.boundingRect()
        painter.setPen(pen)
        painter.drawRect(rect)
        print(rect)

    def boundingRect(self):
        return self.parentItem().boundingRect()

pg.mkQApp()
pw = pg.PlotWidget()
pw.show()

rng = np.random.default_rng()
vals = np.hstack([rng.normal(size=500), rng.normal(size=260, loc=4)])
y,x = np.histogram(vals, bins=np.linspace(-3, 8, 40))

pen = pg.mkPen('w', width=6)
bgi = pg.BarGraphItem(x0=x[:-1], x1=x[1:], height=y, pen=pen, brush=(0,0,255,150))
pw.addItem(bgi)
rect = ObjectBounds(bgi)

pg.exec()

master: PyQt6.QtCore.QRectF(-6.0, -3.0, 17.0, 69.0)
Screenshot 2022-12-25 171523

this PR: PyQt6.QtCore.QRectF(-3.082433908183643, -0.6809981495366643, 11.164867816367286, 67.36199629907333)
Screenshot 2022-12-25 171451

@pijyoi pijyoi force-pushed the bargraph-databounds branch from 0072a10 to fc6a705 Compare December 26, 2022 09:46
@pijyoi pijyoi changed the title implement BarGraphItem dataBounds and pixelPadding BarGraphItem: implement boundingRect with pen width Dec 26, 2022
@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Dec 29, 2022

closing in favor of #2565

@pijyoi pijyoi closed this Dec 29, 2022
@pijyoi pijyoi deleted the bargraph-databounds branch January 24, 2023 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BarGraphItem is not showing if bar coordinates are huge

1 participant