#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"qcustomplot.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QCustomPlot *pCustomPlot = new QCustomPlot(this);
pCustomPlot->resize(400, 300);
// 可变数组存放绘图的坐标的数据,分别存放x和y坐标的数据,101为数据长度
QVector<double> x(101), y(101);
// 添加数据,这里演示y = x^3,为了正负对称,x从-10到+10
for (int i = 0; i < 100; ++i)
{
x[i] = i;
y[i] = 20*sin(x[i]); // x的y次方;
}
// 向绘图区域QCustomPlot添加一条曲线
QCPGraph *pGraph = pCustomPlot->addGraph();
pCustomPlot->addGraph();
pCustomPlot->addGraph();
// 添加数据
pCustomPlot->graph(0)->setData(x, y);
pCustomPlot->graph(1)->setData(x, y);
pCustomPlot->xAxis->setLabel("x");
pCustomPlot->yAxis->setLabel("y");
// 设置背景色
pCustomPlot->setBackground(QColor(50, 50, 50));
pGraph->setPen(QPen(QColor(32, 178, 170)));
pCustomPlot->xAxis->setTickLabelColor(Qt::red);
pCustomPlot->xAxis->setLabelColor(QColor(255, 0, 0));
//设置x基准轴颜色
pCustomPlot->xAxis->setBasePen(QPen(QColor(255, 255, 0)));
pCustomPlot->xAxis->setTickPen(QPen(QColor(255, 255, 0)));
pCustomPlot->xAxis->setSubTickPen(QColor(255, 165, 0));
QFont xFont = pCustomPlot->xAxis->labelFont();
xFont.setPixelSize(20);
pCustomPlot->xAxis->setLabelFont(xFont);
//获取句柄并进行设置,去除默认的虚线网格,导出类,编辑设置相应的参数
QCPGrid* grid=pCustomPlot->xAxis->grid();
grid->setVisible(false);
grid=pCustomPlot->yAxis->grid();
grid->setVisible(false);
//设置刻度文字字体的颜色
pCustomPlot->yAxis->setTickLabelColor(Qt::red);
//设置旁边的标签字体的颜色
pCustomPlot->yAxis->setLabelColor(QColor(255, 0, 0));
//设置Y基准轴的颜色
pCustomPlot->yAxis->setBasePen(QPen(QColor(255, 255, 0)));
//主刻度颜色
pCustomPlot->yAxis->setTickPen(QPen(QColor(255, 255, 255)));
//子刻度颜色
pCustomPlot->yAxis->setSubTickPen(QColor(255, 255, 0));
//yFont先获取y坐标轴的标签的字体,在进行编辑yFont
QFont yFont = pCustomPlot->yAxis->labelFont();
//编辑
yFont.setPixelSize(20);
//设置yFont
pCustomPlot->yAxis->setLabelFont(yFont);
//设置xy坐标基准轴的子刻度主刻度的长度,第一个参数表示向内延的像素单位,第二个向外的像素单位
pCustomPlot->xAxis->setTickLength(10, 0);
pCustomPlot->xAxis->setSubTickLength(5, 0);
pCustomPlot->yAxis->setTickLength(10,0);
pCustomPlot->yAxis->setSubTickLength(5,0);
// pCustomPlot->yAxis->setAutoTicks(false);
// pCustomPlot->yAxis->setAutoTickLabels(false);
//pCustomPlot->yAxis->setTickVector(QVector<double>() << 0 <<10<<20<<30);
//pCustomPlot->yAxis->setTickVectorLabels(QVector<QString>() << "Not so\nhigh" << "Very\nhigh");
// pCustomPlot->xAxis->setAutoTickStep(false);
//pCustomPlot->xAxis->setTickStep(0.1); // one month in seconds
//pCustomPlot->xAxis->setSubTickCount(3);
// 设置坐标轴显示范围,否则只能看到默认范围
pCustomPlot->xAxis->setRange(0, 100, Qt::AlignLeft);
// pCustomPlot->xAxis->setRange(-100, 100);
pCustomPlot->yAxis->setRange(-100, 100);
//保存成图片格式
// pCustomPlot->savePng("D://customPlot.png", 400, 300);
//让坐标系支持拖放,缩放,
// pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
}
MainWindow::~MainWindow()
{
delete ui;
}
CustomPlot test
最新推荐文章于 2024-07-17 11:32:57 发布