21 #include "datastreamplot.h"
22 #include "qwt/qwt_plot_grid.h"
23 #include "qwt/qwt_plot_layout.h"
24 #include "qwt/qwt_plot_canvas.h"
25 #include "qwt/qwt_plot_curve.h"
26 #include "qwt/qwt_symbol.h"
27 #include "qwt/qwt_plot_directpainter.h"
28 #include "qwt/qwt_painter.h"
29 #include "qwt/qwt_legend.h"
30 #include "qwt/qwt_legend_label.h"
31 #include <QPaintEngine>
38 DataStream() : QwtArraySeriesData<QPointF>() { };
40 virtual QRectF boundingRect()
const {
41 if ( d_boundingRect.width() < 0.0 ) {
42 d_boundingRect = qwtBoundingRect( *
this );
44 return d_boundingRect;
46 virtual QPointF sample(
size_t i)
const {
47 QPointF f = QwtArraySeriesData<QPointF>::sample(i);
51 inline void append(
const QPointF &point ) {
59 d_boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 );
66 directPainter(new QwtPlotDirectPainter( this )) {
68 setFrameStyle( QFrame::NoFrame );
71 plotLayout()->setAlignCanvasToScales(
true );
73 QwtPlotGrid *grid =
new QwtPlotGrid;
74 grid->setMajorPen( Qt::gray, 0, Qt::DotLine );
77 QwtLegend *legend =
new QwtLegend;
78 legend->setDefaultItemMode( QwtLegendData::Checkable );
79 insertLegend( legend, QwtPlot::RightLegend );
80 connect( legend, SIGNAL( checked(
const QVariant &,
bool,
int ) ),
81 SLOT( legendChecked(
const QVariant &,
bool ) ) );
83 setCanvasBackground( Qt::lightGray );
90 setAutoReplot(
false );
94 foreach( QwtPlotCurve* curve, streams ) {
100 QwtPlotCurve* curve =
new QwtPlotCurve( name );
102 curve->attach(
this );
103 curve->setYAxis( QwtPlot::yLeft );
104 showCurve( curve,
true );
105 streams.append( curve );
106 return streams.size()-1;
110 color.setAlpha( 150 );
111 streams[streamID]->setPen( color );
112 streams[streamID]->setBrush( color );
117 QPointF point( data->size(), value );
118 data->append( point );
121 directPainter->drawSeries( streams[streamID], data->size()-1, data->size()-1 );
122 maxX = qMax( maxX, (
float)data->size() );
123 if ( streams[streamID]->isVisible() ) {
124 minY = qMin( minY, value );
125 maxY = qMax( maxY, value );
130 setAxisScale( QwtPlot::yLeft, minY, maxY );
131 float max = qMax( windowSize, maxX );
132 float min = qMax( 0.0f, max-windowSize );
133 setAxisScale( QwtPlot::xBottom, min, max );
137 void DataStreamPlot::legendChecked(
const QVariant &itemInfo,
bool on ) {
138 QwtPlotItem *plotItem = infoToItem( itemInfo );
140 showCurve( plotItem, on );
149 void DataStreamPlot::showCurve( QwtPlotItem *item,
bool on ) {
150 item->setVisible( on );
152 QwtLegend *lgd = qobject_cast<QwtLegend *>( legend() );
154 QList<QWidget *> legendWidgets = lgd->legendWidgets( itemToInfo( item ) );
156 if ( legendWidgets.size() == 1 ) {
157 QwtLegendLabel* legendLabel = qobject_cast<QwtLegendLabel*>( legendWidgets[0] );
159 legendLabel->setChecked( on );
void updatePlot()
recalculate the x and y scale axis and replot the data
A macro to deprecate functions.
extend QwtArraySeriesData in order to append data incrementally
FARSA_UTIL_TEMPLATE const T max(const T &t1, const U &t2)
Template for max calculation.
void appendData(int streamID, float value)
append a point to the stream specified
int addDataStream(QString name)
add a new stream to the plot
virtual ~DataStreamPlot()
destructor
FARSA_UTIL_TEMPLATE const T min(const T &t1, const U &t2)
Template for min calculation.
void setDataStreamColor(int streamID, QColor color)
set the color of the stream specified
DataStreamPlot(QWidget *parent=0)
constructor an empty LiveDataPlot without any data