QLineSeries相关内容

荒凉一梦 提交于 2020-02-29 21:59:22

学习一下Qt Charts的折线图部分。参考书籍:Qt5.9 C++开发指南
效果图
在这里插入图片描述
头文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

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

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

public:
    //初始化界面数据
    void InitForm();
    //创建Chart
    void createChart();
    //初始化Chart,赋值数据
    void InitChart();

private slots:
    void on_rdoTop_clicked();

    void on_rdoBottom_clicked();

    void on_rdoLeft_clicked();

    void on_rdoRight_clicked();

    void on_chkLegendVisible_stateChanged(int arg1);

    void on_chkBgVisible_stateChanged(int arg1);

    void on_btnSetLegendFont_clicked();

    void on_btnSetLegendLabelColor_clicked();

    void on_lEditChartTitle_textChanged(const QString &arg1);

    void on_btnChartTitleFont_clicked();

    void on_sboxLeft_valueChanged(int arg1);

    void on_sboxTop_valueChanged(int arg1);

    void on_sboxRight_valueChanged(int arg1);

    void on_sboxBottom_valueChanged(int arg1);

    void on_comboBox_currentIndexChanged(int index);

    void on_comboBox_2_currentIndexChanged(int index);

    void on_rdoSinLine_clicked();

    void on_rdoCosLine_clicked();

    void on_rdoLineName_textChanged(const QString &arg1);

    void on_chkSeriesVisible_stateChanged(int arg1);

    void on_rdoDataPointVisible_stateChanged(int arg1);

    void on_sliderOpacity_valueChanged(int value);

    void on_btnLineColor_clicked();

    void on_chkDataPointLableVisible_stateChanged(int arg1);

    void on_btnDataPointLabelColor_clicked();

    void on_btnDataPointLabelFont_clicked();

    void on_rdoFormatY_clicked();

    void on_rdoFormatXY_clicked();

    void on_rdoAxisX_clicked();

    void on_rdoAxisY_clicked();

    void on_sboxMin_valueChanged(int arg1);

    void on_sboxMax_valueChanged(int arg1);

    void on_chkAxisVisible_stateChanged(int arg1);

    void on_lEditAxisTitle_textChanged(const QString &arg1);

    void on_chkAxisLabelVisible_stateChanged(int arg1);

    void on_lEditAxisLabelFormat_textChanged(const QString &arg1);

    void on_btnAxisTitleTextColor_clicked();

    void on_btnAxisTitleTextFont_clicked();

    void on_chkGridLineVisible_stateChanged(int arg1);

    void on_btnGridLineColor_clicked();

    void on_btnGridLinePen_clicked();

    void on_btnTickVisible_stateChanged(int arg1);

    void on_spinTickCount_valueChanged(int arg1);

    void on_btnTickLinePen_clicked();

    void on_btnTickLinePenColor_clicked();

    void on_chkMinorTickVisible_stateChanged(int arg1);

    void on_spinMinorTickCount_valueChanged(int arg1);

    void on_btnMinorTickLineColor_clicked();

    void on_btnMinorTickLinePen_clicked();

    //刷新
    void onRefresh(bool checked);
    //放大
    void onZoomIn(bool checked);
    //缩小
    void onZoomOut(bool checked);
    //原始大小
    void onOriginSize(bool checked);
    //区域放大
    void onAreaZoomIn(bool checked);
    //退出
    void onQuit(bool checked);

    void on_btnAxisLabelFont_clicked();

private:
    Ui::MainWindow *ui;

    QLineSeries *m_curSeries;   //当前操作的序列
    QValueAxis *m_curAxis;      //当前操作的坐标轴
};

#endif // MAINWINDOW_H

cpp文件

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

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

    InitForm();
    connect(ui->actRefresh, SIGNAL(triggered(bool)), this, SLOT(onRefresh(bool)));
    connect(ui->actZoomIn, SIGNAL(triggered(bool)), this, SLOT(onZoomIn(bool)));
    connect(ui->actZoomOut, SIGNAL(triggered(bool)), this, SLOT(onZoomOut(bool)));
    connect(ui->actOriginSize, SIGNAL(triggered(bool)), this, SLOT(onOriginSize(bool)));
    connect(ui->actQuit, SIGNAL(triggered(bool)), this, SLOT(onQuit(bool)));
    connect(ui->actAreaZoomIn, SIGNAL(triggered(bool)), this, SLOT(onAreaZoomIn(bool)));
}

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

