Mercurial > projects > qtd
annotate examples/qwt/simpleplot/simple.d @ 392:6bd75093d3bb
Allow specifying the location the resulting D modules are installed to.
author | David Nadlinger <code@klickverbot.at> |
---|---|
date | Sat, 28 Aug 2010 02:17:15 +0200 |
parents | bd7f485e3573 |
children |
rev | line source |
---|---|
378 | 1 module simple; |
2 | |
3 import std.math; | |
4 | |
5 import qt.gui.QApplication; | |
6 import qt.qwt.QwtPlot; | |
7 import qt.qwt.QwtPlotMarker; | |
8 import qt.qwt.QwtPlotCurve; | |
9 import qt.qwt.QwtLegend; | |
10 import qt.qwt.QwtData; | |
11 import qt.qwt.QwtText; | |
12 | |
13 //----------------------------------------------------------------- | |
14 // simple.d | |
15 // | |
16 // A simple example which shows how to use QwtPlot and QwtData | |
17 //----------------------------------------------------------------- | |
18 | |
381
347e4c7a9ba1
make QwtD compile on Windows@
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
380
diff
changeset
|
19 // hack to avoid linker error (undefined symbols), possibly a compiler bug |
378 | 20 real mysin(real arg) |
21 { | |
22 return sin(arg); | |
23 } | |
24 | |
25 real mycos(real arg) | |
26 { | |
27 return cos(arg); | |
28 } | |
29 | |
30 alias real function(real) double_func; | |
31 | |
32 class SimpleData: QwtData | |
33 { | |
34 // The x values depend on its index and the y values | |
383 | 35 // can be calculated from the corresponding x value. |
378 | 36 // So we don't need to store the values. |
383 | 37 // Such an implementation is slower because every point |
378 | 38 // has to be recalculated for every replot, but it demonstrates how |
39 // QwtData can be used. | |
40 | |
41 public: | |
42 this(const double_func y, size_t size) | |
43 { | |
44 d_size = size; | |
45 d_y = y; | |
46 } | |
47 | |
48 QwtData copy() const | |
49 { | |
50 return new SimpleData(d_y, d_size); | |
51 } | |
52 | |
53 size_t size() const | |
54 { | |
55 return d_size; | |
56 } | |
57 | |
58 double x(size_t i) const | |
59 { | |
60 return 0.1 * i; | |
61 } | |
62 | |
63 double y(size_t i) const | |
64 { | |
65 return d_y(x(i)); | |
66 } | |
67 private: | |
68 size_t d_size; | |
69 const double_func d_y; | |
70 }; | |
71 | |
72 class Plot : QwtPlot | |
73 { | |
74 public: | |
75 this() | |
76 { | |
77 setTitle("A Simple QwtPlot Demonstration"); | |
380 | 78 insertLegend(new QwtLegend(), QwtPlot.RightLegend); |
378 | 79 |
80 // Set axis titles | |
379 | 81 setAxisTitle(xBottom, "x -->"); |
82 setAxisTitle(yLeft, "y -->"); | |
383 | 83 |
378 | 84 // Insert new curves |
85 auto cSin = new QwtPlotCurve("y = sin(x)"); | |
86 cSin.setRenderHint(QwtPlotItem.RenderAntialiased); | |
87 cSin.setPen(new QPen(new QColor(Qt.red))); | |
88 cSin.attach(this); | |
89 | |
90 auto cCos = new QwtPlotCurve("y = cos(x)"); | |
91 cCos.setRenderHint(QwtPlotItem.RenderAntialiased); | |
92 cCos.setPen(new QPen(new QColor(Qt.blue))); | |
93 cCos.attach(this); | |
94 | |
95 // Create sin and cos data | |
96 const int nPoints = 100; | |
97 cSin.setData(new SimpleData(&mysin, nPoints)); | |
98 cCos.setData(new SimpleData(&mycos, nPoints)); | |
99 | |
100 // Insert markers | |
383 | 101 |
378 | 102 // ...a horizontal line at y = 0... |
103 auto mY = new QwtPlotMarker(); | |
104 mY.setLabel(new QwtText("y = 0")); | |
105 mY.setLabelAlignment(Qt.AlignRight | Qt.AlignTop); | |
106 mY.setLineStyle(QwtPlotMarker.HLine); | |
107 mY.setYValue(0.0); | |
108 mY.attach(this); | |
109 | |
110 // ...a vertical line at x = 2 * pi | |
111 auto mX = new QwtPlotMarker(); | |
112 mX.setLabel(new QwtText("x = 2 pi")); | |
113 mX.setLabelAlignment(Qt.AlignLeft | Qt.AlignBottom); | |
114 mX.setLabelOrientation(Qt.Vertical); | |
115 mX.setLineStyle(QwtPlotMarker.VLine); | |
116 mX.setLinePen(new QPen(new QBrush(Qt.black), 0, Qt.DashDotLine)); | |
117 mX.setXValue(2.0 * PI); | |
118 mX.attach(this); | |
119 } | |
120 | |
121 }; | |
122 | |
123 int main(string[] args) | |
124 { | |
125 auto a = new QApplication(args); | |
126 | |
127 scope plot = new Plot; | |
128 plot.resize(600,400); | |
129 plot.show(); | |
383 | 130 return a.exec(); |
378 | 131 } |