For example, in Visual Studio there is tChart and its Series property, which is responsible for drawing lines of the graph. Here is an example of this code
for (int j = 1; j < Nt - 1; j++)
{
for (int i = 1; i < Nt - 1; i++)
{
chart2->Series["" + (j + 1).ToString()]->Points->AddXY(i, wht[j][i]);
}
}
And draw this graph with a lot of lines.
But my task is transfering in Qt Creator
(because in Qt Creator
may making a lot of opportunities)
This code
void MainWindow::drawdifnet(int Nt)
{
int N=Nt;
int N1=pow(N,2);
QVector<double> x22(N), y22(N1);
int ii=0,jj=0;
for (int j = 0; j < Nt ; j++)
{
for (int i = 0; i < Nt ; i++)
{
x22[jj]=i;
y22[ii]=wht[j][i];
ui->widget_2->addGraph();
ui->widget_2->graph(0)->setData(x22,y22);
ii++;
}
jj++;
}
ui->widget_2->xAxis->setLabel("OsX");
ui->widget_2->yAxis->setLabel("OsY");
ui->widget_2->xAxis->setRange(30,30);
ui->widget_2->replot();
}
Doesn't work correctly. Result is the empty widget
In first I with help debugger check the QVectors
data
In this pictures see that my dinamic array wht[j][i]
in work and loaded in QVector yy[ii]
I think the problem in the loop.
In QtCustomPlot tutorial this problem solving this code
ui->widget_2->graph(0)->setData(x,y);
ui->widget_2->graph(1)->setData(x11,y11);
ui->widget_2->graph(2)->setData(x22,y22);
But in my situation the quantity of lines is know when the program working.
How I create and assigned my array
void created(int Nt, int Nx) ///This function creating my dynamic array
{
wht = new double *[Nt];
for (int i = 0; i < Nt; i++)
wht[i] = new double[Nx];
}
inline double fn(int T, double x) ///these 4 functions for my mathematical part(works good)
{
if (x >= 0)
return T;
return 0;
}
inline double u0(int T, double x)
{
return fn(T, x);
}
inline double u1(int T, double a, int xmin, double t)
{
return fn(T, xmin - a * t);
}
inline double u2(int T, double a, int xmax, double t)
{
return fn(T, xmax - a * t);
}
void calculatedifnet(int xmin, double hx, double ht, double a, int Nx, int Nt, int T)
//These main function.We have the empty array and in this function we fill array. Then we solve in the main loop and the fill the first indexes wht[j]
{
for (int i = 0; i < Nt; i++)
{
wht[0][i] = u0(T, xmin + i*hx);//fill the second indexeswht[null][i]
}
for (int j = 0; j < Nt - 1; j++)//the calculated code(works right).The result writing in wht[j]
{
wht[j + 1][0] = u1(T, a, xmin, j*ht);
for (int i = 1; i < Nt; i++)
{
double dudx = (wht[j][i] - wht[j][i - 1]) / hx;
wht[j + 1][i] = -a * dudx * ht + wht[j][i];
}
}
}
In your code there are the following errors:
0
to Nt-1
, then we only have to create it once:QVector<double> x(Nt);
for (int i = 0; i < Nt ; i++)
x[i]=i;//0 to Nt-1
addGraph()
adds a graph and places it in the last position, if you want to graph you must access by the last index, not by the index 0:ui->widget_2->addGraph()->setData(xx, yy);
wht
is of type QVector<QVector<double>>
and of size NtxNt
, then is not necessary to access each element, we can access each QVector<double>
since the function setData()
accepts as input this type of data. To the function setData()
you must pass 2 vectors of the same size, but you were passing 2 vectors of Nt
and Nt*Nt
, this generated a warning:ui->widget_2->addGraph()->setData(x, wht[j]);
setRange()
places the range from a to b, but if they are the same QCustomPlot will never fit the range, for my test I set it as follows:ui->widget_2->xAxis->setRange(0,Nt);
ui->widget_2->yAxis->setRange(0,Nt*Nt);
In short the code would be as follows:
void MainWindow::drawdifnet(int Nt){
QVector<double> x(Nt);
for (int i = 0; i < Nt ; i++)
x[i]=i;//0 to Nt-1
for (int j = 0; j < Nt ; j++)
ui->widget_2->addGraph()->setData(x, wht[j]);
/* if c++11
for (auto& row: wht)
ui->widget_2->addGraph()->setData(x, row);
*/
ui->widget_2->xAxis->setLabel("OsX");
ui->widget_2->yAxis->setLabel("OsY");
ui->widget_2->xAxis->setRange(0,Nt);
ui->widget_2->yAxis->setRange(0,Nt*Nt);
ui->widget_2->replot();
}
Output:
Note: For the test wht[i][j] = i*j
In your case wht
is a variable of type double**
, also assume that Nx>=Nt
, for this you must use the following code:
void MainWindow::drawdifnet(int Nt)
{
QVector<double> x(Nt);
for (int i = 0; i < Nt ; i++){
x[i]=i;//0 to Nt-1
}
QVector<double> y(Nt);
for(int i=0; i<Nt; i++){
for(int j=0; j<Nt; j++){
y[j] = wht[i][j];
}
ui->widget_2->addGraph()->setData(x, y);
}
ui->widget_2->xAxis->setLabel("OsX");
ui->widget_2->yAxis->setLabel("OsY");
ui->widget_2->xAxis->setRange(0,12);
ui->widget_2->yAxis->setRange(0,3.5);
ui->widget_2->replot();
}
Input:
Output: