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