PDA

View Full Version : 2D array data plot!



kahramonj
18th March 2009, 18:19
Hi QWT Forumers!

I'd like to draw 2d array data and it is my first experience with QWT, everything is OK, but the following code is drawing only ONE(last) curve. Dear fellowship, please help !



Plot::Plot()
{
setTitle("A Simple QwtPlot Demonstration");
insertLegend(new QwtLegend(), QwtPlot::RightLegend);

// Set axis titles
setAxisTitle(xBottom, "x -->");
setAxisTitle(yLeft, "y -->");

// Insert new curves
QwtPlotCurve *cSin = new QwtPlotCurve("y = sin(x)");
#if QT_VERSION >= 0x040000
cSin->setRenderHint(QwtPlotItem::RenderAntialiased);
#endif
cSin->setPen(QPen(Qt::red));
cSin->attach(this);

const int nPoints = 100;
double x[2][nPoints], y[2][nPoints];
for(int j=0; j<2; j++)
{
for(int i=0; i<nPoints; i++)
{
x[j][i] = -3.14 + i*(j+2)*3.14/(nPoints-1);
y[j][i] = sin((j+1)*x[j][i]);
}
}

// Create 2d array data
for(int j=0; j<2; j++)
cSin->setData(x[j],y[j],nPoints);

}

Uwe
18th March 2009, 18:47
I'd like to draw 2d array data and it is my first experience with QWT, everything is OK, but the following code is drawing only ONE(last) curve.
Not surprising, because in your code is only one curve. The second setData call changes the points of the curve, but doesn't create a second one.

Instead you need to attach 2 different QwtPlotCurve objects - one for each array.

Uwe

kahramonj
19th March 2009, 07:56
Not surprising, because in your code is only one curve. The second setData call changes the points of the curve, but doesn't create a second one.

Instead you need to attach 2 different QwtPlotCurve objects - one for each array.

Uwe

Thanks, I think the problem is solved, the changed code:


Plot::Plot()
{
setTitle("A Simple QwtPlot Demonstration");
insertLegend(new QwtLegend(), QwtPlot::RightLegend);

// Set axis titles
setAxisTitle(xBottom, "x -->");
setAxisTitle(yLeft, "y -->");

// Insert new curves
QwtPlotCurve **cSin;
cSin= new QwtPlotCurve* [2]; //("y = sin(x)");
for(int j=0; j<2; j++)
{
cSin[j]=new QwtPlotCurve;
#if QT_VERSION >= 0x040000
cSin[j]->setRenderHint(QwtPlotItem::RenderAntialiased);
#endif
cSin[j]->setPen(QPen(Qt::red));
cSin[j]->attach(this);
}

const int nPoints = 100;
double x[2][nPoints], y[2][nPoints];
for(int j=0; j<2; j++)
{
for(int i=0; i<nPoints; i++)
{
x[j][i] = -3.14 + i*(j+2)*3.14/(nPoints-1);
y[j][i] = sin((j+1)*x[j][i]);
}
}

// Create 2d array data
for(int j=0; j<2; j++)
cSin[j]->setData(x[j],y[j],nPoints);

}

charu
21st March 2009, 11:48
hi,

can u plz tell me how to take data from a .txt file and plot the same?

thanks u so much 4r ur help