PieChart3D 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 (ChartPieSeries3D, ChartPieSeriesLabel, ChartLegend)
if __pyside2__:
from PySide2 import QtCore
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (QApplication, QComboBox, QCheckBox, QSlider, QLabel)
if __pyside6__:
from PySide6 import QtCore
from PySide6.QtCore import Qt
from PySide6.QtWidgets import (QApplication, QComboBox, QCheckBox, QSlider, QLabel)
from DemoChartWindow import DemoChartWindow
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 3D")
self.setChart(None)
self.createSeriesParametrs()
self.createDoughnutSeries()
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)"))
self.aspectAngleSlider = QSlider(Qt.Horizontal, seriesTypeGroup)
self.aspectAngleSlider.setRange(1, 90)
self.aspectAngleSlider.setSingleStep(1)
self.connect(self.aspectAngleSlider, QtCore.SIGNAL("valueChanged(int)"), self, QtCore.SLOT("aspectAngleChanged(int)"))
# Layouts Series
localLayout.addRow(self.explodedCheckBox)
localLayout.addRow(QLabel(self.tr("Pie Rotation Angle:"), seriesTypeGroup))
localLayout.addRow(self.rotateSlider)
self.captionAspectAngle = QLabel(self.tr("Pie Aspect Angle:"), seriesTypeGroup)
localLayout.addRow(self.captionAspectAngle)
localLayout.addRow(self.aspectAngleSlider)
# 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 createDoughnutSeries(self):
self.createTitle(self.tr("Travel"))
self.chart.legend().setVisible(True)
self.chart.legend().setVerticalAlignment(ChartLegend.LegendFar)
series = ChartPieSeries3D()
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)
self.updateValueParameters()
def updateValueParameters(self):
DemoChartWindow.updateValueParameters(self)
self.labelsPositionChanged(self.posDataLabelsSwitcher.currentIndex())
self.explodedSeriesChanged(self.explodedCheckBox.checkState())
self.rotateSeriesChanged(self.rotateSlider.value())
self.aspectAngleSlider.setSliderPosition(self.chart.series()[0].aspectAngle())
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)
def aspectAngleChanged(self, value):
listSeries = self.chart.series()
for series in listSeries:
series.setAspectAngle(value)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())