def __init__(self): QMainWindow.__init__(self) self.series = QtCharts.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 = QtCharts.QChart() self.chart.legend().hide() self.chart.addSeries(self.series) self.chart.createDefaultAxes() self.chart.setTitle("Simple line chart example") self.chartView = QtCharts.QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)
def create_areachart(self): chart = QtCharts.QChart() chart.setTitle("Area Chart") # The lower series initialized to zero values lower_series = None name = "Series " for i in range(len(self.data_table)): upper_series = QtCharts.QLineSeries(chart) for j in range(len(self.data_table[i])): data = self.data_table[i][j] if lower_series: points = lower_series.pointsVector() y_value = points[i].y() + data[0].y() upper_series.append(QPointF(j, y_value)) else: upper_series.append(QPointF(j, data[0].y())) area = QtCharts.QAreaSeries(upper_series, lower_series) area.setName("{}{}".format(name, i)) chart.addSeries(area) lower_series = upper_series chart.createDefaultAxes() chart.axisX().setRange(0, self.value_count - 1) chart.axisY().setRange(0, self.value_max) # Add space to label to add space between labels and axis chart.axisY().setLabelFormat("%.1f ") return chart
def __init__(self): QMainWindow.__init__(self) self.series = QtCharts.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 = QtCharts.QChart() self.chart.addSeries(self.series) self.chart.setTitle('Simple piechart example') self.chart.legend().hide() self.chartView = QtCharts.QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)
def __init__(self): QMainWindow.__init__(self) self.series = QtCharts.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 = QtCharts.QChart() self.chart.addSeries(self.series) self.chart.legend().hide() self.chart.setTitle("Logarithmic axis example") self.axisX = QtCharts.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 = QtCharts.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 = QtCharts.QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)
def __init__(self, parent = None): super(View, self).__init__(parent) self.setScene(QGraphicsScene(self)) self.setDragMode(QGraphicsView.NoDrag) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) # Chart self._chart = QtCharts.QChart() self._chart.setMinimumSize(640, 480) self._chart.setTitle("Hover the line to show callout. Click the line " "to make it stay") self._chart.legend().hide() self.series = QtCharts.QLineSeries() self.series.append(1, 3) self.series.append(4, 5) self.series.append(5, 4.5) self.series.append(7, 1) self.series.append(11, 2) self._chart.addSeries(self.series) self.series2 = QtCharts.QSplineSeries() self.series2.append(1.6, 1.4) self.series2.append(2.4, 3.5) self.series2.append(3.7, 2.5) self.series2.append(7, 4) self.series2.append(10, 2) self._chart.addSeries(self.series2) self._chart.createDefaultAxes() self._chart.setAcceptHoverEvents(True) self.setRenderHint(QPainter.Antialiasing) self.scene().addItem(self._chart) self._coordX = QGraphicsSimpleTextItem(self._chart) self._coordX.setPos( self._chart.size().width()/2 - 50, self._chart.size().height()) self._coordX.setText("X: ") self._coordY = QGraphicsSimpleTextItem(self._chart) self._coordY.setPos( self._chart.size().width()/2 + 50, self._chart.size().height()) self._coordY.setText("Y: ") self._callouts = [] self._tooltip = Callout(self._chart) self.series.clicked.connect(self.keepCallout) self.series.hovered.connect(self.tooltip) self.series2.clicked.connect(self.keepCallout) self.series2.hovered.connect(self.tooltip) self.setMouseTracking(True)
def __init__(self): QWidget.__init__(self) self.setMinimumSize(800, 600) self.donuts = [] self.chart_view = QtCharts.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(QtCharts.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 __init__(self, parent=None): super(DonutBreakdownChart, self).__init__(QtCharts.QChart.ChartTypeCartesian, parent, Qt.WindowFlags()) self.main_series = QtCharts.QPieSeries() self.main_series.setPieSize(0.7) self.addSeries(self.main_series)
def createPieChart(self): chart = QtCharts.QChart() chart.setTitle("Pie chart") series = QtCharts.QPieSeries(chart) for data in self.data_table[0]: slc = series.append(data[1], data[0].y()) if data == self.data_table[0][0]: # Show the first slice exploded with label slc.setLabelVisible() slc.setExploded() slc.setExplodeDistanceFactor(0.5) series.setPieSize(0.4) chart.addSeries(series) return chart
def create_scatterchart(self): chart = QtCharts.QChart() chart.setTitle("Scatter chart") name = "Series " for i, lst in enumerate(self.data_table): series = QtCharts.QScatterSeries(chart) for data in lst: series.append(data[0]) series.setName("{}{}".format(name, i)) chart.addSeries(series) chart.createDefaultAxes() chart.axisX().setRange(0, self.value_max) chart.axisY().setRange(0, self.value_count) # Add space to label to add space between labels and axis chart.axisY().setLabelFormat("%.1f ") return chart
def createBarChart(self): chart = QtCharts.QChart() chart.setTitle("Bar chart") series = QtCharts.QStackedBarSeries(chart) for i in range(len(self.data_table)): barset = QtCharts.QBarSet("Bar set {}".format(i)) for data in self.data_table[i]: barset.append(data[0].y()) series.append(barset) chart.addSeries(series) chart.createDefaultAxes() chart.axisY().setRange(0, self.value_max * 2) # Add space to label to add space between labels and axis chart.axisY().setLabelFormat("%.1f ") return chart
def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle('Memory Usage') memoryUsage = getMemoryUsage() if len(memoryUsage) > 5: memoryUsage = memoryUsage[0:4] self.series = QtCharts.QPieSeries() for item in memoryUsage: self.series.append(item[0], item[1]) slice = self.series.slices()[0] slice.setExploded() slice.setLabelVisible() self.chart = QtCharts.QChart() self.chart.addSeries(self.series) self.chartView = QtCharts.QChartView(self.chart) self.setCentralWidget(self.chartView)
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 = QtCharts.QChart() self.chart.setAnimationOptions(QtCharts.QChart.AllAnimations) self.series = QtCharts.QLineSeries() self.series.setName("Line 1") self.mapper = QtCharts.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 = QtCharts.QLineSeries() self.series.setName("Line 2") self.mapper = QtCharts.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 = QtCharts.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)
def setup_donuts(self): for i in range(self.donut_count): donut = QtCharts.QPieSeries() slccount = randrange(3, 6) for j in range(slccount): value = randrange(100, 200) slc = QtCharts.QPieSlice(str(value), value) slc.setLabelVisible(True) slc.setLabelColor(Qt.white) slc.setLabelPosition(QtCharts.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 __init__(self, parent): QWidget.__init__(self, parent) self.charts = [] self.ui = ui() self.list_count = 3 self.value_max = 10 self.value_count = 7 self.data_table = self.generate_random_data(self.list_count, self.value_max, self.value_count) self.ui.setupUi(self) self.populate_themebox() self.populate_animationbox() self.populate_legendbox() # Area Chart chart_view = QtCharts.QChartView(self.create_areachart()) self.ui.gridLayout.addWidget(chart_view, 1, 0) self.charts.append(chart_view) # Pie Chart chart_view = QtCharts.QChartView(self.createPieChart()) chart_view.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) self.ui.gridLayout.addWidget(chart_view, 1, 1) self.charts.append(chart_view) # Line Chart chart_view = QtCharts.QChartView(self.createLineChart()) self.ui.gridLayout.addWidget(chart_view, 1, 2) self.charts.append(chart_view) # Bar Chart chart_view = QtCharts.QChartView(self.createBarChart()) self.ui.gridLayout.addWidget(chart_view, 2, 0) self.charts.append(chart_view) # Spline Chart chart_view = QtCharts.QChartView(self.createSplineChart()) self.ui.gridLayout.addWidget(chart_view, 2, 1) self.charts.append(chart_view) # Scatter Chart chart_view = QtCharts.QChartView(self.create_scatterchart()) self.ui.gridLayout.addWidget(chart_view, 2, 2) self.charts.append(chart_view) # Set defaults self.ui.antialiasCheckBox.setChecked(True) # Set the colors from the light theme as default ones pal = qApp.palette() pal.setColor(QPalette.Window, QColor(0xf0f0f0)) pal.setColor(QPalette.WindowText, QColor(0x404044)) qApp.setPalette(pal) self.updateUI()
def __init__(self, device): super(MainWindow, self).__init__() self.series = QtCharts.QLineSeries() self.chart = QtCharts.QChart() self.chart.addSeries(self.series) self.axisX = QtCharts.QValueAxis() self.axisX.setRange(0, sampleCount) self.axisX.setLabelFormat("%g") self.axisX.setTitleText("Samples") self.axisY = QtCharts.QValueAxis() self.axisY.setRange(-1, 1) self.axisY.setTitleText("Audio level") self.chart.setAxisX(self.axisX, self.series) self.chart.setAxisY(self.axisY, self.series) self.chart.legend().hide() self.chart.setTitle("Data from the microphone ({})".format( device.deviceName())) formatAudio = QAudioFormat() formatAudio.setSampleRate(8000) formatAudio.setChannelCount(1) formatAudio.setSampleSize(8) formatAudio.setCodec("audio/pcm") formatAudio.setByteOrder(QAudioFormat.LittleEndian) formatAudio.setSampleType(QAudioFormat.UnSignedInt) self.audioInput = QAudioInput(device, formatAudio, self) self.ioDevice = self.audioInput.start() self.ioDevice.readyRead.connect(self._readyRead) self.chartView = QtCharts.QChartView(self.chart) self.setCentralWidget(self.chartView) self.buffer = [QPointF(x, 0) for x in range(sampleCount)] self.series.append(self.buffer)
def __init__(self): QMainWindow.__init__(self) set0 = QtCharts.QBarSet("Jane") set1 = QtCharts.QBarSet("John") set2 = QtCharts.QBarSet("Axel") set3 = QtCharts.QBarSet("Mary") set4 = QtCharts.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 = QtCharts.QPercentBarSeries() series.append(set0) series.append(set1) series.append(set2) series.append(set3) series.append(set4) chart = QtCharts.QChart() chart.addSeries(series) chart.setTitle("Simple percentbarchart example") chart.setAnimationOptions(QtCharts.QChart.SeriesAnimations) categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"] axis = QtCharts.QBarCategoryAxis() axis.append(categories) chart.createDefaultAxes() chart.setAxisX(axis, series) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chart_view = QtCharts.QChartView(chart) chart_view.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(chart_view)
def __init__(self): QMainWindow.__init__(self) low = QtCharts.QBarSet("Min") high = QtCharts.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 = QtCharts.QStackedBarSeries() series.append(low) series.append(high) chart = QtCharts.QChart() chart.addSeries(series) chart.setTitle("Temperature records in celcius") chart.setAnimationOptions(QtCharts.QChart.SeriesAnimations) categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] axisX = QtCharts.QBarCategoryAxis() axisX.append(categories) axisX.setTitleText("Month") chart.addAxis(axisX, Qt.AlignBottom) axisY = QtCharts.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 = QtCharts.QChartView(chart) chart_view.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(chart_view)
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 add_barset(self): series_count = self.series.count() bar_set = QtCharts.QBarSet("set {}".format(series_count)) delta = series_count * 0.1 bar_set.append([1 + delta, 2 + delta, 3 + delta, 4 + delta]) self.series.append(bar_set)
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()
marker.setVisible(False) else: # modify markers from breakdown series marker.setLabel("{} {:.2f}%".format( marker.slice().label(), marker.slice().percentage() * 100, 0)) marker.setFont(QFont("Arial", 8)) if __name__ == "__main__": app = QApplication(sys.argv) # Graph is based on data of: # 'Total consumption of energy increased by 10 per cent in 2010' # Statistics Finland, 13 December 2011 # http://www.stat.fi/til/ekul/2010/ekul_2010_2011-12-13_tie_001_en.html series1 = QtCharts.QPieSeries() series1.setName("Fossil fuels") series1.append("Oil", 353295) series1.append("Coal", 188500) series1.append("Natural gas", 148680) series1.append("Peat", 94545) series2 = QtCharts.QPieSeries() series2.setName("Renewables") series2.append("Wood fuels", 319663) series2.append("Hydro power", 45875) series2.append("Wind power", 1060) series3 = QtCharts.QPieSeries() series3.setName("Others") series3.append("Nuclear energy", 238789)