def getChart(self, lessonname, data): series = QtChart.QStackedBarSeries() for key, name, color, brush in zip( [self.PASS, self.FAIL], ["pass", "fail"], ["green", "red"], [QtCore.Qt.Dense1Pattern, QtCore.Qt.Dense2Pattern]): barset = QtChart.QBarSet(name) barset.append([d[key] for d in data]) barset.setBrush(QtGui.QBrush(QtGui.QColor(color), brush)) barset.setLabel(name) series.append(barset) series.setLabelsFormat("@value%") series.setLabelsVisible(True) chart = QtChart.QChart() chart.addSeries(series) chart.setTitle(lessonname) chart.setAnimationOptions(QtChart.QChart.SeriesAnimations) categories = [d["name"] for d in data] axis = QtChart.QBarCategoryAxis() axis.append(categories) chart.setAxisX(axis, series) axis = QtChart.QValueAxis() axis.setMax(100) axis.setMin(0) chart.setAxisY(axis) chart.legend().setVisible(True) return chart
def calculate_graph(self): """ Calculate the Bar graph for the selected SKU's from database. """ if self.show_graph: bar_set_1, bar_set_2 = self.get_graph_point() series = QtChart.QStackedBarSeries() series.append(bar_set_1) series.append(bar_set_2) chart = QtChart.QChart() chart.setTitle("Good and Bad") chart.addSeries(series) chart.setAnimationOptions(QtChart.QChart.SeriesAnimations) x_axis = QtChart.QBarCategoryAxis() x_axis.append(constants.TIME_VALUES[:-1]) chart.addAxis(x_axis, QtCore.Qt.AlignBottom) series.attachAxis(x_axis) y_axis = QtChart.QValueAxis() chart.addAxis(y_axis, QtCore.Qt.AlignLeft) series.attachAxis(y_axis) chart.legend().setVisible(True) chart.legend().setAlignment(QtCore.Qt.AlignBottom) chart_view = QtChart.QChartView(chart) chart_view.setRenderHint(QtGui.QPainter.Antialiasing) return chart_view
def __init__(self): super().__init__() ################ # Create Chart # ################ # Create qchart object chart = qtch.QChart(title=self.chart_title) self.setChart(chart) # Setup series series = qtch.QStackedBarSeries() chart.addSeries(series) self.phys_set = qtch.QBarSet("Physical") self.swap_set = qtch.QBarSet("Swap") series.append(self.phys_set) series.append(self.swap_set) # Setup Data self.data = deque([(0, 0)] * self.num_data_points, maxlen=self.num_data_points) for phys, swap in self.data: self.phys_set.append(phys) self.swap_set.append(swap) # Setup Axes x_axis = qtch.QValueAxis() x_axis.setRange(0, self.num_data_points) x_axis.setLabelsVisible(False) y_axis = qtch.QValueAxis() y_axis.setRange(0, 100) chart.setAxisX(x_axis, series) chart.setAxisY(y_axis, series) # Start refresh timer self.timer = qtc.QTimer(interval=1000, timeout=self.refresh_stats) self.timer.start() ################### # Style the chart # ################### chart.setAnimationOptions(qtch.QChart.AllAnimations) chart.setAnimationEasingCurve( qtc.QEasingCurve(qtc.QEasingCurve.OutBounce)) chart.setAnimationDuration(1000) # Add shadow around the chart area chart.setDropShadowEnabled(True) # Set the theme chart.setTheme(qtch.QChart.ChartThemeBrownSand) # Configure a background brush gradient = qtg.QLinearGradient(chart.plotArea().topLeft(), chart.plotArea().bottomRight()) gradient.setColorAt(0, qtg.QColor("#333")) gradient.setColorAt(1, qtg.QColor("#660")) chart.setBackgroundBrush(qtg.QBrush(gradient)) # Background Pen draws a border around the chart chart.setBackgroundPen(qtg.QPen(qtg.QColor('black'), 5)) # Set title font and brush chart.setTitleBrush(qtg.QBrush(qtc.Qt.white)) chart.setTitleFont(qtg.QFont('Impact', 32, qtg.QFont.Bold)) # Set axes fonts and brushes axis_font = qtg.QFont('Mono', 16) axis_brush = qtg.QBrush(qtg.QColor('#EEF')) y_axis.setLabelsFont(axis_font) y_axis.setLabelsBrush(axis_brush) # Grid lines grid_pen = qtg.QPen(qtg.QColor('silver')) grid_pen.setDashPattern([1, 1, 0, 1]) x_axis.setGridLinePen(grid_pen) y_axis.setGridLinePen(grid_pen) y_axis.setTickCount(11) #Shades y_axis.setShadesVisible(True) y_axis.setShadesColor(qtg.QColor('#884')) # Styling the legend legend = chart.legend() # Background legend.setBackgroundVisible(True) legend.setBrush(qtg.QBrush(qtg.QColor('white'))) # Font legend.setFont(qtg.QFont('Courier', 14)) legend.setLabelColor(qtc.Qt.darkRed) # Markers legend.setMarkerShape(qtch.QLegend.MarkerShapeCircle)