简体   繁体   English

如何使用QCustomPlot在Qt中绘制几行图形

[英]How to draw several lines of the graph in Qt using QCustomPlot

For example, in Visual Studio there is tChart and its Series property, which is responsible for drawing lines of the graph. 例如,在Visual Studio中有tChart及其Series属性,它负责绘制图形的线条。 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 但我的任务是在Qt Creator转移(因为在Qt Creator可能会提供很多机会)这段代码

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] 在第一个我用帮助调试器检查QVectors数据在这张图片中看到我的dinamic数组wht[j][i]在工作并加载到QVector yy[ii]

在此输入图像描述 在此输入图像描述

I think the problem in the loop. 我认为循环中的问题。

In QtCustomPlot tutorial this problem solving this code 在QtCustomPlot教程中解决此代码的问题

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: 在您的代码中存在以下错误:

  • If we observe x is a constant vector from 0 to Nt-1 , then we only have to create it once: 如果我们观察x是从0Nt-1的常量向量,那么我们只需要创建一次:

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: addGraph()添加一个图形并将其放在最后一个位置,如果你想要图形,你必须通过最后一个索引而不是索引0来访问:

ui->widget_2->addGraph()->setData(xx, yy);
  • Assuming that 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. 假设wht的类型为QVector<QVector<double>>和大小的NtxNt ,然后是没有必要的访问的每个元素,我们可以访问每个QVector<double>因为该函数setData()接受为输入这种类型的数据。 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: 对于函数setData()你必须传递2个相同大小的向量,但是你传递了NtNt*Nt 2个向量,这会产生一个警告:

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: setRange()将范围从a放到b,但是如果它们是相同的,QCustomPlot将永远不适合范围,对于我的测试,我将其设置如下:

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 注意:对于测试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: 在你的情况下, whtdouble**类型的变量,也假设Nx>=Nt ,为此必须使用以下代码:

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: 输入:

  • created(12, 12); 创造(12,12);
  • calculatedifnet(1, .5, .5, 0.9, 12, 12, 3); calculatedifnet(1,.5,.5,0.9,12,12,3);

Output: 输出:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM