1 #include <qapplication.h>
2 #include <qwt_plot.h>
3 #include <qwt_plot_marker.h>
4 #include <qwt_plot_curve.h>
5 #include <qwt_legend.h>
6 #include <qwt_data.h>
7 #include <qwt_text.h>
8 #include <qwt_math.h>
9 #include <math.h>
10 
11 //-----------------------------------------------------------------
12 //              simple.cpp
13 //
14 //      A simple example which shows how to use QwtPlot and QwtData
15 //-----------------------------------------------------------------
16 
17 class SimpleData: public QwtData
18 {
19     // The x values depend on its index and the y values
20     // can be calculated from the corresponding x value.
21     // So we don�t need to store the values.
22     // Such an implementation is slower because every point
23     // has to be recalculated for every replot, but it demonstrates how
24     // QwtData can be used.
25 
26 public:
SimpleData(double (* y)(double),size_t size)27     SimpleData(double(*y)(double), size_t size):
28         d_size(size),
29         d_y(y)
30     {
31     }
32 
copy() const33     virtual QwtData *copy() const
34     {
35         return new SimpleData(d_y, d_size);
36     }
37 
size() const38     virtual size_t size() const
39     {
40         return d_size;
41     }
42 
x(size_t i) const43     virtual double x(size_t i) const
44     {
45         return 0.1 * i;
46     }
47 
y(size_t i) const48     virtual double y(size_t i) const
49     {
50         return d_y(x(i));
51     }
52 private:
53     size_t d_size;
54     double(*d_y)(double);
55 };
56 
57 class Plot : public QwtPlot
58 {
59 public:
60     Plot();
61 };
62 
63 
Plot()64 Plot::Plot()
65 {
66     setTitle("A Simple QwtPlot Demonstration");
67     insertLegend(new QwtLegend(), QwtPlot::RightLegend);
68 
69     // Set axis titles
70     setAxisTitle(xBottom, "x -->");
71     setAxisTitle(yLeft, "y -->");
72 
73     // Insert new curves
74     QwtPlotCurve *cSin = new QwtPlotCurve("y = sin(x)");
75 #if QT_VERSION >= 0x040000
76     cSin->setRenderHint(QwtPlotItem::RenderAntialiased);
77 #endif
78     cSin->setPen(QPen(Qt::red));
79     cSin->attach(this);
80 
81     QwtPlotCurve *cCos = new QwtPlotCurve("y = cos(x)");
82 #if QT_VERSION >= 0x040000
83     cCos->setRenderHint(QwtPlotItem::RenderAntialiased);
84 #endif
85     cCos->setPen(QPen(Qt::blue));
86     cCos->attach(this);
87 
88     // Create sin and cos data
89     const int nPoints = 100;
90     cSin->setData(SimpleData(::sin, nPoints));
91     cCos->setData(SimpleData(::cos, nPoints));
92 
93     // Insert markers
94 
95     //  ...a horizontal line at y = 0...
96     QwtPlotMarker *mY = new QwtPlotMarker();
97     mY->setLabel(QString::fromLatin1("y = 0"));
98     mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
99     mY->setLineStyle(QwtPlotMarker::HLine);
100     mY->setYValue(0.0);
101     mY->attach(this);
102 
103     //  ...a vertical line at x = 2 * pi
104     QwtPlotMarker *mX = new QwtPlotMarker();
105     mX->setLabel(QString::fromLatin1("x = 2 pi"));
106     mX->setLabelAlignment(Qt::AlignLeft | Qt::AlignBottom);
107     mX->setLabelOrientation(Qt::Vertical);
108     mX->setLineStyle(QwtPlotMarker::VLine);
109     mX->setLinePen(QPen(Qt::black, 0, Qt::DashDotLine));
110     mX->setXValue(2.0 * M_PI);
111     mX->attach(this);
112 }
113 
main(int argc,char ** argv)114 int main(int argc, char **argv)
115 {
116     QApplication a(argc, argv);
117 
118     Plot plot;
119 #if QT_VERSION < 0x040000
120     a.setMainWidget(&plot);
121 #endif
122     plot.resize(600,400);
123     plot.show();
124     return a.exec();
125 }
126