void MainWindow::InitForm()
{
    //创建Chart
    createChart();
    //初始化Chart,赋值数据
    InitChart();
    //初始化部分界面,未全部初始化,相关设置代码见槽函数...
    QChart *chart = ui->chartView->chart();         //获取chart
    ui->lEditChartTitle->setText(chart->title());   //图表标题
    //图标边距
    QMargins margin = chart->margins();
    ui->sboxLeft->setValue(margin.left());
    ui->sboxTop->setValue(margin.top());
    ui->sboxRight->setValue(margin.right());
    ui->sboxBottom->setValue(margin.bottom());
    //坐标轴最大值,最小值
    QValueAxis *axisX = (QValueAxis *)ui->chartView->chart()->axisX();
    m_curAxis = axisX;
    ui->sboxMin->setValue(axisX->min());
    ui->sboxMax->setValue(axisX->max());
    m_curSeries = (QLineSeries *)ui->chartView->chart()->series().at(0);
}

void MainWindow::createChart()
{
    //添加Chart
    QChart *chart = new QChart();
    chart->setTitle(QStringLiteral("简单函数曲线"));
    chart->setTheme(QChart::ChartThemeBlueCerulean);
    ui->chartView->setChart(chart);
    ui->chartView->setRenderHint(QPainter::Antialiasing);
    //添加序列
    QLineSeries *series1 = new QLineSeries();
    QLineSeries *series2 = new QLineSeries();
    series1->setName(QStringLiteral("Sin曲线"));
    series2->setName(QStringLiteral("Cos曲线"));
    series1->setPen(QPen(Qt::red, 2, Qt::DotLine));
    series2->setPen(QPen(Qt::blue, 2, Qt::SolidLine));
    chart->addSeries(series1);
    chart->addSeries(series2);
    //创建坐标轴
    QValueAxis *axisX = new QValueAxis;
    axisX->setTitleText(QStringLiteral("角度"));
    axisX->setRange(0, 360);
    axisX->setLabelFormat("%d");
    axisX->setTickCount(10);        //设置主分隔数:10条线,形成九个网格
    axisX->setMinorTickCount(3);    //设置小分隔数:4条线,与主分隔线形成5个网格
    QValueAxis *axisY = new QValueAxis;
    axisY->setTitleText(QStringLiteral("值"));
    axisY->setRange(-2, 2);
    axisY->setLabelFormat("%.1f");
    axisY->setTickCount(5);
    axisY->setMinorTickCount(4);
    //添加坐标轴
    chart->setAxisX(axisX, series1);
    chart->setAxisY(axisY, series1);
    chart->setAxisX(axisX, series2);
    chart->setAxisY(axisY, series2);
    //设置图例字体
    QFont font("SimSun-ExtB", 9);
    ui->chartView->chart()->legend()->setFont(font);
}

void MainWindow::InitChart()
{
    QLineSeries *series1 = (QLineSeries *)ui->chartView->chart()->series().at(0);
    QLineSeries *series2 = (QLineSeries *)ui->chartView->chart()->series().at(1);
    for (int i = 0; i <= 360; i++)
    {
        series1->append(i, qSin(qDegreesToRadians(double(i))));
        series2->append(i, qCos(qDegreesToRadians(double(i))));
    }
}

void MainWindow::on_rdoTop_clicked()
{
    //图例上
    ui->chartView->chart()->legend()->setAlignment(Qt::AlignTop);
}

void MainWindow::on_rdoBottom_clicked()
{
    //图例下
    ui->chartView->chart()->legend()->setAlignment(Qt::AlignBottom);
}

void MainWindow::on_rdoLeft_clicked()
{
    //图例左
    ui->chartView->chart()->legend()->setAlignment(Qt::AlignLeft);
}

void MainWindow::on_rdoRight_clicked()
{
    //图例右
    ui->chartView->chart()->legend()->setAlignment(Qt::AlignRight);
}

