QwtPolar绘制极坐标图
该示例包含以下功能:
1. 创建项目
创建项目名称为QwtPolarDemo1,并添加一个Qt5Class类,命名为myPlot。
2. 配置项目
在项目——属性——C/C++——常规——附加包含目录中添加QWT和QWTpolar
在项目——属性——C/C++——预处理器——预处理器定义中添加QWT_DLL和QWT_POLAR_DLL
在项目——属性——链接器——输入——附加依赖项中添加qwt.lib和qwtpolar.lib
示例程序
myplot.h头文件内容如下:
#ifndef MYPLOT_H
#define MYPLOT_H
#include
#include
#include
class PlotSettings
{
public:
enum Curve
{
Spiral,
Rose,
NumCurves
};
enum Flag
{
MajorGridBegin,
MinorGridBegin = MajorGridBegin + QwtPolar::ScaleCount,
AxisBegin = MinorGridBegin + QwtPolar::ScaleCount,
AutoScaling = AxisBegin + QwtPolar::AxesCount,
Inverted,
Logarithmic,
Antialiasing,
CurveBegin,
NumFlags = CurveBegin + NumCurves
};
bool flags[NumFlags];
};
class myPlot : public QwtPolarPlot
{
Q_OBJECT
public:
myPlot(QWidget *parent);
~myPlot();
QwtPolarCurve *createCurve(int id) const;
QwtPolarGrid *d_grid;
QwtPolarCurve *d_curve[PlotSettings::NumCurves];
};
#endif // MYPLOT_H
myplot.cpp源文件内容如下:
#include "myplot.h"
#include
#include
#include
#include
#include
#include
#include
#define pi 3.1415926535898
const QwtInterval radialInterval(0.0, 10.0); //径向范围
const QwtInterval azimuthInterval(0.0, 360.0); //角度范围
class Data : public QwtSeriesData<QwtPointPolar>
{
public:
Data(const QwtInterval &radialInterval,
const QwtInterval &azimuthInterval, size_t size) :
d_radialInterval(radialInterval),
d_azimuthInterval(azimuthInterval),
d_size(size)
{
}
virtual size_t size() const
{
return d_size;
}
protected:
QwtInterval d_radialInterval;
QwtInterval d_azimuthInterval;
size_t d_size;
};
class SpiralData : public Data
{
public:
SpiralData(const QwtInterval &radialInterval,
const QwtInterval &azimuthInterval, size_t size) :
Data(radialInterval, azimuthInterval, size)
{
}
virtual QwtPointPolar sample(size_t i) const
{
const double stepA = 4 * d_azimuthInterval.width() / d_size;
const double a = d_azimuthInterval.minValue() + i * stepA;
const double stepR = d_radialInterval.width() / d_size;
const double r = d_radialInterval.minValue() + i * stepR;
return QwtPointPolar(a, r);
}
virtual QRectF boundingRect() const
{
if (d_boundingRect.width() < 0.0)
d_boundingRect = qwtBoundingRect(*this);
return d_boundingRect;
}
};
class RoseData : public Data
{
public:
RoseData(const QwtInterval &radialInterval,
const QwtInterval &azimuthInterval, size_t size) :
Data(radialInterval, azimuthInterval, size)
{
}
virtual QwtPointPolar sample(size_t i) const
{
const double stepA = d_azimuthInterval.width() / d_size;
const double a = d_azimuthInterval.minValue() + i * stepA;
const double d = a / 360.0 * M_PI;
const double r = d_radialInterval.maxValue() * qAbs(qSin(4 * d));
return QwtPointPolar(a, r);
}
virtual QRectF boundingRect() const
{
if (d_boundingRect.width() < 0.0)
d_boundingRect = qwtBoundingRect(*this);
return d_boundingRect;
}
};
myPlot::myPlot(QWidget *parent)
: QwtPolarPlot(parent)
{
setAutoReplot(false);
setPlotBackground(Qt::darkBlue); //设置背景颜色
//设置角度顺时针增大
setAzimuthOrigin(2.0*pi / 3.0);
setScale(QwtPolar::Azimuth, 330, -30, azimuthInterval.width() / 12);
setScaleMaxMinor(QwtPolar::Azimuth, 2); //每个角度30,又被分成2小份
//径向范围
setScale(QwtPolar::Radius,
radialInterval.minValue(), radialInterval.maxValue());
// grids, axes
d_grid = new QwtPolarGrid();
d_grid->setPen(QPen(Qt::darkGreen));
d_grid->setAxisPen(QwtPolar::AxisAzimuth, QPen(Qt::black));
d_grid->showAxis(QwtPolar::AxisTop, true);
d_grid->showAxis(QwtPolar::AxisLeft, false);
d_grid->showAxis(QwtPolar::AxisBottom, false);
d_grid->showAxis(QwtPolar::AxisRight, true);
d_grid->attach(this);
// curves
for (int curveId = 0; curveId < PlotSettings::NumCurves; curveId++)
{
d_curve[curveId] = createCurve(curveId);
d_curve[curveId]->attach(this);
}
QwtLegend *legend = new QwtLegend;
insertLegend(legend, QwtPolarPlot::BottomLegend);
}
myPlot::~myPlot()
{
}
QwtPolarCurve *myPlot::createCurve(int id) const
{
const int numPoints = 200;
QwtPolarCurve *curve = new QwtPolarCurve();
curve->setStyle(QwtPolarCurve::Lines);
switch (id)
{
case PlotSettings::Spiral:
{
curve->setTitle("Spiral");
curve->setPen(QPen(Qt::yellow, 2));
curve->setSymbol(new QwtSymbol(QwtSymbol::Rect,
QBrush(Qt::cyan), QPen(Qt::white), QSize(3, 3)));
curve->setData(new SpiralData(radialInterval, azimuthInterval, numPoints));
}
break;
case PlotSettings::Rose:
{
curve->setTitle("Rose");
curve->setPen(QPen(Qt::red, 2));
curve->setSymbol(new QwtSymbol(QwtSymbol::Ellipse,
QBrush(Qt::green), QPen(Qt::white), QSize(3, 3)));
curve->setData(new RoseData(radialInterval, azimuthInterval, numPoints));
}
break;
}
return curve;
}
qwtpolardemo1.h头文件内容如下:
#ifndef QWTPOLARDEMO1_H
#define QWTPOLARDEMO1_H
#include
#include "ui_qwtpolardemo1.h"
class myPlot;
class QwtPolarPanner;
class QwtPolarMagnifier;
class QwtPolarDemo1 : public QWidget
{
Q_OBJECT
public:
QwtPolarDemo1(QWidget *parent = 0);
~QwtPolarDemo1();
private:
Ui::QwtPolarDemo1Class ui;
myPlot *d_plot;
QwtPolarPanner *d_panner;
QwtPolarMagnifier *d_zoomer;
};
#endif // QWTPOLARDEMO1_H
qwtpolardemo1.cpp源文件内容如下:
#include "qwtpolardemo1.h"
#include
#include
#include
#include
#include
#include
#ifdef QT_SVG_LIB
#include
#endif
#include
#include
#include
#include "myPlot.h"
#include
QwtPolarDemo1::QwtPolarDemo1(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
d_plot = new myPlot(this);
//鼠标滚轮缩放
d_zoomer = new QwtPolarMagnifier(d_plot->canvas());
d_zoomer->setEnabled(true);
//鼠标左键平移
d_panner = new QwtPolarPanner(d_plot->canvas());
d_panner->setEnabled(true);
QHBoxLayout *layout = new QHBoxLayout(ui.widget);
layout->addWidget(d_plot);
}
QwtPolarDemo1::~QwtPolarDemo1()
{
}