##// END OF EJS Templates
Fixing review findings in QScatterSeries
Tero Ahola -
r358:543ce926fdb7
parent child
Show More
@@ -1,29 +1,32
1 1 /*!
2 2 \page index.html
3 3 \keyword About
4 4
5 5 \raw HTML
6 6 <div class="qchart">
7 7 <img src="images/qt_commercial_logo.png" alt="qtcommercial"/>
8 8
9 9 <p>
10 QCharts is a part of Qt Commercial addons package. It provides a set of simple chart components which are available for Qt Commercial customers.
11 It uses Qt Graphics View Framework, therefore charts can be easily integrated 2D modern user interfaces. QCharts can be used as QWidgets, QGraphicsWidget or QML elements.
12 Users can easily create impressive graphs by selecting one of the charts themes.
10 QCharts is a part of Qt Commercial addons package. It provides a set of easy to use chart
11 components which are available for Qt Commercial customers. It uses Qt Graphics View
12 Framework, therefore charts can be easily integrated to modern 2D user interfaces. QCharts can
13 be used as QWidgets, QGraphicsWidget or QML elements. Users can easily create impressive
14 graphs by selecting one of the charts themes.
13 15 </p>
16
14 17 <table><tr>
15 18 <td><img src="images/linechart.png" alt="linechart" /></td>
16 19 <td><img src="images/chartview_example_bar.jpg " alt="barchart" /></td>
17 20 <td><img src="images/chartview_example_pie.jpg " alt="piechart" /></td>
18 21 </tr>
19 22 <tr>
20 23 <td><img src="images/chartview_example.jpg " alt="linechart" /></td>
21 24 <td><img src="images/chartview_example_scatter.jpg " alt="scatterchart" /></td>
22 25 <td><img src="images/chartview_example_theme.jpg " alt="themechart" /></td>
23 26 </tr>
24 27 </table>
25 28 </div>
26 29 \endraw
27 30
28 31
29 32 */
@@ -1,67 +1,62
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 13 //! [1]
14 14 // Create chart view
15 15 QChartView *chartView = new QChartView();
16 16 chartView->setChartTheme(QChart::ChartThemeIcy);
17 17
18 18 // Add scatter series with simple test data
19 19 QScatterSeries *scatter = new QScatterSeries();
20 20 *scatter << QPointF(0.5, 5.0)
21 21 << QPointF(1.0, 4.5)
22 22 << QPointF(1.0, 5.5)
23 23 << QPointF(1.5, 5.0)
24 24 << QPointF(2.0, 4.5)
25 25 << QPointF(2.5, 5.0);
26 26 // Chart takes ownership
27 27 chartView->addSeries(scatter);
28 28 //! [1]
29 29
30 // Add some more data
31 //! [2]
32 scatter->addData(QPointF(2.0, 5.5));
33 //! [2]
34
35 30 // And more
36 31 //! [3]
37 *scatter << QPointF(2.0, 5.5);
32 *scatter << QPointF(2.0, 5.5) << QPointF(2.2, 5.4);
38 33 //! [3]
39 34
40 35 // Add another scatter series (re-use the previous pointer)
41 36 // - more data with random component
42 37 scatter = new QScatterSeries();
43 38 for (qreal i(0.0); i < 20; i += 0.15) {
44 39 (*scatter) << QPointF(i + (qreal)(rand() % 100) / 100.0,
45 40 i + (qreal)(rand() % 100) / 100.0);
46 41 }
47 42 //! [4]
48 43 QBrush brush(QColor(255, 0, 0, 100), Qt::SolidPattern);
49 scatter->setMarkerBrush(brush);
44 scatter->setBrush(brush);
50 45 //! [4]
51 46 //! [5]
52 47 QPen pen(QColor(0, 255, 0, 80), 3);
53 scatter->setMarkerPen(pen);
48 scatter->setPen(pen);
54 49 //! [5]
55 50 //! [6]
56 scatter->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
51 scatter->setShape(QScatterSeries::MarkerShapeRectangle);
57 52 //! [6]
58 53 chartView->addSeries(scatter);
59 54
60 55 // Use the chart widget as the central widget
61 56 QMainWindow w;
62 57 w.resize(640, 480);
63 58 w.setCentralWidget(chartView);
64 59 w.show();
65 60
66 61 return a.exec();
67 62 }
@@ -1,68 +1,68
1 1 #include "declarativescatterseries.h"
2 2 #include "declarativechart.h"
3 3 #include "qchart.h"
4 4 #include "qscatterseries.h"
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) :
9 9 QDeclarativeItem(parent),
10 10 m_chart(0),
11 11 m_series(0)
12 12 {
13 13 setFlag(QGraphicsItem::ItemHasNoContents, false);
14 14 connect(this, SIGNAL(parentChanged()),
15 15 this, SLOT(setParentForSeries()));
16 16 }
17 17
18 18 void DeclarativeScatterSeries::setParentForSeries()
19 19 {
20 20 if (!m_series)
21 21 initSeries();
22 22 }
23 23
24 24 void DeclarativeScatterSeries::initSeries()
25 25 {
26 26 Q_ASSERT(!m_series);
27 27 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
28 28
29 29 if (declarativeChart) {
30 30 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
31 31 qDebug() << "creating scatter series for chart: " << chart;
32 32 Q_ASSERT(chart);
33 33
34 34 m_series = new QScatterSeries();
35 35 Q_ASSERT(m_series);
36 36 for (int i(0); i < m_data.count(); i++) {
37 37 ScatterElement *element = m_data.at(i);
38 38 *m_series << QPointF(element->x(), element->y());
39 39 }
40 40 chart->addSeries(m_series);
41 41 }
42 42 }
43 43
44 44 QDeclarativeListProperty<ScatterElement> DeclarativeScatterSeries::data()
45 45 {
46 46 return QDeclarativeListProperty<ScatterElement>(this, 0,
47 47 &DeclarativeScatterSeries::appendData);
48 48 }
49 49
50 50 void DeclarativeScatterSeries::appendData(QDeclarativeListProperty<ScatterElement> *list,
51 51 ScatterElement *element)
52 52 {
53 53 DeclarativeScatterSeries *series = qobject_cast<DeclarativeScatterSeries *>(list->object);
54 54 qDebug() << "appendData: " << series;
55 55 qDebug() << "appendData: " << element;
56 56 qDebug() << "appendData: " << element->x();
57 57 qDebug() << "appendData: " << element->y();
58 58 qDebug() << "appendData: " << series->m_series;
59 59 if (series) {
60 60 series->m_data.append(element);
61 61 if (series->m_series)
62 series->m_series->addData(QPointF(element->x(), element->y()));
62 series->m_series->add(element->x(), element->y());
63 63 }
64 64 }
65 65
66 66 #include "moc_declarativescatterseries.cpp"
67 67
68 68 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,228 +1,225
1 1 #include "qscatterseries.h"
2 2 #include "scatterseries_p.h"
3 3 #include "qchart.h"
4 4
5 5 /*!
6 6 \class QScatterSeries
7 7 \brief QtCommercial Chart series API for showing scatter series.
8 8
9 9 \mainclass
10 10
11 11 Example on how to create a chart with scatter series:
12 12 \snippet ../example/scatter/main.cpp 1
13 13
14 14 The example code would result the following:
15 15
16 16 \image scatter_example1.jpg
17 17 */
18 18
19 19 /*!
20 20 \enum QScatterSeries::MarkerShape
21 21
22 22 This enum describes the shape used when rendering marker items.
23 23
24 24 \value MarkerShapeDefault
25 25 \value MarkerShapePoint
26 26 \value MarkerShapeX
27 27 \value MarkerShapeRectangle
28 28 \value MarkerShapeTiltedRectangle
29 29 \value MarkerShapeTriangle
30 30 \value MarkerShapeCircle
31 31 */
32 32
33 33 /*!
34 34 \fn QChartSeriesType QScatterSeries::type() const
35 35 \brief Returns QChartSeries::SeriesTypeScatter.
36 36 */
37 37
38 38 /*!
39 39 \fn void QScatterSeries::clicked()
40 40 \brief TODO
41 41 */
42 42
43 43 /*!
44 \fn void QScatterSeries::hoverEnter()
45 \brief TODO
46 */
47
48 /*!
49 \fn void QScatterSeries::hoverLeave()
50 \brief TODO
51 */
52
53 /*!
54 44 \fn void QScatterSeries::changed()
55 45 \brief TODO
56 46 */
57 47
58 48 QTCOMMERCIALCHART_BEGIN_NAMESPACE
59 49
60 50 QScatterSeriesPrivate::QScatterSeriesPrivate() :
61 51 m_data(QList<QPointF>()),
62 52 m_markerPen(QPen()),
63 53 m_markerBrush(QBrush()),
64 54 m_markerShape(QScatterSeries::MarkerShapeDefault)
65 55 {
66 56 // Initialize pen color to invalid to use a theme color by default
67 57 m_markerPen.setColor(QColor::Invalid);
68 58 m_markerBrush.setColor(QColor::Invalid);
69 59 }
70 60
71 61 /*!
72 62 Constructs a series object which is a child of \a parent.
73 63 */
74 64 QScatterSeries::QScatterSeries(QObject *parent) :
75 65 QChartSeries(parent),
76 66 d(new QScatterSeriesPrivate())
77 67 {
78 68 }
79 69
80 70 /*!
81 71 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
82 72 */
83 73 QScatterSeries::~QScatterSeries()
84 74 {
85 75 delete d;
86 76 }
87 77
88 78 /*!
79 Add single data point with \a x and \a y coordinates to the series.
80 */
81 void QScatterSeries::add(qreal x, qreal y)
82 {
83 d->m_data.append(QPointF(x, y));
84 emit changed();
85 }
86
87 /*!
89 88 Add single data point with \a value to the series.
90 For example:
91 \snippet ../example/scatter/main.cpp 2
92 89 */
93 void QScatterSeries::addData(QPointF value)
90 void QScatterSeries::add(QPointF value)
94 91 {
95 92 d->m_data.append(value);
96 93 emit changed();
97 94 }
98 95
99 96 /*!
100 97 Add list of \a points to the series.
101 98 */
102 void QScatterSeries::addData(QList<QPointF> points)
99 void QScatterSeries::add(QList<QPointF> points)
103 100 {
104 101 d->m_data.append(points);
105 102 emit changed();
106 103 }
107 104
108 105 /*!
109 106 Stream operator for adding a data point with \a value to the series.
110 \sa addData()
107 \sa add()
111 108
112 109 For example:
113 110 \snippet ../example/scatter/main.cpp 3
114 111 */
115 112 QScatterSeries& QScatterSeries::operator << (const QPointF &value)
116 113 {
117 114 d->m_data.append(value);
118 115 emit changed();
119 116 return *this;
120 117 }
121 118
122 119 /*!
123 120 Stream operator for adding a list of points to the series.
124 \sa addData()
121 \sa add()
125 122 */
126 123 QScatterSeries& QScatterSeries::operator << (QList<QPointF> value)
127 124 {
128 125 d->m_data.append(value);
129 126 emit changed();
130 127 return *this;
131 128 }
132 129
133 130 /*!
134 131 Replaces the data of the series with the given list of data \a points.
135 132 */
136 133 void QScatterSeries::setData(QList<QPointF> points)
137 134 {
138 135 d->m_data = points;
139 136 emit changed();
140 137 }
141 138
142 139 /*!
143 140 Returns the current list of data points of the series.
144 141 */
145 142 QList<QPointF> QScatterSeries::data()
146 143 {
147 144 return d->m_data;
148 145 }
149 146
150 147 /*!
151 148 Overrides the default pen used for drawing a marker item with a user defined \a pen. The
152 149 default pen is defined by chart theme setting.
153 150
154 151 For example:
155 152 \snippet ../example/scatter/main.cpp 5
156 153
157 154 Would present your scatter markers with an opaque, uglyish green outlines instead of the
158 155 beatiful markers defined by the chart's theme:
159 156 \image scatter_example_pen.jpg
160 157
161 \sa setMarkerBrush()
158 \sa setBrush()
162 159 \sa QChart::setChartTheme()
163 160 */
164 void QScatterSeries::setMarkerPen(QPen pen)
161 void QScatterSeries::setPen(QPen pen)
165 162 {
166 163 d->m_markerPen = pen;
167 164 }
168 165
169 166 /*!
170 167 Returns the pen used for drawing markers.
171 168 */
172 QPen QScatterSeries::markerPen()
169 QPen QScatterSeries::pen()
173 170 {
174 171 return d->m_markerPen;
175 172 }
176 173
177 174 /*!
178 175 Overrides the default brush of the marker items with a user defined \a brush. The default brush
179 176 is defined by chart theme setting.
180 177
181 178 For example:
182 179 \snippet ../example/scatter/main.cpp 4
183 180
184 181 Would fill your scatter markers with an opaque red color:
185 182 \image scatter_example_brush.jpg
186 183
187 \sa setMarkerPen()
184 \sa setPen()
188 185 \sa QChart::setChartTheme()
189 186 */
190 void QScatterSeries::setMarkerBrush(QBrush brush)
187 void QScatterSeries::setBrush(QBrush brush)
191 188 {
192 189 d->m_markerBrush = brush;
193 190 }
194 191
195 192 /*!
196 193 Returns the brush used for drawing markers.
197 194 */
198 QBrush QScatterSeries::markerBrush()
195 QBrush QScatterSeries::brush()
199 196 {
200 197 return d->m_markerBrush;
201 198 }
202 199
203 200 /*!
204 201 Overrides the default shape of the marker items with a user defined \a shape. The default shape
205 202 is defined by chart theme setting.
206 203
207 204 For example:
208 205 \snippet ../example/scatter/main.cpp 6
209 206
210 207 Would make your scatter marker items rectangle:
211 208 \image scatter_example_shape.jpg
212 209 */
213 void QScatterSeries::setMarkerShape(MarkerShape shape)
210 void QScatterSeries::setShape(MarkerShape shape)
214 211 {
215 212 d->m_markerShape = shape;
216 213 }
217 214
218 215 /*!
219 216 Returns the shape used for drawing markers.
220 217 */
221 QScatterSeries::MarkerShape QScatterSeries::markerShape()
218 QScatterSeries::MarkerShape QScatterSeries::shape()
222 219 {
223 220 return (QScatterSeries::MarkerShape) d->m_markerShape;
224 221 }
225 222
226 223 #include "moc_qscatterseries.cpp"
227 224
228 225 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,71 +1,68
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 void addData(QPointF value);
37 void addData(QList<QPointF> points);
36 void add(qreal x, qreal y);
37 void add(QPointF value);
38 void add(QList<QPointF> points);
38 39 void setData(QList<QPointF> points);
39 40 QScatterSeries& operator << (const QPointF &value);
40 41 QScatterSeries& operator << (QList<QPointF> points);
41 42 QList<QPointF> data();
42 //TODO: insertData?
43 //TODO: insert, replace, remove, clear...?
43 44
44 QPen markerPen();
45 QBrush markerBrush();
46 MarkerShape markerShape();
45 QPen pen();
46 void setPen(QPen pen);
47 QBrush brush();
48 void setBrush(QBrush brush);
49 MarkerShape shape();
50 void setShape(MarkerShape shape);
47 51 // TODO: marker size?
48 52
49 public Q_SLOTS:
50 void setMarkerPen(QPen pen);
51 void setMarkerBrush(QBrush brush);
52 void setMarkerShape(MarkerShape shape);
53
54 53 Q_SIGNALS:
55 54 void clicked(/* TODO: parameters? */);
56 void hoverEnter(/* TODO: parameters? */);
57 void hoverLeave(/* TODO: parameters? */);
58 55 // TODO: move to PIMPL for simplicity or does the user ever need changed signals?
59 56 // TODO: more finegrained signaling for performance reasons
60 57 // (check QPieSeries implementation with change sets)
61 58 void changed();
62 59
63 60 private:
64 61 Q_DECLARE_PRIVATE(QScatterSeries)
65 62 Q_DISABLE_COPY(QScatterSeries)
66 63 QScatterSeriesPrivate *const d;
67 64 };
68 65
69 66 QTCOMMERCIALCHART_END_NAMESPACE
70 67
71 68 #endif // QSCATTERSERIES_H
@@ -1,129 +1,124
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 <QGraphicsSceneMouseEvent>
7 7 #include <QGraphicsDropShadowEffect>
8 8 #include <QDebug>
9 9 #include <QTime>
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 ScatterPresenter::ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent) :
14 14 ChartItem(parent),
15 15 m_series(series),
16 16 m_boundingRect(),
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 QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
27 27 dropShadow->setOffset(2.0);
28 dropShadow->setBlurRadius(2.0);
28 29 setGraphicsEffect(dropShadow);
29 30 }
30 31
31 32 void ScatterPresenter::handleDomainChanged(const Domain& domain)
32 33 {
33 34 m_visibleChartArea = domain;
34 35 changeGeometry();
35 36 }
36 37
37 38 void ScatterPresenter::handleGeometryChanged(const QRectF& rect)
38 39 {
39 40 m_boundingRect = rect;
40 41 changeGeometry();
41 42 }
42 43
43 44 void ScatterPresenter::handleModelChanged()
44 45 {
45 46 // TODO: more fine grained modelChanged signaling
46 47 changeGeometry();
47 48 }
48 49
49 50 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
50 51 {
51 52 painter->save();
52 53 painter->setClipRect(m_boundingRect);
53 54
54 55 // Paint the shape
55 56 // The custom settings in series override those defined by the theme
56 57 QPen pen = m_markerPen;
57 if (m_series->markerPen().color().isValid())
58 pen = m_series->markerPen();
59 if (m_series->markerBrush().color().isValid())
60 painter->setBrush(m_series->markerBrush());
58 if (m_series->pen().color().isValid())
59 pen = m_series->pen();
60 if (m_series->brush().color().isValid())
61 painter->setBrush(m_series->brush());
61 62 else
62 63 painter->setBrush(m_markerBrush);
63 64 painter->setPen(pen);
64 65 painter->drawPath(m_path);
65 66
66 67 painter->restore();
67 68 }
68 69
69 70 void ScatterPresenter::mousePressEvent(QGraphicsSceneMouseEvent *event)
70 71 {
71 72 qDebug() << "ScatterPresenter::mousePressEvent" << event << " cont: "
72 73 << m_path.contains(event->lastPos());
73 74
74 75 if (m_path.contains(event->lastPos()))
75 76 emit clicked();
76 77 }
77 78
78 79 void ScatterPresenter::changeGeometry()
79 80 {
80 81 if (m_boundingRect.isValid()) {
81 82 prepareGeometryChange();
82 83 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
83 84 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
84 85
85 int shape = m_series->markerShape();
86 int shape = m_series->shape();
86 87 m_path = QPainterPath();
87 88
88 89 foreach (QPointF point, m_series->data()) {
89 90 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
90 91 qreal x = m_boundingRect.left() + point.x() * scalex - m_visibleChartArea.m_minX * scalex;
91 92 qreal y = m_boundingRect.bottom() - point.y() * scaley + m_visibleChartArea.m_minY * scaley;
92 93
93 94 if (scene()->width() > x && scene()->height() > y) {
94 95 switch (shape) {
95 96 case QScatterSeries::MarkerShapeDefault:
96 97 // Fallthrough, defaults to circle
97 98 case QScatterSeries::MarkerShapeCircle:
98 99 m_path.addEllipse(x, y, 9, 9);
99 100 break;
100 101 case QScatterSeries::MarkerShapePoint:
101 102 m_path.addEllipse(x, y, 2, 2);
102 103 break;
103 104 case QScatterSeries::MarkerShapeRectangle:
104 105 m_path.addRect(x, y, 9, 9);
105 106 break;
106 107 case QScatterSeries::MarkerShapeTiltedRectangle: {
107 // TODO:
108 // static const QPointF points[4] = {
109 // QPointF(-1.0 + x, 0.0 + y),
110 // QPointF(0.0 + x, 1.0 + y),
111 // QPointF(1.0 + x, 0.0 + y),
112 // QPointF(0.0 + x, -1.0 + y)
113 // };
114 //m_path.addPolygon(QPolygon(4, &points));
108 // TODO: tilt the rectangle
109 m_path.addRect(x, y, 9, 9);
115 110 break;
116 111 }
117 112 default:
118 113 // TODO: implement the rest of the shapes
119 114 Q_ASSERT(false);
120 115 break;
121 116 }
122 117 }
123 118 }
124 119 }
125 120 }
126 121
127 122 #include "moc_scatterpresenter_p.cpp"
128 123
129 124 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now