基础概念-Qt QCustomPlot 介绍

文章目录

前言

二、QCustomPlot常用功能介绍 

        1.坐标轴设置 

        2.曲线图绘制

        3.散点图绘制

        4.图例设置

         5.图例设置

        6.高级定制

4. QCustomPlot与QChart库的对比

4.1 难易程度

4.2 功能特点

4.3 可定制性和扩展性

总结


该库支持各种类型的图表,如曲线图、散点图、柱状图、饼图等等,而且用户还可以很容易地进行自定义设置和修改。QCustomPlot库有丰富的文档资料和示例代码,非常易于学习和使用。

一、部署QCustomplot

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

第一步,从QCustomPlot的官网 (https://www.qcustomplot.com/)下载最新版本的源代码,并解压到本地。

第二步,将QCustomPlot添加至自己的项目中。

  • 在Qt Creator中打开你的项目,在工程目录中新建一个文件夹,例如 named as lib
  • 复制 QCustomPlot 库中的 qcustomplot.h 和 qcustomplot.cpp 文件至该文件夹中。
  • 在Qt Creator 中添加外部库。

第三步,在项目中创建一个QCustomPlot窗口。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"

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

    // 创建一个QCustomPlot窗口
    QCPCurve *curve = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
    ui->customPlot->addPlottable(curve);

    // 设置曲线颜色
    curve->setPen(QPen(Qt::blue));

    // 设置坐标轴范围
    ui->customPlot->xAxis->setRange(-10, 10);
    ui->customPlot->yAxis->setRange(0, 100);

    // 添加曲线数据并绘制
    QVector<QPointF> data;
    for (double x=-10; x<10; x+=0.1)
    {
        double y = qSin(x) * qCos(x*x);
        data.append(QPointF(x, y));
    }
    curve->setData(data);

    // 显示绘制结果
    ui->customPlot->replot();
}

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

二、QCustomPlot常用功能介绍 

        1.坐标轴设置 

通过设置坐标轴的范围、坐标轴名称、坐标轴刻度等信息来控制图表的展示。

// X轴坐标范围
ui->customPlot->xAxis->setRange(min,max);
// Y轴坐标范围
ui->customPlot->yAxis->setRange(min,max);
// X轴标签
ui->customPlot->xAxis->setLabel("X轴");
// Y轴标签
ui->customPlot->yAxis->setLabel("Y轴");
// X轴网格线设置
ui->customPlot->xAxis->setGrid(true);
// Y轴网格线设置
ui->customPlot->yAxis->setGrid(true);

        2.曲线图绘制

 QCustomPlot支持一种基于QCPCurve的曲线图形式,下面演示如何通过添加曲线数据和调整坐标轴来绘制一张正弦曲线图。

// 创建一个QCPCurve类型对象,作为绘制曲线的容器。
QCPCurve *curve = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);

// 设置曲线样式及颜色
curve->setPen(QPen(Qt::red));
curve->setLineStyle(QCPCurve::lsLine);

// 添加曲线数据
QVector<double> xData(n), yData(n);
for (int i=0; i<n; ++i)
{
  xData[i] = i;
  yData[i] = qSin(i/10.0);
}
curve->setData(xData, yData);

// 设置X轴范围
ui->customPlot->xAxis->setRange(0, n);
// 设置Y轴范围
ui->customPlot->yAxis->setRange(-1, 1);

// 显示绘制结果
ui->customPlot->replot();

        3.散点图绘制

 QCustomPlot还可绘制散点图。下面演示如何通过添加散点数据来绘制一张散点图。

// 创建一个QCPGraph类型对象,作为绘制散点的容器。
QCPGraph *graph = ui->customPlot->addGraph();

// 设置散点样式及颜色
graph->setLineStyle(QCPGraph::lsNone);
graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));

// 添加散点数据
QVector<double> xData(n), yData(n);
for (int i=0; i<n; ++i)
{
  xData[i] = rand() % 100;
  yData[i] = rand() % 100;
}
graph->setData(xData, yData);

