class TestChart(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.series = QLineSeries() self.series.append(0, 6) self.series.append(2, 4) self.series.append(3, 8) self.series.append(7, 4) self.series.append(10, 5) self.series.append(QPointF(11, 1)) self.series.append(QPointF(13, 3)) self.series.append(QPointF(17, 6)) self.series.append(QPointF(18, 3)) self.series.append(QPointF(20, 2)) self.chart = QChart() self.chart.legend().hide() self.chart.addSeries(self.series) self.chart.createDefaultAxes() self.chart.setTitle("Simple line chart example") self.chartView = QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)
class TestChart(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.series = QPieSeries() self.series.append('Jane', 1) self.series.append('Joe', 2) self.series.append('Andy', 3) self.series.append('Barbara', 4) self.series.append('Axel', 5) self.slice = self.series.slices()[1] self.slice.setExploded() self.slice.setLabelVisible() self.slice.setPen(QPen(Qt.darkGreen, 2)) self.slice.setBrush(Qt.green) self.chart = QChart() self.chart.addSeries(self.series) self.chart.setTitle('Simple piechart example') self.chart.legend().hide() self.chartView = QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)
class TableWidget(QWidget): def __init__(self): QWidget.__init__(self) self.model = CustomTableModel() self.table_view = QTableView() self.table_view.setModel(self.model) self.table_view.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.table_view.verticalHeader().setSectionResizeMode(QHeaderView.Stretch) self.chart = QChart() self.chart.setAnimationOptions(QChart.AllAnimations) self.series = QLineSeries() self.series.setName("Line 1") self.mapper = QVXYModelMapper(self) self.mapper.setXColumn(0) self.mapper.setYColumn(1) self.mapper.setSeries(self.series) self.mapper.setModel(self.model) self.chart.addSeries(self.series) # for storing color hex from the series seriesColorHex = "#000000" # get the color of the series and use it for showing the mapped area seriesColorHex = "{}".format(self.series.pen().color().name()) self.model.add_mapping(seriesColorHex, QRect(0, 0, 2, self.model.rowCount())) # series 2 self.series = QLineSeries() self.series.setName("Line 2") self.mapper = QVXYModelMapper(self) self.mapper.setXColumn(2) self.mapper.setYColumn(3) self.mapper.setSeries(self.series) self.mapper.setModel(self.model) self.chart.addSeries(self.series) # get the color of the series and use it for showing the mapped area seriesColorHex = "{}".format(self.series.pen().color().name()) self.model.add_mapping(seriesColorHex, QRect(2, 0, 2, self.model.rowCount())) self.chart.createDefaultAxes() self.chart_view = QChartView(self.chart) self.chart_view.setRenderHint(QPainter.Antialiasing) self.chart_view.setMinimumSize(640, 480) # create main layout self.main_layout = QGridLayout() self.main_layout.addWidget(self.table_view, 1, 0) self.main_layout.addWidget(self.chart_view, 1, 1) self.main_layout.setColumnStretch(1, 1) self.main_layout.setColumnStretch(0, 0) self.setLayout(self.main_layout)
class TestChart(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.set0 = QBarSet("Jane") self.set1 = QBarSet("John") self.set2 = QBarSet("Axel") self.set3 = QBarSet("Mary") self.set4 = QBarSet("Sam") self.set0.append([1, 2, 3, 4, 5, 6]) self.set1.append([5, 0, 0, 4, 0, 7]) self.set2.append([3, 5, 8, 13, 8, 5]) self.set3.append([5, 6, 7, 3, 4, 5]) self.set4.append([9, 7, 5, 3, 1, 2]) self.barSeries = QBarSeries() self.barSeries.append(self.set0) self.barSeries.append(self.set1) self.barSeries.append(self.set2) self.barSeries.append(self.set3) self.barSeries.append(self.set4) self.lineSeries = QLineSeries() self.lineSeries.setName("trend") self.lineSeries.append(QPoint(0, 4)) self.lineSeries.append(QPoint(1, 15)) self.lineSeries.append(QPoint(2, 20)) self.lineSeries.append(QPoint(3, 4)) self.lineSeries.append(QPoint(4, 12)) self.lineSeries.append(QPoint(5, 17)) self.chart = QChart() self.chart.addSeries(self.barSeries) self.chart.addSeries(self.lineSeries) self.chart.setTitle("Line and barchart example") self.categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"] self.axisX = QBarCategoryAxis() self.axisX.append(self.categories) self.chart.setAxisX(self.axisX, self.lineSeries) self.chart.setAxisX(self.axisX, self.barSeries) self.axisX.setRange("Jan", "Jun") self.axisY = QValueAxis() self.chart.setAxisY(self.axisY, self.lineSeries) self.chart.setAxisY(self.axisY, self.barSeries) self.axisY.setRange(0, 20) self.chart.legend().setVisible(True) self.chart.legend().setAlignment(Qt.AlignBottom) self.chartView = QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)
def __init__(self): QMainWindow.__init__(self) set0 = QBarSet("Jane") set1 = QBarSet("John") set2 = QBarSet("Axel") set3 = QBarSet("Mary") set4 = QBarSet("Samantha") set0.append([1, 2, 3, 4, 5, 6]) set1.append([5, 0, 0, 4, 0, 7]) set2.append([3, 5, 8, 13, 8, 5]) set3.append([5, 6, 7, 3, 4, 5]) set4.append([9, 7, 5, 3, 1, 2]) series = QPercentBarSeries() series.append(set0) series.append(set1) series.append(set2) series.append(set3) series.append(set4) chart = QChart() chart.addSeries(series) chart.setTitle("Simple percentbarchart example") chart.setAnimationOptions(QChart.SeriesAnimations) categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"] axis = QBarCategoryAxis() axis.append(categories) chart.createDefaultAxes() chart.setAxisX(axis, series) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chart_view = QChartView(chart) chart_view.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(chart_view)
def __init__(self): QMainWindow.__init__(self) low = QBarSet("Min") high = QBarSet("Max") low.append([-52, -50, -45.3, -37.0, -25.6, -8.0, -6.0, -11.8, -19.7, -32.8, -43.0, -48.0]) high.append([11.9, 12.8, 18.5, 26.5, 32.0, 34.8, 38.2, 34.8, 29.8, 20.4, 15.1, 11.8]) series = QStackedBarSeries() series.append(low) series.append(high) chart = QChart() chart.addSeries(series) chart.setTitle("Temperature records in celcius") chart.setAnimationOptions(QChart.SeriesAnimations) categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] axisX = QBarCategoryAxis() axisX.append(categories) axisX.setTitleText("Month") chart.addAxis(axisX, Qt.AlignBottom) axisY = QValueAxis() axisY.setRange(-52, 52) axisY.setTitleText("Temperature [°C]") chart.addAxis(axisY, Qt.AlignLeft) series.attachAxis(axisX) series.attachAxis(axisY) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chart_view = QChartView(chart) chart_view.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(chart_view)
class TestChart(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.series = QLineSeries() self.series.append([ QPointF(1.0, 1.0), QPointF(2.0, 73.0), QPointF(3.0, 268.0), QPointF(4.0, 17.0), QPointF(5.0, 4325.0), QPointF(6.0, 723.0) ]) self.chart = QChart() self.chart.addSeries(self.series) self.chart.legend().hide() self.chart.setTitle("Logarithmic axis example") self.axisX = QValueAxis() self.axisX.setTitleText("Data point") self.axisX.setLabelFormat("%i") self.axisX.setTickCount(self.series.count()) self.chart.addAxis(self.axisX, Qt.AlignBottom) self.series.attachAxis(self.axisX) self.axisY = QLogValueAxis() self.axisY.setTitleText("Values") self.axisY.setLabelFormat("%g") self.axisY.setBase(8.0) self.axisY.setMinorTickCount(-1) self.chart.addAxis(self.axisY, Qt.AlignLeft) self.series.attachAxis(self.axisY) self.chartView = QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)
class Widget(QWidget): def __init__(self): QWidget.__init__(self) self.items = 0 # Left self.table = QTableWidget() self.table.setColumnCount(2) self.table.setHorizontalHeaderLabels(["Description", "Quantity"]) self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) # Chart self.chart_view = QChartView() self.chart_view.setRenderHint(QPainter.Antialiasing) # Right self.description = QLineEdit() self.quantity = QLineEdit() self.add = QPushButton("Add") self.clear = QPushButton("Clear") self.quit = QPushButton("Quit") self.plot = QPushButton("Plot") # Disabling 'Add' button self.add.setEnabled(False) self.right = QVBoxLayout() self.right.setContentsMargins(10, 10, 10, 10) self.right_top = QGridLayout() self.right_top.addWidget(QLabel("Description"), 0, 0, 1, 1) self.right_top.addWidget(self.description, 0, 1, 1, 3) self.right_top.addWidget(QLabel("Quantity"), 1, 0, 1, 1) self.right_top.addWidget(self.quantity, 1, 1, 1, 1) self.right_top.addWidget(self.add, 1, 2, 1, 2) self.right.addLayout(self.right_top) self.right.addWidget(self.chart_view) self.right_bottom = QGridLayout() self.right_bottom.addWidget(self.plot, 0, 0, 1, 2) self.right_bottom.addWidget(self.clear, 1, 0) self.right_bottom.addWidget(self.quit, 1, 1) self.right.addLayout(self.right_bottom) # QWidget Layout self.layout = QHBoxLayout() self.layout.addWidget(self.table) self.layout.addLayout(self.right) # Set the layout to the QWidget self.setLayout(self.layout) # Signals and Slots self.add.clicked.connect(self.add_element) self.quit.clicked.connect(self.quit_application) self.plot.clicked.connect(self.plot_data) self.clear.clicked.connect(self.clear_table) self.description.textChanged[str].connect(self.check_disable) self.quantity.textChanged[str].connect(self.check_disable) @Slot() def add_element(self): des = self.description.text() qty = self.quantity.text() self.table.insertRow(self.items) self.table.setItem(self.items, 0, QTableWidgetItem(des)) self.table.setItem(self.items, 1, QTableWidgetItem(qty)) self.description.setText("") self.quantity.setText("") self.items += 1 @Slot() def check_disable(self, s): if not self.description.text() or not self.quantity.text(): self.add.setEnabled(False) else: self.add.setEnabled(True) @Slot() def plot_data(self): # Get table information series = QBarSeries() for i in range(self.table.rowCount()): text = self.table.item(i, 0).text() number = float(self.table.item(i, 1).text()) bar_set = QBarSet(text) bar_set.append(number) series.append(bar_set) chart = QChart() chart.addSeries(series) chart.legend().setAlignment(Qt.AlignLeft) self.chart_view.setChart(chart) @Slot() def quit_application(self): QApplication.quit() @Slot() def clear_table(self): self.table.setRowCount(0) self.items = 0
class MainWidget(QWidget): def __init__(self, parent=None): super(MainWidget, self).__init__(parent) self.chart = QChart() self.series = QBarSeries() self.main_layout = QGridLayout() self.button_layout = QGridLayout() self.font_layout = QFormLayout() self.font_size = QDoubleSpinBox() self.legend_posx = QDoubleSpinBox() self.legend_posy = QDoubleSpinBox() self.legend_width = QDoubleSpinBox() self.legend_height = QDoubleSpinBox() self.detach_legend_button = QPushButton("Toggle attached") self.detach_legend_button.clicked.connect(self.toggle_attached) self.button_layout.addWidget(self.detach_legend_button, 0, 0) self.add_set_button = QPushButton("add barset") self.add_set_button.clicked.connect(self.add_barset) self.button_layout.addWidget(self.add_set_button, 2, 0) self.remove_barset_button = QPushButton("remove barset") self.remove_barset_button.clicked.connect(self.remove_barset) self.button_layout.addWidget(self.remove_barset_button, 3, 0) self.align_button = QPushButton("Align (Bottom)") self.align_button.clicked.connect(self.set_legend_alignment) self.button_layout.addWidget(self.align_button, 4, 0) self.bold_button = QPushButton("Toggle bold") self.bold_button.clicked.connect(self.toggle_bold) self.button_layout.addWidget(self.bold_button, 8, 0) self.italic_button = QPushButton("Toggle italic") self.italic_button.clicked.connect(self.toggle_italic) self.button_layout.addWidget(self.italic_button, 9, 0) self.legend_posx.valueChanged.connect(self.update_legend_layout) self.legend_posy.valueChanged.connect(self.update_legend_layout) self.legend_width.valueChanged.connect(self.update_legend_layout) self.legend_height.valueChanged.connect(self.update_legend_layout) legend_layout = QFormLayout() legend_layout.addRow("HPos", self.legend_posx) legend_layout.addRow("VPos", self.legend_posy) legend_layout.addRow("Width", self.legend_width) legend_layout.addRow("Height", self.legend_height) self.legend_settings = QGroupBox("Detached legend") self.legend_settings.setLayout(legend_layout) self.button_layout.addWidget(self.legend_settings) self.legend_settings.setVisible(False) # Create chart view with the chart self.chart_view = QChartView(self.chart, self) # Create spinbox to modify font size self.font_size.setValue(self.chart.legend().font().pointSizeF()) self.font_size.valueChanged.connect(self.font_size_changed) self.font_layout.addRow("Legend font size", self.font_size) # Create layout for grid and detached legend self.main_layout.addLayout(self.button_layout, 0, 0) self.main_layout.addLayout(self.font_layout, 1, 0) self.main_layout.addWidget(self.chart_view, 0, 1, 3, 1) self.setLayout(self.main_layout) self.create_series() def create_series(self): self.add_barset() self.add_barset() self.add_barset() self.add_barset() self.chart.addSeries(self.series) self.chart.setTitle("Legend detach example") self.chart.createDefaultAxes() self.chart.legend().setVisible(True) self.chart.legend().setAlignment(Qt.AlignBottom) self.chart_view.setRenderHint(QPainter.Antialiasing) def show_legend_spinbox(self): self.legend_settings.setVisible(True) chart_viewrect = self.chart_view.rect() self.legend_posx.setMinimum(0) self.legend_posx.setMaximum(chart_viewrect.width()) self.legend_posx.setValue(150) self.legend_posy.setMinimum(0) self.legend_posy.setMaximum(chart_viewrect.height()) self.legend_posy.setValue(150) self.legend_width.setMinimum(0) self.legend_width.setMaximum(chart_viewrect.width()) self.legend_width.setValue(150) self.legend_height.setMinimum(0) self.legend_height.setMaximum(chart_viewrect.height()) self.legend_height.setValue(75) def hideLegendSpinbox(self): self.legend_settings.setVisible(False) def toggle_attached(self): legend = self.chart.legend() if legend.isAttachedToChart(): legend.detachFromChart() legend.setBackgroundVisible(True) legend.setBrush(QBrush(QColor(128, 128, 128, 128))) legend.setPen(QPen(QColor(192, 192, 192, 192))) self.show_legend_spinbox() self.update_legend_layout() else: legend.attachToChart() legend.setBackgroundVisible(False) self.hideLegendSpinbox() self.update() def add_barset(self): series_count = self.series.count() bar_set = QBarSet(f"set {series_count}") delta = series_count * 0.1 bar_set.append([1 + delta, 2 + delta, 3 + delta, 4 + delta]) self.series.append(bar_set) def remove_barset(self): sets = self.series.barSets() len_sets = len(sets) if len_sets > 0: self.series.remove(sets[len_sets - 1]) def set_legend_alignment(self): button = self.sender() legend = self.chart.legend() alignment = legend.alignment() if alignment == Qt.AlignTop: legend.setAlignment(Qt.AlignLeft) if button: button.setText("Align (Left)") elif alignment == Qt.AlignLeft: legend.setAlignment(Qt.AlignBottom) if button: button.setText("Align (Bottom)") elif alignment == Qt.AlignBottom: legend.setAlignment(Qt.AlignRight) if button: button.setText("Align (Right)") else: if button: button.setText("Align (Top)") legend.setAlignment(Qt.AlignTop) def toggle_bold(self): legend = self.chart.legend() font = legend.font() font.setBold(not font.bold()) legend.setFont(font) def toggle_italic(self): legend = self.chart.legend() font = legend.font() font.setItalic(not font.italic()) legend.setFont(font) def font_size_changed(self): legend = self.chart.legend() font = legend.font() font_size = self.font_size.value() if font_size < 1: font_size = 1 font.setPointSizeF(font_size) legend.setFont(font) def update_legend_layout(self): legend = self.chart.legend() rect = QRectF(self.legend_posx.value(), self.legend_posy.value(), self.legend_width.value(), self.legend_height.value()) legend.setGeometry(rect) legend.update()
class Widget(QWidget): def __init__(self): QWidget.__init__(self) self.setMinimumSize(800, 600) self.donuts = [] self.chart_view = QChartView() self.chart_view.setRenderHint(QPainter.Antialiasing) self.chart = self.chart_view.chart() self.chart.legend().setVisible(False) self.chart.setTitle("Nested donuts demo") self.chart.setAnimationOptions(QChart.AllAnimations) self.min_size = 0.1 self.max_size = 0.9 self.donut_count = 5 self.setup_donuts() # create main layout self.main_layout = QGridLayout(self) self.main_layout.addWidget(self.chart_view, 1, 1) self.setLayout(self.main_layout) self.update_timer = QTimer(self) self.update_timer.timeout.connect(self.update_rotation) self.update_timer.start(1250) def setup_donuts(self): for i in range(self.donut_count): donut = QPieSeries() slccount = randrange(3, 6) for j in range(slccount): value = randrange(100, 200) slc = QPieSlice(str(value), value) slc.setLabelVisible(True) slc.setLabelColor(Qt.white) slc.setLabelPosition(QPieSlice.LabelInsideTangential) # Connection using an extra parameter for the slot slc.hovered[bool].connect(partial(self.explode_slice, slc=slc)) donut.append(slc) size = (self.max_size - self.min_size) / self.donut_count donut.setHoleSize(self.min_size + i * size) donut.setPieSize(self.min_size + (i + 1) * size) self.donuts.append(donut) self.chart_view.chart().addSeries(donut) def update_rotation(self): for donut in self.donuts: phase_shift = randrange(-50, 100) donut.setPieStartAngle(donut.pieStartAngle() + phase_shift) donut.setPieEndAngle(donut.pieEndAngle() + phase_shift) def explode_slice(self, exploded, slc): if exploded: self.update_timer.stop() slice_startangle = slc.startAngle() slice_endangle = slc.startAngle() + slc.angleSpan() donut = slc.series() idx = self.donuts.index(donut) for i in range(idx + 1, len(self.donuts)): self.donuts[i].setPieStartAngle(slice_endangle) self.donuts[i].setPieEndAngle(360 + slice_startangle) else: for donut in self.donuts: donut.setPieStartAngle(0) donut.setPieEndAngle(360) self.update_timer.start() slc.setExploded(exploded)
class Widget(QWidget): def __init__(self): QWidget.__init__(self) self.items = 0 # Example data self._data = { "Water": 24.5, "Electricity": 55.1, "Rent": 850.0, "Supermarket": 230.4, "Internet": 29.99, "Spätkauf": 21.85, "BVG Ticket": 60.0, "Coffee": 22.45, "Meetup": 0.0 } # Left self.table = QTableWidget() self.table.setColumnCount(2) self.table.setHorizontalHeaderLabels(["Description", "Quantity"]) self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) # Chart self.chart_view = QChartView() self.chart_view.setRenderHint(QPainter.Antialiasing) # Right self.description = QLineEdit() self.quantity = QLineEdit() self.add = QPushButton("Add") self.clear = QPushButton("Clear") self.quit = QPushButton("Quit") self.plot = QPushButton("Plot") # Disabling 'Add' button self.add.setEnabled(False) self.right = QVBoxLayout() self.right.setContentsMargins(10, 10, 10, 10) self.right.addWidget(QLabel("Description")) self.right.addWidget(self.description) self.right.addWidget(QLabel("Quantity")) self.right.addWidget(self.quantity) self.right.addWidget(self.add) self.right.addWidget(self.plot) self.right.addWidget(self.chart_view) self.right.addWidget(self.clear) self.right.addWidget(self.quit) # QWidget Layout self.layout = QHBoxLayout() self.layout.addWidget(self.table) self.layout.addLayout(self.right) # Set the layout to the QWidget self.setLayout(self.layout) # Signals and Slots self.add.clicked.connect(self.add_element) self.quit.clicked.connect(self.quit_application) self.plot.clicked.connect(self.plot_data) self.clear.clicked.connect(self.clear_table) self.description.textChanged[str].connect(self.check_disable) self.quantity.textChanged[str].connect(self.check_disable) # Fill example data self.fill_table() @Slot() def add_element(self): des = self.description.text() qty = self.quantity.text() self.table.insertRow(self.items) self.table.setItem(self.items, 0, QTableWidgetItem(des)) self.table.setItem(self.items, 1, QTableWidgetItem(qty)) self.description.setText("") self.quantity.setText("") self.items += 1 @Slot() def check_disable(self, s): if not self.description.text() or not self.quantity.text(): self.add.setEnabled(False) else: self.add.setEnabled(True) @Slot() def plot_data(self): # Get table information series = QPieSeries() for i in range(self.table.rowCount()): text = self.table.item(i, 0).text() number = float(self.table.item(i, 1).text()) series.append(text, number) chart = QChart() chart.addSeries(series) chart.legend().setAlignment(Qt.AlignLeft) self.chart_view.setChart(chart) @Slot() def quit_application(self): QApplication.quit() def fill_table(self, data=None): data = self._data if not data else data for desc, price in data.items(): self.table.insertRow(self.items) self.table.setItem(self.items, 0, QTableWidgetItem(desc)) self.table.setItem(self.items, 1, QTableWidgetItem(str(price))) self.items += 1 @Slot() def clear_table(self): self.table.setRowCount(0) self.items = 0
series2 = QPieSeries() series2.setName("Renewables") series2.append("Wood fuels", 319663) series2.append("Hydro power", 45875) series2.append("Wind power", 1060) series3 = QPieSeries() series3.setName("Others") series3.append("Nuclear energy", 238789) series3.append("Import energy", 37802) series3.append("Other", 32441) donut_breakdown = DonutBreakdownChart() donut_breakdown.setAnimationOptions(QChart.AllAnimations) donut_breakdown.setTitle("Total consumption of energy in Finland 2010") donut_breakdown.legend().setAlignment(Qt.AlignRight) donut_breakdown.add_breakdown_series(series1, Qt.red) donut_breakdown.add_breakdown_series(series2, Qt.darkGreen) donut_breakdown.add_breakdown_series(series3, Qt.darkBlue) window = QMainWindow() chart_view = QChartView(donut_breakdown) chart_view.setRenderHint(QPainter.Antialiasing) window.setCentralWidget(chart_view) available_geometry = window.screen().availableGeometry() size = available_geometry.height() * 0.75 window.resize(size, size * 0.8) window.show() sys.exit(app.exec_())