##// END OF EJS Templates
Fixed a bug in QChartView example code
Tero Ahola -
r322:13217cbedecd
parent child
Show More
@@ -1,372 +1,368
1 #include "qchartview.h"
1 #include "qchartview.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4 #include <QGraphicsView>
4 #include <QGraphicsView>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QRubberBand>
6 #include <QRubberBand>
7 #include <QResizeEvent>
7 #include <QResizeEvent>
8 #include <QDebug>
8 #include <QDebug>
9
9
10 /*!
10 /*!
11 \enum QChartView::RubberBandPolicy
11 \enum QChartView::RubberBandPolicy
12
12
13 This enum describes the different types of rubber bands that can be used for zoom rect selection
13 This enum describes the different types of rubber bands that can be used for zoom rect selection
14
14
15 \value NoRubberBand
15 \value NoRubberBand
16 \value VerticalRubberBand
16 \value VerticalRubberBand
17 \value HorizonalRubberBand
17 \value HorizonalRubberBand
18 \value RectangleRubberBand
18 \value RectangleRubberBand
19 */
19 */
20
20
21 /*!
21 /*!
22 \class QChartView
22 \class QChartView
23 \brief Standalone charting widget.
23 \brief Standalone charting widget.
24
24
25 QChartView is a standalone widget that can display charts. It does not require separate
25 QChartView is a standalone widget that can display charts. It does not require separate
26 QGraphicsScene to work. It manages the graphical representation of different types of
26 QGraphicsScene to work. It manages the graphical representation of different types of
27 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
27 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
28 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
28 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
29
29
30 For example, to create a chart with line series using a widget based application:
30 For example, to create a chart with line series using a widget based application:
31 \snippet ../example/chartview/main.cpp 1
31 \snippet ../example/chartview/main.cpp 1
32 \image chartview_example.jpg
32 \image chartview_example.jpg
33
33
34 Showing a few more series:
34 Showing a few more series:
35 \snippet ../example/chartview/main.cpp 3
35 \snippet ../example/chartview/main.cpp 3
36 \codeline
37 \snippet ../example/chartview/main.cpp 4
38 \codeline
39 \snippet ../example/chartview/main.cpp 5
40
41 And the corresponding results:
42 \image chartview_example_pie.jpg
36 \image chartview_example_pie.jpg
37 \snippet ../example/chartview/main.cpp 4
43 \image chartview_example_scatter.jpg
38 \image chartview_example_scatter.jpg
39 \snippet ../example/chartview/main.cpp 5
44 \image chartview_example_bar.jpg
40 \image chartview_example_bar.jpg
45
41
46 If you need to give a more professional touch to your chart you can switch to one of the
42 If you need to give a more professional touch to your chart you can switch to one of the
47 pre-defined themes:
43 pre-defined themes:
48 \snippet ../example/chartview/main.cpp 2
44 \snippet ../example/chartview/main.cpp 2
49 \image chartview_example_theme.jpg
45 \image chartview_example_theme.jpg
50
46
51 \sa QChart
47 \sa QChart
52 */
48 */
53
49
54 QTCOMMERCIALCHART_BEGIN_NAMESPACE
50 QTCOMMERCIALCHART_BEGIN_NAMESPACE
55
51
56 /*!
52 /*!
57 Constructs a chartView object which is a child of a\a parent.
53 Constructs a chartView object which is a child of a\a parent.
58 */
54 */
59 QChartView::QChartView(QWidget *parent) :
55 QChartView::QChartView(QWidget *parent) :
60 QGraphicsView(parent),
56 QGraphicsView(parent),
61 m_scene(new QGraphicsScene(this)),
57 m_scene(new QGraphicsScene(this)),
62 m_chart(new QChart()),
58 m_chart(new QChart()),
63 m_rubberBand(0),
59 m_rubberBand(0),
64 m_verticalRubberBand(false),
60 m_verticalRubberBand(false),
65 m_horizonalRubberBand(false)
61 m_horizonalRubberBand(false)
66 {
62 {
67 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
68 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
64 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
69 setScene(m_scene);
65 setScene(m_scene);
70 m_chart->setMargin(50);
66 m_chart->setMargin(50);
71 m_scene->addItem(m_chart);
67 m_scene->addItem(m_chart);
72 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
68 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
73 }
69 }
74
70
75
71
76 /*!
72 /*!
77 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
73 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
78 */
74 */
79 QChartView::~QChartView()
75 QChartView::~QChartView()
80 {
76 {
81 }
77 }
82
78
83 /*!
79 /*!
84 Resizes and updates the chart area using the \a event data
80 Resizes and updates the chart area using the \a event data
85 */
81 */
86 void QChartView::resizeEvent(QResizeEvent *event)
82 void QChartView::resizeEvent(QResizeEvent *event)
87 {
83 {
88 m_scene->setSceneRect(0,0,size().width(),size().height());
84 m_scene->setSceneRect(0,0,size().width(),size().height());
89 m_chart->resize(size());
85 m_chart->resize(size());
90 QWidget::resizeEvent(event);
86 QWidget::resizeEvent(event);
91 }
87 }
92
88
93 /*!
89 /*!
94 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
90 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
95 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
91 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
96 the y axis).
92 the y axis).
97 \sa removeSeries(), removeAllSeries()
93 \sa removeSeries(), removeAllSeries()
98 */
94 */
99 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
95 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
100 {
96 {
101 m_chart->addSeries(series,axisY);
97 m_chart->addSeries(series,axisY);
102 }
98 }
103
99
104 /*!
100 /*!
105 Removes the \a series specified in a perameter from the QChartView.
101 Removes the \a series specified in a perameter from the QChartView.
106 It releses its ownership of the specified QChartSeries object.
102 It releses its ownership of the specified QChartSeries object.
107 It does not delete the pointed QChartSeries data object
103 It does not delete the pointed QChartSeries data object
108 \sa addSeries(), removeAllSeries()
104 \sa addSeries(), removeAllSeries()
109 */
105 */
110 void QChartView::removeSeries(QChartSeries* series)
106 void QChartView::removeSeries(QChartSeries* series)
111 {
107 {
112 m_chart->removeSeries(series);
108 m_chart->removeSeries(series);
113 }
109 }
114
110
115 /*!
111 /*!
116 Removes all the QChartSeries that have been added to the QChartView
112 Removes all the QChartSeries that have been added to the QChartView
117 It also deletes the pointed QChartSeries data objects
113 It also deletes the pointed QChartSeries data objects
118 \sa addSeries(), removeSeries()
114 \sa addSeries(), removeSeries()
119 */
115 */
120 void QChartView::removeAllSeries()
116 void QChartView::removeAllSeries()
121 {
117 {
122 m_chart->removeAllSeries();
118 m_chart->removeAllSeries();
123 }
119 }
124
120
125 /*!
121 /*!
126 Zooms in the view by a factor of 2
122 Zooms in the view by a factor of 2
127 */
123 */
128 void QChartView::zoomIn()
124 void QChartView::zoomIn()
129 {
125 {
130 m_chart->zoomIn();
126 m_chart->zoomIn();
131 }
127 }
132
128
133 /*!
129 /*!
134 Zooms in the view to a maximum level at which \a rect is still fully visible.
130 Zooms in the view to a maximum level at which \a rect is still fully visible.
135 */
131 */
136 void QChartView::zoomIn(const QRect& rect)
132 void QChartView::zoomIn(const QRect& rect)
137 {
133 {
138 m_chart->zoomIn(rect);
134 m_chart->zoomIn(rect);
139 }
135 }
140
136
141 /*!
137 /*!
142 Restores the view zoom level to the previous one.
138 Restores the view zoom level to the previous one.
143 */
139 */
144 void QChartView::zoomOut()
140 void QChartView::zoomOut()
145 {
141 {
146 m_chart->zoomOut();
142 m_chart->zoomOut();
147 }
143 }
148
144
149 /*!
145 /*!
150 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
146 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
151 */
147 */
152 int QChartView::margin() const
148 int QChartView::margin() const
153 {
149 {
154 return m_chart->margin();
150 return m_chart->margin();
155 }
151 }
156
152
157 /*!
153 /*!
158 Sets the chart \a title. A description text that is rendered above the chart.
154 Sets the chart \a title. A description text that is rendered above the chart.
159 */
155 */
160 void QChartView::setChartTitle(const QString& title)
156 void QChartView::setChartTitle(const QString& title)
161 {
157 {
162 m_chart->setChartTitle(title);
158 m_chart->setChartTitle(title);
163 }
159 }
164
160
165 /*!
161 /*!
166 Sets the \a font that is used for rendering the description text that is rendered above the chart.
162 Sets the \a font that is used for rendering the description text that is rendered above the chart.
167 */
163 */
168 void QChartView::setChartTitleFont(const QFont& font)
164 void QChartView::setChartTitleFont(const QFont& font)
169 {
165 {
170 m_chart->setChartTitleFont(font);
166 m_chart->setChartTitleFont(font);
171 }
167 }
172
168
173 /*!
169 /*!
174 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
170 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
175 */
171 */
176 void QChartView::setChartBackgroundBrush(const QBrush& brush)
172 void QChartView::setChartBackgroundBrush(const QBrush& brush)
177 {
173 {
178 m_chart->setChartBackgroundBrush(brush);
174 m_chart->setChartBackgroundBrush(brush);
179 }
175 }
180
176
181 /*!
177 /*!
182 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
178 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
183 */
179 */
184 void QChartView::setChartBackgroundPen(const QPen& pen)
180 void QChartView::setChartBackgroundPen(const QPen& pen)
185 {
181 {
186 m_chart->setChartBackgroundPen(pen);
182 m_chart->setChartBackgroundPen(pen);
187 }
183 }
188
184
189 /*!
185 /*!
190 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
186 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
191 */
187 */
192 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
188 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
193 {
189 {
194 switch(policy) {
190 switch(policy) {
195 case VerticalRubberBand:
191 case VerticalRubberBand:
196 m_verticalRubberBand = true;
192 m_verticalRubberBand = true;
197 m_horizonalRubberBand = false;
193 m_horizonalRubberBand = false;
198 break;
194 break;
199 case HorizonalRubberBand:
195 case HorizonalRubberBand:
200 m_verticalRubberBand = false;
196 m_verticalRubberBand = false;
201 m_horizonalRubberBand = true;
197 m_horizonalRubberBand = true;
202 break;
198 break;
203 case RectangleRubberBand:
199 case RectangleRubberBand:
204 m_verticalRubberBand = true;
200 m_verticalRubberBand = true;
205 m_horizonalRubberBand = true;
201 m_horizonalRubberBand = true;
206 break;
202 break;
207 case NoRubberBand:
203 case NoRubberBand:
208 default:
204 default:
209 delete m_rubberBand;
205 delete m_rubberBand;
210 m_rubberBand=0;
206 m_rubberBand=0;
211 m_horizonalRubberBand = false;
207 m_horizonalRubberBand = false;
212 m_verticalRubberBand = false;
208 m_verticalRubberBand = false;
213 return;
209 return;
214 }
210 }
215 if(!m_rubberBand) {
211 if(!m_rubberBand) {
216 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
212 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
217 m_rubberBand->setEnabled(true);
213 m_rubberBand->setEnabled(true);
218 }
214 }
219 }
215 }
220
216
221 /*!
217 /*!
222 Returns the RubberBandPolicy that is currently being used by the widget.
218 Returns the RubberBandPolicy that is currently being used by the widget.
223 */
219 */
224 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
220 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
225 {
221 {
226 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
222 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
227 if(m_horizonalRubberBand) return HorizonalRubberBand;
223 if(m_horizonalRubberBand) return HorizonalRubberBand;
228 if(m_verticalRubberBand) return VerticalRubberBand;
224 if(m_verticalRubberBand) return VerticalRubberBand;
229 return NoRubberBand;
225 return NoRubberBand;
230 }
226 }
231
227
232 /*!
228 /*!
233 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
229 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
234 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation is called.
230 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation is called.
235 */
231 */
236 void QChartView::mousePressEvent(QMouseEvent *event)
232 void QChartView::mousePressEvent(QMouseEvent *event)
237 {
233 {
238 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
234 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
239
235
240 int margin = m_chart->margin();
236 int margin = m_chart->margin();
241 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
237 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
242
238
243 if (rect.contains(event->pos())) {
239 if (rect.contains(event->pos())) {
244 m_rubberBandOrigin = event->pos();
240 m_rubberBandOrigin = event->pos();
245 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
241 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
246 m_rubberBand->show();
242 m_rubberBand->show();
247 event->accept();
243 event->accept();
248 }
244 }
249 }
245 }
250 else {
246 else {
251 QGraphicsView::mousePressEvent(event);
247 QGraphicsView::mousePressEvent(event);
252 }
248 }
253 }
249 }
254
250
255 /*!
251 /*!
256 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
252 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
257 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
253 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
258 */
254 */
259 void QChartView::mouseMoveEvent(QMouseEvent *event)
255 void QChartView::mouseMoveEvent(QMouseEvent *event)
260 {
256 {
261 if(m_rubberBand && m_rubberBand->isVisible()) {
257 if(m_rubberBand && m_rubberBand->isVisible()) {
262 int margin = m_chart->margin();
258 int margin = m_chart->margin();
263 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
259 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
264 int width = event->pos().x() - m_rubberBandOrigin.x();
260 int width = event->pos().x() - m_rubberBandOrigin.x();
265 int height = event->pos().y() - m_rubberBandOrigin.y();
261 int height = event->pos().y() - m_rubberBandOrigin.y();
266 if(!m_verticalRubberBand) {
262 if(!m_verticalRubberBand) {
267 m_rubberBandOrigin.setY(rect.top());
263 m_rubberBandOrigin.setY(rect.top());
268 height = rect.height();
264 height = rect.height();
269 }
265 }
270 if(!m_horizonalRubberBand) {
266 if(!m_horizonalRubberBand) {
271 m_rubberBandOrigin.setX(rect.left());
267 m_rubberBandOrigin.setX(rect.left());
272 width= rect.width();
268 width= rect.width();
273 }
269 }
274 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
270 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
275 }
271 }
276 else {
272 else {
277 QGraphicsView::mouseMoveEvent(event);
273 QGraphicsView::mouseMoveEvent(event);
278 }
274 }
279 }
275 }
280
276
281 /*!
277 /*!
282 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
278 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
283 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
279 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
284 */
280 */
285 void QChartView::mouseReleaseEvent(QMouseEvent *event)
281 void QChartView::mouseReleaseEvent(QMouseEvent *event)
286 {
282 {
287 if(m_rubberBand) {
283 if(m_rubberBand) {
288 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
284 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
289 m_rubberBand->hide();
285 m_rubberBand->hide();
290 QRect rect = m_rubberBand->geometry();
286 QRect rect = m_rubberBand->geometry();
291 m_chart->zoomIn(rect);
287 m_chart->zoomIn(rect);
292 event->accept();
288 event->accept();
293 }
289 }
294
290
295 if(event->button()==Qt::RightButton)
291 if(event->button()==Qt::RightButton)
296 m_chart->zoomReset();
292 m_chart->zoomReset();
297 }
293 }
298 else {
294 else {
299 QGraphicsView::mouseReleaseEvent(event);
295 QGraphicsView::mouseReleaseEvent(event);
300 }
296 }
301 }
297 }
302
298
303 /*!
299 /*!
304 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
300 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
305 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
301 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
306 */
302 */
307 void QChartView::keyPressEvent(QKeyEvent *event)
303 void QChartView::keyPressEvent(QKeyEvent *event)
308 {
304 {
309 switch (event->key()) {
305 switch (event->key()) {
310 case Qt::Key_Plus:
306 case Qt::Key_Plus:
311 zoomIn();
307 zoomIn();
312 break;
308 break;
313 case Qt::Key_Minus:
309 case Qt::Key_Minus:
314 zoomOut();
310 zoomOut();
315 break;
311 break;
316 default:
312 default:
317 QGraphicsView::keyPressEvent(event);
313 QGraphicsView::keyPressEvent(event);
318 break;
314 break;
319 }
315 }
320 }
316 }
321
317
322 /*!
318 /*!
323 Sets the \a theme used by the chart for rendering the graphical representation of the data
319 Sets the \a theme used by the chart for rendering the graphical representation of the data
324 \sa QChart::ChartTheme, chartTheme()
320 \sa QChart::ChartTheme, chartTheme()
325 */
321 */
326 void QChartView::setChartTheme(QChart::ChartTheme theme)
322 void QChartView::setChartTheme(QChart::ChartTheme theme)
327 {
323 {
328 m_chart->setChartTheme(theme);
324 m_chart->setChartTheme(theme);
329 }
325 }
330
326
331 /*!
327 /*!
332 Returns the theme enum used by the chart.
328 Returns the theme enum used by the chart.
333 \sa setChartTheme()
329 \sa setChartTheme()
334 */
330 */
335 QChart::ChartTheme QChartView::chartTheme() const
331 QChart::ChartTheme QChartView::chartTheme() const
336 {
332 {
337 return m_chart->chartTheme();
333 return m_chart->chartTheme();
338 }
334 }
339
335
340 /*!
336 /*!
341 Returns the pointer to the x axis object of the chart
337 Returns the pointer to the x axis object of the chart
342 */
338 */
343 QChartAxis* QChartView::axisX() const
339 QChartAxis* QChartView::axisX() const
344 {
340 {
345 return m_chart->axisX();
341 return m_chart->axisX();
346 }
342 }
347
343
348 /*!
344 /*!
349 Returns the pointer to the y axis object of the chart
345 Returns the pointer to the y axis object of the chart
350 */
346 */
351 QChartAxis* QChartView::axisY() const
347 QChartAxis* QChartView::axisY() const
352 {
348 {
353 return m_chart->axisY();
349 return m_chart->axisY();
354 }
350 }
355
351
356 /*!
352 /*!
357 Sets animation \a options for the chart
353 Sets animation \a options for the chart
358 */
354 */
359 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
355 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
360 {
356 {
361 m_chart->setAnimationOptions(options);
357 m_chart->setAnimationOptions(options);
362 }
358 }
363
359
364 /*!
360 /*!
365 Returns animation options for the chart
361 Returns animation options for the chart
366 */
362 */
367 QChart::AnimationOptions QChartView::animationOptions() const
363 QChart::AnimationOptions QChartView::animationOptions() const
368 {
364 {
369 return m_chart->animationOptions();
365 return m_chart->animationOptions();
370 }
366 }
371
367
372 QTCOMMERCIALCHART_END_NAMESPACE
368 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now