void MainWindow::on_chkLegendVisible_stateChanged(int arg1)
{//设置图例:图例可见
    if (Qt::Checked == arg1)
        ui->chartView->chart()->legend()->setVisible(true);
    else
        ui->chartView->chart()->legend()->setVisible(false);
}

void MainWindow::on_chkBgVisible_stateChanged(int arg1)
{//设置图例:背景显示
    if (Qt::Checked == arg1)
        ui->chartView->chart()->legend()->setBackgroundVisible(true);
    else
        ui->chartView->chart()->legend()->setBackgroundVisible(false);
}

void MainWindow::on_btnSetLegendFont_clicked()
{//设置图例:字体
    bool ok = false;
    QFont initialFont = ui->chartView->chart()->titleFont();
    QFont font = QFontDialog::getFont(&ok, initialFont);
    qDebug() << ok;
    if (ok)
        ui->chartView->chart()->legend()->setFont(font);
}

void MainWindow::on_btnSetLegendLabelColor_clicked()
{//设置图例:标签颜色
    //setLabelColor函数会:改变图例颜色的同时,改变图例边框颜色
    QColor initialColor = ui->chartView->chart()->legend()->labelColor();
    initialColor = QColorDialog::getColor(initialColor);
    ui->chartView->chart()->legend()->setLabelColor(initialColor);
//    //注意:setColor和setBorderColor设置的是背景颜色
//    ui->chartView->chart()->legend()->setBackgroundVisible(true);//背景显示
//    ui->chartView->chart()->legend()->setColor(/*borderColor*/Qt::red);//背景颜色
//    ui->chartView->chart()->legend()->setBorderColor(/*borderColor*/Qt::black);//背景边框颜色
}

void MainWindow::on_lEditChartTitle_textChanged(const QString &arg1)
{//设置图表:标题内容
    ui->chartView->chart()->setTitle(arg1);
}

void MainWindow::on_btnChartTitleFont_clicked()
{//设置图表:标题字体
    bool ok = false;
    QFont initialFont = ui->chartView->chart()->titleFont();
    QFont font = QFontDialog::getFont(&ok, initialFont);
    qDebug() << ok;
    if (ok)
        ui->chartView->chart()->setTitleFont(font);
}

void MainWindow::on_sboxLeft_valueChanged(int arg1)
{//设置图表:左边距
    QMargins margins = ui->chartView->chart()->margins();
    margins.setLeft(arg1);
    ui->chartView->chart()->setMargins(margins);
}

void MainWindow::on_sboxTop_valueChanged(int arg1)
{//设置图表:上边距
    QMargins margins = ui->chartView->chart()->margins();
    margins.setTop(arg1);
    ui->chartView->chart()->setMargins(margins);
}

void MainWindow::on_sboxRight_valueChanged(int arg1)
{//设置图表:右边距
    QMargins margins = ui->chartView->chart()->margins();
    margins.setRight(arg1);
    ui->chartView->chart()->setMargins(margins);
}

void MainWindow::on_sboxBottom_valueChanged(int arg1)
{//设置图表:下边距
    QMargins margins = ui->chartView->chart()->margins();
    margins.setBottom(arg1);
    ui->chartView->chart()->setMargins(margins);
}

void MainWindow::on_comboBox_currentIndexChanged(int index)
{//设置图表:动画效果
    ui->chartView->chart()->setAnimationOptions((QChart::AnimationOptions)index);
}

void MainWindow::on_comboBox_2_currentIndexChanged(int index)
{//设置图表:背景主题效果
    ui->chartView->chart()->setTheme((QChart::ChartTheme)index);
}

void MainWindow::on_rdoSinLine_clicked()
{//设置序列:当前序列
    m_curSeries = (QLineSeries *)ui->chartView->chart()->series().at(0);
}

void MainWindow::on_rdoCosLine_clicked()
{//设置序列:当前序列
    m_curSeries = (QLineSeries *)ui->chartView->chart()->series().at(1);
}

void MainWindow::on_rdoLineName_textChanged(const QString &arg1)
{//设置序列:序列名称
    m_curSeries->setName(arg1);
}

