// Usage
Plot2D* pLogSpectrum = new Plot2D( "Spectrum", "frequency", "magnitude [dB]", Qt::darkBlue, app );
pLogSpectrum->attachCurve( pXData, pLogMag, HALF_LENGTH, "noisy sine", Qt::cyan );
pLogSpectrum->setMarkerOnMax( pXData, pLogMag, HALF_LENGTH, -3 );
// Usage
Plot2D* pLogSpectrum = new Plot2D( "Spectrum", "frequency", "magnitude [dB]", Qt::darkBlue, app );
pLogSpectrum->attachCurve( pXData, pLogMag, HALF_LENGTH, "noisy sine", Qt::cyan );
pLogSpectrum->setMarkerOnMax( pXData, pLogMag, HALF_LENGTH, -3 );
To copy to clipboard, switch view to plain text mode
// Declaration
{
Q_OBJECT
public:
Plot2D
( QString plotTitle
= "Data Plot",
QColor backgoundColor
= Qt
::darkGray,
void attachCurve( double* xData,
double *yData,
int dataCount,
QColor lineColor
= Qt
::yellow );
void setMarkerOnMax( SLData_t* xData,
SLData_t* yData,
SLArrayIndex_t dataCount,
int precision = 4, // positive value for '%.*g', negative value for '%.*f'
Qt::Alignment alignment = Qt::AlignHCenter | Qt::AlignTop );
void setMarker( double X,
double Y,
int precision = 4, // positive value for '%.*g', negative value for '%.*f'
Qt::Alignment alignment = Qt::AlignHCenter | Qt::AlignTop );
...
public Q_SLOTS:
void printDocument();
void exportDocument();
protected:
private:
int mWindowHeight, mWindowWidth;
int mWindowXPosition, mWindowYPosition;
};
// Declaration
class Plot2D: public QwtPlot
{
Q_OBJECT
public:
Plot2D( QString plotTitle = "Data Plot",
QString xLabel = "x-axis",
QString yLabel = "y-axis",
QColor backgoundColor = Qt::darkGray,
QWidget *parent = NULL );
void attachCurve( double* xData,
double *yData,
int dataCount,
QString curveLabel = "",
QColor lineColor = Qt::yellow );
void setMarkerOnMax( SLData_t* xData,
SLData_t* yData,
SLArrayIndex_t dataCount,
int precision = 4, // positive value for '%.*g', negative value for '%.*f'
Qt::Alignment alignment = Qt::AlignHCenter | Qt::AlignTop );
void setMarker( double X,
double Y,
int precision = 4, // positive value for '%.*g', negative value for '%.*f'
Qt::Alignment alignment = Qt::AlignHCenter | Qt::AlignTop );
...
public Q_SLOTS:
void autoRescale( QRectF );
void printDocument();
void exportDocument();
protected:
virtual void resizeEvent( QResizeEvent * );
private:
int mWindowHeight, mWindowWidth;
int mWindowXPosition, mWindowYPosition;
QwtPlotGrid* grid;
QwtLegend* legend;
QwtPlotZoomer* zoomer;
QPushButton* btnPrint;
QPushButton* btnExport;
};
To copy to clipboard, switch view to plain text mode
// implementation
{
// plot object
setWindowTitle
( QString( "SigLibGraph - Plot2D" ));
setTitle( plotTitle );
setCanvasBackground( backgroundColor);
// window size and position
setMinimumSize
( QSize( 240,
170 ));
mWindowWidth = 550; mWindowHeight = 365;
mWindowXPosition = 200; mWindowYPosition = 200;
setGeometry( mWindowXPosition, mWindowYPosition, mWindowWidth, mWindowHeight );
// axis
setAxisTitle
( QwtPlot::xBottom, xLabel
);
setAxisTitle
( QwtPlot::yLeft, yLabel
);
// grid
grid->enableXMin( true );
grid
->setMajPen
( QPen( Qt
::white,
0, Qt
::DotLine ));
grid
->setMinPen
( QPen( Qt
::gray,
0 , Qt
::DotLine ));
grid->attach( this );
// legend
insertLegend
( legend,
QwtPlot::RightLegend );
//zoomer
...
// print functionality
...
// export functionality
...
show();
}
void Plot2D::attachCurve( SLData_t* xData,
SLData_t* yData,
int dataCount,
{
curve->setSamples( xData, yData, dataCount );
// following works well
//qDebug() << "xMin=" << curve->minXValue() << " | xMax=" << curve->maxXValue()
// << " | yMin=" << curve->minYValue() << " | yMax=" << curve->maxYValue()
// << " | data count=" << curve->dataSize();
curvePen.setColor( lineColor );
curve->setPen( curvePen );
curve
->setRenderHint
( QwtPlotItem::RenderAntialiased );
curve->setTitle( curveLabel );
if ( curveLabel.isEmpty() )
else
curve->attach( this );
}
void Plot2D::setMarkerOnMax( SLData_t* xData,
SLData_t* yData,
SLArrayIndex_t dataCount,
int precision, // positive value for '%.*g', negative value for '%.*f'
Qt::Alignment alignment )
{
SLArrayIndex_t index = SDA_MaxPos( xData, dataCount ); // get the index of max value
setMarker( xData[index], yData[index], precision, alignment ); // set marker position
}
void Plot2D::setMarker( double X,
double Y,
int precision,
Qt::Alignment alignment )
{
marker->setValue( X, Y );
if (precision < 0)
label.sprintf( "(%.*f | %.*f)", -precision, X, -precision, Y );
else
label.sprintf( "(%.*g | %.*g)", precision, X, precision, Y );
text.
setColor( QColor( Qt
::white ));
text.
setFont( QFont("Helvetica",
8,
QFont::Bold ));
marker->setLabel( text );
marker->setLabelAlignment( alignment );
marker->attach( this );
}
// implementation
Plot2D::Plot2D( QString plotTitle,
QString xLabel,
QString yLabel,
QColor backgroundColor,
QWidget *parent ):
QwtPlot( parent )
{
// plot object
setWindowTitle ( QString( "SigLibGraph - Plot2D" ));
setTitle( plotTitle );
setCanvasBackground( backgroundColor);
// window size and position
setMinimumSize( QSize( 240, 170 ));
mWindowWidth = 550; mWindowHeight = 365;
mWindowXPosition = 200; mWindowYPosition = 200;
setGeometry( mWindowXPosition, mWindowYPosition, mWindowWidth, mWindowHeight );
// axis
setAxisTitle( QwtPlot::xBottom, xLabel );
setAxisTitle( QwtPlot::yLeft, yLabel );
// grid
grid = new QwtPlotGrid;
grid->enableXMin( true );
grid->setMajPen( QPen( Qt::white, 0, Qt::DotLine ));
grid->setMinPen( QPen( Qt::gray, 0 , Qt::DotLine ));
grid->attach( this );
// legend
legend = new QwtLegend;
insertLegend( legend, QwtPlot::RightLegend );
//zoomer
...
// print functionality
btnPrint = new QPushButton( "&Print", this );
...
// export functionality
btnExport = new QPushButton( "&Export", this );
...
show();
}
void Plot2D::attachCurve( SLData_t* xData,
SLData_t* yData,
int dataCount,
QString curveLabel,
QColor lineColor )
{
QwtPlotCurve* curve = new QwtPlotCurve;
curve->setSamples( xData, yData, dataCount );
// following works well
//qDebug() << "xMin=" << curve->minXValue() << " | xMax=" << curve->maxXValue()
// << " | yMin=" << curve->minYValue() << " | yMax=" << curve->maxYValue()
// << " | data count=" << curve->dataSize();
QPen curvePen;
curvePen.setColor( lineColor );
curve->setPen( curvePen );
curve->setRenderHint( QwtPlotItem::RenderAntialiased );
curve->setTitle( curveLabel );
if ( curveLabel.isEmpty() )
curve->setItemAttribute( QwtPlotItem::Legend, false );
else
curve->setLegendAttribute( QwtPlotCurve::LegendShowLine );
curve->attach( this );
}
void Plot2D::setMarkerOnMax( SLData_t* xData,
SLData_t* yData,
SLArrayIndex_t dataCount,
int precision, // positive value for '%.*g', negative value for '%.*f'
Qt::Alignment alignment )
{
SLArrayIndex_t index = SDA_MaxPos( xData, dataCount ); // get the index of max value
setMarker( xData[index], yData[index], precision, alignment ); // set marker position
}
void Plot2D::setMarker( double X,
double Y,
int precision,
Qt::Alignment alignment )
{
QwtPlotMarker* marker = new QwtPlotMarker();
marker->setValue( X, Y );
marker->setSymbol( new QwtSymbol( QwtSymbol::Cross,
QColor( Qt::white ),
QColor( Qt::white ),
QSize( 10, 10 )));
QString label;
if (precision < 0)
label.sprintf( "(%.*f | %.*f)", -precision, X, -precision, Y );
else
label.sprintf( "(%.*g | %.*g)", precision, X, precision, Y );
QwtText text( label );
text.setColor( QColor( Qt::white ));
text.setFont( QFont("Helvetica", 8, QFont::Bold ));
marker->setLabel( text );
marker->setLabelAlignment( alignment );
marker->attach( this );
}
To copy to clipboard, switch view to plain text mode
Bookmarks