Skip to content

ChartView

Qt Base Class: QChartView

Signature: QChartView(self, chart: PySide6.QtCharts.QChart, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None QChartView(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None

Base classes

Name Children Inherits
GraphicsViewMixin
prettyqt.widgets.graphicsview
QChartView
PySide6.QtCharts
QChartView(self, chart: PySide6.QtCharts.QChart, parent: Optional[PySide6.QtWidgets.QWidget] \= None) -> None

⋔ Inheritance diagram

graph TD
  1473367657600["charts.ChartView"]
  1473296174880["widgets.GraphicsViewMixin"]
  1473293679456["widgets.AbstractScrollAreaMixin"]
  1473293662864["widgets.FrameMixin"]
  1473293688240["widgets.WidgetMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473245548480["gui.PaintDeviceMixin"]
  1473367559024["QtCharts.QChartView"]
  1473241433520["QtWidgets.QGraphicsView"]
  1473290616416["QtWidgets.QAbstractScrollArea"]
  1473290626176["QtWidgets.QFrame"]
  1473290849680["QtWidgets.QWidget"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473300082368["QtGui.QPaintDevice"]
  1473296174880 --> 1473367657600
  1473293679456 --> 1473296174880
  1473293662864 --> 1473293679456
  1473293688240 --> 1473293662864
  1473299815024 --> 1473293688240
  140713234304496 --> 1473299815024
  1473245548480 --> 1473293688240
  140713234304496 --> 1473245548480
  1473367559024 --> 1473367657600
  1473241433520 --> 1473367559024
  1473290616416 --> 1473241433520
  1473290626176 --> 1473290616416
  1473290849680 --> 1473290626176
  1473288842240 --> 1473290849680
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473300082368 --> 1473290849680
  1473291690208 --> 1473300082368

🛈 DocStrings

Bases: GraphicsViewMixin, QChartView

Source code in prettyqt\charts\chartview.py
class ChartView(widgets.GraphicsViewMixin, charts.QChartView):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if not args or not isinstance(args[0], charts.QChart):
            self.setChart(charts.Chart())
        self.setRenderHint(gui.Painter.RenderHint.Antialiasing)
        self.set_rubber_band("rectangle")
        # self.setDragMode(self.RubberBandDrag)

    def keyPressEvent(self, event: gui.QKeyEvent):
        """Handle keypress events to allow navigation via keyboard."""
        match event.key():
            case constants.Key.Key_Escape:
                self.chart().zoomReset()
            case constants.Key.Key_Plus:
                self.chart().zoom_by_factor(ZOOM_IN_FACTOR)
            case constants.Key.Key_Minus:
                self.chart().zoom_by_factor(ZOOM_OUT_FACTOR)
            case constants.Key.Key_Left:
                self.chart().scroll(-SCROLL_STEP_SIZE, 0)
            case constants.Key.Key_Right:
                self.chart().scroll(SCROLL_STEP_SIZE, 0)
            case constants.Key.Key_Up:
                self.chart().scroll(0, SCROLL_STEP_SIZE)
            case constants.Key.Key_Down:
                self.chart().scroll(0, -SCROLL_STEP_SIZE)
            case constants.Key.Key_0:
                self.chart().apply_nice_numbers()
            case _:
                super().keyPressEvent(event)
                return
        event.accept()

    def wheelEvent(self, event: gui.QWheelEvent):
        """Handle wheel event for zooming."""
        fct = ZOOM_IN_FACTOR if event.angleDelta().y() > 0 else ZOOM_OUT_FACTOR
        self.chart().zoom_by_factor(fct)
        event.accept()

    def mouseReleaseEvent(self, event: gui.QMouseEvent):
        """Override to allow dragging the chart."""
        if event.button() == constants.MouseButton.RightButton:
            widgets.Application.restoreOverrideCursor()
            event.accept()
            return
        super().mouseReleaseEvent(event)

    def mousePressEvent(self, event: gui.QMouseEvent):
        """Override to allow dragging the chart."""
        if event.button() == constants.MouseButton.RightButton:
            widgets.Application.set_override_cursor("size_all")
            self.last_mouse_pos = event.position()
            event.accept()

        super().mousePressEvent(event)

    def mouseMoveEvent(self, event: gui.QMouseEvent):
        """Override to allow dragging the chart."""
        # pan the chart with a middle mouse drag
        if event.buttons() & constants.MouseButton.RightButton:  # type: ignore
            if not self.last_mouse_pos:
                return
            pos_diff = event.position() - self.last_mouse_pos
            self.chart().scroll(-pos_diff.x(), pos_diff.y())

            self.last_mouse_pos = event.position()
            event.accept()

        super().mouseMoveEvent(event)

    @core.Slot()
    def save_as_image(self):
        """Let user choose folder and save chart as an image file."""
        dlg = widgets.FileDialog(mode="save", caption="Save image")
        filters = {"Bmp files": [".bmp"], "Jpeg files": [".jpg"], "Png files": [".png"]}
        dlg.set_extension_filter(filters)
        filename = dlg.open_file()
        if not filename:
            return
        self.chart().show_legend()
        image = self.get_image()
        image.save(str(filename[0]))
        self.chart().hide_legend()

    def set_rubber_band(self, typ: RubberBandStr | charts.QChartView.RubberBand):
        """Set the rubber band type.

        Args:
            typ: rubber band type
        """
        self.setRubberBand(RUBBER_BAND.get_enum_value(typ))

    def get_rubber_band(self) -> RubberBandStr:
        """Return current rubber band type.

        Returns:
            Rubber band type
        """
        return RUBBER_BAND.inverse[self.rubberBand()]

get_rubber_band() -> RubberBandStr

Return current rubber band type.

Source code in prettyqt\charts\chartview.py
def get_rubber_band(self) -> RubberBandStr:
    """Return current rubber band type.

    Returns:
        Rubber band type
    """
    return RUBBER_BAND.inverse[self.rubberBand()]

keyPressEvent(event: gui.QKeyEvent)

Handle keypress events to allow navigation via keyboard.

Source code in prettyqt\charts\chartview.py
def keyPressEvent(self, event: gui.QKeyEvent):
    """Handle keypress events to allow navigation via keyboard."""
    match event.key():
        case constants.Key.Key_Escape:
            self.chart().zoomReset()
        case constants.Key.Key_Plus:
            self.chart().zoom_by_factor(ZOOM_IN_FACTOR)
        case constants.Key.Key_Minus:
            self.chart().zoom_by_factor(ZOOM_OUT_FACTOR)
        case constants.Key.Key_Left:
            self.chart().scroll(-SCROLL_STEP_SIZE, 0)
        case constants.Key.Key_Right:
            self.chart().scroll(SCROLL_STEP_SIZE, 0)
        case constants.Key.Key_Up:
            self.chart().scroll(0, SCROLL_STEP_SIZE)
        case constants.Key.Key_Down:
            self.chart().scroll(0, -SCROLL_STEP_SIZE)
        case constants.Key.Key_0:
            self.chart().apply_nice_numbers()
        case _:
            super().keyPressEvent(event)
            return
    event.accept()

mouseMoveEvent(event: gui.QMouseEvent)

Override to allow dragging the chart.

Source code in prettyqt\charts\chartview.py
def mouseMoveEvent(self, event: gui.QMouseEvent):
    """Override to allow dragging the chart."""
    # pan the chart with a middle mouse drag
    if event.buttons() & constants.MouseButton.RightButton:  # type: ignore
        if not self.last_mouse_pos:
            return
        pos_diff = event.position() - self.last_mouse_pos
        self.chart().scroll(-pos_diff.x(), pos_diff.y())

        self.last_mouse_pos = event.position()
        event.accept()

    super().mouseMoveEvent(event)

mousePressEvent(event: gui.QMouseEvent)

Override to allow dragging the chart.

Source code in prettyqt\charts\chartview.py
def mousePressEvent(self, event: gui.QMouseEvent):
    """Override to allow dragging the chart."""
    if event.button() == constants.MouseButton.RightButton:
        widgets.Application.set_override_cursor("size_all")
        self.last_mouse_pos = event.position()
        event.accept()

    super().mousePressEvent(event)

mouseReleaseEvent(event: gui.QMouseEvent)

Override to allow dragging the chart.

Source code in prettyqt\charts\chartview.py
def mouseReleaseEvent(self, event: gui.QMouseEvent):
    """Override to allow dragging the chart."""
    if event.button() == constants.MouseButton.RightButton:
        widgets.Application.restoreOverrideCursor()
        event.accept()
        return
    super().mouseReleaseEvent(event)

save_as_image()

Let user choose folder and save chart as an image file.

Source code in prettyqt\charts\chartview.py
@core.Slot()
def save_as_image(self):
    """Let user choose folder and save chart as an image file."""
    dlg = widgets.FileDialog(mode="save", caption="Save image")
    filters = {"Bmp files": [".bmp"], "Jpeg files": [".jpg"], "Png files": [".png"]}
    dlg.set_extension_filter(filters)
    filename = dlg.open_file()
    if not filename:
        return
    self.chart().show_legend()
    image = self.get_image()
    image.save(str(filename[0]))
    self.chart().hide_legend()

set_rubber_band(typ: RubberBandStr | charts.QChartView.RubberBand)

Set the rubber band type.

Parameters:

Name Type Description Default
typ RubberBandStr | RubberBand

rubber band type

required
Source code in prettyqt\charts\chartview.py
def set_rubber_band(self, typ: RubberBandStr | charts.QChartView.RubberBand):
    """Set the rubber band type.

    Args:
        typ: rubber band type
    """
    self.setRubberBand(RUBBER_BAND.get_enum_value(typ))

wheelEvent(event: gui.QWheelEvent)

Handle wheel event for zooming.

Source code in prettyqt\charts\chartview.py
def wheelEvent(self, event: gui.QWheelEvent):
    """Handle wheel event for zooming."""
    fct = ZOOM_IN_FACTOR if event.angleDelta().y() > 0 else ZOOM_OUT_FACTOR
    self.chart().zoom_by_factor(fct)
    event.accept()

⌗ Property table

Qt Property Type Doc
objectName QString
modal bool
windowModality Qt::WindowModality
enabled bool
geometry QRect
frameGeometry QRect
normalGeometry QRect
x int
y int
pos QPoint
frameSize QSize
size QSize
width int
height int
rect QRect
childrenRect QRect
childrenRegion QRegion
sizePolicy QSizePolicy
minimumSize QSize
maximumSize QSize
minimumWidth int
minimumHeight int
maximumWidth int
maximumHeight int
sizeIncrement QSize
baseSize QSize
palette QPalette
font QFont
cursor QCursor
mouseTracking bool
tabletTracking bool
isActiveWindow bool
focusPolicy Qt::FocusPolicy
focus bool
contextMenuPolicy Qt::ContextMenuPolicy
updatesEnabled bool
visible bool
minimized bool
maximized bool
fullScreen bool
sizeHint QSize
minimumSizeHint QSize
acceptDrops bool
windowTitle QString
windowIcon QIcon
windowIconText QString
windowOpacity double
windowModified bool
toolTip QString
toolTipDuration int
statusTip QString
whatsThis QString
accessibleName QString
accessibleDescription QString
layoutDirection Qt::LayoutDirection
autoFillBackground bool
styleSheet QString
locale QLocale
windowFilePath QString
inputMethodHints QFlags
frameShape QFrame::Shape
frameShadow QFrame::Shadow
lineWidth int
midLineWidth int
frameWidth int
frameRect QRect
verticalScrollBarPolicy Qt::ScrollBarPolicy
horizontalScrollBarPolicy Qt::ScrollBarPolicy
sizeAdjustPolicy QAbstractScrollArea::SizeAdjustPolicy
backgroundBrush QBrush
foregroundBrush QBrush
interactive bool
sceneRect QRectF
alignment QFlags
renderHints QFlags
dragMode QGraphicsView::DragMode
cacheMode QFlags
transformationAnchor QGraphicsView::ViewportAnchor
resizeAnchor QGraphicsView::ViewportAnchor
viewportUpdateMode QGraphicsView::ViewportUpdateMode
rubberBandSelectionMode Qt::ItemSelectionMode
optimizationFlags QFlags