void MainWindow::on_chkSeriesVisible_stateChanged(int arg1)
{//设置序列:序列可见
    if (Qt::Checked == arg1)
        m_curSeries->setVisible(true);
    else
        m_curSeries->setVisible(false);
}

void MainWindow::on_rdoDataPointVisible_stateChanged(int arg1)
{//设置序列:数据点可见
    if (Qt::Checked == arg1)
        m_curSeries->setPointsVisible(true);
    else
        m_curSeries->setPointsVisible(false);
}
void MainWindow::on_sliderOpacity_valueChanged(int value)
{//设置序列:序列(曲线)透明度
    m_curSeries->setOpacity(value / 100.0);
}

void MainWindow::on_btnLineColor_clicked()
{//设置序列:序列(曲线)颜色
    QPen pen = m_curSeries->pen();
    QColor initialColor = pen.color();
    initialColor = QColorDialog::getColor(initialColor);
    pen.setColor(initialColor);
    m_curSeries->setPen(pen);
}

void MainWindow::on_chkDataPointLableVisible_stateChanged(int arg1)
{//设置序列:数据点标签可见
    if (Qt::Checked == arg1)
        m_curSeries->setPointLabelsVisible(true);
    else
        m_curSeries->setPointLabelsVisible(false);
}

void MainWindow::on_btnDataPointLabelColor_clicked()
{//设置序列:数据点 标签颜色
    QColor initialColor = m_curSeries->pointLabelsColor();
    initialColor = QColorDialog::getColor(initialColor);
    m_curSeries->setPointLabelsColor(initialColor);
}

void MainWindow::on_btnDataPointLabelFont_clicked()
{//设置序列:数据点 标签字体
    QFont initialFont = m_curSeries->pointLabelsFont();
    bool ok = false;
    QFont font = QFontDialog::getFont(&ok, initialFont);
    if (ok)
        m_curSeries->setPointLabelsFont(font);
}

void MainWindow::on_rdoFormatY_clicked()
{//设置序列:数据点 标签格式 @yPoint
    m_curSeries->setPointLabelsFormat(ui->rdoFormatY->text());
}

void MainWindow::on_rdoFormatXY_clicked()
{//设置序列:数据点 标签格式 (@xPoint,@yPoint)
    m_curSeries->setPointLabelsFormat(ui->rdoFormatXY->text());
}

void MainWindow::on_rdoAxisX_clicked()
{//设置坐标轴:选择当前操作的坐标轴
    m_curAxis = (QValueAxis *)ui->chartView->chart()->axisX();
}

void MainWindow::on_rdoAxisY_clicked()
{//设置坐标轴:选择当前操作的坐标轴
    m_curAxis = (QValueAxis *)ui->chartView->chart()->axisY();
}

void MainWindow::on_sboxMin_valueChanged(int arg1)
{//设置坐标轴:坐标轴最小值改变
    m_curAxis->setMin(arg1);
}

void MainWindow::on_sboxMax_valueChanged(int arg1)
{//设置坐标轴:坐标轴最大值改变
    if (m_curAxis->max() != arg1)
        m_curAxis->setMax(arg1);
}

void MainWindow::on_chkAxisVisible_stateChanged(int arg1)
{//设置坐标轴:轴可见
    if (Qt::Checked == arg1)
        m_curAxis->setVisible(true);
    else
        m_curAxis->setVisible(false);
}

void MainWindow::on_lEditAxisTitle_textChanged(const QString &arg1)
{//设置坐标轴:轴标题设置
    m_curAxis->setTitleText(arg1);
}

void MainWindow::on_chkAxisLabelVisible_stateChanged(int arg1)
{//设置坐标轴:轴标签可见
    if (Qt::Checked == arg1)
        m_curAxis->setLabelsVisible(true);
    else
        m_curAxis->setLabelsVisible(false);
}

void MainWindow::on_lEditAxisLabelFormat_textChanged(const QString &arg1)
{//设置坐标轴:轴刻度标签 标签格式
    m_curAxis->setLabelFormat(arg1);
}

void MainWindow::on_btnAxisTitleTextColor_clicked()
{//设置坐标轴:轴刻度标签 文字颜色
    QColor initialColor = m_curAxis->labelsColor();
    initialColor = QColorDialog::getColor(initialColor);
    m_curAxis->setLabelsColor(initialColor);
}

