使用 QtChart 的简单条形图

Simple Bar chart with QtChart

我喜欢用 QChart 创建一个简单的条形图。在吧里找了很多系列教程,像这样https://codeloop.org/qt5-tutorial-creating-barchart-with-qtchart/。但我不需要系列,每个月只需要一个带有值的栏,如下所示:

在哪里可以找到此类图表的教程?

谢谢

我找到了一种方法。这是使用列表中的数据创建图表的代码。

def show_reports_by_year(self):
    # get the data
    year_list = self.get_cases_by_year()
    years = []
    reports = []
    for year in year_list:
        years.append(year[0])
        reports.append(year[1])

    # create the chart
    chart = QChart()
    chart.setAnimationOptions(QChart.SeriesAnimations)
    chart.setTheme(QChart.ChartThemeBlueIcy)
    
    # set font for chart title
    font = QFont()
    font.setPixelSize(20)
    chart.setTitleFont(font)
    chart.setTitle("Cases per year")

    #data to series and put to chart
    cases_max = []
    i = 0
    for year in year_list:
        series = QBarSeries()
        curr_set = "set" + str(i)
        curr_set = QBarSet(str(year[0]))
        curr_set << int(year[1])
        series.append(curr_set)
        series.setLabelsVisible(True)
        series.labelsPosition()
        chart.addSeries(series)
        cases_max.append(int(year[1]))

        i += 1

    #create axis
    axisX = QBarCategoryAxis()
    axisX.setLabelsVisible(True)
    axisX.append(years)

    axisY = QValueAxis()
    axisY.setLabelsVisible(True)
    axisY.setMin(0)
    axisY.setMax(max(reports))
    axisY.setLabelFormat("%.0f")
    axisY.setTitleText("Reports")

    # bild the chart
    chart.createDefaultAxes()
    chart.legend().hide()
    chart.setAxisX(axisX)
    chart.setAxisY(axisY)

    # create view
    chartview = QChartView(chart)
    vbox = QVBoxLayout()
    vbox.addWidget(chartview)

    # put view to qt grid layout
    self.ui.gridLayout.addWidget(chartview,0,0)