How to implement QwtPlotSpectrogram with QwtMatrixRasterData
I implemented an example based on the 'spectrogram' example by exchanging the 'SpectrogramData' class with the build in 'QwtMatrixRasterData' class.
The problem is that the data is not shown and the plot remains empty. I would like to know what is missing in this code.
Code:
#include <qwt_plot.h>
#include <qwt_plot_spectrogram.h>
#include <qwt_matrix_raster_data.h>
{
Q_OBJECT
public:
QMatrixPlot
(QWidget *parent
= NULL);
virtual ~QMatrixPlot()
{
if (d_spectrogram)
delete d_spectrogram;
}
void setMatrixData(const QVector< double > &values, int numColumns);
private:
QwtMatrixRasterData * m_MatrixRasterData;
int d_alpha;
int d_mapType;
};
Code:
#include "QMatrixPlot.h"
#include "qcolormap.h"
#include "qwt_plot_layout.h"
#include "qwt_scale_widget.h"
QMatrixPlot
::QMatrixPlot(QWidget *parent
): d_alpha(255)
{
d_spectrogram->setRenderThreadCount( 0 ); // use system specific thread count
m_MatrixRasterData = new QwtMatrixRasterData();
d_spectrogram->setData( m_MatrixRasterData );
d_spectrogram->attach( this );
}
void QMatrixPlot::setMatrixData(const QVector< double > &values, int numColumns)
{
m_MatrixRasterData->setValueMatrix (values, numColumns);
const QwtInterval zInterval = d_spectrogram->data()->interval( Qt::ZAxis );
setAxisScale
( QwtPlot::yRight, zInterval.
minValue(), zInterval.
maxValue() );
axis->setColorMap( zInterval, QColorMap::map(d_mapType) );
}
Code:
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include "qmatrixplot.h"
#include <vector>
using std::vector;
int main( int argc, char **argv )
{
QMatrixPlot * plot = new QMatrixPlot();
plot->setTitle( "2D Plot Demo" );
// create data
QVector<double> x(100);
QVector<double> y1(x.size());
for (size_t i = 0; i< x.size(); ++i) { x[i] = int(i)-50; }
for (size_t i = 0; i< y1.size(); ++i) { y1[i] = sin(double(x[i])/10.0); }
plot->setMatrixData(y1, 10);
plot->replot();
window.setCentralWidget(plot);
window.resize(800, 600);
window.show();
return a.exec();
}
Re: How to implement QwtPlotSpectrogram with QwtMatrixRasterData
It seems as if the intervall for all axis (x, y, z) must be set:
Code:
void QMatrixPlot::setMatrixData(const QVector< double > &values, int numColumns)
{
size_t rows = values.size()/numColumns;
m_MatrixRasterData->setInterval( Qt::XAxis, QwtInterval( 0, numColumns ) );
m_MatrixRasterData->setInterval( Qt::YAxis, QwtInterval( 0, rows ) );
double minValue = *std::min_element( std::begin(values), std::end(values) );
double maxValue = *std::max_element( std::begin(values), std::end(values) );
m_MatrixRasterData->setInterval( Qt::ZAxis, QwtInterval(minValue, maxValue) );
m_MatrixRasterData->setValueMatrix (values, numColumns);
d_spectrogram->setData( m_MatrixRasterData );
const QwtInterval zInterval = d_spectrogram->data()->interval( Qt::ZAxis );
setAxisScale
( QwtPlot::yRight, zInterval.
minValue(), zInterval.
maxValue() );
axis->setColorMap( zInterval, QColorMap::map(d_mapType) );
}
However since QwtMatrixRasterData is to my understanding supposed to be a complete class, I do not understand why this is not implemented within setData.
The other thing I do not understand is why the QwtPlotSpectrogram needs a new QwtMatrixRasterData pointer for every new data. Why is this not attached
and a signal used?
Re: How to implement QwtPlotSpectrogram with QwtMatrixRasterData
Quote:
Originally Posted by
pospiech
However since QwtMatrixRasterData is to my understanding supposed to be a complete class, I do not understand why this is not implemented within setData.
It is a convenience class, but in the majority of the use cases copying values into a matrix is not the best approach. In fact I would say in about 90% of my own use cases I implemented an individual type of bridge between the data and the plot ( = QwtRasterData ) tailored for the specific situation of the application.
Always keep in mind, that we are talking about potentially tera bytes of data, or the other way round no data at all - like in the spectrogram example.
Uwe