void MainWindow::on_btnAxisTitleTextFont_clicked()
{//设置坐标轴:轴刻度标题 设置字体
    QFont initialFont = m_curAxis->titleFont();
    bool ok = false;
    QFont font = QFontDialog::getFont(&ok, initialFont);
    if (ok)
        m_curAxis->setTitleFont(font);
}

void MainWindow::on_btnAxisLabelFont_clicked()
{//设置坐标轴:轴刻度标签 设置字体
    QFont initialFont = m_curAxis->labelsFont();
    bool ok = false;
    QFont font = QFontDialog::getFont(&ok, initialFont);
    if (ok)
        m_curAxis->setLabelsFont(font);
}

void MainWindow::on_chkGridLineVisible_stateChanged(int arg1)
{//设置网格:网格线可见
    if (Qt::Checked == arg1)
        m_curAxis->setGridLineVisible(true);
    else
        m_curAxis->setGridLineVisible(false);
}

void MainWindow::on_btnGridLineColor_clicked()
{//设置网格:网格线颜色
    QColor initialColor = m_curAxis->gridLineColor();
    initialColor = QColorDialog::getColor(initialColor);
    m_curAxis->setGridLineColor(initialColor);
}

void MainWindow::on_btnGridLinePen_clicked()
{//设置网格:网格线画笔
    //QPen(const QBrush &brush, qreal width, Qt::PenStyle style = Qt::SolidLine,
    //Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJoin)
}

void MainWindow::on_btnTickVisible_stateChanged(int arg1)
{//设置网格:主刻度 主刻度可见
    if (Qt::Checked == arg1)
        m_curAxis->setLineVisible(true);
    else
        m_curAxis->setLineVisible(false);
}

void MainWindow::on_spinTickCount_valueChanged(int arg1)
{//设置网格:主刻度 主刻度个数
    m_curAxis->setTickCount(arg1);
}

void MainWindow::on_btnTickLinePen_clicked()
{//设置网格:主刻度 主刻度画笔设置
    //QPen(const QBrush &brush, qreal width, Qt::PenStyle style = Qt::SolidLine,
    //Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJoin)
}

void MainWindow::on_btnTickLinePenColor_clicked()
{//设置网格:主刻度 主刻度画笔颜色设置
    QColor initialColor = m_curAxis->linePenColor();
    initialColor = QColorDialog::getColor(initialColor);
    m_curAxis->setLinePenColor(initialColor);
}

void MainWindow::on_chkMinorTickVisible_stateChanged(int arg1)
{//设置网格:次刻度 此刻度可见
    if (Qt::Checked == arg1)
        m_curAxis->setMinorGridLineVisible(true);
    else
        m_curAxis->setMinorGridLineVisible(false);
}

void MainWindow::on_spinMinorTickCount_valueChanged(int arg1)
{//设置网格:次刻度 此刻度个数
    m_curAxis->setMinorTickCount(arg1);
}

void MainWindow::on_btnMinorTickLineColor_clicked()
{//设置网格:次刻度 此刻度线条颜色
    QColor initialColor = m_curAxis->minorGridLineColor();
    initialColor = QColorDialog::getColor(initialColor);
    m_curAxis->setMinorGridLineColor(initialColor);
}

void MainWindow::on_btnMinorTickLinePen_clicked()
{//设置网格:次刻度 次刻度画笔设置
    //QPen(const QBrush &brush, qreal width, Qt::PenStyle style = Qt::SolidLine,
    //Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJoin)
}

void MainWindow::onRefresh(bool checked)
{//刷新
    ui->chartView->chart()->update();
}

void MainWindow::onZoomIn(bool checked)
{//放大
    ui->chartView->chart()->zoom(1.2);
}

void MainWindow::onZoomOut(bool checked)
{//缩小
    ui->chartView->chart()->zoom(0.8);
}

void MainWindow::onOriginSize(bool checked)
{//恢复原始大小
    ui->chartView->chart()->zoomReset();
}

void MainWindow::onAreaZoomIn(bool checked)
{//区域放大
    //ui->chartView->chart()->zoomIn();
}

void MainWindow::onQuit(bool checked)
{//退出
    QApplication::quit();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!