Using QwtPlot to plot binary data from file
Hi
I have some data files which I would like to plot using Qwt. The setData member function requires data of type double, however my data could be uchar, int or float. I know I could copy the data to an array of type double and then pass this to setData. Is there a better way of doing this?
Regards
Re: Using QwtPlot to plot binary data from file
Derive YourData from QwtData, and implement a bridge to your array. Be careful with reimplementing YourData::copy: you don't need to copy the values - only the interface.
Note, that QwtPlotCurve always iterates over the samples in increasing order. So you don't need to have all your data in memory at the same time.
Uwe
Re: Using QwtPlot to plot binary data from file
Thanks Uwe
You have lost me, could you elaborate on
"implementing a bridge to my array "
and
"with reimplementing YourData::copy: you don't need to copy the values - only the interface"
Regards
Re: Using QwtPlot to plot binary data from file
Code:
{
public:
YourData(uchar *values, int size):
m_values(values),
m_size(size)
{
}
virtual size_t size() const
{
return m_size;
}
virtual double x(size_t i) const
{
return (double)m_values[2 * i];
}
virtual double y(size_t i) const
{
return (double)m_values[2 *i + 1];
}
{
return new YourData(m_values, m_size);
}
};
The default implementation of QwtData::boundingRect iterates over all values. If you have many points and you know your bounding rect I recommend to implement YoutData::boundingRect too.
If possible I would recommend to use a QVector instead. Because it implicitely shares the values you can copy it and it is more safe, when you want to unload your values.
And yes passing a pointer of QwtData would have been a better API than this copy construction.
Uwe
Re: Using QwtPlot to plot binary data from file
Thanks Uwe
You have been a great help. Just one question about boundingRect, What is this used for?
Regards
Re: Using QwtPlot to plot binary data from file
hi
I am charu, totally New to Qwt , i have to plot from .txt file containing floating point. i want to know how to load/read a datafile and plot it row by row (that is: one value by one)
thank you for ur grt help
Re: Using QwtPlot to plot binary data from file
Hi,
I have implemented a realtime plot using my own Qwtdata interface. Each time I plot the curve a new copy of the interface is created. This is a small memory leak which leads to a big one over an extended period of time.
I cannot use an array without having to do a lot of memory shifting which is too expensive given I replot 5 times a second for a period ranging between a few minutes to many hours.
Is there a better way to do this and avoid the memory leak?
Here is a snippet;
Code:
double spdData[50];
int spdCur;
// Speed history
double RealtimeSpdData::x(size_t i) const { return (double)50-i; }
double RealtimeSpdData::y(size_t i) const { return spdData[(spdCur+i) < 50 ? (spdCur+i) : (spdCur+i-50)]; }
size_t RealtimeSpdData::size() const { return 50; }
QwtData *RealtimeSpdData
::copy() const { return new RealtimeSpdData
();
} //<<<LEAK void RealtimeSpdData::init() { spdCur=0; for (int i=0; i<50; i++) spdData[i]=0; }
void RealtimeSpdData::addData(double v) { spdData[spdCur++] = v; if (spdCur==50) spdCur=0; }
Re: Using QwtPlot to plot binary data from file
Quote:
Code:
QwtData *RealtimeSpdData
::copy() const { return new RealtimeSpdData
();
} //<<<LEAK
QwtPlotCurve::setData() always deletes the previously allocated data object before it creates a new clone ( check the code ). So I guess you are calling RealtimeSpdData::copy in your code without releasing the clone or the leak is from somewhere else. ( What about the object you pass to QwtPlotCurve::setData() - this one needs to be relased by the application ).
By the way: you don't need to create a new QwtData object when ever your data changes - it's enough to call plot->replot(). If you need the auto-replot feature you also have to call curve->itemChanged() after each addData().
Maybe its worth to implement RealtimeSpdData::boundingRect(). The default implementation iterates over all points, what could be done ore efficient in your case. For 50 points this will be an unimportant optimization, but maybe you want to have more points later.
Uwe
Re: Using QwtPlot to plot binary data from file
Quote:
Originally Posted by
Uwe
QwtPlotCurve::setData() always deletes the previously allocated data object before it creates a new clone ( check the code ). So I guess you are calling RealtimeSpdData::copy in your code without releasing the clone or the leak is from somewhere else. ( What about the object you pass to QwtPlotCurve::setData() - this one needs to be relased by the application ).
By the way: you don't need to create a new QwtData object when ever your data changes - it's enough to call plot->replot(). If you need the auto-replot feature you also have to call curve->itemChanged() after each addData().
Maybe its worth to implement RealtimeSpdData::boundingRect(). The default implementation iterates over all points, what could be done ore efficient in your case. For 50 points this will be an unimportant optimization, but maybe you want to have more points later.
Uwe
Thanks for the quick response. That's excellent. I guess my leak is somewhere else :-) I thought copy() was called by each replot but obviously not (I should have checked more thoroughly before posting, my apologies).
FWIW, the scrolling graph works really nicely (flicker free, smooth) on Win32, Mac and Linux.
Good Job Uwe!
Re: Using QwtPlot to plot binary data from file
I have a small question that could be obvious for many of you, but not for me...
Once you have your data inherited from QwtData
(e.g. in my case I have float numbers)
How do you pass it to setData (or setRawData in my case)?
Here the definitions of setData and setRawData:
(From qwtplotcurve manual)
Code:
void QwtPlotCurve::setData ( const double * xData,
const double * yData,
int size
)
Code:
void QwtPlotCurve::setRawData ( const double * xData,
const double * yData,
int size
)
I do not see the link in between my YourData (derived from QwtData) and how to "link" it to my QwtPlot...
Thank you...
Re: Using QwtPlot to plot binary data from file
All other setData methods are convenience methods creating an internal QwtData object. Passing values to your individual data object needs to be done via the API of your derived data class.
Uwe
Re: Using QwtPlot to plot binary data from file
Hi, I also want to plot some data from a data file. I have QWT 6.0 and it doesn't include QwtData. Does anyone know where I can get this code, or if there is another way to make a plot from a data file with the tools in qwt 6.0?
Thanks
Re: Using QwtPlot to plot binary data from file
Hi,
I just posted a thread about using data from a file to make a plot so I guess it doesn't hurt to ask here too.
Basically after reading the whole thread and trying to make sense of things, I still don't full understand... >.<
My main problem is:
The data is in a file; I want to make a bridge to this data...
Does this mean a bridge to the data file? or has the data already been read in somewhere. If so, then how?
somewhere mentioned above is a "bridge to an array", but my data is too big to fit into an array and multi-dimensional arrays dont seem to work (for qwtplot).
Any help will be appretiated!
Thanks