无需额外编程或设置,程序运行时在图表单击左键可以选择图表中的某个项、某个列或行(与选择办法的设置有关),按住鼠标右键时高下、旁边移动可以进行水平和垂直方向的旋转,鼠标滚轮可以进行缩放。
下面是主窗口类的定义的紧张部分(省略了界面组件的槽函数定义):
class Widget : public QWidget{Q_OBJECTpublic: Widget(QWidget parent = nullptr); ~Widget();private: Ui::Widget ui; QWidget graphContainer; //图表的容器 Q3DSurface graph3D; //三维图表 QSurface3DSeries series; //序列 QSurfaceDataProxy proxy; //数据代理 void iniGraph3D(); //创建图表};
Widget类定义了Q3DSurface类的变量graph3D,QSurface3DSeries类的序列变量series,QSurfaceDataProxy类的数据代理变量proxy。
下面是Widget类的布局函数,用于图表绘制。
Widget::Widget(QWidget parent): QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); //创建图表 graph3D = new Q3DSurface(); graphContainer = QWidget::createWindowContainer(graph3D); QLayout layout=new QVBoxLayout(this); layout->addWidget(graphContainer); this->setLayout(layout); //创建坐标轴 QValue3DAxis axisX=new QValue3DAxis; axisX->setTitle("Axis X"); axisX->setTitleVisible(true); axisX->setRange(-11,11); graph3D->setAxisX(axisX); QValue3DAxis axisY=new QValue3DAxis; axisY->setTitle("Axis Y"); axisY->setTitleVisible(true); axisY->setAutoAdjustRange(true); graph3D->setAxisY(axisY); QValue3DAxis axisZ=new QValue3DAxis; axisZ->setTitle("Axis Z"); axisZ->setTitleVisible(true); axisZ->setRange(-11,11); graph3D->setAxisZ(axisZ); //创建数据代理 proxy = new QSurfaceDataProxy(); series = new QSurface3DSeries(proxy); graph3D->addSeries(series); series->setItemLabelFormat("(QxLabel @yLabel @zLabel)"); series->setMeshSmooth(true); graph3D->activeTheme()->setLabelBackgroundEnabled(false); series->setDrawMode(QSurface3DSeries::DrawSurfaceAndWireframe); //渐变颜色 QLinearGradient gr; gr.setColorAt(0.0, Qt::black); gr.setColorAt(0.33, Qt::blue); gr.setColorAt(0.67, Qt::red); gr.setColorAt(1.0, Qt::yellow); series->setBaseGradient(gr); series->setColorStyle(Q3DTheme::ColorStyleRangeGradient); //创建数据,墨西哥草帽 int N=41;//-10:0.5:10, N 个数据点 QSurfaceDataArray dataArray = new QSurfaceDataArray; //数组 dataArray->reserve(N); float x=-10,y,z; int i,j; for(i=1;i<=N;i++){ QSurfaceDataRow newRow = new QSurfaceDataRow(N) ; //一行的数据 y=-10; int index=0; for(j=1; j<=N; j++){ z=qSqrt(xx+yy); if(z!=0) z=10qSin(z)/z; else z=10; (newRow)[index++].setPosition(QVector3D(x, z, y)); y=y+0.5; } x=x+0.5; dataArray<<newRow; } proxy->resetArray(dataArray);}
代码首先创建三维图表graph3D,并为其创建窗口容器,然后创建了3个QValue3DAxis类型的坐标轴工具,设置基本属性后作为图表的相应坐标轴。
程序创建QSurfaceDataProxy类型的数据代理变量proxy,并在创建序列时作为参数通报给它,然后将序列添加到图表。
下面的代码对序列做了一些属性的设置。QSurface3DSeries的函数setDrawMode(DrawFlags mode)用于设置曲面样式,参数是列举类型QSurface3DSeries::DrawFlag的组合,取值有以下几种:
QSurface3DSeries::DrawWireframe,只绘制线网; QSurface3DSeries::DrawSurface,只绘制曲面; QSurface3DSeries: :DrawSurfaceAndWireframe,绘制线网和曲面。这里设置了绘图模式为曲面加线网的模式,即:
series->setDrawMode(QSurface3DSeries::DrawSurfaceAndWireframe);
然后是创建数据,采取“墨西哥草帽”函数天生数据点。QSurfaceDataProxy数据代理采取两层一维数组的模式存储三维曲面的数据,与三维柱状图的数据代理类存储数据的模式类似。
这里创建一个QSurfaceDataArray类型的向量dataArray,它的元素个数即是行数,每一行又是一个QSurfaceDataRow类型的列表,用于存储一行中多列的数据。QSurfaceDataArray和QSurfaceDataRow是类型定义,其定义为:
typedef QVector<QSurfaceDataItem> QSurfaceDataRow;typedef QList<QSurfaceDataRow > QSurfaceDataArray;
每个基本的数据点都是QSurfaceDataItem类,有x、y、z个坐标,通过setPosition(const QVector3D &pos)函数可以设置一个点的三维坐标。
在改变图表的主题时,曲面的颜色会根据主题的设置而改变,也可以单独改变曲面的颜色。setColorStyle()设置的参数是列举类型Q3DTheme=ColorStyle,其取值有以下几种:
Q3DTheme::ColorStyleUniform,单一颜色;Q3DTheme::ColorStyleObjectGradient,不考虑工具高度,只根据工具渐变;Q3DTheme::ColorStyleRangeGradient,根据工具高度渐变。这里设置为渐变色
//渐变颜色QLinearGradient gr;gr.setColorAt(0.0, Qt::black);gr.setColorAt(0.33, Qt::blue);gr.setColorAt(0.67, Qt::red);gr.setColorAt(1.0, Qt::yellow);series->setBaseGradient(gr);series->setColorStyle(Q3DTheme::ColorStyleRangeGradient);
程序定义一个QLinearGradient线性渐变类的变量,定义了渐变颜色属性后,调用序列的setBaseGradient()函数设置渐变颜色,并设置颜色样式为Q3DTheme::ColorStyleRangeGradient。
————————————————
以为有用的话请关注点赞,感激您的支持!
对付本系列文章干系示例完全代码有须要的朋友,可关注并在评论区留言!