LineChart2D 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 (ChartLineSeries2D, ChartSplineSeries2D, ChartStepLineSeries2D)

if __pyside2__:
    from PySide2 import QtCore
    from PySide2.QtCore import Qt
    from PySide2.QtGui import QBrush, QPixmap, QColor
    from PySide2.QtWidgets import (QApplication, QComboBox, QCheckBox, QLabel)

if __pyside6__:
    from PySide6 import QtCore
    from PySide6.QtCore import Qt
    from PySide6.QtGui import QBrush, QPixmap, QColor
    from PySide6.QtWidgets import (QApplication, QComboBox, QCheckBox, QLabel)

from DemoChartWindow import DemoChartWindow
import LineChart2D_rc

class MainWindow(DemoChartWindow):
    LineSeries = 0
    SplineSeries = 1
    StepLineSeries = 2
    def __init__(self):
        DemoChartWindow.__init__(self, "Line Charts")

        self.displayShadow = True
        self.lineThicknessIndex = 2
        self.arYears = ["2015", "2016", "2017", "2018", "2019"]

        self.setChart(None)
        self.createSeriesParametrs()
        self.createMarkersGroup()
        self.createLabelsGroup()
        self.seriesChanged(self.seriesSwitcher.currentIndex())

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

        self.seriesSwitcher = QComboBox(seriesTypeGroup)
        self.seriesSwitcher.addItem("Line", MainWindow.LineSeries)
        self.seriesSwitcher.addItem("Spline", MainWindow.SplineSeries)
        self.seriesSwitcher.addItem("StepLine", MainWindow.StepLineSeries)
        self.connect(self.seriesSwitcher, QtCore.SIGNAL("currentIndexChanged(int)"),
            self, QtCore.SLOT("seriesChanged(int)"))

        self.lineThicknessSwitcher = QComboBox(seriesTypeGroup)
        self.lineThicknessSwitcher.addItem("1 Pixel",  1)
        self.lineThicknessSwitcher.addItem("2 Pixels", 2)
        self.lineThicknessSwitcher.addItem("3 Pixels", 3)
        self.lineThicknessSwitcher.addItem("4 Pixels", 4)
        self.lineThicknessSwitcher.addItem("5 Pixels", 5)
        self.connect(self.lineThicknessSwitcher, QtCore.SIGNAL("currentIndexChanged(int)"),
            self, QtCore.SLOT("lineThicknessChanged(int)"))

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

        # Layouts Series
        localLayout.addRow(self.seriesSwitcher)
        localLayout.addRow(QLabel(self.tr("Line thickness:")), self.lineThicknessSwitcher)
        localLayout.addRow(self.displayShadowBox)

    def createLineSeries(self):
        self.createTitle(self.tr("PC Sales"))

        self.chart.legend().setVisible(True)
        self.chart.legend().setTextColor(Qt.black)

        series1 = ChartLineSeries2D()
        self.chart.appendSeries(series1)
        series1.setDashStyle(Qt.DashLine)
        series1.setName(self.tr("HP"))

        series1.addAxisPointY(1500, self.arYears[0])
        series1.addAxisPointY(2300, self.arYears[1])
        series1.addAxisPointY(2500, self.arYears[2])
        series1.addAxisPointY(2800, self.arYears[3])
        series1.addAxisPointY(3000, self.arYears[4])

        series2 = ChartLineSeries2D()
        self.chart.appendSeries(series2)
        series2.setDashStyle(Qt.DashDotLine)
        series2.setName(self.tr("Samsung"))

        series2.addAxisPointY(1200, self.arYears[0])
        series2.addAxisPointY(1400, self.arYears[1])
        series2.addAxisPointY(1500, self.arYears[2])
        series2.addAxisPointY(1700, self.arYears[3])
        series2.addAxisPointY(1800, self.arYears[4])

        series3 = ChartLineSeries2D()
        self.chart.appendSeries(series3)
        series3.setName(self.tr("Acer"))

        series3.addAxisPointY(1300, self.arYears[0])
        series3.addAxisPointY(1500, self.arYears[1])
        series3.addAxisPointY(2000, self.arYears[2])
        series3.addAxisPointY(2600, self.arYears[3])
        series3.addAxisPointY(3200, self.arYears[4])

    def createSplineSeries(self):
        self.createTitle(self.tr("Population"))
        self.chart.legend().setVisible(True)
        self.chart.legend().setTextColor(Qt.black)

        series1 = ChartSplineSeries2D()
        self.chart.appendSeries(series1)
        series1.setName(self.tr("Moscow"))

        series1.addXY(1900, 8268)
        series1.addXY(1950, 24830)
        series1.addXY(1990, 27990)
        series1.addXY(2000, 28976)
        series1.addXY(2008, 29490)

        series2 = ChartSplineSeries2D()
        self.chart.appendSeries(series2)
        series2.setName(self.tr("Voronezh"))

        series2.addXY(1900, 7485)
        series2.addXY(1950, 20586)
        series2.addXY(1990, 23760)
        series2.addXY(2000, 23871)
        series2.addXY(2008, 26756)

        series3 = ChartSplineSeries2D()
        self.chart.appendSeries(series3)
        series3.setName(self.tr("Ryazan"))

        series3.addXY(1900, 1028)
        series3.addXY(1950, 1871)
        series3.addXY(1990, 12100)
        series3.addXY(2000, 14982)
        series3.addXY(2008, 15328)

        series4 = ChartSplineSeries2D()
        self.chart.appendSeries(series4)
        series4.setName(self.tr("Cherepovets"))

        series4.addXY(1900, 2048)
        series4.addXY(1950, 5711)
        series4.addXY(1990, 12986)
        series4.addXY(2000, 10851)
        series4.addXY(2008, 20326)

        listSeries = self.chart.series()
        # Set the X and Y Axis title for the series.
        view = listSeries[0].view()
        axisX = view.axisX()
        if axisX != None:
            title = axisX.title()
            title.setText(self.tr("Decade / Year"))
            title.setVisible(True)

        axisY = view.axisY()
        if axisY != None:
            title = axisY.title()
            title.setText(self.tr("Population Growth (Thousands)"))
            title.setVisible(True)

    def createStepLineSeries(self):
        self. createTitle(self.tr("Transport expenses"))
        title = self.chart.titles()[0]
        title.setTextColor(Qt.white)

        self.chart.legend().setVisible(True)
        self.chart.legend().setTextColor(Qt.white)
        self.chart.setBackgroundBrush(QBrush(QPixmap(":/res/cars.png")))

        series = ChartStepLineSeries2D()
        self.chart.appendSeries(series)
        view = series.view()
        view.setBackgroundColor(QColor(192, 192, 192, 150))
        view.axisX().label().setTextColor(Qt.white)
        view.axisY().label().setTextColor(Qt.white)
        view.axisX().setColor(Qt.white)
        view.axisY().setColor(Qt.white)

        series.setName(self.tr("Series 1"))

        colorPoint = QColor(Qt.red).darker()
        series.setColor(colorPoint)
        series.addAxisPointY(50.0, self.arYears[0]).setColor(colorPoint)
        series.addAxisPointY(80.0, self.arYears[1]).setColor(colorPoint)
        series.addAxisPointY(40.0,  self.arYears[2]).setColor(colorPoint)
        series.addAxisPointY(100.0, self.arYears[3]).setColor(colorPoint)
        series.addAxisPointY(200.0, self.arYears[4]).setColor(colorPoint)

    def updateValueParameters(self):
        DemoChartWindow.updateValueParameters(self)
        if self.displayShadow:
            self.displayShadowBox.setCheckState(Qt.Checked)
        else:
            self.displayShadowBox.setCheckState(Qt.Unchecked)
        self.lineThicknessSwitcher.setCurrentIndex(self.lineThicknessIndex)

    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 int(v) == MainWindow.LineSeries:
            self.createLineSeries()
        elif int(v) == MainWindow.SplineSeries:
            self.createSplineSeries()
        elif MainWindow.StepLineSeries:
            self.createStepLineSeries()
        self.displayShadowSeries()
        self.lineThicknessSeries()
        self.updateValueParameters()

    def lineThicknessChanged(self, index):
        if self.lineThicknessIndex != index:
            self.lineThicknessIndex = index
            self.lineThicknessSeries()

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

    def lineThicknessSeries(self):
        v = self.lineThicknessSwitcher.itemData(self.lineThicknessIndex)

        listSeries = self.chart.series()
        for series in listSeries:
            if series.inherits("Qtitan::ChartLineSeries2D"):
                series.setThickness(int(v))

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