#include "waveform.h"
#include "qcustomplot.h"
#include <QVector>
WaveForm::WaveForm(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
this->resize(1550,800);
//定义图像对象
QCustomPlot* customPlot = new QCustomPlot(this);
//设置图像位置和大小
customPlot->setGeometry(1, 1, 1540, 350);
// 添加五条线
QCPGraph* A = customPlot->addGraph();
QCPGraph* B = customPlot->addGraph();
QCPGraph* C = customPlot->addGraph();
QCPGraph* D = customPlot->addGraph();
QCPGraph* E = customPlot->addGraph();
// 假设您已经有了一个QCustomPlot实例,名为customPlot
//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
QCPAxis* yAxisA = customPlot->axisRect()->addAxis(QCPAxis::atLeft);
yAxisA->setLabel("Y Axis A Label"); // 设置轴的标签文本
//yAxisA->setVisible(false); //设置坐标轴不显示(默认显示)
// 设置轴的刻度范围等其他属性
yAxisA->setRange(0, 10);
// 设置Y轴标签的可见性和字体等(如果需要)
yAxisA->setLabelFont(QFont(QFont().family(), 10)); // 例如,设置字体大小和家族
yAxisA->setLabelColor(Qt::red); // 设置标签颜色
// 设置曲线Y坐标轴
A->setValueAxis(yAxisA);
//设置折线颜色
A->setPen(QPen(Qt::red));
A->setVisible(true);
//BBBBBBBBBBBBBBBBBBBBBBBBBB
QCPAxis* yAxisB = customPlot->axisRect()->addAxis(QCPAxis::atLeft);
yAxisB->setLabel("Y Axis B Label"); // 设置轴的标签文本
//yAxisB->setVisible(false); //设置坐标轴不显示(默认显示)
// 设置轴的刻度范围等其他属性
yAxisB->setRange(0, 100);
// 设置Y轴标签的可见性和字体等(如果需要)
yAxisB->setLabelFont(QFont(QFont().family(), 10)); // 例如,设置字体大小和家族
yAxisB->setLabelColor(Qt::green); // 设置标签颜色
// 设置曲线Y坐标轴
B->setValueAxis(yAxisB);
//设置折线颜色
B->setPen(QPen(Qt::green));
B->setVisible(true);
//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
QCPAxis* yAxisC = customPlot->axisRect()->addAxis(QCPAxis::atLeft);
yAxisC->setLabel("Y Axis C Label"); // 设置轴的标签文本
//yAxisC->setVisible(false); //设置坐标轴不显示(默认显示)
// 设置轴的刻度范围等其他属性
yAxisC->setRange(0, 100);
// 设置Y轴标签的可见性和字体等(如果需要)
yAxisC->setLabelFont(QFont(QFont().family(), 10)); // 例如,设置字体大小和家族
yAxisC->setLabelColor(Qt::yellow); // 设置标签颜色
// 设置曲线Y坐标轴
C->setValueAxis(yAxisC);
//设置折线颜色
C->setPen(QPen(Qt::yellow));
C->setVisible(true);
//DDDDDDDDDDDDDDDDDDDDDDDDDDDD
QCPAxis* yAxisD = customPlot->axisRect()->addAxis(QCPAxis::atLeft);
yAxisD->setLabel("Y Axis D Label"); // 设置轴的标签文本
//yAxisD->setVisible(false); //设置坐标轴不显示(默认显示)
// 设置轴的刻度范围等其他属性
yAxisD->setRange(0, 100);
// 设置Y轴标签的可见性和字体等(如果需要)
yAxisD->setLabelFont(QFont(QFont().family(), 10)); // 例如,设置字体大小和家族
yAxisD->setLabelColor(Qt::black); // 设置标签颜色
// 设置曲线Y坐标轴
D->setValueAxis(yAxisD);
//设置折线颜色
D->setPen(QPen(Qt::black));
D->setVisible(true);
//EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
QCPAxis* yAxisE = customPlot->axisRect()->addAxis(QCPAxis::atLeft);
yAxisE->setLabel("Y Axis E Label"); // 设置轴的标签文本
//yAxisE->setVisible(false); //设置坐标轴不显示(默认显示)
// 设置轴的刻度范围等其他属性
yAxisE->setRange(0, 100);
// 设置Y轴标签的可见性和字体等(如果需要)
yAxisE->setLabelFont(QFont(QFont().family(), 10)); // 例如,设置字体大小和家族
yAxisE->setLabelColor(Qt::blue); // 设置标签颜色
// 设置曲线Y坐标轴
E->setValueAxis(yAxisE);
//设置折线颜色
E->setPen(QPen(Qt::blue));
E->setVisible(true);
// 模拟添加一些数据
QVector<double> x(100), yA(100), yB(100), yC(100), yD(100), yE(100);
for (int i = 0; i < 100; ++i) {
x[i] = i / 50.0 - 1;
yA[i] = qSin(x[i]) * 5 + 5; // A线数据
yB[i] = qCos(x[i]) * 5 + 150; // B线数据
yC[i] = qSin(x[i] / 0.5) * 5 + 250; // C线数据
yD[i] = qCos(x[i] / 0.3) * 5 + 350; // D线数据
yE[i] = qSin(x[i] * 2) * 5 + 450; // E线数据
}
A->setData(x, yA);
B->setData(x, yB);
C->setData(x, yC);
D->setData(x, yD);
E->setData(x, yE);
// 根据需要调整图表布局和其他设置
customPlot->rescaleAxes();
customPlot->replot();
}
WaveForm::~WaveForm()
{}
QCustomPlot多线同X轴不同Y轴
于 2024-04-13 11:58:22 首次发布