QScatterSeries相关内容

学习一下Qt Charts的柱状图部分。参考书籍:Qt5.9 C++开发指南
效果图
QScatterSeries相关内容
头文件

#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 createScatterSeries();

private slots:
    void on_rdoCircle_clicked();

    void on_rdoRectangle_clicked();

    void on_rdoStar_clicked();

    void on_btnScatterColor_clicked();

    void on_btnScatterBorderColor_clicked();

private:
    Ui::MainWindow *ui;

    QPainterPath m_starPath;    //保存星形路径
};

#endif // MAINWINDOW_H

cpp文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QScatterSeries>
#include <QValueAxis>
#include <QtMath>
#include <QColorDialog>
#include <QDebug>

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

    QStringList headText = QStringList() << QStringLiteral("产品价格") << QStringLiteral("空调(件)") << QStringLiteral("冰箱(件)") << QStringLiteral("洗衣机(件)") << QStringLiteral("总计(件)");
    ui->tableWidget->setHorizontalHeaderLabels(headText);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        int AirConditioner = ui->tableWidget->item(i, 1)->text().toInt();
        int Refrigerator   = ui->tableWidget->item(i, 2)->text().toInt();
        int WashingMachine = ui->tableWidget->item(i, 3)->text().toInt();
        int sum = AirConditioner + Refrigerator + WashingMachine;
        QTableWidgetItem *item = new QTableWidgetItem(QString::number(sum));
        ui->tableWidget->setItem(i, 4, item);
    }

    createScatterSeries();
}

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

void MainWindow::createScatterSeries()
{
    ui->chartViewScatter->setRenderHint(QPainter::Antialiasing);
    ui->chartViewScatter->chart()->setTitle(QStringLiteral("QScatterSeries演示"));
    ui->chartViewScatter->chart()->setTheme(QChart::ChartThemeBlueCerulean);
    //创建散点图序列,并添加数据
    QScatterSeries *scatterSeries[4];
    for (int i = 0; i < 4; i++)
    {
        scatterSeries[i] = new QScatterSeries;
        scatterSeries[i]->setMarkerSize(30.0);
    }
    for (int row = 0; row < 4; row++)
    {
        scatterSeries[0]->append((row + 1) * 1000, ui->tableWidget->item(row, 1)->text().toInt());
        scatterSeries[1]->append((row + 1) * 1000, ui->tableWidget->item(row, 2)->text().toInt());
        scatterSeries[2]->append((row + 1) * 1000, ui->tableWidget->item(row, 3)->text().toInt());
        scatterSeries[3]->append((row + 1) * 1000, ui->tableWidget->item(row, 4)->text().toInt());
    }
    //各物品设置散点样式:圆形
    scatterSeries[0]->setMarkerShape(QScatterSeries::MarkerShapeCircle);
    scatterSeries[1]->setMarkerShape(QScatterSeries::MarkerShapeCircle);
    scatterSeries[2]->setMarkerShape(QScatterSeries::MarkerShapeCircle);
    //平均分设置散点样式:星形
    m_starPath.moveTo(28, 15);
    for (int i = 1; i < 5; ++i) {
        m_starPath.lineTo(14 + 14 * qCos(0.8 * i * M_PI),
                        15 + 14 * qSin(0.8 * i * M_PI));
    }
    m_starPath.closeSubpath();
    QImage star(30, 30, QImage::Format_ARGB32);
    star.fill(Qt::transparent);
    QPainter painter(&star);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::yellow);
    painter.drawPath(m_starPath);
    scatterSeries[3]->setBrush(star);
    scatterSeries[3]->setPen(QColor(Qt::transparent));

    scatterSeries[0]->setName(QStringLiteral("空调"));
    scatterSeries[1]->setName(QStringLiteral("冰箱"));
    scatterSeries[2]->setName(QStringLiteral("洗衣机"));
    scatterSeries[3]->setName(QStringLiteral("总计"));
    for (int i = 0; i < 4; i++)
    {
        ui->chartViewScatter->chart()->addSeries(scatterSeries[i]);
    }
    //坐标轴
    QValueAxis *axisX = new QValueAxis;
    QValueAxis *axisY = new QValueAxis;
    axisX->setRange(0, 5000);
    axisX->setTickCount(6);
    axisX->setTitleText(QStringLiteral("产品价格(元)"));
    axisX->setLabelFormat("%d");
    axisX->setVisible(true);
    axisY->setRange(0, 300);
    axisY->setTitleText(QStringLiteral("销售数量(件)"));
    axisY->setLabelFormat("%d");
    axisY->setVisible(true);
    for (int i = 0; i < 4; i++)
    {
        ui->chartViewScatter->chart()->setAxisX(axisX, scatterSeries[i]);
        ui->chartViewScatter->chart()->setAxisY(axisY, scatterSeries[i]);
    }
    //图例
    ui->chartViewScatter->chart()->legend()->setAlignment(Qt::AlignRight);
    ui->chartViewScatter->chart()->legend()->setMarkerShape(QLegend::MarkerShapeFromSeries);
}

