QCustomPlot曲線圖顯示值

效果

cursor

代碼

 QCustomPlot需自己導入到工程中,鏈接上篇文有。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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


private:
    Ui::MainWindow *ui;

private slots:
    void myMoveEvent(QMouseEvent *);
};

#endif // MAINWINDOW_H

mainwindow.cpp

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

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

    // generate some data:
    QVector<double> x(10001), y(10001), y2(10001); // initialize with entries 0..10001
    for (int i=0; i<10001; ++i)
    {
      x[i] = i/50.0 - 1;
      y[i] = x[i]*x[i]; // let's plot a quadratic function
      y2[i] = x[i];
    }

    // create graph and assign data to it:
    ui->customPlot->addGraph();
    ui->customPlot->graph(0)->setData(x, y);
    // give the axes some labels:
    ui->customPlot->xAxis->setLabel("x");
    ui->customPlot->yAxis->setLabel("y");
    // set axes ranges, so we see all data:
    ui->customPlot->xAxis->setRange(-1, 1);
    ui->customPlot->yAxis->setRange(0, 1);
    ui->customPlot->replot();

    // 設置座標分段數
    ui->customPlot->yAxis->ticker()->setTickCount(10);

    ui->customPlot->addGraph();
    ui->customPlot->graph(1)->setPen(QPen(Qt::red));
    ui->customPlot->graph(1)->setData(x, y2);
    // make left and bottom axes always transfer their ranges to right and top axes:
    connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
    connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));

    ui->customPlot->graph(0)->rescaleAxes(false);
    ui->customPlot->graph(1)->rescaleAxes(false);
    ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->customPlot->graph(0)->setBrush((QBrush(QColor(155, 0, 255, 20))));

    ui->customPlot->legend->setVisible(true);
    ui->customPlot->legend->setBrush(QColor(255, 255, 255, 150));
    connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(myMoveEvent(QMouseEvent*)));
}

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

void MainWindow::myMoveEvent(QMouseEvent *e)
{
    /* 獲取光標位置 */
    int x_pos = e->pos().x();
    int y_pos = e->pos().y();

    /* 轉化爲座標系位置 */
    double xv = ui->customPlot->xAxis->pixelToCoord(x_pos);
    double yv = ui->customPlot->yAxis->pixelToCoord(y_pos);
    QString str;
    int count = 0;
    /* 遍歷兩個圖 */
    for (int i = 0; i < ui->customPlot->xAxis->graphs().count(); i++)
    {
        double y = 0;
        int start = 0, end = ui->customPlot->graph(i)->data()->size(), cur = start;
        /* 二分查找 */
        do{
            if (xv <= ui->customPlot->graph(i)->data()->at((start + end)/2)->key)
            {
                end = (start + end) / 2;
            }
            else
            {
                start = (start + end) / 2;
            }
            y = ui->customPlot->graph(i)->data()->at(end)->value;

        }while(end - start > 1);

        /* 顯示文本 */
        str += QString("\nx:%1\ny%2:%3").arg(QString::asprintf("%.3f", xv)).arg(i).arg(QString::asprintf("%.3f", y));
    }

    QToolTip::showText(cursor().pos(), str, ui->customPlot);

}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>621</width>
    <height>433</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QCustomPlot" name="customPlot" native="true"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>621</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>QCustomPlot</class>
   <extends>QWidget</extends>
   <header>qcustomplot.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章