/qtcharts_barchart.py Secret
Created
November 23, 2021 00:33
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# Reference | |
# https://doc.qt.io/qt-6/qtcharts-barchart-example.html | |
import sys | |
from PySide6.QtCharts import ( | |
QBarCategoryAxis, | |
QBarSet, | |
QBarSeries, | |
QChart, | |
QChartView, | |
QValueAxis, | |
) | |
from PySide6.QtCore import Qt | |
from PySide6.QtGui import QPainter | |
from PySide6.QtWidgets import ( | |
QApplication, | |
QMainWindow, | |
) | |
class BarChart(QChartView): | |
def __init__(self): | |
super().__init__() | |
chart = self.init_ui() | |
self.setChart(chart) | |
self.setRenderHint(QPainter.Antialiasing) | |
def init_ui(self): | |
set0 = QBarSet('Jane') | |
set1 = QBarSet('John') | |
set2 = QBarSet('Axel') | |
set3 = QBarSet('Mary') | |
set4 = QBarSet('Samantha') | |
set0 << 1 << 2 << 3 << 4 << 5 << 6 | |
set1 << 5 << 0 << 0 << 4 << 0 << 7 | |
set2 << 3 << 5 << 8 << 13 << 8 << 5 | |
set3 << 5 << 6 << 7 << 3 << 4 << 5 | |
set4 << 9 << 7 << 5 << 3 << 1 << 2 | |
series = QBarSeries() | |
series.append(set0) | |
series.append(set1) | |
series.append(set2) | |
series.append(set3) | |
series.append(set4) | |
chart = QChart() | |
chart.addSeries(series) | |
chart.setTitle('Simple barchart example') | |
chart.setAnimationOptions(QChart.SeriesAnimations) | |
categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'] | |
axisX = QBarCategoryAxis() | |
axisX.append(categories) | |
chart.addAxis(axisX, Qt.AlignBottom) | |
series.attachAxis(axisX) | |
axisY = QValueAxis() | |
axisY.setRange(0, 15) | |
chart.addAxis(axisY, Qt.AlignLeft) | |
series.attachAxis(axisY) | |
chart.legend().setVisible(True) | |
chart.legend().setAlignment(Qt.AlignBottom) | |
return chart | |
class Example(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
barchart = BarChart() | |
self.setCentralWidget(barchart) | |
self.resize(500, 300) | |
self.setWindowTitle('BarChart') | |
def main(): | |
app = QApplication(sys.argv) | |
ex = Example() | |
ex.show() | |
sys.exit(app.exec()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment