曲线图绘制
qt的曲线绘制有三种类,分别是Qcustomplot、QChart、Qwt。
比较
美观方面:
- Qcustomplot≈Qchart > Qwt
- Qcustomplot界面简洁大方;
- Qchart界面华丽优美,在数据量大的时候会引响鼠标的缩放、移动图表操作;
- Qwt界面老旧,在美观上不如Qcustomplot和Qchart ,并且在使用鼠标移动图表时会产生空白区域。
性能方面:
绘制数据性能:Qchart>Qcustomplot > Qwt
绘制数据数量:Qcustomplot >Qchart> Qwt
绘制1000个数据点时,qcustomplot平均耗时 13.6毫秒,Qwt平均耗时40毫秒,QChart平均耗时12.5毫秒;
绘制10000个数据点时,qcustomplot平均耗时 21.6毫秒,Qwt平均耗时78毫秒,QChart平均耗时13.5毫秒;
绘制100000个数据点时,qcustomplot平均耗时22.5毫秒,Qwt平均耗时524毫秒,QChart平均耗时20.7毫秒;
绘制500000个数据点时,qcustomplot平均耗时43.3毫秒,QChart平均耗时194.25毫秒。
功能方面:
- QChart、Qwt功能比较齐全,并且可绘制图表种类多;
- qcustomplot在图表种类上、常用功能上较少。
使用方面:
- QChart无需配置,主要在安装Qt时勾选就可以,在程序编写时上手较慢,许多功能需要重写;
- Qwt安装配置比较复杂;
- QCustomPlot体积小、简单易用,上手快,并且QCustomPlot只有两个源文件,可直接添加进工程,更容易直接修改源码。
Qwt 下载地址
Qcustomplot下载地址
QChart不用下载,只需在安装qt时候进行选择组件。
在这里我使用的是QChart来进行设计的毕设,首先是对.pro文件进行追加代码charts
1 2
| QT += core gui QT += charts
|
进行编写mainwindow.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QChartView> #include <QLineSeries> #include <QChart> #include <QValueAxis> #include <QMouseEvent> #include <QGraphicsSimpleTextItem> #include <QSplineSeries> #include <QDebug> #include <QTimer> #include <math.h>
QT_CHARTS_USE_NAMESPACE
QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE
class MainWindow : public QMainWindow { Q_OBJECT
public: MainWindow(QWidget *parent = nullptr); ~MainWindow();
protected: void mouseDoubleClickEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void wheelEvent(QWheelEvent *event); private: Ui::MainWindow *ui;
QPoint m_pointUsed; bool m_isPress;
double m_dMinX, m_dMaxX, m_dMinY, m_dMaxY; void receivedData(int); int maxSize; int maxX; int maxY; QValueAxis *axisY; QValueAxis *axisX; QList<int>data; QSplineSeries *splineSeries; QChart *chart; QChartView *chartView; QTimer *timer; }; #endif
|
mainwindow.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| #include "mainwindow.h" #include "ui_mainwindow.h" #include <QRandomGenerator> #include <QWidget> #include <QDebug> #include <math.h> #include <QToolTip>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this);
this->setGeometry(0, 0, 800, 480); maxSize = 1000-1; maxX = 2000; maxY = 64000; splineSeries = new QSplineSeries(); splineSeries->setName("第一条线");
chart = new QChart(); chartView = new QChartView();
axisY = new QValueAxis(); axisX = new QValueAxis(); timer = new QTimer(this);
chart->legend()->hide(); chart->setTitle("拉曼光谱"); chart->addSeries(splineSeries);
axisY->setLabelFormat("%i"); axisY->setTitleText("强度"); chart->addAxis(axisY, Qt::AlignLeft); axisY->setRange(0, maxY); splineSeries->attachAxis(axisY); axisX->setLabelFormat("%i"); axisX->setTitleText("拉曼位移(cm-1)"); chart->addAxis(axisX, Qt::AlignBottom); axisX->setRange(-28000, maxX); splineSeries->attachAxis(axisX); chartView->setChart(chart); chartView->setRenderHint(QPainter::Antialiasing); setCentralWidget(chartView);
timer = new QTimer(this); connect(timer, &QTimer::timeout, [&]() { QVector<QPointF> point(maxSize + 1); for(int i = 0; i < maxSize + 1; i++) { point[i].setX(i); point[i].setY((int)QRandomGenerator::global()->bounded(1,1000)); } splineSeries->replace(point); }); timer->start(20);
}
MainWindow::~MainWindow() { delete ui; }
void MainWindow::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) { m_pointUsed = event->pos(); m_isPress = true; } else if(event->button() == Qt::MidButton) { QLineSeries* l_series = (QLineSeries*)this->chart->series().at(0); QVector<QPointF> l_vpointF = l_series->pointsVector(); QPointF l_curVal = this->chart->mapToValue(event->pos()); QPointF l_pointF = l_vpointF.at(qRound(l_curVal.x()));
QString l_strPos = QString("x:%0 y:%1").arg(l_pointF.x()).arg(l_pointF.y()); QString l_stStyle = "<p style=\"background:#00FFFF; border-radius: 1px 5px;font:12pt '宋体'\">%1</p>"; QToolTip::showText(cursor().pos(), l_stStyle.arg(l_strPos), this, QRect(0,0,1,1), 10000); } }
void MainWindow::mouseDoubleClickEvent(QMouseEvent *event) { if (event->button() == Qt::RightButton) { this->chart->axes(Qt::Horizontal).at(0)->setRange(m_dMinX, m_dMaxX); this->chart->axes(Qt::Vertical).at(0)->setRange(m_dMinY, m_dMaxY); } }
void MainWindow::mouseMoveEvent(QMouseEvent *event) { if(m_isPress) { QPoint l_pointDiff = event->pos() - m_pointUsed; this->chart->scroll(-l_pointDiff.x(), l_pointDiff.y()); } m_pointUsed = event->pos(); }
void MainWindow::mouseReleaseEvent(QMouseEvent *event) { m_isPress = false; }
void MainWindow::wheelEvent(QWheelEvent *event) { QPointF l_curVal = this->chart->mapToValue(event->pos());
QValueAxis *l_axisX = (QValueAxis*)this->chart->axes(Qt::Horizontal).at(0); double l_fMinX = l_axisX->min(); double l_fMaxX = l_axisX->max(); QValueAxis *l_axisY = (QValueAxis*)this->chart->axes(Qt::Vertical).at(0); double l_fMinY = l_axisY->min(); double l_fMaxY = l_axisY->max();
double l_fZoomMinX,l_fZoomMaxX,l_fZoomMinY,l_fZoomMaxY; if(event->delta() > 0) { l_fZoomMinX = l_curVal.x() - (l_curVal.x() - l_fMinX) / 1.5; l_fZoomMaxX = l_curVal.x() + (l_fMaxX - l_curVal.x()) / 1.5; l_fZoomMinY = l_curVal.y() - (l_curVal.y() - l_fMinY) / 1.5; l_fZoomMaxY = l_curVal.y() + (l_fMaxY - l_curVal.y()) / 1.5; } else { l_fZoomMinX = l_curVal.x() - (l_curVal.x() - l_fMinX) * 1.5; l_fZoomMaxX = l_curVal.x() + (l_fMaxX - l_curVal.x()) * 1.5; l_fZoomMinY = l_curVal.y() - (l_curVal.y() - l_fMinY) * 1.5; l_fZoomMaxY = l_curVal.y() + (l_fMaxY - l_curVal.y()) * 1.5; } this->chart->axes(Qt::Vertical).at(0)->setRange(l_fZoomMinY, l_fZoomMaxY); this->chart->axes(Qt::Horizontal).at(0)->setRange(l_fZoomMinX, l_fZoomMaxX); }
|