##// END OF EJS Templates
Scatter series documentation; now uses snippets
Tero Ahola -
r300:fbedbdf7642a
parent child
Show More
@@ -1,51 +1,66
1 1 #include <QtGui/QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartglobal.h>
4 4 #include <qchartview.h>
5 5 #include <qscatterseries.h>
6 6
7 7 QTCOMMERCIALCHART_USE_NAMESPACE
8 8
9 9 int main(int argc, char *argv[])
10 10 {
11 11 QApplication a(argc, argv);
12 12
13 // Create chart widget
13 //! [1]
14 // Create chart view
14 15 QChartView *chartView = new QChartView();
15 16
16 //! [1]
17 17 // Add scatter series with simple test data
18 18 QScatterSeries *scatter = new QScatterSeries();
19 19 *scatter << QPointF(0.5, 5.0)
20 20 << QPointF(1.0, 4.5)
21 21 << QPointF(1.0, 5.5)
22 22 << QPointF(1.5, 5.0)
23 23 << QPointF(2.0, 4.5)
24 << QPointF(2.0, 5.5)
25 24 << QPointF(2.5, 5.0);
26 25 // Chart takes ownership
27 26 chartView->addSeries(scatter);
28 27 //! [1]
29 28
30 // Add another scatter series
29 // Add some more data
30 //! [2]
31 scatter->addData(QPointF(2.0, 5.5));
32 //! [2]
33
34 // And more
35 //! [3]
36 *scatter << QPointF(2.0, 5.5);
37 //! [3]
38
39 // Add another scatter series (re-use the previous pointer)
31 40 // - more data with random component
32 QScatterSeries *scatter2 = new QScatterSeries();
41 scatter = new QScatterSeries();
33 42 for (qreal i(0.0); i < 20; i += 0.15) {
34 (*scatter2) << QPointF(i + (qreal)(rand() % 100) / 100.0,
43 (*scatter) << QPointF(i + (qreal)(rand() % 100) / 100.0,
35 44 i + (qreal)(rand() % 100) / 100.0);
36 45 }
46 //! [4]
37 47 QBrush brush(QColor(255, 0, 0, 100), Qt::SolidPattern);
38 scatter2->setMarkerBrush(brush);
48 scatter->setMarkerBrush(brush);
49 //! [4]
50 //! [5]
39 51 QPen pen(QColor(0, 255, 0, 80), 3);
40 scatter2->setMarkerPen(pen);
41 scatter2->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
42 chartView->addSeries(scatter2);
52 scatter->setMarkerPen(pen);
53 //! [5]
54 //! [6]
55 scatter->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
56 //! [6]
57 chartView->addSeries(scatter);
43 58
44 59 // Use the chart widget as the central widget
45 60 QMainWindow w;
46 61 w.resize(640, 480);
47 62 w.setCentralWidget(chartView);
48 63 w.show();
49 64
50 65 return a.exec();
51 66 }
@@ -1,208 +1,228
1 1 #include "qscatterseries.h"
2 2 #include "scatterseries_p.h"
3 3 #include "qchart.h"
4 4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 /*!
6 \class QScatterSeries
7 \brief QtCommercial Chart series API for showing scatter series.
6 8
7 QScatterSeriesPrivate::QScatterSeriesPrivate() :
8 m_data(QList<QPointF>()),
9 m_markerPen(QPen()),
10 m_markerBrush(QBrush()),
11 m_markerShape(QScatterSeries::MarkerShapeDefault)
12 {
13 // Initialize pen color to invalid to use a theme color by default
14 m_markerPen.setColor(QColor::Invalid);
15 m_markerBrush.setColor(QColor::Invalid);
16 }
9 \mainclass
10
11 Example on how to create a chart with scatter series:
12 \snippet ../example/scatter/main.cpp 1
13
14 The example code would result the following:
15
16 \image scatter_example1.jpg
17 */
17 18
18 19 /*!
19 20 \enum QScatterSeries::MarkerShape
20 21
21 22 This enum describes the shape used when rendering marker items.
22 23
23 24 \value MarkerShapeDefault
24 25 \value MarkerShapePoint
25 26 \value MarkerShapeX
26 27 \value MarkerShapeRectangle
27 28 \value MarkerShapeTiltedRectangle
28 29 \value MarkerShapeTriangle
29 30 \value MarkerShapeCircle
30 31 */
31 32
32 33 /*!
33 \class QScatterSeries
34 \brief QtCommercial Chart series API for showing scatter series.
34 \fn QChartSeriesType QScatterSeries::type() const
35 \brief Returns QChartSeries::SeriesTypeScatter.
36 */
35 37
36 \snippet ../../example/scatter/main.cpp 1
37 Example on how to create a chart with scatter series:
38 \code
39 #include <qchartglobal.h>
40 #include <qchartview.h>
41 #include <qscatterseries.h>
42 ...
43 QTCOMMERCIALCHART_USE_NAMESPACE
44
45 // Create chart widget
46 QChartView *chartView = new QChartView();
47 QScatterSeries *scatter = new QScatterSeries();
48 *scatter << QPointF(0.5, 5.0) << QPointF(1.0, 4.5) << QPointF(1.0, 5.5) << QPointF(1.5, 5.0);
49 chartView->addSeries(scatter);
50 // Then add the QChartView into a layout...
51 \endcode
38 /*!
39 \fn void QScatterSeries::clicked()
40 \brief TODO
41 */
52 42
53 The example code would result the following:
43 /*!
44 \fn void QScatterSeries::hoverEnter()
45 \brief TODO
46 */
54 47
55 \image scatter_example1.jpg
48 /*!
49 \fn void QScatterSeries::hoverLeave()
50 \brief TODO
56 51 */
57 52
58 53 /*!
54 \fn void QScatterSeries::changed()
55 \brief TODO
56 */
57
58 QTCOMMERCIALCHART_BEGIN_NAMESPACE
59
60 QScatterSeriesPrivate::QScatterSeriesPrivate() :
61 m_data(QList<QPointF>()),
62 m_markerPen(QPen()),
63 m_markerBrush(QBrush()),
64 m_markerShape(QScatterSeries::MarkerShapeDefault)
65 {
66 // Initialize pen color to invalid to use a theme color by default
67 m_markerPen.setColor(QColor::Invalid);
68 m_markerBrush.setColor(QColor::Invalid);
69 }
70
71 /*!
59 72 Constructs a series object which is a child of \a parent.
60 73 */
61 74 QScatterSeries::QScatterSeries(QObject *parent) :
62 75 QChartSeries(parent),
63 76 d(new QScatterSeriesPrivate())
64 77 {
65 78 }
66 79
67 80 /*!
68 81 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
69 82 */
70 83 QScatterSeries::~QScatterSeries()
71 84 {
72 85 delete d;
73 86 }
74 87
75 88 /*!
76 Add single data point to the series.
89 Add single data point with \a value to the series.
77 90 For example:
78 \code
79 mySeries.addData(QPointF(0.5, 5.0));
80 \endcode
91 \snippet ../example/scatter/main.cpp 2
81 92 */
82 93 void QScatterSeries::addData(QPointF value)
83 94 {
84 95 d->m_data.append(value);
85 96 emit changed();
86 97 }
87 98
88 99 /*!
89 Stream operator for adding a data point to the series.
90 \sa addData(), QScatterSeries::addData(QPointF value)
100 Add list of \a points to the series.
101 */
102 void QScatterSeries::addData(QList<QPointF> points)
103 {
104 d->m_data.append(data);
105 emit changed();
106 }
91 107
92 For example:
93 \code
94 mySeries << QPointF(0.5, 5.0)
95 << QPointF(1.0, 4.5);
96 \endcode
108 /*!
109 Stream operator for adding a data point with \a value to the series.
110 \sa addData()
97 111
112 For example:
113 \snippet ../example/scatter/main.cpp 3
98 114 */
99 115 QScatterSeries& QScatterSeries::operator << (const QPointF &value)
100 116 {
101 117 d->m_data.append(value);
102 118 emit changed();
103 119 return *this;
104 120 }
105 121
106 122 /*!
107 Replaces the data of the series with the given list of data points.
123 Stream operator for adding a list of points to the series.
124 \sa addData()
125 */
126 QScatterSeries& QScatterSeries::operator << (QList<QPointF> value)
127 {
128 d->m_data.append(value);
129 emit changed();
130 return *this;
131 }
132
133 /*!
134 Replaces the data of the series with the given list of data \a points.
108 135 */
109 void QScatterSeries::setData(QList<QPointF> data)
136 void QScatterSeries::setData(QList<QPointF> points)
110 137 {
111 138 d->m_data = data;
112 139 emit changed();
113 140 }
114 141
115 142 /*!
116 143 Returns the current list of data points of the series.
117 144 */
118 145 QList<QPointF> QScatterSeries::data()
119 146 {
120 147 return d->m_data;
121 148 }
122 149
123 150 /*!
124 Overrides the default pen used for drawing a marker item with a user defined pen. The default
125 pen is defined by chart theme setting.
151 Overrides the default pen used for drawing a marker item with a user defined \a pen. The
152 default pen is defined by chart theme setting.
126 153
127 154 For example:
128 \code
129 QPen pen(QColor(0, 255, 0, 80), 3);
130 myScatter->setMarkerPen(pen);
131 \endcode
155 \snippet ../example/scatter/main.cpp 5
132 156
133 Would present your scatter markers with an opaque, uglyish green outlines:
157 Would present your scatter markers with an opaque, uglyish green outlines instead of the
158 beatiful markers defined by the chart's theme:
134 159 \image scatter_example_pen.jpg
135 160
136 161 \sa setMarkerBrush()
137 \sa QChart::setTheme()
162 \sa QChart::setChartTheme()
138 163 */
139 164 void QScatterSeries::setMarkerPen(QPen pen)
140 165 {
141 166 d->m_markerPen = pen;
142 167 }
143 168
144 169 /*!
145 170 Returns the pen used for drawing markers.
146 171 */
147 172 QPen QScatterSeries::markerPen()
148 173 {
149 174 return d->m_markerPen;
150 175 }
151 176
152 177 /*!
153 Overrides the default brush of the marker items with a user defined brush. The default
154 brush is defined by chart theme setting.
178 Overrides the default brush of the marker items with a user defined \a brush. The default brush
179 is defined by chart theme setting.
155 180
156 181 For example:
157 \code
158 QBrush brush(QColor(255, 0, 0, 100), Qt::SolidPattern);
159 myRandomScatter->setMarkerBrush(brush);
160 \endcode
182 \snippet ../example/scatter/main.cpp 4
161 183
162 184 Would fill your scatter markers with an opaque red color:
163 185 \image scatter_example_brush.jpg
164 186
165 187 \sa setMarkerPen()
166 \sa QChart::setTheme()
188 \sa QChart::setChartTheme()
167 189 */
168 190 void QScatterSeries::setMarkerBrush(QBrush brush)
169 191 {
170 192 d->m_markerBrush = brush;
171 193 }
172 194
173 195 /*!
174 196 Returns the brush used for drawing markers.
175 197 */
176 198 QBrush QScatterSeries::markerBrush()
177 199 {
178 200 return d->m_markerBrush;
179 201 }
180 202
181 203 /*!
182 Overrides the default shape of the marker items with a user defined shape. The default
183 shape is defined by chart theme setting.
204 Overrides the default shape of the marker items with a user defined \a shape. The default shape
205 is defined by chart theme setting.
184 206
185 207 For example:
186 \code
187 myScatter->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
188 \endcode
208 \snippet ../example/scatter/main.cpp 6
189 209
190 210 Would make your scatter marker items rectangle:
191 211 \image scatter_example_shape.jpg
192 212 */
193 213 void QScatterSeries::setMarkerShape(MarkerShape shape)
194 214 {
195 215 d->m_markerShape = shape;
196 216 }
197 217
198 218 /*!
199 219 Returns the shape used for drawing markers.
200 220 */
201 221 QScatterSeries::MarkerShape QScatterSeries::markerShape()
202 222 {
203 223 return (QScatterSeries::MarkerShape) d->m_markerShape;
204 224 }
205 225
206 226 #include "moc_qscatterseries.cpp"
207 227
208 228 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,66 +1,71
1 1 #ifndef QSCATTERSERIES_H
2 2 #define QSCATTERSERIES_H
3 3
4 4 #include "qchartseries.h"
5 5 #include <QRectF>
6 6 #include <QColor>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9 class QScatterSeriesPrivate;
10 10
11 11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
12 12 {
13 13 Q_OBJECT
14 14
15 15 public:
16 16 enum MarkerShape {
17 17 // TODO: to be defined by the graphics design
18 18 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
19 19 MarkerShapeDefault = 0,
20 20 MarkerShapePoint,
21 21 MarkerShapeX,
22 22 MarkerShapeRectangle,
23 23 MarkerShapeTiltedRectangle,
24 24 MarkerShapeTriangle,
25 25 MarkerShapeCircle
26 26 };
27 27
28 28 public:
29 29 QScatterSeries(QObject *parent = 0);
30 30 ~QScatterSeries();
31 31
32 32 public: // from QChartSeries
33 33 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
34 34
35 35 public:
36 // TODO: the name of the function? addPoint? addData? addValue?
37 36 void addData(QPointF value);
37 void addData(QList<QPointF> points);
38 void setData(QList<QPointF> points);
38 39 QScatterSeries& operator << (const QPointF &value);
39 void setData(QList<QPointF> data);
40 QScatterSeries& operator << (QList<QPointF> points);
40 41 QList<QPointF> data();
41 42 //TODO: insertData?
42 43
43 void setMarkerPen(QPen pen);
44 44 QPen markerPen();
45 void setMarkerBrush(QBrush brush);
46 45 QBrush markerBrush();
47 void setMarkerShape(MarkerShape shape);
48 46 MarkerShape markerShape();
49 47 // TODO: marker size?
50 48
49 public Q_SLOTS:
50 void setMarkerPen(QPen pen);
51 void setMarkerBrush(QBrush brush);
52 void setMarkerShape(MarkerShape shape);
53
51 54 Q_SIGNALS:
52 // TODO: move to PIMPL for simplicity or does the user ever need these signals?
55 void clicked(/* TODO: parameters? */);
56 void hoverEnter(/* TODO: parameters? */);
57 void hoverLeave(/* TODO: parameters? */);
58 // TODO: move to PIMPL for simplicity or does the user ever need changed signals?
53 59 // TODO: more finegrained signaling for performance reasons
54 60 // (check QPieSeries implementation with change sets)
55 61 void changed();
56 62
57 //public Q_SLOTS:
58 63 private:
59 64 Q_DECLARE_PRIVATE(QScatterSeries)
60 65 Q_DISABLE_COPY(QScatterSeries)
61 66 QScatterSeriesPrivate *const d;
62 67 };
63 68
64 69 QTCOMMERCIALCHART_END_NAMESPACE
65 70
66 71 #endif // QSCATTERSERIES_H
@@ -1,157 +1,162
1 1 #include "scatterpresenter_p.h"
2 2 #include "qscatterseries.h"
3 3 #include <QPen>
4 4 #include <QPainter>
5 5 #include <QGraphicsScene>
6 6 #include <QDebug>
7 7 #include <QTime>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 ScatterPresenter::ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent) :
12 12 ChartItem(parent),
13 13 m_series(series),
14 14 m_boundingRect(),
15 15 //m_markerColor(QColor()),
16 16 // m_markerColor(QColor(255, 0, 0)),
17 17 m_visibleChartArea()
18 18 {
19 19 if (parent)
20 20 m_boundingRect = parent->boundingRect();
21 21
22 22 if (series) {
23 23 connect(series, SIGNAL(changed()), this, SLOT(handleModelChanged()));
24 24 }
25 25 }
26 26
27 27 void ScatterPresenter::handleDomainChanged(const Domain& domain)
28 28 {
29 29 m_visibleChartArea = domain;
30 30 changeGeometry();
31 31 }
32 32
33 33 void ScatterPresenter::handleGeometryChanged(const QRectF& rect)
34 34 {
35 35 m_boundingRect = rect;
36 36 changeGeometry();
37 37 }
38 38
39 39 void ScatterPresenter::handleModelChanged()
40 40 {
41 41 // TODO: more fine grained modelChanged signaling
42 42 changeGeometry();
43 43 }
44 44
45 45 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
46 46 {
47 47 // TODO: Optimization: avoid setting on every paint method call?
48 48 // The custom settings in series override those defined by the theme
49 49 int shape = m_series->markerShape();
50 50
51 51 painter->save();
52 52 painter->setClipRect(m_boundingRect);
53 53
54 54 // Paint dropshadow
55 55 QPen dropShadowPen(QColor(0, 0, 0, 70));
56 56 dropShadowPen.setWidth(3);
57 57 painter->setPen(dropShadowPen);
58 58 painter->setBrush(Qt::NoBrush);
59 59 painter->setRenderHint(QPainter::Antialiasing);
60 60 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
61 61 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
62 62 switch (shape) {
63 63 case QScatterSeries::MarkerShapeDefault:
64 64 // Fallthrough, defaults to circle
65 65 case QScatterSeries::MarkerShapeCircle:
66 66 painter->drawChord(m_scenex.at(i) + 2, m_sceney.at(i) + 2, 9, 9, 0, 5760);
67 67 break;
68 68 case QScatterSeries::MarkerShapePoint:
69 69 //painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
70 70 break;
71 71 case QScatterSeries::MarkerShapeRectangle:
72 72 painter->drawRect(m_scenex.at(i) + 2, m_sceney.at(i) + 2, 8, 8);
73 73 break;
74 74 case QScatterSeries::MarkerShapeTiltedRectangle: {
75 75 // TODO:
76 76 static const QPointF points[4] = {
77 77 QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
78 78 QPointF(0.0 + m_scenex.at(i), 1.0 + m_sceney.at(i)),
79 79 QPointF(1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
80 80 QPointF(0.0 + m_scenex.at(i), -1.0 + m_sceney.at(i))
81 81 };
82 82 painter->drawPolygon(points, 4);
83 83 break;
84 84 }
85 85 default:
86 86 // TODO: implement the rest of the shapes
87 87 Q_ASSERT(false);
88 88 break;
89 89 }
90 90 }
91 91
92 92 // Paint the shape
93 93 QPen pen = m_markerPen;
94 94 if (m_series->markerPen().color().isValid())
95 95 pen = m_series->markerPen();
96 96 if (m_series->markerBrush().color().isValid())
97 97 painter->setBrush(m_series->markerBrush());
98 98 else
99 99 painter->setBrush(m_markerBrush);
100 100 painter->setPen(pen);
101 101 painter->setRenderHint(QPainter::Antialiasing, false);
102 102 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
103 103 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
104 104 switch (shape) {
105 105 case QScatterSeries::MarkerShapeDefault:
106 106 // Fallthrough, defaults to circle
107 107 case QScatterSeries::MarkerShapeCircle:
108 108 painter->drawChord(m_scenex.at(i), m_sceney.at(i), 9, 9, 0, 5760);
109 109 break;
110 110 case QScatterSeries::MarkerShapePoint:
111 111 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
112 112 break;
113 113 case QScatterSeries::MarkerShapeRectangle:
114 114 painter->drawRect(m_scenex.at(i), m_sceney.at(i), 9, 9);
115 115 break;
116 116 case QScatterSeries::MarkerShapeTiltedRectangle: {
117 117 // TODO:
118 118 static const QPointF points[4] = {
119 119 QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
120 120 QPointF(0.0 + m_scenex.at(i), 1.0 + m_sceney.at(i)),
121 121 QPointF(1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
122 122 QPointF(0.0 + m_scenex.at(i), -1.0 + m_sceney.at(i))
123 123 };
124 124 painter->drawPolygon(points, 4);
125 125 break;
126 126 }
127 127 default:
128 128 // TODO: implement the rest of the shapes
129 129 Q_ASSERT(false);
130 130 break;
131 131 }
132 132 }
133 133
134 134 painter->restore();
135 135 }
136 136
137 void ScatterPresenter::mousePressEvent(QGraphicsSceneMouseEvent *event)
138 {
139 qDebug() << "ScatterPresenter::mousePressEvent" << event;
140 }
141
137 142 void ScatterPresenter::changeGeometry()
138 143 {
139 144 if (m_boundingRect.isValid()) {
140 145
141 146 prepareGeometryChange();
142 147 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
143 148 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
144 149 m_scenex.clear();
145 150 m_sceney.clear();
146 151
147 152 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
148 153 foreach (QPointF point, m_series->data()) {
149 154 m_scenex.append(m_boundingRect.left() + point.x() * scalex - m_visibleChartArea.m_minX * scalex);
150 155 m_sceney.append(m_boundingRect.bottom() - point.y() * scaley + m_visibleChartArea.m_minY * scaley);
151 156 }
152 157 }
153 158 }
154 159
155 160 #include "moc_scatterpresenter_p.cpp"
156 161
157 162 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,48 +1,49
1 1 #ifndef SCATTERPRESENTER_H
2 2 #define SCATTERPRESENTER_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "chartitem_p.h"
6 6 #include <QObject>
7 7 #include <QPen>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class QScatterSeries;
12 12
13 13 /*!
14 14 * The "business logic" of scatter series. This is a QObject that does not have a parent QObject.
15 15 * The QGraphicsItem parent owns the object instead.
16 16 */
17 17 class ScatterPresenter : public QObject, public ChartItem
18 18 {
19 19 Q_OBJECT
20 20 public:
21 21 explicit ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent = 0);
22 22
23 23 public: // from ChartItem
24 24 QRectF boundingRect() const { return m_boundingRect; }
25 25 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
26 void mousePressEvent (QGraphicsSceneMouseEvent * event);
26 27
27 28 signals:
28 29
29 30 public Q_SLOTS:
30 31 void handleDomainChanged(const Domain& domain);
31 32 void handleGeometryChanged(const QRectF& rect);
32 33 void handleModelChanged();
33 34
34 35 public:
35 36 void changeGeometry();
36 37
37 38 QScatterSeries *m_series;
38 39 QRectF m_boundingRect;
39 40 QList<qreal> m_scenex;
40 41 QList<qreal> m_sceney;
41 42 Domain m_visibleChartArea;
42 43 QPen m_markerPen;
43 44 QBrush m_markerBrush;
44 45 };
45 46
46 47 QTCOMMERCIALCHART_END_NAMESPACE
47 48
48 49 #endif // SCATTERPRESENTER_H
General Comments 0
You need to be logged in to leave comments. Login now