QChart,极坐标,方向图

效果图
QChart,极坐标,方向图_第1张图片
先添加
QT += charts
头文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
//以下是QtChart使用必备的几个
#include 
#include 
#include 
QT_CHARTS_USE_NAMESPACE

#include 
#include 



QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    void InitPolarChart(QChartView *chartViewWD);               //初始极坐标绘图区域

    QLineSeries *seriesWD;  //曲线声明
    QScatterSeries *seriesWD2;
    QPolarChart *chartWD; //极坐标图表声明
    QValueAxis *angleAxis; //角坐标,相对应于直角坐标系的X轴
    QValueAxis* radiaAxis;//极坐标,相对应于直角坐标系的Y轴
    QChartView* chartViewWD; //画布声明


private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

主函数

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

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //曲线
    QLineSeries *seriesLine = new QLineSeries();
    seriesLine->setName("LinePolar");
    //图表
    QPolarChart *polarChart = new QPolarChart();
    polarChart->setParent(this);
    polarChart->addSeries(seriesLine);
    polarChart->setTitle("First Polar Chart");
  //坐标轴
    QValueAxis *angleAxis = new QValueAxis(this);
    angleAxis->setTickCount(9);
    angleAxis->setLabelFormat("%d");
    //角度
    polarChart->addAxis(angleAxis, QPolarChart::PolarOrientationAngular);

    QValueAxis *radiusAxis = new QValueAxis();
    radiusAxis->setTickCount(9);
    radiusAxis->setLabelFormat("%d");
    //半径
    polarChart->addAxis(radiusAxis, QPolarChart::PolarOrientationRadial);

    seriesLine->attachAxis(radiusAxis);
    seriesLine->attachAxis(angleAxis);

    radiusAxis->setRange(0, 100); //设置半径范围
    angleAxis->setRange(0, 360);    //设置角度范围

    //用公式来存坐标点
    float M_PI=3.14;
    for (int angle = 0; angle<= 360; angle += 10)
    {
        if(1)
            {
            double data;
            data = (angle)/180.0 *M_PI;
              float t=data;

             double lamda=0.01;
             double beta=200*M_PI;
             int  n=10;                 //天线阵元数目
             double d=lamda/4;  //0.01/4         //假设阵元间距为波长的四分之一

             double  W=beta*d*cos(t);
             double  z1=5*(W-0.5*M_PI);
             double  z2=0.5*(W-0.5*M_PI);

             double F1=sin(z1)/(10*sin(z2));
              F1=abs(F1);         //函数对数组元素进行绝对值处理的函数
             int K3=F1*100; //我去掉了小数点取整
         //把点存进去
             seriesLine->append(angle,K3);
             }
    }

    //绑定极坐标和视图
    QGridLayout *girdout=new QGridLayout(this);
    QChartView *polarChartView = new QChartView();
    polarChartView->setChart(polarChart);
    polarChartView->setRenderHint(QPainter::Antialiasing);

    girdout->addWidget(polarChartView);
    girdout->setParent(ui->centralwidget);
    polarChartView->setParent(ui->centralwidget);
}

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

你可能感兴趣的:(qt)