BarChart2D 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 (ChartBarSeries2D, ChartStackedBarSeries2D, ChartBarSeriesLabel, ChartLegend)

if __pyside2__:
    from PySide2 import QtCore
    from PySide2.QtCore import Qt
    from PySide2.QtGui import QBrush
    from PySide2.QtWidgets import QApplication, QComboBox, QSlider, QLabel, QCheckBox

if __pyside6__:
    from PySide6 import QtCore
    from PySide6.QtCore import Qt
    from PySide6.QtGui import QBrush
    from PySide6.QtWidgets import QApplication, QComboBox, QSlider, QLabel, QCheckBox

from DemoChartWindow import DemoChartWindow

class MainWindow(DemoChartWindow):
    ClusteredSeries = 0
    StackedSeries = 1
    Stacked100Series = 2
    SideBySideSeries = 3
    def __init__(self):
        DemoChartWindow.__init__(self, "Bar Chart")
        self.setChart(None)
        self.displayShadow = True
        self.createSeriesParametrs()
        self.seriesChanged(self.seriesSwitcher.currentIndex())

    def createSeriesParametrs(self):
        # Option Series
        seriesTypeGroup = self.createGroupParameters(self.tr("Series"))

        self.seriesSwitcher = QComboBox(seriesTypeGroup)
        self.seriesSwitcher.addItem("Clustered", MainWindow.ClusteredSeries)
        self.seriesSwitcher.addItem("Stacked", MainWindow.StackedSeries)
        self.seriesSwitcher.addItem("100% Stacked", MainWindow.Stacked100Series)
        self.seriesSwitcher.addItem("Side by Side Stacked", MainWindow.SideBySideSeries)
        self.connect(self.seriesSwitcher, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("seriesChanged(int)"))

        layout = seriesTypeGroup.layout()
        layout.addRow(self.seriesSwitcher)

        self.displayShadowBox = QCheckBox(self.tr("Display shadow"), self)
        self.connect(self.displayShadowBox, QtCore.SIGNAL("toggled(bool)"), self, QtCore.SLOT("displayShadowChanged(bool)"))
        layout.addRow(self.displayShadowBox)

        # Option Labels
        self.dataLabelsGroup = self.createGroupParameters(self.tr("Show Data Labels"), True)
        layout = self.dataLabelsGroup.layout()

        self.connect(self.dataLabelsGroup, QtCore.SIGNAL("toggled(bool)"), self, QtCore.SLOT("showDataLabels(bool)"))
        self.posDataLabelsSwitcher = QComboBox(self.dataLabelsGroup)
        self.posDataLabelsSwitcher.addItem(self.tr("Top"), ChartBarSeriesLabel.ChartBarLabelTop)
        self.posDataLabelsSwitcher.addItem(self.tr("Center"), ChartBarSeriesLabel.ChartBarLabelCenter)
        self.posDataLabelsSwitcher.setCurrentIndex(1)
        self.connect(self.posDataLabelsSwitcher, QtCore.SIGNAL("currentIndexChanged(int)"),
        self, QtCore.SLOT("labelsPositionChanged(int)"))
        layout.addRow(self.posDataLabelsSwitcher)

    def createSeries(self, _type):
        series = None
        if _type == MainWindow.StackedSeries or _type == MainWindow.Stacked100Series:
            series = ChartStackedBarSeries2D()
        else:
            series = ChartBarSeries2D()

        if _type == MainWindow.Stacked100Series:
            series.setStackHeight(100.0)

        return series

    def createBarSeries(self, _type):
        self.createTitle(self.tr("Results"))

        self.chart.legend().setVisible(True)
        self.chart.legend().setVerticalAlignment(ChartLegend.LegendCenter)

        series1 = self.createSeries(_type)
        self.chart.appendSeries(series1)

        series1.setName(self.tr("Atom 330"))
        series1.addAxisPointY(490, "Double-Precision 2")
        series1.addAxisPointY(50,  "Double-Precision 3")
        series1.addAxisPointY(100, "Double-Precision 4")
        series1.addAxisPointY(400, "Double-Precision 5")
        series1.addAxisPointY(60,  "Double-Precision 6")
        series1.addAxisPointY(530, "Double-Precision 7")
        series1.addAxisPointY(230, "Double-Precision 8")
        series1.addAxisPointY(510, "Double-Precision 9")
        series1.addAxisPointY(505, "Double-Precision 10")
        series1.addAxisPointY(490, "Double-Precision 11")

        listViews = self.chart.views()
        view2D = listViews[0]
        axisX = view2D.axisX()
        axisX.label().setAngle(45)

        series2 = self.createSeries(_type)
        self.chart.appendSeries(series2)

        series2.setName(self.tr("Athlon 64 x2"))
        series2.addAxisPointY(1000, "Double-Precision 2" )
        series2.addAxisPointY(500,  "Double-Precision 3" )
        series2.addAxisPointY(100,  "Double-Precision 4" )
        series2.addAxisPointY(900,  "Double-Precision 5" )
        series2.addAxisPointY(100,  "Double-Precision 6" )
        series2.addAxisPointY(730,  "Double-Precision 7" )
        series2.addAxisPointY(30,   "Double-Precision 8" )
        series2.addAxisPointY(100,  "Double-Precision 9" )
        series2.addAxisPointY(1600, "Double-Precision 10")
        series2.addAxisPointY(890 , "Double-Precision 11")

        series3 = self.createSeries(_type)
        self.chart.appendSeries(series3)

        series3.setName(self.tr("2x Xeon 5500"))
        series3.addAxisPointY(1900, "Double-Precision 2" )
        series3.addAxisPointY(600,  "Double-Precision 3" )
        series3.addAxisPointY(500,  "Double-Precision 4" )
        series3.addAxisPointY(2500, "Double-Precision 5" )
        series3.addAxisPointY(1500, "Double-Precision 6" )
        series3.addAxisPointY(1500, "Double-Precision 7" )
        series3.addAxisPointY(3000, "Double-Precision 8" )
        series3.addAxisPointY(2400, "Double-Precision 9" )
        series3.addAxisPointY(1600, "Double-Precision 10")
        series3.addAxisPointY(890,  "Double-Precision 11")

    def createSideBySideSeries(self):
        self.chart.legend().setVisible(True)
        self.chart.legend().setVerticalAlignment(ChartLegend.LegendNear)

        for i in range(0, 4):
            series = ChartStackedBarSeries2D()
            self.chart.appendSeries(series)

            series.setName("Qtitan {0:d} Project 1".format(i + 1))
            series.setStackGroup(i)

            series.addAxisPointY(8 + QtCore.QRandomGenerator.global_().generate() % 20,  self.tr("USA"))
            series.addAxisPointY(12 + QtCore.QRandomGenerator.global_().generate() % 20, self.tr("Europe"))
            series.addAxisPointY(7 + QtCore.QRandomGenerator.global_().generate() % 20,  self.tr("Asia"))

            series = ChartStackedBarSeries2D()
            self.chart.appendSeries(series)
            series.setName("Qtitan {0:d} Project 2".format(i + 1))
            series.setStackGroup(i)

            series.addAxisPointY(10 + QtCore.QRandomGenerator.global_().generate() % 20, self.tr("USA"))
            series.addAxisPointY(8 + QtCore.QRandomGenerator.global_().generate() % 20,  self.tr("Europe"))
            series.addAxisPointY(12 + QtCore.QRandomGenerator.global_().generate() % 20, self.tr("Asia"))

    def updateValueParameters(self):
        DemoChartWindow.updateValueParameters(self)
        if self.displayShadow:
            self.displayShadowBox.setCheckState(Qt.Checked)
        else:
            self.displayShadowBox.setCheckState(Qt.Unchecked)
        self.labelsPositionChanged(self.posDataLabelsSwitcher.currentIndex())

    def displayShadowSeries(self):
        listSeries = self.chart.series()
        for series in listSeries:
            series.setShadow(self.displayShadow)

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

        v = self.seriesSwitcher.itemData(index)
        if v == MainWindow.ClusteredSeries or v == MainWindow.StackedSeries or v == MainWindow.Stacked100Series:
            self.createBarSeries(v)
        elif v == MainWindow.SideBySideSeries:
            self.createSideBySideSeries()

        self.displayShadowSeries()
        self.updateValueParameters()

    def labelsPositionChanged(self, index):
        v = self.posDataLabelsSwitcher.itemData(index)
        listSeries = self.chart.series()
        for series in listSeries:
            label = series.label()
            label.setPosition(ChartBarSeriesLabel.BarLabelPosition(int(v)))

    def typeSeriesChanged(self, state):
        state = state

    def displayShadowChanged(self, checked):
        if self.displayShadow != checked:
            self.displayShadow = checked
            self.displayShadowSeries()

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