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