QCustomPlot implements polar coordinate chart - QtWidgets

Preface

I used QtChart to implement a polar coordinate chart before, but it was not very convenient. Especially some point graphics generally need to be drawn by yourself, and QCustomPlot comes with quite a few; there are also polar coordinate angle axes (i.e. circles). In comparison, QCustomPlot is much clearer.

Comparison between QtChart and QCustomPlot in various aspects

Qwt, QChart, QCustomPlot use_qcustomplot qwt_mahuifa's blog-CSDN blog

This blog is very well written and compares the appearance, use, and performance.

Resource address and version 

Download address of QCustomPlot: Qt Plotting Widget QCustomPlot - Download

Version: Version 2.1.0 released on 29.03.21

Preparation

I downloaded the largest one. It also contains source code, examples and help documents. The help documents can also be added to the Qt help document for use. Just put qcustomplot\documentation\qcustomplot.qch in the Qt installation directory Qt5.xx. x\Docs\Qt-5.xx.x, Qt can be automatically recognized.

I directly added the qcustomplot.cpp file and qcustomplot.h file to the project, but I also had to add the library printsupport to the pro file, which is used in QCustomPlot.

QT += printsupport

 

Instructions and code

QCustomPlot is simpler to use and is roughly divided into three parts:

Axis : A general view comes with x1y1 axis and x2y2 axis. However, the polar axis is different from usual, use QCPPolarAxisAngular

Line chart (data ): A common line chart is CPGraph. Generally, the function addGraph is used to create an object. After it is created, it is saved in the container QList, so it can be obtained according to the index, as follows; however, the polar coordinate chart uses QCPPolarGraph, it also needs to be reset.

  customPlot->addGraph();
  customPlot->graph(0)->setPen(QPen(Qt::blue));

View : QCustomPlot, which inherits QWidget, so QWidget can be promoted to QCustomPlot on QtDesigner.

By the way, one thing is that in the polar coordinate sample code that comes with QCustomPlot, there is a sentence

// Warning: Polar plots are a still a tech preview

It is just a preview and is not yet mature, so problems may occur in actual projects.

The code is as follows, which is similar to the polar coordinates I wrote before using QtChart.

void FirstCustomPlot::polarPlotDemo()
{
    ui->qcustomplot->plotLayout()->clear();
    QCPPolarAxisAngular* angularAxis=new QCPPolarAxisAngular(ui->qcustomplot);
    ui->qcustomplot->plotLayout()->addElement(0,0,angularAxis);
    ui->qcustomplot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom);
    angularAxis->setRangeDrag(false);
    angularAxis->setTickLabelMode(QCPPolarAxisAngular::lmUpright);
    angularAxis->radialAxis()->setTickLabelRotation(0);
    angularAxis->radialAxis()->setAngle(45);

    angularAxis->grid()->setAngularPen(QPen(QColor(200,200,200),0,Qt::SolidLine));
    angularAxis->grid()->setSubGridType(QCPPolarGrid::gtAll);

    QCPPolarGraph* g1=new QCPPolarGraph(angularAxis,angularAxis->radialAxis());
    QCPPolarGraph* g2=new QCPPolarGraph(angularAxis,angularAxis->radialAxis());
    g2->setPen(QPen(QColor(255,150,20)));
    g2->setBrush(QColor(255,150,20,50));
    g1->setScatterStyle(QCPScatterStyle::ssStar);
    g1->setPen(QPen(Qt::blue));
//    g1->setLineStyle(QCPPolarGraph::lsNone);
    for(int i=0;i<100;i++){
        g1->addData(i/100.0*360.0,qSin(i/100.0*M_PI*8)*8+1);
        g2->addData(i/100.0*360.0,qSin(i/100.0*M_PI*6)*2);
    }

    angularAxis->setRange(0,360);
    angularAxis->radialAxis()->setRange(-10,10);
}

renderings

 

Guess you like

Origin blog.csdn.net/xiaopei_yan/article/details/129993633