ScatterChart2D Example

import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../shared")

from DevMachines import __pyside2__, __pyside6__
from DevMachines.QtitanBase import Qtitan
from DevMachines.QtitanChart import (Chart, ChartPointSeries, ChartBubbleSeries2D)

if __pyside2__:
    from PySide2 import QtCore
    from PySide2.QtCore import Qt
    from PySide2.QtGui import QBrush, QIcon, QPixmap, QColor, QFont, QCursor
    from PySide2.QtWidgets import (QApplication, QComboBox, QDoubleSpinBox, QSlider, QSpinBox, QLabel)

if __pyside6__:
    from PySide6 import QtCore
    from PySide6.QtCore import Qt
    from PySide6.QtGui import QBrush, QIcon, QPixmap, QColor, QFont, QCursor
    from PySide6.QtWidgets import (QApplication, QComboBox, QDoubleSpinBox, QSlider, QSpinBox, QLabel)

from DemoChartWindow import DemoChartWindow

class MainWindow(DemoChartWindow):
    PointsSeries = 0
    BubbleSeries = 1
    Bubble3DSeries = 2
    def __init__(self):
        DemoChartWindow.__init__(self, "Scatter Charts")
        self.setChart(None)
        self.createSeriesParametrs()
        self.createMarkersGroup()
        self.createLabelsGroup()
        self.seriesChanged(self.seriesSwitcher.currentIndex())

    def createSeriesParametrs(self):
        seriesTypeGroup = self.createGroupParameters(self.tr("Series"))
        localLayout = seriesTypeGroup.layout()

        self.seriesSwitcher = QComboBox(seriesTypeGroup)
        self.seriesSwitcher.addItem("Points", MainWindow.PointsSeries)
        self.seriesSwitcher.addItem("Bubble", MainWindow.BubbleSeries)
    #    self.seriesSwitcher.addItem("Bubble 3D Effect", MainWindow.Bubble3DSeries)
        self.connect(self.seriesSwitcher, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("seriesChanged(int)"))

        self.transparencyLabel = QLabel(self.tr("Transparency:"))
        self.transparency = QSlider(Qt.Horizontal, seriesTypeGroup)
        self.transparency.setRange(0, 255)
        self.transparency.setSliderPosition(255-175)
        self.transparency.setSingleStep(5)
        self.connect(self.transparency, QtCore.SIGNAL("valueChanged(int)"), self, QtCore.SLOT("transparencyChanged(int)"))

        self.minLabelBox = QLabel(self.tr("MinSize"), seriesTypeGroup)
        self.minBox = QDoubleSpinBox(seriesTypeGroup)
        self.minBox.setValue(0.7)
        self.minBox.setSingleStep(0.1)
        self.minBox.setRange(0.1, 1.5)
        self.connect(self.minBox, QtCore.SIGNAL("valueChanged(double)"), self, QtCore.SLOT("valueMinChanged(double)"))

        self.maxLabelBox = QLabel(self.tr("MaxSize"), seriesTypeGroup)
        self.maxBox = QDoubleSpinBox(seriesTypeGroup)
        self.maxBox.setValue(1.9)
        self.maxBox.setSingleStep(0.1)
        self.maxBox.setRange(0.1, 3.5)
        self.connect(self.maxBox, QtCore.SIGNAL("valueChanged(double)"), self, QtCore.SLOT("valueMaxChanged(double)"))

        localLayout.addRow(self.seriesSwitcher)
        localLayout.addRow(self.transparencyLabel, self.transparency)
        localLayout.addRow(self.minLabelBox, self.minBox)
        localLayout.addRow(self.maxLabelBox, self.maxBox)

    def createPointsSeries(self):
        self.createTitle(self.tr("Points (Scatter)"))
        self.chart.legend().setVisible(True)

        series1 = ChartPointSeries()
        series1.setName(self.tr("Points 1"))
        for i in range(0, 20):
            series1.addXY(QtCore.QRandomGenerator.global_().generate() % 40, 50 + (QtCore.QRandomGenerator.global_().generate() % 50))
        self.chart.appendSeries(series1)

        series2 = ChartPointSeries()
        series2.setName(self.tr("Points 2"))
        for i in range(0, 20):
            series2.addXY(40 + (QtCore.QRandomGenerator.global_().generate() % 40), QtCore.QRandomGenerator.global_().generate() % 50)
        self.chart.appendSeries(series2)

    def createBubbleSeries(self):
        self.createTitle(self.tr("Site visitors from US:"))
        self.chart.legend().setVisible(True)

        series = ChartBubbleSeries2D()
        p = series.addXYY1(2, 3, 7700)
        p.setLegendText(self.tr("California"))
        p = series.addXYY1(3, 4, 6500)
        p.setLegendText(self.tr("Virginia"))
        p = series.addXYY1(4, 5, 4200)
        p.setLegendText(self.tr("York"))
        p = series.addXYY1(5, 6, 3500)
        p.setLegendText(self.tr("Pennsylvania"))
        p = series.addXYY1(6, 5, 2400)
        p.setLegendText(self.tr("Georgia"))
        p = series.addXYY1(7, 4, 1550)
        p.setLegendText(self.tr("Colorado"))
        p = series.addXYY1(7.5, 8, 1800)
        p.setLegendText(self.tr("Other States"))

        series.setMinSize(0.7)
        series.setMaxSize(1.9)
        series.setTransparency(135)

        self.chart.appendSeries(series)

        view = series.view()
        view.axisY().setAutoRange(False)
        view.axisY().setMaxValue(10)
        view.axisY().setMinValue(0)

    def createBubble3DSeries(self):
        pass

    def updateValueParameters(self):
        DemoChartWindow.updateValueParameters(self)
        self.transparencyChanged(self.transparency.value())
        self.valueMinChanged(self.minBox.value())
        self.valueMaxChanged(self.maxBox.value())

    def seriesChanged(self, index):
        self.chart.clearSeries()
        self.chart.clearTitles()
        self.chart.setBackgroundBrush(QBrush())

        v = self.seriesSwitcher.itemData(index)
        if int(v) == MainWindow.PointsSeries:
            self.dataLabelsGroup.show()
            self.markerShapeSizeTitleSwitcher.show()
            self.markerShapeSizeSwitcher.show()
            self.transparencyLabel.hide()
            self.transparency.hide()

            self.minLabelBox.hide()
            self.minBox.hide()
            self.maxLabelBox.hide()
            self.maxBox.hide()
            self.createPointsSeries()

        elif int(v) == MainWindow.BubbleSeries:
            self.markerShapeSizeTitleSwitcher.hide()
            self.markerShapeSizeSwitcher.hide()
            self.dataLabelsGroup.hide()
            self.transparencyLabel.show()
            self.transparency.show()

            self.minLabelBox.show()
            self.minBox.show()
            self.maxLabelBox.show()
            self.maxBox.show()
            self.createBubbleSeries()
        elif int(v) == MainWindow.Bubble3DSeries:
            self.createBubble3DSeries()
        self.updateValueParameters()

    def transparencyChanged(self, value):
        listSeries = self.chart.series()
        for series in listSeries:
            if series.inherits("Qtitan::ChartBubbleSeries2D"):
                series.setTransparency(255 - value)

    def valueMinChanged(self, value):
        listSeries = self.chart.series()
        for series in listSeries:
            if series.inherits("Qtitan::ChartBubbleSeries2D"):
                series.setMinSize(value)

    def valueMaxChanged(self, value):
        listSeries = self.chart.series()
        for series in listSeries:
            if series.inherits("Qtitan::ChartBubbleSeries2D"):
                series.setMaxSize(value)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())