PDA

View Full Version : Problem with scales



fear
4th May 2008, 18:48
In below code I'm trying to set scales for X and Y axis. I use setAxisScale but scale doesn't change and I don't see any curves. I must admit Qwt doesn't have user-friendly designed interface.



#ifndef PLOT_H
#define PLOT_H

#include <qvaluelist.h>

#include <qwt_plot.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_curve.h>
#include <qwt_legend.h>
#include <qwt_scale_engine.h>
#include <qwt_data.h>
#include <qwt_text.h>

#include <math.h>

class Plot : public QwtPlot
{
public:
Plot()
{
curves.setAutoDelete(true);
X.setAutoDelete(true);
Y.setAutoDelete(true);

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

// Set axis titles
setAxisTitle(xBottom, "x -->");
setAxisTitle(yLeft, "y -->");
/*
setAxisAutoScale(xBottom);
setAxisAutoScale(xTop);
setAxisAutoScale(yLeft);
setAxisAutoScale(yRight);
*/
minX = 0.0;
maxX = 0.0;
minY = 0.0;
maxY = 0.0;

setAxisScaleEngine(xBottom, new QwtLinearScaleEngine());
setAxisScaleEngine(yLeft, new QwtLinearScaleEngine());

// Insert markers

// ...a horizontal line at y = 0...
/*
QwtPlotMarker *mY = new QwtPlotMarker();
mY->setLabel(QString::fromLatin1("y = 0"));
mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
mY->setLineStyle(QwtPlotMarker::HLine);
mY->setYValue(0.0);
mY->attach(this);
*/

// ...a vertical line at x = 2 * pi
/*
QwtPlotMarker *mX = new QwtPlotMarker();
mX->setLabel(QString::fromLatin1("x = 2 pi"));
mX->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
mX->setLineStyle(QwtPlotMarker::VLine);
mX->setXValue(1000.0);
mX->attach(this);
*/
}

~Plot()
{
curves.clear();
X.clear();
Y.clear();
}

void addCurve(QValueList<double> & list)
{
double * x = new double[list.count()];
double * y = new double[list.count()];

for (unsigned int i = 0 ; i < list.count() ; i++)
{
x[i] = i;
y[i] = list[i];
}

X.append(x);
Y.append(y);

QwtPlotCurve * c = new QwtPlotCurve();

c->setRawData(x, y, list.count());
c->attach(this);

if (c->minXValue() < minX)
minX = c->minXValue();

if (c->maxXValue() > maxX)
maxX = c->maxXValue();

if (c->minYValue() < minY)
minY = c->minYValue();

if (c->maxYValue() > maxY)
maxY = c->maxYValue();

setAxisScale(xBottom, minX, maxX);
setAxisScale(yLeft, minY, maxY);

curves.append(c);
}

private:
QPtrList<QwtPlotCurve> curves;
QPtrList<double> X;
QPtrList<double> Y;
double minX;
double maxX;
double minY;
double maxY;
};

#endif

Uwe
5th May 2008, 07:21
a) Guess you miss to call replot ( or enable autoReplot() ).
b) Adopting the scales to the bounding rect of your curves is what autoscaling does. So better remove your redundant code.
c) Using setRawData is intended to avoid pointless copying of values. Copying + using setRawData doesn't make much sense because you have to take care of the temporary memory (better copy your values into a QwtArray<double> ). But in your example I would implement an individual data class to avoid wasting memory for the pointless x array.



#ifndef PLOT_H
#define PLOT_H

#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_legend.h>
#include <qwt_data.h>

class YourData: public QwtData
{
public:
YourData(const QValueList<double>& values):
_values(values)
{
}

virtual QwtData *copy() const
{
return new YourData(_values);
}

virtual size_t size() const
{
return _values.count();
}

virtual double x(size_t i) const
{
return i;
}

virtual double y(size_t i) const
{
return _values[i];
}
private:
QValueList<double> _values;
};


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

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

void addCurve(QValueList<double> & list)
{
QwtPlotCurve * c = new QwtPlotCurve();
c->setData(YourData(list));
c->attach(this);

replot();
}
};


Uwe

fear
5th May 2008, 23:18
Yes it was because of no replot(). I also used idea with setData(). Thanks.