MavPlot::MavPlot(QWidget *parent) : QwtPlot(parent), _havePrintColors(false) { setAutoReplot(false); setTitle("MAV System Data Plot"); setCanvasBackground(QColor(Qt::darkGray)); // legend QwtLegend *legend = new QwtLegend; insertLegend(legend, QwtPlot::BottomLegend); #if (QWT_VERSION < QWT_VERSION_CHECK(6,1,0)) legend->setItemMode(QwtLegend::ClickableItem); connect(this, SIGNAL(legendClicked(QwtPlotItem*)), SLOT(legendClicked(QwtPlotItem*))); #else legend->setDefaultItemMode(QwtLegendData::Clickable); connect(legend, SIGNAL(clicked(const QVariant&, int)), SLOT(legendClickedNew(const QVariant&, int))); #endif // grid QwtPlotGrid *grid = new QwtPlotGrid; grid->enableXMin(true); #if (QWT_VERSION < QWT_VERSION_CHECK(6,1,0)) grid->setMajPen(QPen(Qt::gray, 0, Qt::DotLine)); grid->setMinPen(QPen(QColor(0x8, 0x8, 0x8), 0 , Qt::DotLine)); #else grid->setMajorPen(QPen(Qt::gray, 0, Qt::DotLine)); grid->setMinorPen(QPen(QColor(0x8, 0x8, 0x8), 0 , Qt::DotLine)); #endif grid->attach(this); // axes //enableAxis(QwtPlot::yRight); setAxisTitle(QwtPlot::xBottom, "time"); setAxisScaleDraw(QwtPlot::xBottom, new HumanReadableTime()); // no need to de-alloc or store, plot does it // A-B markers for (int i=0; i<2; i++) { _user_markers[i].setLineStyle(QwtPlotMarker::VLine); _user_markers[i].setLabelAlignment(Qt::AlignRight | Qt::AlignBottom); _user_markers[i].setLinePen(QPen(QColor(255, 140, 0), 0, Qt::SolidLine)); _user_markers_visible[i] = false; } _user_markers[0].setLabel(QwtText("A")); _user_markers[1].setLabel(QwtText("B")); // data marker _data_marker.setLineStyle(QwtPlotMarker::VLine); _data_marker.setLinePen(QPen(QColor(255, 160, 47), 2, Qt::SolidLine)); _data_marker.setLabel(QwtText("data")); _data_marker.setLabelAlignment(Qt::AlignTop | Qt::AlignRight); _data_marker_visible = false; _data_marker.setZ(9999); // higher value -> paint on top of everything else setAutoReplot(true); }
TVPlot::TVPlot( QWidget *parent ): QwtPlot( parent ) { setTitle( "Watching TV during a weekend" ); QwtPlotCanvas *canvas = new QwtPlotCanvas(); canvas->setPalette( Qt::gray ); canvas->setBorderRadius( 10 ); setCanvas( canvas ); plotLayout()->setAlignCanvasToScales( true ); setAxisTitle( QwtPlot::yLeft, "Number of People" ); setAxisTitle( QwtPlot::xBottom, "Number of Hours" ); QwtLegend *legend = new QwtLegend; legend->setDefaultItemMode( QwtLegendData::Checkable ); insertLegend( legend, QwtPlot::RightLegend ); populate(); connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ), SLOT( showItem( const QVariant &, bool ) ) ); replot(); // creating the legend items QwtPlotItemList items = itemList( QwtPlotItem::Rtti_PlotHistogram ); for ( int i = 0; i < items.size(); i++ ) { if ( i == 0 ) { const QVariant itemInfo = itemToInfo( items[i] ); QwtLegendLabel *legendLabel = qobject_cast<QwtLegendLabel *>( legend->legendWidget( itemInfo ) ); if ( legendLabel ) legendLabel->setChecked( true ); items[i]->setVisible( true ); } else { items[i]->setVisible( false ); } } setAutoReplot( true ); }
void Graph::initLegend() { QwtLegend *legend = new QwtLegend; legend->setDefaultItemMode(QwtLegendData::Checkable); insertLegend(legend, QwtPlot::BottomLegend); connect(legend, SIGNAL(checked(QVariant,bool,int)), SLOT(showCurve(QVariant,bool,int))); QwtPlotItemList l = this->itemList(QwtPlotItem::Rtti_PlotCurve); for(int i = 0; i < l.size(); ++i) { QwtPlotItem *curve = l[i]; QVariant info = itemToInfo(curve); QWidget *w = legend->legendWidget(info); if(w && w->inherits("QwtLegendLabel")) ((QwtLegendLabel*)w)->setChecked(curve->isVisible()); } }
Plot::Plot( QWidget *parent ): QwtPlot( parent ) { setTitle( "Trading Chart" ); QwtDateScaleDraw *scaleDraw = new DateScaleDraw( Qt::UTC ); QwtDateScaleEngine *scaleEngine = new QwtDateScaleEngine( Qt::UTC ); setAxisTitle( QwtPlot::xBottom, QString( "2010" ) ); setAxisScaleDraw( QwtPlot::xBottom, scaleDraw ); setAxisScaleEngine( QwtPlot::xBottom, scaleEngine ); setAxisLabelRotation( QwtPlot::xBottom, -50.0 ); setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom ); setAxisTitle( QwtPlot::yLeft, QString( "Price [EUR]" ) ); #if 0 QwtLegend *legend = new QwtLegend; legend->setDefaultItemMode( QwtLegendData::Checkable ); insertLegend( legend, QwtPlot::RightLegend ); #else Legend *legend = new Legend; insertLegend( legend, QwtPlot::RightLegend ); #endif populate(); // LeftButton for the zooming // MidButton for the panning // RightButton: zoom out by 1 // Ctrl+RighButton: zoom out to full size Zoomer* zoomer = new Zoomer( canvas() ); zoomer->setMousePattern( QwtEventPattern::MouseSelect2, Qt::RightButton, Qt::ControlModifier ); zoomer->setMousePattern( QwtEventPattern::MouseSelect3, Qt::RightButton ); QwtPlotPanner *panner = new QwtPlotPanner( canvas() ); panner->setMouseButton( Qt::MidButton ); connect( legend, SIGNAL( checked( QwtPlotItem *, bool, int ) ), SLOT( showItem( QwtPlotItem *, bool ) ) ); }
CpuPlot::CpuPlot( QWidget *parent ): QwtPlot( parent ), dataCount( 0 ) { setAutoReplot( false ); QwtPlotCanvas *canvas = new QwtPlotCanvas(); canvas->setBorderRadius( 10 ); setCanvas( canvas ); plotLayout()->setAlignCanvasToScales( true ); QwtLegend *legend = new QwtLegend; legend->setDefaultItemMode( QwtLegendData::Checkable ); insertLegend( legend, QwtPlot::RightLegend ); setAxisTitle( QwtPlot::xBottom, "System Uptime [h:m:s]" ); setAxisScaleDraw( QwtPlot::xBottom, new TimeScaleDraw( cpuStat.upTime() ) ); setAxisScale( QwtPlot::xBottom, 0, HISTORY ); //setAxisLabelRotation( QwtPlot::xBottom, -50.0 ); setAxisLabelRotation( QwtPlot::xBottom, 0.0 ); setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom ); /* In situations, when there is a label at the most right position of the scale, additional space is needed to display the overlapping part of the label would be taken by reducing the width of scale and canvas. To avoid this "jumping canvas" effect, we add a permanent margin. We don't need to do the same for the left border, because there is enough space for the overlapping label below the left scale. */ QwtScaleWidget *scaleWidget = axisWidget( QwtPlot::xBottom ); const int fmh = QFontMetrics( scaleWidget->font() ).height(); scaleWidget->setMinBorderDist( 0, fmh / 2 ); setAxisTitle( QwtPlot::yLeft, "Cpu Usage [%]" ); setAxisScale( QwtPlot::yLeft, 0, 100 ); //attach方法将元素关联到qwtplot空间上 Background *bg = new Background(); bg->attach( this ); CpuPieMarker *pie = new CpuPieMarker(); pie->attach( this ); CpuCurve *curve; curve = new CpuCurve( "System" ); curve->setColor( Qt::red ); curve->attach( this ); data[System].curve = curve; curve = new CpuCurve( "User" ); curve->setColor( Qt::blue ); curve->setZ( curve->z() - 1 ); curve->attach( this ); data[User].curve = curve; curve = new CpuCurve( "Total" ); curve->setColor( Qt::black ); curve->setZ( curve->z() - 2 ); curve->attach( this ); data[Total].curve = curve; curve = new CpuCurve( "Idle" ); curve->setColor( Qt::darkCyan ); curve->setZ( curve->z() - 3 ); curve->attach( this ); data[Idle].curve = curve; showCurve( data[System].curve, true ); showCurve( data[User].curve, true ); showCurve( data[Total].curve, false ); showCurve( data[Idle].curve, false ); for ( int i = 0; i < HISTORY; i++ ) timeData[HISTORY - 1 - i] = i; ( void )startTimer( 1000 ); // 1 second connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ), SLOT( legendChecked( const QVariant &, bool ) ) ); }
int main( int argc, char **argv ) { QApplication a( argc, argv ); QwtPlot plot; plot.setTitle( "Plot Demo" ); plot.setCanvasBackground( Qt::white ); plot.setAxisScale( QwtPlot::xBottom, -1.0, 6.0 ); QwtLegend *legend = new QwtLegend(); legend->setDefaultItemMode( QwtLegendData::Checkable ); plot.insertLegend( legend ); for ( int i = 0; i < 4; i++ ) { QwtPlotCurve *curve = new QwtPlotCurve(); curve->setRenderHint( QwtPlotItem::RenderAntialiased, true ); curve->setPen( Qt::blue ); QBrush brush; QwtSymbol::Style style = QwtSymbol::NoSymbol; QString title; if ( i == 0 ) { brush = Qt::magenta; style = QwtSymbol::Path; title = "Path"; } else if ( i == 2 ) { brush = Qt::red; style = QwtSymbol::Graphic; title = "Graphic"; } else if ( i == 1 ) { brush = Qt::yellow; style = QwtSymbol::SvgDocument; title = "Svg"; } else if ( i == 3 ) { brush = Qt::cyan; style = QwtSymbol::Pixmap; title = "Pixmap"; } MySymbol *symbol = new MySymbol( style, brush ); curve->setSymbol( symbol ); curve->setTitle( title ); curve->setLegendAttribute( QwtPlotCurve::LegendShowSymbol, true ); curve->setLegendIconSize( QSize( 15, 18 ) ); QPolygonF points; points << QPointF( 0.0, 4.4 ) << QPointF( 1.0, 3.0 ) << QPointF( 2.0, 4.5 ) << QPointF( 3.0, 6.8 ) << QPointF( 4.0, 7.9 ) << QPointF( 5.0, 7.1 ); points.translate( 0.0, i * 2.0 ); curve->setSamples( points ); curve->attach( &plot ); } plot.resize( 600, 400 ); plot.show(); return a.exec(); }
BrainPlot::BrainPlot(QWidget *parent, int nChannels, int Length, int Amplitude): QwtPlot( parent ), d_curves( nChannels ), d_zeros( nChannels ) { /*Set some attributes*/ minusExpectedAmplitude = -Amplitude; plotLength = Length; this->nChannels = nChannels; /*Start sample counter*/ sample = 0; /*Painter*/ d_directPainter = new QwtPlotDirectPainter( this ); if ( QwtPainter::isX11GraphicsSystem() ) { #if QT_VERSION < 0x050000 canvas()->setAttribute( Qt::WA_PaintOutsidePaintEvent, true ); #endif canvas()->setAttribute( Qt::WA_PaintOnScreen, true ); } /*Activates the legend for channel tracking*/ QwtLegend *legend = new QwtLegend; legend->setDefaultItemMode( QwtLegendData::ReadOnly); insertLegend( legend, QwtPlot::LeftLegend ); /*Instantiate the reference lines and the data curves for each channel*/ int Hfactor = 360/(nChannels+1);//different colors for each curve char name[15]; for(int c = 0; c < nChannels; c++) { /*Curve*/ sprintf(name, "Channel %d", c+1); d_curves[c] = new QwtPlotCurve( name); d_curves[c]->setData( new CurveData(plotLength, minusExpectedAmplitude*(2*c+1)) ); d_curves[c]->setPen( QColor::fromHsv ( Hfactor*c, 255, 255)); d_curves[c]->setStyle( QwtPlotCurve::Lines ); d_curves[c]->setSymbol( new QwtSymbol( QwtSymbol::NoSymbol)); d_curves[c]->attach( this ); /*Reference line*/ d_zeros[c] = new QwtPlotCurve; d_zeros[c]->setData( new CurveData() ); d_zeros[c]->setPen( QColor::fromHsv ( Hfactor*c, 255, 140)); d_zeros[c]->setStyle( QwtPlotCurve::Lines ); d_zeros[c]->setSymbol( new QwtSymbol( QwtSymbol::NoSymbol)); d_zeros[c]->setItemAttribute(QwtPlotItem::Legend, false); d_zeros[c]->attach( this ); /*Draw reference lines*/ CurveData *data = static_cast<CurveData *>( d_zeros[c]->data() ); data->append(QPointF(0, minusExpectedAmplitude*(2*c+1))); data->append(QPointF(plotLength, minusExpectedAmplitude*(2*c+1))); d_directPainter->drawSeries( d_zeros[c], 0, 1); } /*Sweeping line*/ d_sweep = new QwtPlotCurve; d_sweep->setData( new CurveData() ); d_sweep->setPen( Qt::black, plotLength/100, Qt::SolidLine); d_sweep->setStyle( QwtPlotCurve::Lines ); d_sweep->setSymbol( new QwtSymbol( QwtSymbol::NoSymbol)); d_sweep->setItemAttribute(QwtPlotItem::Legend, false); d_sweep->attach( this ); CurveData *data = static_cast<CurveData *>( d_sweep->data() ); data->append(QPointF(0, plotLength)); data->append(QPointF(0, 2*nChannels*minusExpectedAmplitude)); /*Axis*/ setAxisScale( xBottom, 0, plotLength ); setAxisScale( yLeft, 2*nChannels*minusExpectedAmplitude, 0 ); enableAxis(xBottom, false); enableAxis(yLeft, false); /*Frame*/ setFrameStyle( QFrame::NoFrame ); setLineWidth( 0 ); /*Canvas*/ plotLayout()->setAlignCanvasToScales( true ); plotLayout()->setCanvasMargin(100, QwtPlot::xBottom); plotLayout()->setCanvasMargin(100, QwtPlot::xTop); setCanvasBackground( Qt::black ); /*Grid*/ QwtPlotGrid *grid = new QwtPlotGrid; grid->setMajorPen( Qt::gray, 0, Qt::DotLine ); grid->setMinorPen( Qt::gray, 0, Qt::DotLine ); grid->enableYMin(true); grid->attach( this ); /*Optimizaion for real-time data collecting*/ setAutoReplot( false ); /*Show*/ replot(); }