def __init__(self): QMainWindow.__init__(self) self.set0 = QtCharts.QBarSet("Jane") self.set1 = QtCharts.QBarSet("John") self.set2 = QtCharts.QBarSet("Axel") self.set3 = QtCharts.QBarSet("Mary") self.set4 = QtCharts.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 = QtCharts.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 = QtCharts.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 = QtCharts.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 = QtCharts.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 = QtCharts.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 = QtCharts.QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)
def __init__(self, parent=None): super(MainWidget, self).__init__(parent) self.chart = QtCharts.QChart() self.series = QtCharts.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 = QtCharts.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()