// main.cpp
// AW - 14.03.2012
#include <QApplication> // Qt library
#include <qwt_plot.h> // Qwt Plot library
#include <qwt_plot_curve.h>
#include <qwt_math.h>
#include <siglib.h> // SigLib DSP library
#include "SigLibGraph.h" // Qt/Qwt SigLib Graphics library
int main( int argc, char * argv[] )
{
const unsigned int FFT_LENGTH = 512;
double *pXData = new double[FFT_LENGTH];
double *pResult = new double[FFT_LENGTH];
double *pResult2 = new double[FFT_LENGTH];
// fill data arrays
for ( unsigned int idx = 0; idx < FFT_LENGTH; idx++ ) {
pXData[idx] = double( idx );
pResult[idx] = sin( M_PI * 0.02 * double( idx ));
pResult2[idx] = cos( M_PI * 0.02 * double( idx ));
}
// plot object
Plot2D *myPlot = new Plot2D( "function plot", "time", "amplitude" );
myPlot->attachCurve( pXData, pResult, FFT_LENGTH, "sine" );
myPlot->attachCurve( pXData, pResult2, FFT_LENGTH, "cosine", Qt::cyan );
myPlot->show();
return app.exec();
}
// main.cpp
// AW - 14.03.2012
#include <QApplication> // Qt library
#include <qwt_plot.h> // Qwt Plot library
#include <qwt_plot_curve.h>
#include <qwt_math.h>
#include <siglib.h> // SigLib DSP library
#include "SigLibGraph.h" // Qt/Qwt SigLib Graphics library
int main( int argc, char * argv[] )
{
QApplication app( argc,argv );
const unsigned int FFT_LENGTH = 512;
double *pXData = new double[FFT_LENGTH];
double *pResult = new double[FFT_LENGTH];
double *pResult2 = new double[FFT_LENGTH];
// fill data arrays
for ( unsigned int idx = 0; idx < FFT_LENGTH; idx++ ) {
pXData[idx] = double( idx );
pResult[idx] = sin( M_PI * 0.02 * double( idx ));
pResult2[idx] = cos( M_PI * 0.02 * double( idx ));
}
// plot object
Plot2D *myPlot = new Plot2D( "function plot", "time", "amplitude" );
myPlot->attachCurve( pXData, pResult, FFT_LENGTH, "sine" );
myPlot->attachCurve( pXData, pResult2, FFT_LENGTH, "cosine", Qt::cyan );
myPlot->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
/***************************************************************
SigLibGraph.h
AW - 31.03.2012
***************************************************************/
#ifndef SIGLIBGRAPH_H
#define SIGLIBGRAPH_H
#include <siglib.h> // SigLib DSP library
#include <qwt_plot.h> // Qwt library
#include <qwt_plot_grid.h>
#include <qwt_legend.h>
#include <qwt_plot_zoomer.h>
{
Q_OBJECT
public:
void attachCurve( double *xData,
double *yData,
uint dataCount,
QColor curveColor
= Qt
::yellow );
void setXAxisScale( double firstValue, double lastValue );
void setYAxisScale( double firstValue, double lastValue );
void setXLogAxis();
void setYLogAxis();
void setBackgroundColor
( QColor backgroundColor
);
public Q_SLOTS:
protected:
private:
};
#endif // SIGLIBGRAPH_H
/***************************************************************
SigLibGraph.h
AW - 31.03.2012
***************************************************************/
#ifndef SIGLIBGRAPH_H
#define SIGLIBGRAPH_H
#include <siglib.h> // SigLib DSP library
#include <qwt_plot.h> // Qwt library
#include <qwt_plot_grid.h>
#include <qwt_legend.h>
#include <qwt_plot_zoomer.h>
class Plot2D: public QwtPlot
{
Q_OBJECT
public:
Plot2D( QString plotTitle = "",
QString xLabel = "x-axis",
QString yLabel = "y-axis",
QWidget *parent = NULL );
void attachCurve( double *xData,
double *yData,
uint dataCount,
QString curveLabel = "",
QColor curveColor = Qt::yellow );
void setXAxisScale( double firstValue, double lastValue );
void setYAxisScale( double firstValue, double lastValue );
void setXLogAxis();
void setYLogAxis();
void setBackgroundColor( QColor backgroundColor );
public Q_SLOTS:
void autoRescale( QRectF );
protected:
virtual void resizeEvent( QResizeEvent * );
private:
QwtPlotGrid *grid;
QwtLegend *legend;
QwtPlotZoomer *zoomer;
};
#endif // SIGLIBGRAPH_H
To copy to clipboard, switch view to plain text mode
/************************************************************
SigLibGraph.cpp
AW - 31.03.2012
************************************************************/
#include <QApplication>
#include <QtDebug>
#include <qpainter.h>
#include <qwt_plot.h>
#include <qwt_scale_engine.h>
#include <qwt_symbol.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>
#include <qwt_legend.h>
#include <qwt_text.h>
#include <qwt_picker.h>
#include <qwt_picker_machine.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_renderer.h>
#include <qwt_event_pattern.h>
#include <qwt_text.h>
#include "SigLibGraph.h"
{
// plot object
setTitle( plotTitle );
setCanvasBackground
( QColor( Qt
::darkBlue ));
// 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
this->canvas() );
zoomer
->setRubberBand
( QwtPicker::RectRubberBand );
zoomer
->setRubberBandPen
( QColor( Qt
::green ));
zoomer
->setTrackerMode
( QwtPicker::ActiveOnly );
zoomer
->setTrackerPen
( QColor( Qt
::white ));
// RightButton: zoom out by one step
Qt::RightButton );
// Ctrl+RightButton: zoom out to full size
Qt::RightButton,
Qt::ControlModifier );
// handle zoom base
connect( zoomer,
SLOT( autoRescale
( QRectF )));
// resize Widget
resize( 600, 400 );
show();
}
void Plot2D
::autoRescale( QRectF ) {
qDebug("Plot2D::autoRescale called!");
if( zoomer->zoomRectIndex() == 0 ) // autorescale only if you are at base...
{
this
->setAxisAutoScale
( QwtPlot::xBottom );
this
->setAxisAutoScale
( QwtPlot::yLeft );
this->updateAxes();
zoomer->setZoomBase( false );
}
}
void Plot2D::attachCurve( double *xData,
double *yData,
uint dataCount,
{
// define the curve object
curve->setSamples( xData, yData, dataCount );
curve
->setPen
( QPen( curveColor
));
curve
->setRenderHint
( QwtPlotItem::RenderAntialiased );
curve->setTitle( curveLabel );
// qDebug() << "curveLabel.isEmpty = " << curveLabel.isEmpty();
if ( curveLabel.isEmpty() )
else
curve->attach( this );
}
void Plot2D::setXAxisScale( double firstValue, double lastValue )
{
setAxisScale( xBottom, firstValue, lastValue );
}
void Plot2D::setYAxisScale( double firstValue, double lastValue )
{
setAxisScale( yLeft, firstValue, lastValue );
}
void Plot2D::setXLogAxis()
{
}
void Plot2D::setYLogAxis()
{
}
void Plot2D
::setBackgroundColor( QColor backgroundColor
) {
setCanvasBackground( backgroundColor );
}
{
}
/************************************************************
SigLibGraph.cpp
AW - 31.03.2012
************************************************************/
#include <QApplication>
#include <QtDebug>
#include <qpainter.h>
#include <qwt_plot.h>
#include <qwt_scale_engine.h>
#include <qwt_symbol.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>
#include <qwt_legend.h>
#include <qwt_text.h>
#include <qwt_picker.h>
#include <qwt_picker_machine.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_renderer.h>
#include <qwt_event_pattern.h>
#include <qwt_text.h>
#include "SigLibGraph.h"
Plot2D::Plot2D( QString plotTitle,
QString xLabel,
QString yLabel,
QWidget *parent ):
QwtPlot( parent )
{
// plot object
setTitle( plotTitle );
setCanvasBackground( QColor( Qt::darkBlue ));
// axis
setAxisTitle( QwtPlot::xBottom, xLabel );
setAxisTitle( QwtPlot::yLeft, yLabel );
// grid
QwtPlotGrid* 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
QwtLegend* legend = new QwtLegend;
insertLegend( legend, QwtPlot::RightLegend );
//zoomer
QwtPlotZoomer* zoomer = new QwtPlotZoomer( QwtPlot::xBottom,
QwtPlot::yLeft,
this->canvas() );
zoomer->setRubberBand( QwtPicker::RectRubberBand );
zoomer->setRubberBandPen( QColor( Qt::green ));
zoomer->setTrackerMode( QwtPicker::ActiveOnly );
zoomer->setTrackerPen( QColor( Qt::white ));
// RightButton: zoom out by one step
zoomer->setMousePattern( QwtEventPattern::MouseSelect3,
Qt::RightButton );
// Ctrl+RightButton: zoom out to full size
zoomer->setMousePattern( QwtEventPattern::MouseSelect2,
Qt::RightButton,
Qt::ControlModifier );
// handle zoom base
connect( zoomer,
SIGNAL( zoomed( QRectF )),
SLOT( autoRescale( QRectF )));
// resize Widget
resize( 600, 400 );
show();
}
void Plot2D::autoRescale( QRectF )
{
qDebug("Plot2D::autoRescale called!");
if( zoomer->zoomRectIndex() == 0 ) // autorescale only if you are at base...
{
this->setAxisAutoScale( QwtPlot::xBottom );
this->setAxisAutoScale( QwtPlot::yLeft );
this->updateAxes();
zoomer->setZoomBase( false );
}
}
void Plot2D::attachCurve( double *xData,
double *yData,
uint dataCount,
QString curveLabel,
QColor curveColor )
{
// define the curve object
QwtPlotCurve* curve = new QwtPlotCurve;
curve->setSamples( xData, yData, dataCount );
curve->setPen( QPen( curveColor ));
curve->setRenderHint( QwtPlotItem::RenderAntialiased );
curve->setTitle( curveLabel );
// qDebug() << "curveLabel.isEmpty = " << curveLabel.isEmpty();
if ( curveLabel.isEmpty() )
curve->setItemAttribute( QwtPlotItem::Legend, false );
else
curve->setLegendAttribute( QwtPlotCurve::LegendShowLine );
curve->attach( this );
}
void Plot2D::setXAxisScale( double firstValue, double lastValue )
{
setAxisScale( xBottom, firstValue, lastValue );
}
void Plot2D::setYAxisScale( double firstValue, double lastValue )
{
setAxisScale( yLeft, firstValue, lastValue );
}
void Plot2D::setXLogAxis()
{
setAxisScaleEngine( QwtPlot::xBottom, new QwtLog10ScaleEngine );
}
void Plot2D::setYLogAxis()
{
setAxisScaleEngine( QwtPlot::yLeft, new QwtLog10ScaleEngine );
}
void Plot2D::setBackgroundColor( QColor backgroundColor )
{
setCanvasBackground( backgroundColor );
}
void Plot2D::resizeEvent( QResizeEvent *event )
{
QwtPlot::resizeEvent( event );
}
To copy to clipboard, switch view to plain text mode
Bookmarks