PDA

View Full Version : Strange extra line in my plotting, help please



cooper
26th October 2010, 00:24
Hi everyone,

I am new to the Qwt. I need to plot a curve against time. Here is my code:




int plotSize = 100;
int timeCnt = 0;

void cycleScrDialog::setPlot()
{
// create a plot
QwtPlot *cyclePlot;
QwtText title("cyclePlot");
cyclePlot = new QwtPlot(title);

// add curves
QwtPlotCurve *curveT = new QwtPlotCurve("Temperature");
curveT->setPen(QPen(Qt::red,1));
curveT->attach(ui->qwtPlot);
QwtPlotCurve *curveP = new QwtPlotCurve("Pressure");
curveP->setPen(QPen(Qt::blue,1));
curveP->attach(ui->qwtPlot);

// add Axis
ui->qwtPlot->setAxisScale(QwtPlot::xBottom, 0, 100);
ui->qwtPlot->setAxisFont(QwtPlot::xBottom, QFont(QString("Sans Serif"), 5, 0, false));
ui->qwtPlot->setAxisScale(QwtPlot::yLeft, -1, 1);
ui->qwtPlot->setAxisFont(QwtPlot::yLeft, QFont(QString("Sans Serif"), 5, 0, false));

// initialize plot data
for (int i = 0; i < plotSize; i ++)
{
x[i] = 0;
yT[i] = 0;
yP[i] = 0;
}

curveT->setRawData(x,yT,plotSize);
curveP->setRawData(x,yP,plotSize);
}

void cycleScrDialog::timerEvent() // timer generates event every 1/4 second
{
if (timeCnt < plotSize)
{
x[timeCnt] = timeCnt;
yT[timeCnt] = sin(timeCnt * 0.25);
yP[timeCnt] = cos(timeCnt * 0.5);
ui->qwtPlot->replot();
timeCnt ++;
}
else
timeCnt = 0;
}


I have two questions here:
1. when i run the program, it is plotting a straight line between the (0,0) and new plot dot coordinate.
http://i53.tinypic.com/3450ljm.jpg

I have read through this forum. but i didn't anyone get the same problem.

2. I can resize the axis font. However, I can't find a way to resize the axis itself (in green area). I wonder the axis size is fixed, and cannot be changed. Am I right?

http://i52.tinypic.com/w1tmo4.jpg

Can anyone give me a hint, please?
Thanks in advance.

Uwe
26th October 2010, 06:51
1) Obviously there is a (0, 0) at the end of your data left from the initialization
2) plotLayout()->setAlignCanvasToScales(true);

Uwe

cooper
27th October 2010, 02:24
Hi Uwe,

Thanks for your kind reply.

1) You can see I want to plot temperature and pressure data against time. I have tried different code. But I either got curve1 or curve2
http://i56.tinypic.com/qqs4us.jpg
curve 1

http://i51.tinypic.com/qpry1k.jpg
curve 2

the code for curve1:



int plotSize = 100;
int timeCnt = 0;

// initialize plot data
for (int i = 0; i < plotSize; i ++)
{
x[i] = 0;
yT[i] = 0;
yP[i] = 0;
}
curveT->setRawData(x,yT,plotSize);
curveP->setRawData(x,yP,plotSize);

void cycleScrDialog::timerEvent() // timer1 generate event every 1/4 second
{
timeCnt ++;
if (timeCnt % 4 == 0)
{
x[chartCnt] = chartCnt;
yT[chartCnt] = 0.9;
yP[chartCnt] = -0.5;
ui->qwtPlot->replot();
if (chartCnt < plotSize)
chartCnt ++;
}
}


code for curve 2



int plotSize = 100;
int timeCnt = 0;

// initialize plot data
for (int i = 0; i < plotSize; i ++)
{
x[i] = i; // here is the only difference
yT[i] = 0;
yP[i] = 0;
}
curveT->setRawData(x,yT,plotSize);
curveP->setRawData(x,yP,plotSize);

void cycleScrDialog::timerEvent() // timer1 generate event every 1/4 second
{
timeCnt ++;
if (timeCnt % 4 == 0)
{
x[chartCnt] = chartCnt;
yT[chartCnt] = 0.9;
yP[chartCnt] = -0.5;
ui->qwtPlot->replot();
if (chartCnt < plotSize)
chartCnt ++;
}
}