// 设置X轴范围
ui->customPlot->xAxis->setRange(0, 100);
// 设置Y轴范围
ui->customPlot->yAxis->setRange(0, 100);

// 显示绘制结果
ui->customPlot->replot();

        4.图例设置

通过QCustomPlot的legend属性,可以对图例进行控制。下面演示如何添加一张包含曲线图和散点图的图例。

// 添加曲线
QCPCurve *curve = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
ui->customPlot->addPlottable(curve);
curve->setPen(QPen(Qt::blue));
QVector<QPointF> data;
for (double x=-10; x<10; x+=0.1)
{
    double y = qSin(x) * qCos(x*x);
    data.append(QPointF(x, y));
}
curve->setData(data);

// 添加散点
QCPGraph *graph = ui->customPlot->addGraph();
graph->setLineStyle(QCPGraph::lsNone);
graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));
QVector<double> xData(n), yData(n);
for (int i=0; i<n; ++i)
{
    xData[i] = rand() % 100;
    yData[i] = rand() % 100;
}
graph->setData(xData, yData);
graph->setName("散点图");

// 添加图例
ui->customPlot->legend->setVisible(true);
ui->customPlot->legend->setFont(QFont("Helvetica", 9));
ui->customPlot->legend->setBrush(QBrush(QColor(255,255,255,230)));
ui->customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignRight);
ui->customPlot->legend->addItem(new QCPPlottableLegendItem(ui->customPlot->legend, curve));
ui->customPlot->legend->addItem(new QCPPlottableLegendItem(ui->customPlot->legend, graph));

// 显示绘制结果
ui->customPlot->replot();

         5.图例设置

 为了让用户能够更加方便地查看曲线图的数据,QCustomPlot库提供了鼠标交互的机制。例如,我们可以通过以下代码启用鼠标选区功能:

plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

        6.高级定制

 QCustomPlot库提供了丰富的自定义绘制函数和事件机制,可以让用户实现更加灵活的需求。例如,我们可以通过自定义绘制函数来实现曲线图上的标记:

void MyPlot::drawItem(QCPPainter *painter, const QCPAbstractItem *item)
{
    if (dynamic_cast<const QCPItemCurve *>(item))
    {
        painter->save();
        painter->setPen(QPen(Qt::green, 2));
        painter->drawEllipse(QPointF(1, 2), 5, 5);
        painter->restore();
    }
}

4. QCustomPlot与QChart库的对比

4.1 难易程度

从使用难度方面来说,QCustomPlot相较于QChart更容易上手。QCustomPlot库提供了良好的文档和示例代码,让用户更快地了解库的使用方法。而QChart虽然也有详细的文档说明,但是需要用户熟悉其内部机制和一些特殊的概念才能够实现一些定制性较高的图表。

4.2 功能特点

在功能方面,QCustomPlot比较注重曲线图的绘制和可视化效果,同时可以通过自定义绘制和鼠标交互等方式实现更加灵活的需求。而QChart相对来说则是一种更加通用的图表库,包括折线图、柱状图等多种图表类型,适用范围更广。

4.3 可定制性和扩展性

从可定制性和扩展性方面来看,QCustomPlot也更加能够满足用户的需求。QCustomPlot库提供了插件式的结构设计,使得其扩展性非常出色,同时提供了丰富的API和事件机制,让用户可以很容易地实现自定义的绘图和交互操作。而QChart则相对来说更加封闭,扩展性不如QCustomPlot。


总结

QCustomPlot库是一个非常实用的Qt图表绘制库,支持多种类型的图表,同时也具有一些强大的定制功能。通过上述介绍,相信大家已经掌握了QCustomPlot库的基本用法和常用功能。在实际应用中,可以根据需要进行更加复杂的操作和定制化,为数据展示和分析提供更好的解决方案。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值