void MainWindow::on_rdoCircle_clicked()
{
    //各物品设置散点样式:圆形

    QList<QAbstractSeries *> lisSeries = ui->chartViewScatter->chart()->series();
    for (int i = 0; i < 3; i++)
    {
        QScatterSeries *series = dynamic_cast<QScatterSeries *>(lisSeries.at(i));
        series->setMarkerShape(QScatterSeries::MarkerShapeCircle);
        series->setBrush(Qt::green);
        series->setPen(QPen(Qt::white, 2));
    }
}

void MainWindow::on_rdoRectangle_clicked()
{
    //各物品设置散点样式:矩形
    QList<QAbstractSeries *> lisSeries = ui->chartViewScatter->chart()->series();
    for (int i = 0; i < 3; i++)
    {
        QScatterSeries *series = dynamic_cast<QScatterSeries *>(lisSeries.at(i));
        series->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
        series->setBrush(Qt::green);
        series->setPen(QPen(Qt::white, 2));
    }
}

void MainWindow::on_rdoStar_clicked()
{
    //各物品设置散点样式:星形
    QImage star(30, 30, QImage::Format_ARGB32);
    star.fill(Qt::transparent);
    QPainter painter(&star);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::green);
    painter.drawPath(m_starPath);

    QList<QAbstractSeries *> lisSeries = ui->chartViewScatter->chart()->series();
    for (int i = 0; i < 3; i++)
    {
        QScatterSeries *series = dynamic_cast<QScatterSeries *>(lisSeries.at(i));
        series->setBrush(star);
        series->setPen(QColor(Qt::transparent));
    }
}

void MainWindow::on_btnScatterColor_clicked()
{
    QColor color(Qt::white);
    QColorDialog dlg(color);
    if (dlg.exec() == QDialog::Accepted)
    {
        color = dlg.currentColor();
        QList<QAbstractSeries *> lisSeries = ui->chartViewScatter->chart()->series();
        for (int i = 0; i < 3; i++)
        {
            QScatterSeries *series = dynamic_cast<QScatterSeries *>(lisSeries.at(i));
            series->setBrush(color);
            series->setPen(QColor(Qt::white));
        }
    }
}

void MainWindow::on_btnScatterBorderColor_clicked()
{
    QColor color(Qt::white);
    QColorDialog dlg(color);
    if (dlg.exec() == QDialog::Accepted)
    {
        color = dlg.currentColor();
        QList<QAbstractSeries *> lisSeries = ui->chartViewScatter->chart()->series();
        for (int i = 0; i < 3; i++)
        {
            QScatterSeries *series = dynamic_cast<QScatterSeries *>(lisSeries.at(i));
            series->setPen(color);
        }
    }
}
QScatterSeries相关内容QScatterSeries相关内容 浮生卍流年 发布了228 篇原创文章 · 获赞 44 · 访问量 10万+ 私信 关注
上一篇:DataGrip安装及激活


下一篇:dialog、widget、mainwindow的区别