it looks like it always links to next data (initialized data) when it plotting the last data. Can do I get rid of last plotting data, then it will look like:
http://i51.tinypic.com/2ce10cn.jpg

2) to resize the scale, i changed my code as follow. However, no matter how do I changed the “int margin” value, I didn't see any changed of the scale. Could you please give me some advice about my code?



void cycleScrDialog::setPlot()
{
// create plot
QwtPlot *cyclePlot;
QwtText title("cyclePlot");
cyclePlot = new QwtPlot(title);

// resize scale
QwtPlotLayout *layout = new QwtPlotLayout;
layout->setAlignCanvasToScales(false);
layout->setCanvasMargin(2,-1);

// add curves
QwtPlotCurve *curveT = new QwtPlotCurve("Temperature");
curveT->setPen(QPen(Qt::red,1));
curveT->attach(ui->qwtPlot);
QwtPlotCurve *curveP = new QwtPlotCurve("Pressure");
curveP->setPen(QPen(Qt::blue,1));
curveP->attach(ui->qwtPlot);

// add Axis
ui->qwtPlot->setAxisScale(QwtPlot::xBottom, 0, 100);
ui->qwtPlot->setAxisFont(QwtPlot::xBottom, QFont(QString("Sans Serif"), 5, 0, false));
ui->qwtPlot->setAxisScale(QwtPlot::yLeft, -1, 1);
ui->qwtPlot->setAxisFont(QwtPlot::yLeft, QFont(QString("Sans Serif"), 5, 0, false));



Thanks a lot :)

Uwe
27th October 2010, 06:48
The margin has no effect, when you align the scales to the canvas.

Uwe

cooper
27th October 2010, 23:44
Hi Uwe,

Thanks for your reply.
Do you have any idea about the extra straight line in the end of curve?

Thanks a lot

cooper
28th October 2010, 02:17
I solved the first problem, but i think i did it in hard way. if anyone would point me a good and right way, i will be really appreciate.
here is my code:



void cycleScrDialog::timerEvent() // timer1 generate event every 1/4 second
{
timeCnt ++;
if (timeCnt % 4 == 0)
{
x[chartCnt] = chartCnt;
yT[chartCnt] = 0.9;
yP[chartCnt] = -0.5;
curveT->setRawData(x,yT,chartCnt); // here is the point
ui->qwtPlot->replot();
if (chartCnt < plotSize)
chartCnt ++;
}
}


Uwe,

I am not quite clear about your reply.
in your first reply, you said:


plotLayout()->setAlignCanvasToScales(true);
I though i need to use setAlignCanvasToScales(true) to achieve it.

but in your second reply, you told me

The margin has no effect, when you align the scales to the canvas.

do you mind give me some code please?

Thank you very much

Uwe
28th October 2010, 06:44
You need to understand the meaning of the different layout options - no code.

Uwe

cooper
28th October 2010, 20:35
thanks Uwe, i will read the qwt layout class again.

cooper
29th October 2010, 02:29
Hi Uwe,

I have tried the plotLayout()->setAlignCanvasToScales. Here is my code:


ui->qwtPlot->plotLayout()->setAlignCanvasToScales(false);
ui->qwtPlot->plotLayout()->setCanvasMargin(50, -1);


I got this:
http://i54.tinypic.com/jg4f9s.jpg

the length of the scale backbone has been shorted.
What i really want is to make the scale backbone skinner. The reason is that the screen what i am running my application is only 4.3". I need get more space to display the chart. Therefore, i want to save some scale backbone space (even a little bit) to my chart. I wish the scale backbone will be looks like:

http://i52.tinypic.com/6xubsj.jpg

Is that possible to achieve? Thanks for your help.

Uwe
29th October 2010, 06:41
Maybe plot->axisWidget(...)->setMargin(0); is what you want. You can also save some pixels by reducing the spacing and you could disable the frame of the canvas. Another idea is to disable the left axis completely and to insert a QwtPlotScaleItem on the canvas instead.

Uwe

cooper
1st November 2010, 00:24
Thanks Uwe,

after i setup "setMargin(0)" and disable the frame of canvas, I got saved some pixels in my plotting screen.

Thank for your help