概述
三维柱状图
QWidget *graphContainer= QWidget::createWindowContainer(graph3D);//图表的容器
Q3DBars *graph3D= new Q3DBars(); //图表
graph3D->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetFrontHigh); //设置视角
QValue3DAxis *axisV=new QValue3DAxis; //数值坐标
axisV->setTitle("value");
axisV->setTitleVisible(true);
QCategory3DAxis *axisRow=new QCategory3DAxis;
graph3D->setValueAxis(axisV);
graph3D->setRowAxis(axisRow);
QBar3DSeries *series= new QBar3DSeries; //序列
series->setMesh(QAbstract3DSeries::MeshCylinder);
series->setItemLabelFormat("(@rowLabel,@colLabel): %.1f"); //项的标签显示格式
graph3D->addSeries(series);
QBarDataArray *dataSet = new QBarDataArray; //数据代理
dataSet->reserve(rowLabs.count());
QBarDataRow *dataRow = new QBarDataRow;
*dataRow << 1 << 2<< 3<< 4<<5; //第1行数据,有5列
dataSet->append(dataRow);
series->dataProxy()->resetArray(dataSet);
//图表的网格
graph3D->activeTheme()->setGridEnabled(checked);
//柱状图的光滑性
series->setMeshSmooth(checked);
//反射
graph3D->setReflection(checked);
//轴标题
graph3D->valueAxis()->setTitleVisible(checked);
graph3D->rowAxis()->setTitleVisible(checked);
graph3D->columnAxis()->setTitleVisible(checked);
//轴标签背景
graph3D->activeTheme()->setLabelBackgroundEnabled(checked);
//Z轴反向
graph3D->valueAxis()->setReversed(checked);
//图表的背景
graph3D->activeTheme()->setBackgroundEnabled(checked);
//轴标签字体大小
QFont font = graph3D->activeTheme()->font();
font.setPointSize(arg1);
graph3D->activeTheme()->setFont(font);
//设置主题
Q3DTheme *currentTheme = graph3D->activeTheme();
currentTheme->setType(Q3DTheme::Theme(index));
//设置序列柱状图的颜色
QColor color=series->baseColor();
color=QColorDialog::getColor(color);
if (color.isValid())
series->setBaseColor(color);
//项的标签
series->setItemLabelFormat("value at (@rowLabel,@colLabel): %.1f");
series->setItemLabelVisible(checked);
//棒图的样式
QAbstract3DSeries::Mesh aMesh;
aMesh=QAbstract3DSeries::Mesh(index);
series->setMesh(aMesh);
//选择模式
graph3D->setSelectionMode(QAbstract3DGraph::SelectionFlags(index));
//变换视角
Q3DCamera::CameraPreset cameraPos=Q3DCamera::CameraPreset(index);
graph3D->scene()->activeCamera()->setCameraPreset(cameraPos);
//设置变换视角
graph3D->scene()->activeCamera()->setCameraPosition(xRot, yRot,zoom);
三维散点图
Q3DScatter *graph3D=new Q3DScatter();; //散点图
QScatter3DSeries *series=new QScatter3DSeries(proxy);//散点序列
QScatterDataProxy *proxy = new QScatterDataProxy(); //数据代理
series->setMeshSmooth(true);
graph3D->addSeries(series);
//创建坐标轴
graph3D->axisX()->setTitle("axis X");
graph3D->axisX()->setTitleVisible(true);
QScatterDataArray *dataArray = new QScatterDataArray();
dataArray->resize(itemCount);
QScatterDataItem *ptrToDataArray = &dataArray->first();
for(i=1,i<100,++i)
{
ptrToDataArray->setPosition(QVector3D(x,z,y));
ptrToDataArray++;
}
三维曲面图
#include <QtDataVisualization>
using namespace QtDataVisualization;
QWidget* graphContainer=QWidget::createWindowContainer(graph3D);
Q3DSurface *graph3D= new Q3DSurface(); //三维图表
//创建坐标轴
QValue3DAxis *axisX=new QValue3DAxis;
axisX->setTitle("Axis X");
axisX->setTitleVisible(true);
axisX->setRange(-11,11);
graph3D->setAxisX(axisX);
QSurfaceDataProxy *proxy= new QSurfaceDataProxy();//数据代理
QSurface3DSeries* series= new QSurface3DSeries(proxy);//序列
series->setItemLabelFormat("(@xLabel @yLabel @zLabel)");
series->setMeshSmooth(true);
series->setDrawMode(QSurface3DSeries::DrawSurfaceAndWireframe);
graph3D->addSeries(series);
//创建数据, 墨西哥草帽
QSurfaceDataArray *dataArray = new QSurfaceDataArray; //数组
dataArray->reserve(N);
proxy->resetArray(dataArray);
//渐变颜色按钮
QLinearGradient grBtoY(0, 0, 100, 0); //线型渐变
grBtoY.setColorAt(1.0, Qt::black);
grBtoY.setColorAt(0.0, Qt::yellow);
QPixmap pm(160, 20); //图片
QPainter pmp(&pm);//画家
pmp.setBrush(QBrush(grBtoY)); //渐变颜色1
pmp.setPen(Qt::NoPen);
pmp.drawRect(0, 0, 160, 20);
ui->btnGrad1->setIcon(QIcon(pm));//渐变颜色按钮1
//单一曲面颜色
QColor color=series->baseColor();
color=QColorDialog::getColor(color);
if (color.isValid())
{
series->setBaseColor(color);
series->setColorStyle(Q3DTheme::ColorStyleUniform);
}
//选择模式(如剖面图)
graph3D->setSelectionMode(QAbstract3DGraph::SelectionSlice);
//曲面样式(入是否是框架图)
series->setDrawMode(QSurface3DSeries::DrawWireframe);
void MainWindow::on_btnGrad1_clicked()
{//设置曲面渐变颜色
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);
}
//选择标识的形状,如一个点,一个三角
QAbstract3DSeries::Mesh aMesh;
aMesh=QAbstract3DSeries::Mesh(index+1);
series->setMesh(aMesh);
三维地形图
Q3DSurface *graph3D= new Q3DSurface();//三维图表
//创建数据代理
QImage heightMapImage(":/map/sea.png");
QHeightMapSurfaceDataProxy *proxy= new QHeightMapSurfaceDataProxy(heightMapImage); //数据代理
proxy->setValueRanges(-5000, 5000, -5000, 5000);
QSurface3DSeries *series= new QSurface3DSeries(proxy);//序列
ghtMapSurfaceDataProxy *proxy= new QHeightMapSurfaceDataProxy(heightMapImage); //数据代理
proxy->setValueRanges(-5000, 5000, -5000, 5000);
QSurface3DSeries *series= new QSurface3DSeries(proxy);//序列