使用Qt的QChartView實現(xiàn)縮放和放大功能
Qt的常用圖表方式Qwt、QChart、QCustomPlot等。QCharts是官方的,功能強大些。QCustomPlot是一個小型的Qt畫圖標類,支持繪制靜態(tài)曲線、動態(tài)曲線、多重坐標曲線,柱狀圖,蠟燭圖等。QCustomPlot比Qchat簡單好用些。
這里介紹下QChartView縮放和放大功能的實現(xiàn)。
這里介紹下QChartView縮放和放大功能的實現(xiàn)。
QChartView開啟鼠標拖動放大功能:
ui->wdChart->setRubberBand(QChartView::RectangleRubberBand);
開啟后,拖動鼠標區(qū)域自動放大,鼠標右鍵點擊自動縮小。
恢復的話重新設置下軸的最大最小范圍setRange即可。這里介紹下鼠標左鍵單擊實現(xiàn)恢復的辦法:
自定以一個MyChartView,繼承自QChartView。增加一個信號:
signals: void sgl_recoverRange(MyChartView *p);
需要在自定義的MyChartView中區(qū)分出來是否是鼠標左鍵的單擊事件還是鼠標左鍵的拖動。這里巧妙實現(xiàn)了下。原理很簡單,如果是鼠標拖動的話mouseMoveEvent中把is_Pressed_清掉。
#include "mychartview.h" MyChartView::MyChartView(QWidget *parent):QChartView(parent) { is_Pressed_ = false; } void MyChartView::mouseMoveEvent(QMouseEvent *event) { is_Pressed_ = false; QChartView::mouseMoveEvent(event); } void MyChartView::mouseReleaseEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton){ if(is_Pressed_){ is_Pressed_ = false; // 單擊鼠標恢復縮放 emit sgl_recoverRange(this); } } QChartView::mouseReleaseEvent(event); } void MyChartView::mousePressEvent(QMouseEvent *event) { is_Pressed_ = true; QChartView::mousePressEvent(event); }
綁定信號和槽函數(shù):
connect(ui->wdChart,&MyChartView::sgl_recoverRange, this,&MainWindow::slot_recoverChartRange); m_tooltip = new Callout(myChart);
在槽函數(shù)中對縮放和放大功能進行恢復處理,重新設置range.
void MainWindow::slot_recoverChartRange() { qDebug() << "slot_recoverChartRange"; int maxVal = 0; if(mTbData.recList.size() == 0){ mAxisY->setRange(0,12); mAxisY1->setRange(0,12); mAxisX->setRange(0,10); return; } }
更好用的QCustomPlot
QCustomPlot介紹
QCustomPlot 是一個基于Qt的畫圖和數(shù)據(jù)可視化C++控件。QCustomPlot 致力于提供美觀的界面,高質量的2D畫圖、圖畫和圖表,同時為實時數(shù)據(jù)可視化應用提供良好的解決方案。 該繪圖庫專注于制作美觀、出版物質量高的2D繪圖、圖形和圖表,并為實時可視化應用程序提供高性能。
QCustomPlot的下載與安裝
QCustomPlot官網(wǎng)鏈接:Qt Plotting Widget QCustomPlot - Introduction
下載鏈接:Qt Plotting Widget QCustomPlot - Download
QCustomPlot的使用
QCustomPlot 是一個超強超小巧的qt繪圖類,非常漂亮,非常易用。只需要把下載下來的qcustomplot.h和qcustomplot.cpp文件加入項目工程即可使用,遠比qwt方便和漂亮,可以自己使用兩個源文件也可以自己編譯成庫文件,非常方便。
把qcustomplot.cpp和qcustomplot.h拷貝到工程目錄下,在項目中點右鍵添加現(xiàn)有文件,把兩個文件加入工程。
這時pro文件會添加上qcustomplot.cpp和qcustomplot.h,如果Qt版本在5.0以上,需要在.pro文件中的QT變量加上printsupport,QT += printsupport。
界面上拖上去一個widget控件,然后使一個widget提升為QCustomPlot類,即可使用。
使用示例
void OneGraph::OneGraph_Drawing(QCustomPlot *customPlot) { // 將數(shù)據(jù)用曲線實時顯示 QVector<double> x(101),y(101);// x軸數(shù)據(jù),y軸數(shù)據(jù) for(int i = 0; i < 101;i++) { x[i] = i / 50.0 - 1;// x軸數(shù)據(jù)范圍:-1 ~ 1 y[i] = x[i] * x[i];// y軸數(shù)據(jù)范圍:0 ~ 1 } // 添加一個曲線圖QGraph, customPlot->addGraph(); customPlot->graph(0)->setData(x,y);//為坐標軸添加數(shù)據(jù) customPlot->graph(0)->setName("示例1:繪制一個曲線");// 設置曲線圖的名字 // 如果需要添加多個曲線,就需要多次調用addGraph()函數(shù) // customPlot->addGraph(); // customPlot->graph(1)->setData("x軸數(shù)據(jù)","y軸數(shù)據(jù)"); // customPlot->graph(1)->setName("示例1:繪制第二個一個曲線"); // 設置圖表標題 QCPTextElement *title = new QCPTextElement(customPlot,"標題:繪制一個曲線",QFont("sans",10,QFont::Bold)); title->setTextColor(Qt::green); title->setMargins(QMargins(0,6,0,10)); // 在第一行第一列添加標題 customPlot->plotLayout()->insertRow(0);// 插入一行 customPlot->plotLayout()->addElement(0, 0, title); //為圖例添加標題 QCPTextElement *legend_title = new QCPTextElement(customPlot,"這是圖例的標題",QFont("sans",10,QFont::Bold)); legend_title->setTextColor(Qt::red); legend_title->setMargins(QMargins(0,6,0,10));// 為了效果更好,添加一些邊距 legend_title->setLayer("legend");// 一定要把標題的層設置為legend層 customPlot->legend->insertRow(0);// 插入一行 customPlot->legend->addElement(0,0,legend_title);// 在第一行第一列添加標題 // x軸設置屬性 customPlot->xAxis->setLabel("x軸數(shù)據(jù)");// 設置x軸的標簽 customPlot->xAxis->setRange(-1,1);// 設置x軸的范圍為(-1,1) customPlot->xAxis->setPadding(30);//設置外邊距,數(shù)值可以改大或者改小來觀察效果 customPlot->xAxis->setLabelPadding(20);//設置標簽內邊距 customPlot->xAxis->setTickLabelPadding(10); // y軸設置屬性 customPlot->yAxis->setLabel("y軸數(shù)據(jù)"); customPlot->yAxis->setRange(-1,1); customPlot->yAxis->setPadding(10); //設置QCustomPlot的背景顏色 QLinearGradient plotGradient; plotGradient.setStart(0,0);//背景顏色起始點,從圖左上角開始,y方向0~400之間為紅色漸變,開始位置為紅色 plotGradient.setFinalStop(0,400);//y方向 >400 為綠色漸變,結束位置為綠色 plotGradient.setColorAt(0,QColor(200,200,200));//黑色,透明度從 0 ~ 1, plotGradient.setColorAt(1,QColor(120,120,120)); customPlot->setBackground(plotGradient); //設置QCPAxisRect軸矩形的背景顏色 QLinearGradient axisRectGradient; axisRectGradient.setStart(0,0); axisRectGradient.setFinalStop(0,350); axisRectGradient.setColorAt(0,QColor("#87CEFA"));//亮天藍色 axisRectGradient.setColorAt(1,QColor("#FFB6C1"));//淺粉紅 customPlot->axisRect()->setBackground(axisRectGradient); //設置QCPAxis軸的風格 customPlot->xAxis->setBasePen(QPen(Qt::white,2));// x軸線的畫筆顏色和粗細 customPlot->xAxis->setTickPen(QPen(Qt::white,3));// x軸線上的主刻度線(有數(shù)字的刻度線)的畫筆顏色和粗細 customPlot->xAxis->setTickLabelColor(Qt::green);// x軸線上的主刻度線下的文字顏色 customPlot->xAxis->setTickLengthIn(6);// 軸線內主刻度線的長度 customPlot->xAxis->setTickLengthOut(15);// 軸線外主刻度線的長度 customPlot->xAxis->setSubTickPen(QPen(QColor(220,20,60),1));//粉紅色,x軸線上的子刻度線(有數(shù)字的刻度線)的畫筆顏色和粗細 customPlot->xAxis->setLabelColor(Qt::red);// 只有設置了標簽,軸標簽的顏色才會顯示 customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);// 設置軸線結束時的風格為 實角三角形但內部有凹陷的形狀 customPlot->xAxis->setLowerEnding(QCPLineEnding::esDisc);//setLowerEnding設置軸線開始時的風格 //設置QCPGrid網(wǎng)格的風格,每條網(wǎng)格對應一個刻度 customPlot->xAxis->grid()->setPen(QPen(QColor(255,0,255),1,Qt::SolidLine));//實線 customPlot->yAxis->grid()->setPen(QPen(QColor(255,0,255),2,Qt::DotLine));//點線 customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine)); // 子網(wǎng)格線(對應子刻度)畫筆 customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::SolidLine));//設置顏色后,需要顯示子網(wǎng)格線,才能看到效果 customPlot->xAxis->grid()->setSubGridVisible(true);// 顯示子網(wǎng)格線 customPlot->yAxis->grid()->setSubGridVisible(true); customPlot->xAxis->grid()->setZeroLinePen(QPen(Qt::black,3));// 設置刻度為0時的網(wǎng)格線的畫筆 customPlot->yAxis->grid()->setZeroLinePen(QPen(Qt::black,3)); customPlot->legend->setVisible(true);// 顯示圖例:曲線顏色和曲線名,一般在右上角,可以設置 // customPlot->replot();重繪 }
到此這篇關于使用Qt的QChartView實現(xiàn)縮放和放大功能的文章就介紹到這了,更多相關Qt QChartView縮放和放大內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!