PieChart2D 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 (ChartPieSeries2D, ChartPieSeriesLabel, ChartLegend)

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

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

from DemoChartWindow import DemoChartWindow

import PieChart2D_rc

def createPiePoint(series, legendText, value, special = False):
    strValue = ", {:f}".format(value)
    s = legendText + strValue
    point = series.add(value, special)
    point.setLabel(s)
    point.setLegendText(legendText)

class MainWindow(DemoChartWindow):
    def __init__(self):
        DemoChartWindow.__init__(self, "Pie Chart 2D")
        self.setChart(None)
        self.createSeriesParametrs()
        self.createPieSeries()

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

        self.explodedCheckBox = QCheckBox(self.tr("Exploded"), seriesTypeGroup)
        self.connect(self.explodedCheckBox, QtCore.SIGNAL("stateChanged(int)"), self, QtCore.SLOT("explodedSeriesChanged(int)"))

        self.rotateSlider = QSlider(Qt.Horizontal, seriesTypeGroup)
        self.rotateSlider.setRange(-180, 180)
        self.rotateSlider.setSliderPosition(0)
        self.rotateSlider.setSingleStep(1)
        self.connect(self.rotateSlider, QtCore.SIGNAL("valueChanged(int)"), self, QtCore.SLOT("rotateSeriesChanged(int)"))

        # Layouts Series
        localLayout.addRow(self.explodedCheckBox)
        localLayout.addRow(QLabel(self.tr("Pie Rotation Angle:"), seriesTypeGroup))
        localLayout.addRow(self.rotateSlider)

        # Option Labels
        self.dataLabelsGroup = self.createGroupParameters(self.tr("Show Data Labels"), True)
        localLayout = 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("Label Outside"), ChartPieSeriesLabel.PieLabelOutside)
        self.posDataLabelsSwitcher.addItem(self.tr("Label Inside"), ChartPieSeriesLabel.PieLabelInside)
        self.posDataLabelsSwitcher.addItem(self.tr("Label Radial"), ChartPieSeriesLabel.PieLabelRadial)
        self.posDataLabelsSwitcher.setCurrentIndex(0)
        self.connect(self.posDataLabelsSwitcher, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("labelsPositionChanged(int)"))
        # Layouts Series
        localLayout.addRow(self.posDataLabelsSwitcher)

    def createPieSeries(self):
        self.createTitle(self.tr("Travel"), QColor(Qt.white))
        self.chart.legend().setVisible(True)
        self.chart.legend().setTextColor(Qt.white)
        self.chart.legend().setVerticalAlignment(ChartLegend.LegendFar)
        self.chart.setBackgroundBrush(QBrush(QPixmap(":/res/plane.png")))

        series = ChartPieSeries2D()
        self.chart.appendSeries(series)

        createPiePoint(series, self.tr("Other"), 6700, True)
        createPiePoint(series, self.tr("USA"), 4500, True)
        createPiePoint(series, self.tr("Taiwan"), 2200, True)
        createPiePoint(series, self.tr("France"), 1500, True)
        createPiePoint(series, self.tr("China"), 1400, True)
        createPiePoint(series, self.tr("Australia"), 1350, True)
        createPiePoint(series, self.tr("Germany"), 1050, True)
        createPiePoint(series, self.tr("Italy"), 1600, True)

        seriesLabel = self.chart.series()[0].label()
        seriesLabel.setLineColor(Qt.white)
        seriesLabel.setTextColor(Qt.white)
        seriesLabel.setBackgroundColor(QColor(Qt.transparent))
        seriesLabel.setBorderColor(Qt.white)
        self.chart.legend().setBorderColor(Qt.white)

        self.updateValueParameters()

    def updateValueParameters(self):
        DemoChartWindow.updateValueParameters(self)
        self.labelsPositionChanged(self.posDataLabelsSwitcher.currentIndex())
        self.explodedSeriesChanged(self.explodedCheckBox.checkState())
        self.rotateSeriesChanged(self.rotateSlider.value())

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

    def explodedSeriesChanged(self, state):
        listSeries = self.chart.series()
        for series in listSeries:
            if state == Qt.Checked:
                series.setExplodeRelativeSizePercent(15)
            else:
                series.setExplodeRelativeSizePercent(0)

    def rotateSeriesChanged(self, value):
        listSeries = self.chart.series()
        for series in listSeries:
            series.setRotation(value)

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