def make_cake( self ): score_1 = self.players[ 0 ].score score_2 = self.players[ 1 ].score series = QPieSeries() free_score = 120 - score_1 - score_2 series.append( f"{free_score}", free_score ) series.append( f"{score_1}", score_1 ) series.append( f"{score_2}", score_2 ) series.setPieStartAngle( 3 * score_2 ) series.setPieEndAngle( 3 * score_2 + 360 ) slices = series.slices() for slice in slices: slice.setLabelVisible( True ) if slice.angleSpan() < 45: slice.setLabelPosition( QPieSlice.LabelPosition.LabelInsideNormal ) elif slice.angleSpan() < 90: slice.setLabelPosition( QPieSlice.LabelPosition.LabelInsideTangential ) else: slice.setLabelPosition( QPieSlice.LabelPosition.LabelInsideHorizontal ) slice.setLabelColor( QColor( "#00000" ) ) slice.setLabelFont( QFont( "Fira Sans", 60, weight=QFont.Weight.Black ) ) slice.setBorderWidth( 0 ) slices[ 0 ].setLabelPosition( QPieSlice.LabelPosition.LabelInsideHorizontal ) if score_1 < 10: slices[ 1 ].setLabelFont( QFont( "Fira Sans", score_1 * 6, weight=QFont.Weight.Black ) ) if score_2 < 10: slices[ 2 ].setLabelFont( QFont( "Fira Sans", score_2 * 6, weight=QFont.Weight.Black ) ) slices[ 0 ].setLabelColor( QColor( "#FFFFFF" ) ) slices[ 0 ].setColor( remaining_points_color ) slices[ 1 ].setColor( player_1.color ) slices[ 2 ].setColor( player_2.color ) chart = QChart() chart.legend().hide() chart.addSeries( series ) chart.createDefaultAxes() chart.setBackgroundVisible( False ) chart.setContentsMargins( -120, -120, -120, -120 ) chart.layout().setContentsMargins( 0, 0, 0, 0 ) chart_view = QChartView( chart ) chart_view.setRenderHint( QPainter.Antialiasing ) return chart_view
class VCPie(VCCommons): def __init__(self): VCCommons.__init__(self) self.setRenderHint(QPainter.Antialiasing) self.clear() def setCurrency(self, currency): """ currency is a Currency Object """ self.currency=currency def appendData(self, name, value, exploded=False): slice=self.serie.append(name, value) slice.setExploded(exploded) slice.setLabelVisible() def display(self): self.setChart(self.__chart) self.chart().layout().setContentsMargins(0,0,0,0); if self._animations==True: self.chart().setAnimationOptions(QChart.AllAnimations); else: self.chart().setAnimationOptions(QChart.NoAnimation) self._display_set_title() tooltip="" c=self.currency.string for slice in self.serie.slices(): tooltip=tooltip+"{}: {} ({})\n".format(slice.label(), c(slice.value()), Percentage(slice.percentage(), 1)).upper() slice.setLabel("{}: {}".format(slice.label(), Percentage(slice.percentage(), 1)).upper()) if slice.percentage()<0.005: slice.setLabelVisible(False) tooltip=tooltip+"*** Total: {} ***".format(c(self.serie.sum())).upper() self.chart().addSeries(self.serie) self.setToolTip(tooltip) self.repaint() def clear(self): self.__chart=QChart() self.setChart(self.__chart) self.chart().legend().hide() self.serie=QPieSeries() self.serie.setPieStartAngle(90) self.serie.setPieEndAngle(450)
class VCPieAlone(VCCommons): def __init__(self): VCCommons.__init__(self) self.data = [] #Dta with float only for chart self.dataobjects = [] #Data with objects self.setRenderHint(QPainter.Antialiasing) self.clear() ## If you use VCPieAlone you can add a context menu setting boolean to True def setCustomContextMenu(self, boolean): self.customContextMenuRequested.connect( self.on_customContextMenuRequested) def appendData(self, name, value, exploded=False): self.data.append([name, value]) slice = self.serie.append(name, object2value(value)) #only float slice.setExploded(exploded) slice.setLabelVisible() def display(self): self.setChart(self.__chart) self.chart().layout().setContentsMargins(0, 0, 0, 0) if self._animations == True: self.chart().setAnimationOptions(QChart.AllAnimations) else: self.chart().setAnimationOptions(QChart.NoAnimation) self._display_set_title() for slice in self.serie.slices(): slice.setLabel("{}: {}".format(slice.label(), Percentage(slice.percentage(), 1))) if slice.percentage() < 0.005: slice.setLabelVisible(False) self.chart().addSeries(self.serie) self.repaint() self.displayed.emit() def sum_values(self): if len(self.data) == 0: return None cls = self.data[0][1].__class__.__name__ if cls in ["int", "float", "Decimal"]: s = 0 elif cls in [ "Currency", ]: s = self.data[0][1].__class__(0, self.data[0][1].currency) elif cls in [ "Money", ]: s = self.data[0][1].__class__(self.data[0][1].mem, 0, self.data[0][1].currency) for row in self.data: s = s + row[1] return s def clear(self): self.__chart = QChart() self.setChart(self.__chart) self.chart().legend().hide() self.serie = QPieSeries() self.serie.setPieStartAngle(90) self.serie.setPieEndAngle(450) ## Returns a qmenu to be used in other qmenus def qmenu(self, title="Pie chart options"): menu = QMenu(self) menu.setTitle(self.tr(title)) menu.addAction(self.actionSave) return menu def on_customContextMenuRequested(self, pos): self.qmenu().exec_(self.mapToGlobal(pos))