Qt圖形影象開發之高效能曲線圖模組QCustomplot庫詳細使用方法與例項(支援動、靜曲線圖)
阿新 • • 發佈:2020-03-11
Qt曲線圖模組QCustomPlot庫介紹
QCustomPlot是一個小型的Qt畫圖示類,支援繪製靜態曲線、動態曲線、多重座標曲線,柱狀圖,蠟燭圖等
前段時間用QChart模組畫圖,一條曲線上面放8000條資料就會卡的不行必須要換個其他的控制元件,後來找到了曲線圖模組QCustomplot庫
這個庫效能非常好,畫曲線圖折線圖柱狀圖動態靜態,放大縮小,都很好用,10w條資料量無壓力秒畫出來一點也不卡
下載地址
https://www.qcustomplot.com/index.php/download
裡面分為
QCustomPlot 2和QCustomPlot 1我用的2這兩個有一些函式的差異
下載解壓以後我們只需要qcustomplot.h和qcustomplot.cpp
注意
pro 檔案裡面 寫入 QT+= printsupport
動態效果
QCustomplot靜態曲線圖生成
//他繼承QWidget 所以構造裡面 放控制元件就會畫到控制元件上 QCustomPlot *pCustomPlot = new QCustomPlot(ui->label); //新增一條曲線 QCPGraph* pgraph = pCustomPlot->addGraph(); //給曲線準備資料 設定資料 QVector<double> x(80000); QVector<double> y(80000); for(int i = 0; i<x.size();i++) { x[i] = i; if(i%2==0) y[i] = 10; else y[i] = 20; } //設定資料 pCustomPlot->graph(0)->setData(x,y); //設定Y軸範圍 pCustomPlot->yAxis->setRange(0,30); //x軸名字 pCustomPlot->xAxis->setLabel("X"); //Y軸名字 pCustomPlot->yAxis->setLabel("Y"); //設定大小 pCustomPlot->resize(ui->label->width(),ui->label->height()); //可以進行滑鼠位置 放大縮小 拖拽 放大縮小座標系!!!功能非常強大 pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); //重繪 每次改變完以後都要呼叫這個進行重新繪製 pCustomPlot->replot();
執行效果如下:
時間為座標軸的靜曲線圖
大致差不多 區別在於x軸改為時間
QCustomPlot* p2 = new QCustomPlot(ui->label_2); QVector<double> time; QVector<double> y; //模擬幾個時間 .toTime_t()是轉換為 時間戳 從1970年到現在的秒數 time<<QDateTime::fromString("2019-01-15 17:08:23","yyyy-MM-dd hh:mm:ss").toTime_t(); time<<QDateTime::fromString("2019-01-25 17:08:23","yyyy-MM-dd hh:mm:ss").toTime_t(); time<<QDateTime::fromString("2019-02-15 17:08:23","yyyy-MM-dd hh:mm:ss").toTime_t(); time<<QDateTime::fromString("2019-02-25 17:08:23","yyyy-MM-dd hh:mm:ss").toTime_t(); time<<QDateTime::fromString("2019-03-27 13:08:23","yyyy-MM-dd hh:mm:ss").toTime_t(); y<<5<<15<<5<<15<<5; //增加一條線 p2->addGraph(); //設定Y軸範圍 p2->yAxis->setRange(0,20); //QCPAxisTickerDateTime 時間座標軸 必須要用 智慧指標 QSharedPointer<QCPAxisTickerDateTime> timer(new QCPAxisTickerDateTime); //設定時間格式 timer->setDateTimeFormat("yyyy-MM-dd"); //設定時間軸 一共幾格 //timer->setTickCount(6); //設定label 旋轉30° 橫著顯示可能顯示不全 p2->xAxis->setTickLabelRotation(30); // timer->setTickStepStrategy(QCPAxisTicker::tssMeetTickCount); //設定座標軸 p2->xAxis->setTicker(timer); p2->xAxis->setRange(time.at(0),time.at(4)); p2->graph(0)->setData(time,y); p2->resize(ui->label_2->width(),ui->label_2->height()); p2->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
執行效果如下:
QCustomplot動態曲線圖生成
下圖動態曲線是我用感測器採集的,大家可以用一些隨機資料來測試
假設影象只顯示10個點 第11個點將會把第一個點擠出去 就是一個vector 出棧入棧 裡面一直保持10個數據
//QVector<double> sx_vec,xAxis_vec 存放資料的容器 //m_chartPoint_counter 計數器 一直增加 來一條資料增加一下 控制x軸前進 實現動態效果 //這時容器裡面還沒10個點 所有一直向裡面存 if(m_chartPoint_counter < 10) { sx_vec.append(sx_); xAxis_vec.append(m_chartPoint_counter); //設定範圍正好 能顯示當前點 sx_plot->xAxis->setRange(0,xAxis_vec.at(xAxis_vec.size()-1)); } else { //容器資料現在是正好10個 把第一個出棧 把第11個入棧 正好還是10個數據 sx_vec.removeFirst(); xAxis_vec.removeFirst(); //入棧 xAxis_vec.append(m_chartPoint_counter); sx_vec.append(sx_); //設定範圍正好 能顯示當前點 sx_plot->xAxis->setRange(xAxis_vec.at(0),xAxis_vec.at( xAxis_vec.size()-1)); } //設定Y軸座標系 自動縮放以正常顯示所有的資料 sx_plot->yAxis->rescale(true); //設定資料 sx_plot->graph()->setData(xAxis_vec,sx_vec); //重繪製 sx_plot->replot(); //這裡必須要一直增加 如果增加到10就不增加 效果就是第10個點一直變化 不會出現動態效果 m_chartPoint_counter++;
影象資料清空
//影象資料清空 QCPGraph* thresholdY_line; thresholdY_line->data().data()->clear();
這裡只是介紹一些基本的功能 ,一些強大的功能 在 下載的examples裡有
本文主要講解了Qt圖形影象開發之高效能曲線圖模組QCustomplot庫詳細使用方法與例項,更多關於QT開發的知識請檢視下面的相關連結