1 #include "plot.h"
2 #include <qwt_plot_curve.h>
3 #include <qwt_plot_grid.h>
4 #include <qwt_symbol.h>
5 #include <qwt_plot_picker.h>
6 #include <qwt_scale_engine.h>
7 
Plot(QWidget * parent)8 Plot::Plot( QWidget *parent ):
9     QwtPlot( parent )
10 {
11     setCanvasBackground( Qt::white );
12 
13     setAxisScale(QwtPlot::yLeft, 0.0, 10.0 );
14     setTransformation( new QwtNullTransform() );
15 
16     populate();
17 
18     QwtPlotPicker *picker = new QwtPlotPicker( canvas() );
19     picker->setTrackerMode( QwtPlotPicker::AlwaysOn );
20 }
21 
populate()22 void Plot::populate()
23 {
24     QwtPlotGrid *grid = new QwtPlotGrid();
25     grid->setMinorPen( Qt::black, 0, Qt::DashLine );
26     grid->enableXMin( true );
27     grid->attach( this );
28 
29     QwtPlotCurve *curve = new QwtPlotCurve();
30     curve->setTitle("Some Points");
31     curve->setPen( Qt::blue, 4 ),
32     curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
33 
34     QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
35         QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
36     curve->setSymbol( symbol );
37 
38     QPolygonF points;
39     points << QPointF( 10.0, 4.4 )
40         << QPointF( 100.0, 3.0 ) << QPointF( 200.0, 4.5 )
41         << QPointF( 300.0, 6.8 ) << QPointF( 400.0, 7.9 )
42         << QPointF( 500.0, 7.1 ) << QPointF( 600.0, 7.9 )
43         << QPointF( 700.0, 7.1 ) << QPointF( 800.0, 5.4 )
44         << QPointF( 900.0, 2.8 ) << QPointF( 1000.0, 3.6 );
45     curve->setSamples( points );
46     curve->attach( this );
47 }
48 
setTransformation(QwtTransform * transform)49 void Plot::setTransformation( QwtTransform *transform )
50 {
51     QwtLinearScaleEngine *scaleEngine = new QwtLinearScaleEngine();
52     scaleEngine->setTransformation( transform );
53 
54     setAxisScaleEngine( QwtPlot::xBottom, scaleEngine );
55 
56     // we have to reassign the axis settinge, because they are
57     // invalidated, when the scale engine has changed
58 
59     QwtScaleDiv scaleDiv =
60         axisScaleEngine( QwtPlot::xBottom )->divideScale( 10.0, 1000.0, 8, 10 );
61 
62     QList<double> ticks;
63     ticks += 10.0;
64     ticks += scaleDiv.ticks( QwtScaleDiv::MajorTick );
65     scaleDiv.setTicks( QwtScaleDiv::MajorTick, ticks );
66 
67     setAxisScaleDiv( QwtPlot::xBottom, scaleDiv );
68 
69     replot();
70 }
71