##// END OF EJS Templates
Fixed QML ChartView background color property
Tero Ahola -
r1473:a0b240042977
parent child
Show More
@@ -1,355 +1,378
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativechart.h"
22 22 #include <QPainter>
23 23 #include "declarativelineseries.h"
24 24 #include "declarativeareaseries.h"
25 25 #include "declarativebarseries.h"
26 26 #include "declarativepieseries.h"
27 27 #include "declarativesplineseries.h"
28 28 #include "declarativescatterseries.h"
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 /*!
33 33 \qmlclass ChartView DeclarativeChart
34 34
35 35 ChartView element is the parent that is responsible for showing different chart series types.
36 36
37 37 \section1 Example Usage
38 38
39 39 \beginfloatleft
40 40 \image demos_qmlchart1.png
41 41 \endfloat
42 42 \clearfloat
43 43 */
44 44
45 45 /*!
46 46 \qmlproperty Theme ChartView::theme
47 47 Theme defines the visual appearance of the chart, including for example colors, fonts, line
48 48 widths and chart background.
49 49 */
50 50
51 51 /*!
52 52 \qmlproperty Animation ChartView::animation
53 53 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
54 54 ChartView.SeriesAnimations or ChartView.AllAnimations.
55 55 */
56 56
57 57 /*!
58 58 \qmlproperty string ChartView::title
59 59 The title of the chart, shown on top of the chart.
60 \sa ChartView::titleColor
60 61 */
61 62
62 63 /*!
63 64 \qmlproperty string ChartView::titleColor
64 65 The color of the title text.
65 66 */
66 67
67 68 /*!
68 69 \qmlproperty Axis ChartView::axisX
69 70 The x-axis of the chart.
70 71 */
71 72
72 73 /*!
73 74 \qmlproperty Axis ChartView::axisY
74 75 The default y-axis of the chart.
75 76 */
76 77
77 78 /*!
79 \qmlproperty Legend ChartView::legend
80 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
81 */
82
83 /*!
84 \qmlproperty int ChartView::count
85 The count of series added to the chart.
86 */
87
88 /*!
89 \qmlproperty color ChartView::backgroundColor
90 The color of the chart's background. By default background color is defined by chart theme.
91 \sa ChartView::theme
92 */
93
94 /*!
95 \qmlproperty bool ChartView::dropShadowEnabled
96 The chart's border drop shadow. Set to true to enable drop shadow.
97 */
98
99 /*!
78 100 \qmlmethod Axis ChartView::axisY(QAbstractSeries *series)
79 101 The y-axis of the series. This is the same as the default y-axis of the chart, unless you have
80 102 explicitly defined the series to have a non-default y-axis.
81 103 */
82 104
83 105 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
84 106 : QDeclarativeItem(parent),
85 107 m_chart(new QChart(this))
86 108 {
87 109 setFlag(QGraphicsItem::ItemHasNoContents, false);
88 110 // m_chart->axisX()->setNiceNumbersEnabled(false);
89 111 }
90 112
91 113 DeclarativeChart::~DeclarativeChart()
92 114 {
93 115 delete m_chart;
94 116 }
95 117
96 118 void DeclarativeChart::childEvent(QChildEvent *event)
97 119 {
98 120 if (event->type() == QEvent::ChildAdded) {
99 121 if (qobject_cast<QAbstractSeries *>(event->child())) {
100 122 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
101 123 }
102 124 }
103 125 }
104 126
105 127 void DeclarativeChart::componentComplete()
106 128 {
107 129 foreach(QObject *child, children()) {
108 130 if (qobject_cast<QAbstractSeries *>(child)) {
109 131 // qDebug() << "DeclarativeChart::componentComplete(), add: " << child;
110 132 // TODO: how about optional y-axis?
111 133 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
112 134 }
113 135 }
114 136 QDeclarativeItem::componentComplete();
115 137 }
116 138
117 139 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
118 140 {
119 141 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
120 142 if (newGeometry.isValid()) {
121 143 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
122 144 m_chart->resize(newGeometry.width(), newGeometry.height());
123 145 }
124 146 }
125 147 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
126 148 }
127 149
128 150 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
129 151 {
130 152 Q_UNUSED(option)
131 153 Q_UNUSED(widget)
132 154
133 155 // TODO: optimized?
134 156 painter->setRenderHint(QPainter::Antialiasing, true);
135 157 }
136 158
137 159 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
138 160 {
139 161 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
140 162 if (chartTheme != m_chart->theme()) {
141 163 m_chart->setTheme(chartTheme);
142 164 themeChanged();
143 165 }
144 166 }
145 167
146 168 DeclarativeChart::Theme DeclarativeChart::theme()
147 169 {
148 170 return (DeclarativeChart::Theme) m_chart->theme();
149 171 }
150 172
151 173 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
152 174 {
153 175 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
154 176 if (animationOptions != m_chart->animationOptions()) {
155 177 m_chart->setAnimationOptions(animationOptions);
156 178 animationOptionsChanged();
157 179 }
158 180 }
159 181
160 182 DeclarativeChart::Animation DeclarativeChart::animationOptions()
161 183 {
162 184 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
163 185 return DeclarativeChart::AllAnimations;
164 186 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
165 187 return DeclarativeChart::GridAxisAnimations;
166 188 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
167 189 return DeclarativeChart::SeriesAnimations;
168 190 else
169 191 return DeclarativeChart::NoAnimation;
170 192 }
171 193
172 194 void DeclarativeChart::setTitle(QString title)
173 195 {
174 196 if (title != m_chart->title()) {
175 197 m_chart->setTitle(title);
176 198 emit titleChanged();
177 199 }
178 200 }
179 201 QString DeclarativeChart::title()
180 202 {
181 203 return m_chart->title();
182 204 }
183 205
184 206 QAxis *DeclarativeChart::axisX()
185 207 {
186 208 return m_chart->axisX();
187 209 }
188 210
189 211 QAxis *DeclarativeChart::axisY(QAbstractSeries *series)
190 212 {
191 213 return m_chart->axisY(series);
192 214 }
193 215
194 216 QLegend *DeclarativeChart::legend()
195 217 {
196 218 return m_chart->legend();
197 219 }
198 220
199 221 QVariantList DeclarativeChart::axisXLabels()
200 222 {
201 223 QVariantList labels;
202 224 foreach (qreal value, m_chart->axisX()->categories()->values()) {
203 225 labels.append(value);
204 226 labels.append(m_chart->axisX()->categories()->label(value));
205 227 }
206 228 return labels;
207 229 }
208 230
209 231 void DeclarativeChart::setAxisXLabels(QVariantList list)
210 232 {
211 233 QVariant value(QVariant::Invalid);
212 234 foreach (QVariant element, list) {
213 235 if (value.isValid() && element.type() == QVariant::String) {
214 236 m_chart->axisX()->categories()->insert(value.toDouble(), element.toString());
215 237 value = QVariant(QVariant::Invalid);
216 238 } else {
217 239 if (element.canConvert(QVariant::Double))
218 240 value = element;
219 241 }
220 242 }
221 243 emit axisLabelsChanged();
222 244 }
223 245
224 246 void DeclarativeChart::setTitleColor(QColor color)
225 247 {
226 248 QBrush b = m_chart->titleBrush();
227 249 if (color != b.color()) {
228 250 b.setColor(color);
229 251 m_chart->setTitleBrush(b);
230 252 emit titleColorChanged();
231 253 }
232 254 }
233 255
234 256 QColor DeclarativeChart::titleColor()
235 257 {
236 258 return m_chart->titleBrush().color();
237 259 }
238 260
239 261 void DeclarativeChart::setBackgroundColor(QColor color)
240 262 {
241 263 QBrush b = m_chart->backgroundBrush();
242 if (color != b.color()) {
264 if (b.style() != Qt::SolidPattern || color != b.color()) {
265 b.setStyle(Qt::SolidPattern);
243 266 b.setColor(color);
244 267 m_chart->setBackgroundBrush(b);
245 268 emit backgroundColorChanged();
246 269 }
247 270 }
248 271
249 272 QColor DeclarativeChart::backgroundColor()
250 273 {
251 274 return m_chart->backgroundBrush().color();
252 275 }
253 276
254 277 int DeclarativeChart::count()
255 278 {
256 279 return m_chart->series().count();
257 280 }
258 281
259 282 void DeclarativeChart::setDropShadowEnabled(bool enabled)
260 283 {
261 284 if (enabled != m_chart->isDropShadowEnabled()) {
262 285 m_chart->setDropShadowEnabled(enabled);
263 286 dropShadowEnabledChanged(enabled);
264 287 }
265 288 }
266 289
267 290 bool DeclarativeChart::dropShadowEnabled()
268 291 {
269 292 return m_chart->isDropShadowEnabled();
270 293 }
271 294
272 295 void DeclarativeChart::zoom(qreal factor)
273 296 {
274 297 m_chart->zoom(factor);
275 298 }
276 299
277 300 void DeclarativeChart::scrollLeft(qreal pixels)
278 301 {
279 302 m_chart->scroll(QPointF(pixels, 0));
280 303 }
281 304
282 305 void DeclarativeChart::scrollRight(qreal pixels)
283 306 {
284 307 m_chart->scroll(QPointF(-pixels, 0));
285 308 }
286 309
287 310 void DeclarativeChart::scrollUp(qreal pixels)
288 311 {
289 312 m_chart->scroll(QPointF(0, pixels));
290 313 }
291 314
292 315 void DeclarativeChart::scrollDown(qreal pixels)
293 316 {
294 317 m_chart->scroll(QPointF(0, -pixels));
295 318 }
296 319
297 320 QAbstractSeries *DeclarativeChart::series(int index)
298 321 {
299 322 if (index < m_chart->series().count()) {
300 323 return m_chart->series().at(index);
301 324 }
302 325 return 0;
303 326 }
304 327
305 328 QAbstractSeries *DeclarativeChart::series(QString seriesName)
306 329 {
307 330 foreach(QAbstractSeries *series, m_chart->series()) {
308 331 if (series->name() == seriesName)
309 332 return series;
310 333 }
311 334 return 0;
312 335 }
313 336
314 337 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
315 338 {
316 339 QAbstractSeries *series = 0;
317 340 switch (type) {
318 341 case DeclarativeChart::SeriesTypeLine:
319 342 series = new DeclarativeLineSeries();
320 343 break;
321 344 case DeclarativeChart::SeriesTypeArea:
322 345 series = new DeclarativeAreaSeries();
323 346 break;
324 347 case DeclarativeChart::SeriesTypeBar:
325 348 series = new DeclarativeBarSeries();
326 349 break;
327 350 case DeclarativeChart::SeriesTypeStackedBar:
328 351 // TODO
329 352 break;
330 353 case DeclarativeChart::SeriesTypePercentBar:
331 354 // TODO
332 355 break;
333 356 case DeclarativeChart::SeriesTypeGroupedBar:
334 357 series = new DeclarativeGroupedBarSeries();
335 358 break;
336 359 case DeclarativeChart::SeriesTypePie:
337 360 series = new DeclarativePieSeries();
338 361 break;
339 362 case DeclarativeChart::SeriesTypeScatter:
340 363 series = new DeclarativeScatterSeries();
341 364 break;
342 365 case DeclarativeChart::SeriesTypeSpline:
343 366 series = new DeclarativeSplineSeries();
344 367 break;
345 368 default:
346 369 qWarning() << "Illegal series type";
347 370 }
348 371 series->setName(name);
349 372 m_chart->addSeries(series);
350 373 return series;
351 374 }
352 375
353 376 #include "moc_declarativechart.cpp"
354 377
355 378 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now