AreaChart2D 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 (ChartStackedSplineAreaSeries2D, ChartSplineAreaSeries2D, ChartAreaSeries2D, ChartStackedAreaSeries2D)

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

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

from DemoChartWindow import DemoChartWindow

class MainWindow(DemoChartWindow):
    AreaLineSeries = 0
    StackedAreaLineSeries = 1
    Stacked100AreaLineSeries = 2
    AreaSplineSeries = 3
    StackedAreaSplineSeries = 4
    Stacked100AreaSplineSeries = 5

    def __init__(self):
        DemoChartWindow.__init__(self, "Area Chart")
        self.setChart(None)
        self.createSeriesParametrs()
        self.createMarkersGroup()
        self.createLabelsGroup()
        self.seriesChanged(self.seriesSwitcher.currentIndex())

    def createChartSeries(self, _format, _type):
        series = None
        if _type == MainWindow.Stacked100AreaSplineSeries:
            series = ChartStackedSplineAreaSeries2D()
        elif _type == MainWindow.StackedAreaSplineSeries:
            series = ChartStackedSplineAreaSeries2D()
        elif _type == MainWindow.AreaSplineSeries:
            series = ChartSplineAreaSeries2D()
        elif _type == MainWindow.Stacked100AreaLineSeries:
            series = ChartStackedAreaSeries2D()
        elif _type == MainWindow.StackedAreaLineSeries:
            series = ChartStackedAreaSeries2D()
        elif _type == MainWindow.AreaLineSeries:
            series = ChartAreaSeries2D()
        else:
            series = ChartAreaSeries2D()

        if series != None:
            series.label().setFormat(_format)

        series.setThickness(3)

        return series

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

        # Option Series
        self.seriesSwitcher = QComboBox(seriesTypeGroup)
        self.seriesSwitcher.addItem("Area", MainWindow.AreaLineSeries)
        self.seriesSwitcher.addItem("Stacked Area", MainWindow.StackedAreaLineSeries)
        self.seriesSwitcher.addItem("100% Stacked Area", MainWindow.Stacked100AreaLineSeries)
        self.seriesSwitcher.addItem("Area (Spline)", MainWindow.AreaSplineSeries)
        self.seriesSwitcher.addItem("Stacked Area (Spline)", MainWindow.StackedAreaSplineSeries)
        self.seriesSwitcher.addItem("100% Stacked Area (Spline)", MainWindow.Stacked100AreaSplineSeries)
        self.connect(self.seriesSwitcher, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("seriesChanged(int)"))

        self.transparency = QSlider(Qt.Horizontal, seriesTypeGroup)
        self.transparency.setRange(0, 255)
        self.transparency.setSliderPosition(190)
        self.transparency.setSingleStep(5)
        self.connect(self.transparency, QtCore.SIGNAL("valueChanged(int)"),
            self, QtCore.SLOT("transparencyChanged(int)"))

        layout = seriesTypeGroup.layout()
        layout.addRow(self.seriesSwitcher)
        layout.addRow(QLabel(self.tr("Transparency:"), seriesTypeGroup))
        layout.addRow(self.transparency)

    def createAreaSeries(self, _type):
        self.createTitle(self.tr("Average Temperatures by Month/Year:"))
        self.chart.legend().setVisible(True)

        series1 = self.createChartSeries("%.00f", type)
        self.chart.appendSeries(series1)
        series1.setName(self.tr("2010"))

        series2 = self.createChartSeries("%.00f", type)
        self.chart.appendSeries(series2)
        series2.setName(self.tr("2011"))

        series3 = self.createChartSeries("%.00f", type)
        self.chart.appendSeries(series3)
        series3.setName(self.tr("2012"))

        arTemp = [5., 12., 19, 22, 19, 24, 22, 24, 18, 14, 10, 7]
        for month in range(1, 13):
            strMonth = QtCore.QLocale.system().monthName(month, QtCore.QLocale.ShortFormat)
            series1.addAxisPointY(arTemp[month - 1], strMonth)
            series2.addAxisPointY(arTemp[month - 1] + self.getRandomValue(-4, 4), strMonth)
            series3.addAxisPointY(arTemp[month - 1] + self.getRandomValue(-4, 4), strMonth)

        view = self.chart.views()[0]
        view.axisX().setSideMargins(False)

    def updateValueParameters(self):
        DemoChartWindow.updateValueParameters(self)
        self.transparencyChanged(self.transparency.value())

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

        var = self.seriesSwitcher.itemData(index)
        self.createAreaSeries(int(var))
        self.updateValueParameters()

    def transparencyChanged(self, value):
        seriesList = self.chart.series()
        for series in seriesList:
            series.setTransparency(255 - value)

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