.h file
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QVector>
#include "qcustomplot.h"
#include <QVBoxLayout>
#include <QFile>
#include <QTextStream>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void plotData(); // Slot function to handle data plotting
private:
Ui::MainWindow *ui;
//bool readData(const QString &filename, QVector<double> &x, QVector<double> &y, QVector<double> &z);
};
#endif // MAINWINDOW_H
.cpp file
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QVector>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
plotData(); // Call plotData to load data and plot it
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::plotData()
{
// Prepare data
int nx = 100; // Number of steps in x
int ny = 100; // Number of steps in y
QVector<double> x(nx), y(ny);
QVector<QVector<double>> z(nx, QVector<double>(ny));
// Fill x and y arrays
for (int i = 0; i < nx; ++i) {
x = -1.0 + (i / static_cast<double>(nx - 1)) * 2.0; // Range: -1 to 1
}
for (int j = 0; j < ny; ++j) {
y[j] = 7.5 + (j / static_cast<double>(ny - 1)) * 3.5; // Range: 7.5 to 11.0
}
// Fill z array with some example data
for (int i = 0; i < nx; ++i) {
for (int j = 0; j < ny; ++j) {
z[j] = 1.5 + 0.3 * sin(M_PI * x) * cos(M_PI * y[j] / 11); // Example function
}
}
// Create a color map
QCPColorMap *colorMap = new QCPColorMap(ui->customPlot->xAxis, ui->customPlot->yAxis);
colorMap->data()->setSize(nx, ny);
colorMap->data()->setRange(QCPRange(-1.0, 1.0), QCPRange(7.5, 11.5));
// Fill colorMap with data
for (int i = 0; i < nx; ++i) {
for (int j = 0; j < ny; ++j) {
colorMap->data()->setCell(i, j, z[j]);
}
}
// Set up color scale and gradient
colorMap->setInterpolate(true);
colorMap->setColorScale(new QCPColorScale(ui->customPlot));
colorMap->colorScale()->setDataRange(QCPRange(1.5, 2.0)); // Example range, adjust as needed
colorMap->colorScale()->setGradient(QCPColorGradient::gpJet); // Set color gradient
// Add color scale
QCPColorScale *colorScale = new QCPColorScale(ui->customPlot);
ui->customPlot->plotLayout()->addElement(0, 1, colorScale);
// Add contour lines
//colorMap->setColorScale( setContourBegin(1.5);
//colorMap->setContourEnd(2.0);
//colorMap->setContourLineCount(10); // Number of contour lines
// Set color gradient
//colorMap->setGradient(QCPColorGradient::gpPolar);
//colorMap->rescaleDataRange();
// Add contour lines (optional)
// contour lines can be plotted using QCPGraph or another QCustomPlot feature
// Add color scale
//QCPColorScale *colorScale = new QCPColorScale(ui->customPlot);
// ui->customPlot->plotLayout()->addElement(0, 1, colorScale);
//colorScale->attachAxis(customPlot->yAxis);
//colorScale->setRange(1.47, 1.82); // Set according to your z data range
// Set axes labels and other customizations
ui->customPlot->xAxis->setLabel("Theta");
ui->customPlot->yAxis->setLabel("Radius");
//customPlot->setTitle("Contour Plot PTT");
// Resize window
resize(800, 600);
ui->customPlot->replot();
}
.ui file
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QCustomPlot" name="customPlot" native="true">
<property name="geometry">
<rect>
<x>30</x>
<y>30</y>
<width>731</width>
<height>511</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>QCustomPlot</class>
<extends>QWidget</extends>
<header location="global">qcustomplot.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
can anyone quickly help into it