欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QAreaSeries相关内容

程序员文章站 2022-05-02 20:20:33
...

针对全国二月份下旬疫情数据,简单学习QAreaSeries相关内容。
效果图
QAreaSeries相关内容
头文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtCharts/QChart>
using namespace QtCharts;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    void createQAreaSeries();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

cpp文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QAreaSeries>
#include <QLineSeries>
#include <QValueAxis>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QStringList headText = QStringList() << QStringLiteral("日期") << QStringLiteral("新增确诊") << QStringLiteral("新增疑似")
                                         << QStringLiteral("新增治愈") << QStringLiteral("新增死亡")<< QStringLiteral("现有确诊") << QStringLiteral("累计确诊");
    ui->tableWidget->setHorizontalHeaderLabels(headText);
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        QTableWidgetItem *itemNowDefinite = ui->tableWidget->item(i, 5);
        itemNowDefinite->setBackground(QBrush(Qt::yellow));
        QTableWidgetItem *itemAllDefinite = ui->tableWidget->item(i, 6);
        itemAllDefinite->setBackground(QBrush(Qt::red));
    }

    createQAreaSeries();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::createQAreaSeries()
{
    //基本设置
    ui->charViewQArea->setRenderHint(QPainter::Antialiasing);
    ui->charViewQArea->chart()->setTitle(QStringLiteral("QAreaSeries演示"));
    ui->charViewQArea->chart()->setTheme(QChart::ChartThemeBlueCerulean);
    //创建折线序列
    QLineSeries *lsNowDefinite = new QLineSeries;
    QLineSeries *lsAllDefinite = new QLineSeries;
    lsNowDefinite->setName(QStringLiteral("现有确诊"));
    lsAllDefinite->setName(QStringLiteral("累计确诊"));
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        int date = ui->tableWidget->item(i, 0)->text().toInt();
        lsNowDefinite->append(date, ui->tableWidget->item(i, 5)->text().toInt());
        lsAllDefinite->append(date, ui->tableWidget->item(i, 6)->text().toInt());
    }
    //创建面积图序列
    QAreaSeries *areaSeries = new QAreaSeries(lsAllDefinite, lsNowDefinite);
    areaSeries->setName(QStringLiteral("累计治愈面积图"));
    areaSeries->setPointsVisible(true);
    ui->charViewQArea->chart()->addSeries(areaSeries);
    //创建坐标轴
    QValueAxis *axisX = new QValueAxis;
    axisX->setRange(20, 29);
    axisX->setTitleText(QStringLiteral("日期"));
    axisX->setTickCount(10);
    axisX->setLabelFormat("%d");
    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(35000, 80000);
    axisY->setTitleText(QStringLiteral("人数"));
    axisY->setLabelFormat("%d");
    ui->charViewQArea->chart()->setAxisX(axisX, areaSeries);
    ui->charViewQArea->chart()->setAxisY(axisY, areaSeries);
}
相关标签: Qt Charts