@@ -1,688 +1,688 | |||
|
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 <QDeclarativeEngine> |
|
24 | 24 | #include "declarativelineseries.h" |
|
25 | 25 | #include "declarativeareaseries.h" |
|
26 | 26 | #include "declarativebarseries.h" |
|
27 | 27 | #include "declarativepieseries.h" |
|
28 | 28 | #include "declarativesplineseries.h" |
|
29 | 29 | #include "declarativescatterseries.h" |
|
30 | 30 | #include "qbarcategoryaxis.h" |
|
31 | 31 | #include "qvalueaxis.h" |
|
32 | 32 | #include "qcategoryaxis.h" |
|
33 | 33 | #include "qabstractseries_p.h" |
|
34 | 34 | #include "declarativemargins.h" |
|
35 | 35 | |
|
36 | 36 | #ifndef QT_ON_ARM |
|
37 | 37 | #include "qdatetimeaxis.h" |
|
38 | 38 | #endif |
|
39 | 39 | |
|
40 | 40 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
41 | 41 | |
|
42 | 42 | /*! |
|
43 | 43 | \qmlclass ChartView DeclarativeChart |
|
44 | 44 | |
|
45 | 45 | ChartView element is the parent that is responsible for showing different chart series types. |
|
46 | 46 | |
|
47 | 47 | The following QML shows how to create a simple chart with one pie series: |
|
48 | 48 | \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1 |
|
49 | 49 | \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2 |
|
50 | 50 | \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3 |
|
51 | 51 | |
|
52 | 52 | \beginfloatleft |
|
53 | 53 | \image examples_qmlpiechart.png |
|
54 | 54 | \endfloat |
|
55 | 55 | \clearfloat |
|
56 | 56 | */ |
|
57 | 57 | |
|
58 | 58 | /*! |
|
59 | 59 | \qmlproperty Theme ChartView::theme |
|
60 | 60 | Theme defines the visual appearance of the chart, including for example colors, fonts, line |
|
61 | 61 | widths and chart background. |
|
62 | 62 | */ |
|
63 | 63 | |
|
64 | 64 | /*! |
|
65 | 65 | \qmlproperty Animation ChartView::animation |
|
66 | 66 | Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations, |
|
67 | 67 | ChartView.SeriesAnimations or ChartView.AllAnimations. |
|
68 | 68 | */ |
|
69 | 69 | |
|
70 | 70 | /*! |
|
71 | 71 | \qmlproperty Font ChartView::titleFont |
|
72 | 72 | The title font of the chart |
|
73 | 73 | |
|
74 | 74 | See the \l {Font} {QML Font Element} for detailed documentation. |
|
75 | 75 | */ |
|
76 | 76 | |
|
77 | 77 | /*! |
|
78 | 78 | \qmlproperty string ChartView::title |
|
79 | 79 | The title of the chart, shown on top of the chart. |
|
80 | 80 | \sa ChartView::titleColor |
|
81 | 81 | */ |
|
82 | 82 | |
|
83 | 83 | /*! |
|
84 | 84 | \qmlproperty string ChartView::titleColor |
|
85 | 85 | The color of the title text. |
|
86 | 86 | */ |
|
87 | 87 | |
|
88 | 88 | /*! |
|
89 | 89 | \qmlproperty Axis ChartView::axisX |
|
90 | 90 | The x-axis of the chart. |
|
91 | 91 | */ |
|
92 | 92 | |
|
93 | 93 | /*! |
|
94 | 94 | \qmlproperty Axis ChartView::axisY |
|
95 | 95 | The default y-axis of the chart. |
|
96 | 96 | */ |
|
97 | 97 | |
|
98 | 98 | /*! |
|
99 | 99 | \qmlproperty Legend ChartView::legend |
|
100 | 100 | The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart. |
|
101 | 101 | */ |
|
102 | 102 | |
|
103 | 103 | /*! |
|
104 | 104 | \qmlproperty int ChartView::count |
|
105 | 105 | The count of series added to the chart. |
|
106 | 106 | */ |
|
107 | 107 | |
|
108 | 108 | /*! |
|
109 | 109 | \qmlproperty color ChartView::backgroundColor |
|
110 | 110 | The color of the chart's background. By default background color is defined by chart theme. |
|
111 | 111 | \sa ChartView::theme |
|
112 | 112 | */ |
|
113 | 113 | |
|
114 | 114 | /*! |
|
115 | 115 | \qmlproperty bool ChartView::dropShadowEnabled |
|
116 | 116 | The chart's border drop shadow. Set to true to enable drop shadow. |
|
117 | 117 | */ |
|
118 | 118 | |
|
119 | 119 | /*! |
|
120 | 120 | \qmlproperty real ChartView::topMargin |
|
121 | 121 | Deprecated. Use minimumMargins and plotArea instead. |
|
122 | 122 | */ |
|
123 | 123 | |
|
124 | 124 | /*! |
|
125 | 125 | \qmlproperty real ChartView::bottomMargin |
|
126 | 126 | Deprecated. Use minimumMargins and plotArea instead. |
|
127 | 127 | */ |
|
128 | 128 | |
|
129 | 129 | /*! |
|
130 | 130 | \qmlproperty real ChartView::leftMargin |
|
131 | 131 | Deprecated. Use minimumMargins and plotArea instead. |
|
132 | 132 | */ |
|
133 | 133 | |
|
134 | 134 | /*! |
|
135 | 135 | \qmlproperty real ChartView::rightMargin |
|
136 | 136 | Deprecated. Use minimumMargins and plotArea instead. |
|
137 | 137 | */ |
|
138 | 138 | |
|
139 | 139 | /*! |
|
140 | 140 | \qmlproperty Margins ChartView::minimumMargins |
|
141 | 141 | The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins |
|
142 | 142 | area of ChartView is used for drawing title, axes and legend. Please note that setting the |
|
143 | 143 | properties of minimumMargins may be bigger than the defined value, depending on other ChartView |
|
144 | 144 | properties that affect it's layout. If you need to know the actual plotting area used at any |
|
145 | 145 | given time, you can check ChartView::plotArea instead. |
|
146 | 146 | */ |
|
147 | 147 | |
|
148 | 148 | /*! |
|
149 | 149 | \qmlproperty rect ChartView::plotArea |
|
150 | 150 | The area on the ChartView that is used for drawing series. This is the ChartView rect without the |
|
151 | 151 | margins. |
|
152 | 152 | \sa ChartView::minimumMargins |
|
153 | 153 | */ |
|
154 | 154 | |
|
155 | 155 | /*! |
|
156 | 156 | \qmlmethod AbstractSeries ChartView::series(int index) |
|
157 | 157 | Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with |
|
158 | 158 | the count property of the chart. |
|
159 | 159 | */ |
|
160 | 160 | |
|
161 | 161 | /*! |
|
162 | 162 | \qmlmethod AbstractSeries ChartView::series(string name) |
|
163 | 163 | Returns the first series on the chart with \a name. If there is no series with that name, returns null. |
|
164 | 164 | */ |
|
165 | 165 | |
|
166 | 166 | /*! |
|
167 | 167 | \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name) |
|
168 | 168 | Creates a series object of \a type to the chart. For example: |
|
169 | 169 | \code |
|
170 | 170 | var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series"); |
|
171 | 171 | scatter.markerSize = 22; |
|
172 | 172 | scatter.append(1.1, 2.0); |
|
173 | 173 | \endcode |
|
174 | 174 | */ |
|
175 | 175 | |
|
176 | 176 | /*! |
|
177 | 177 | \qmlmethod ChartView::removeSeries(AbstractSeries series) |
|
178 | 178 | Removes the \a series from the chart. The series object is also destroyed. |
|
179 | 179 | */ |
|
180 | 180 | |
|
181 | 181 | /*! |
|
182 | 182 | \qmlmethod ChartView::removeAllSeries() |
|
183 | 183 | Removes all series from the chart. All the series objects are also destroyed. |
|
184 | 184 | */ |
|
185 | 185 | |
|
186 | 186 | /*! |
|
187 | 187 | \qmlmethod Axis ChartView::axisY(QAbstractSeries *series) |
|
188 | 188 | The y-axis of the series. This is the same as the default y-axis of the chart as multiple y-axes are not yet supported. |
|
189 | 189 | */ |
|
190 | 190 | |
|
191 | 191 | /*! |
|
192 | 192 | \qmlmethod ChartView::zoomY(real factor) |
|
193 | 193 | Zooms in by \a factor on the center of the chart. |
|
194 | 194 | */ |
|
195 | 195 | |
|
196 | 196 | /*! |
|
197 | 197 | \qmlmethod ChartView::scrollLeft(real pixels) |
|
198 | 198 | Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation. |
|
199 | 199 | \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max |
|
200 | 200 | */ |
|
201 | 201 | |
|
202 | 202 | /*! |
|
203 | 203 | \qmlmethod ChartView::scrollRight(real pixels) |
|
204 | 204 | Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation. |
|
205 | 205 | \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max |
|
206 | 206 | */ |
|
207 | 207 | |
|
208 | 208 | /*! |
|
209 | 209 | \qmlmethod ChartView::scrollUp(real pixels) |
|
210 | 210 | Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation. |
|
211 | 211 | \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max |
|
212 | 212 | */ |
|
213 | 213 | |
|
214 | 214 | /*! |
|
215 | 215 | \qmlmethod ChartView::scrollDown(real pixels) |
|
216 | 216 | Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation. |
|
217 | 217 | \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max |
|
218 | 218 | */ |
|
219 | 219 | |
|
220 | 220 | /*! |
|
221 | 221 | \qmlsignal ChartView::onPlotAreaChanged(rect plotArea) |
|
222 | 222 | The plot area of the chart has changed. This may happen for example, if you modify minimumMargins |
|
223 | 223 | or if you resize the chart, or if you modify font size related properties of the legend or chart |
|
224 | 224 | title. |
|
225 | 225 | */ |
|
226 | 226 | |
|
227 | 227 | DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) |
|
228 | 228 | : QDeclarativeItem(parent), |
|
229 | 229 | m_chart(new QChart(this)) |
|
230 | 230 | { |
|
231 | 231 | setFlag(QGraphicsItem::ItemHasNoContents, false); |
|
232 | 232 | m_minMargins = new DeclarativeMargins(this); |
|
233 | 233 | connect(m_minMargins, SIGNAL(topChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); |
|
234 | 234 | connect(m_minMargins, SIGNAL(bottomChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); |
|
235 | 235 | connect(m_minMargins, SIGNAL(leftChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); |
|
236 | 236 | connect(m_minMargins, SIGNAL(rightChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); |
|
237 | 237 | } |
|
238 | 238 | |
|
239 | 239 | void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right) |
|
240 | 240 | { |
|
241 | 241 | m_chart->setMinimumMargins(QMargins(left, top, right, bottom)); |
|
242 | 242 | emit minimumMarginsChanged(); |
|
243 | 243 | emit plotAreaChanged(m_chart->plotArea()); |
|
244 | 244 | } |
|
245 | 245 | |
|
246 | 246 | DeclarativeChart::~DeclarativeChart() |
|
247 | 247 | { |
|
248 | 248 | delete m_chart; |
|
249 | 249 | } |
|
250 | 250 | |
|
251 | 251 | void DeclarativeChart::childEvent(QChildEvent *event) |
|
252 | 252 | { |
|
253 | 253 | if (event->type() == QEvent::ChildAdded) { |
|
254 | 254 | if (qobject_cast<QAbstractSeries *>(event->child())) { |
|
255 | 255 | m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child())); |
|
256 | 256 | } |
|
257 | 257 | } |
|
258 | 258 | } |
|
259 | 259 | |
|
260 | 260 | void DeclarativeChart::componentComplete() |
|
261 | 261 | { |
|
262 | 262 | foreach(QObject *child, children()) { |
|
263 | 263 | if (qobject_cast<QAbstractSeries *>(child)) { |
|
264 | 264 | // Add series to the chart |
|
265 | 265 | QAbstractSeries *series = qobject_cast<QAbstractSeries *>(child); |
|
266 | 266 | m_chart->addSeries(series); |
|
267 | 267 | |
|
268 | 268 | // Set optional user defined axes for the series and connect axis related signals |
|
269 | 269 | if (qobject_cast<DeclarativeLineSeries *>(child)) { |
|
270 | 270 | DeclarativeLineSeries *s = qobject_cast<DeclarativeLineSeries *>(child); |
|
271 | 271 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
272 | 272 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
273 | 273 | setAxisX(s->axisX(), s); |
|
274 | 274 | setAxisY(s->axisY(), s); |
|
275 | 275 | } else if (qobject_cast<DeclarativeSplineSeries *>(child)) { |
|
276 | 276 | DeclarativeSplineSeries *s = qobject_cast<DeclarativeSplineSeries *>(child); |
|
277 | 277 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
278 | 278 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
279 | 279 | setAxisX(s->axisX(), s); |
|
280 | 280 | setAxisY(s->axisY(), s); |
|
281 | 281 | } else if (qobject_cast<DeclarativeScatterSeries *>(child)) { |
|
282 | 282 | DeclarativeScatterSeries *s = qobject_cast<DeclarativeScatterSeries *>(child); |
|
283 | 283 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
284 | 284 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
285 | 285 | setAxisX(s->axisX(), s); |
|
286 | 286 | setAxisY(s->axisY(), s); |
|
287 | 287 | } else if (qobject_cast<DeclarativeAreaSeries *>(child)) { |
|
288 | 288 | DeclarativeAreaSeries *s = qobject_cast<DeclarativeAreaSeries *>(child); |
|
289 | 289 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
290 | 290 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
291 | 291 | setAxisX(s->axisX(), s); |
|
292 | 292 | setAxisY(s->axisY(), s); |
|
293 | 293 | } else if (qobject_cast<DeclarativeBarSeries *>(child)) { |
|
294 | 294 | DeclarativeBarSeries *s = qobject_cast<DeclarativeBarSeries *>(child); |
|
295 | 295 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
296 | 296 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
297 | 297 | setAxisX(s->axisX(), s); |
|
298 | 298 | setAxisY(s->axisY(), s); |
|
299 | 299 | } else if (qobject_cast<DeclarativeStackedBarSeries *>(child)) { |
|
300 | 300 | DeclarativeStackedBarSeries *s = qobject_cast<DeclarativeStackedBarSeries *>(child); |
|
301 | 301 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
302 | 302 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
303 | 303 | setAxisX(s->axisX(), s); |
|
304 | 304 | setAxisY(s->axisY(), s); |
|
305 | 305 | } else if (qobject_cast<DeclarativePercentBarSeries *>(child)) { |
|
306 | 306 | DeclarativePercentBarSeries *s = qobject_cast<DeclarativePercentBarSeries *>(child); |
|
307 | 307 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
308 | 308 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
309 | 309 | setAxisX(s->axisX(), s); |
|
310 | 310 | setAxisY(s->axisY(), s); |
|
311 | 311 | } else if (qobject_cast<DeclarativeHorizontalBarSeries *>(child)) { |
|
312 | 312 | DeclarativeHorizontalBarSeries *s = qobject_cast<DeclarativeHorizontalBarSeries *>(child); |
|
313 | 313 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
314 | 314 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
315 | 315 | setAxisX(s->axisX(), s); |
|
316 | 316 | setAxisY(s->axisY(), s); |
|
317 | 317 | } else if (qobject_cast<DeclarativeHorizontalStackedBarSeries *>(child)) { |
|
318 | 318 | DeclarativeHorizontalStackedBarSeries *s = qobject_cast<DeclarativeHorizontalStackedBarSeries *>(child); |
|
319 | 319 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
320 | 320 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
321 | 321 | setAxisX(s->axisX(), s); |
|
322 | 322 | setAxisY(s->axisY(), s); |
|
323 | 323 | } else if (qobject_cast<DeclarativeHorizontalPercentBarSeries *>(child)) { |
|
324 | 324 | DeclarativeHorizontalPercentBarSeries *s = qobject_cast<DeclarativeHorizontalPercentBarSeries *>(child); |
|
325 | 325 | connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*))); |
|
326 | 326 | connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*))); |
|
327 | 327 | setAxisX(s->axisX(), s); |
|
328 | 328 | setAxisY(s->axisY(), s); |
|
329 | 329 | } |
|
330 | 330 | } |
|
331 | 331 | } |
|
332 | 332 | |
|
333 | 333 | // Create the missing axes for the series that cannot be painted without axes |
|
334 | 334 | foreach (QAbstractSeries *chartSeries, m_chart->series()) |
|
335 | 335 | createDefaultAxes(chartSeries); |
|
336 | 336 | |
|
337 | 337 | QDeclarativeItem::componentComplete(); |
|
338 | 338 | } |
|
339 | 339 | |
|
340 | 340 | void DeclarativeChart::handleAxisXSet(QAbstractAxis* axis) |
|
341 | 341 | { |
|
342 | 342 | // qDebug() << "DeclarativeChart::handleAxisXSet" << sender() << axis; |
|
343 | 343 | if (axis && qobject_cast<QAbstractSeries *>(sender())) |
|
344 | 344 | m_chart->setAxisX(axis, qobject_cast<QAbstractSeries *>(sender())); |
|
345 | 345 | else |
|
346 | 346 | qWarning() << "Trying to set axisX to null."; |
|
347 | 347 | } |
|
348 | 348 | |
|
349 | 349 | void DeclarativeChart::handleAxisYSet(QAbstractAxis* axis) |
|
350 | 350 | { |
|
351 | 351 | // qDebug() << "DeclarativeChart::handleAxisYSet" << sender() << axis; |
|
352 | 352 | if (axis && qobject_cast<QAbstractSeries *>(sender())) |
|
353 | 353 | m_chart->setAxisY(axis, qobject_cast<QAbstractSeries *>(sender())); |
|
354 | 354 | else |
|
355 | 355 | qWarning() << "Trying to set axisY to null."; |
|
356 | 356 | } |
|
357 | 357 | |
|
358 | 358 | void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) |
|
359 | 359 | { |
|
360 | 360 | // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height(); |
|
361 | 361 | if (newGeometry.isValid()) { |
|
362 | 362 | if (newGeometry.width() > 0 && newGeometry.height() > 0) { |
|
363 | 363 | m_chart->resize(newGeometry.width(), newGeometry.height()); |
|
364 | 364 | } |
|
365 | 365 | } |
|
366 | 366 | QDeclarativeItem::geometryChanged(newGeometry, oldGeometry); |
|
367 | 367 | |
|
368 | 368 | // It would be better to trigger the plotAreaChanged signal from QChart::plotAreaChanged or |
|
369 | 369 | // similar. Since that kind of a signal is not clearly needed in the C++ API the work-around is |
|
370 | 370 | // to implement it here for the QML API purposes. |
|
371 | 371 | emit plotAreaChanged(m_chart->plotArea()); |
|
372 | 372 | } |
|
373 | 373 | |
|
374 | 374 | void DeclarativeChart::setTheme(DeclarativeChart::Theme theme) |
|
375 | 375 | { |
|
376 | 376 | QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme; |
|
377 | 377 | if (chartTheme != m_chart->theme()) |
|
378 | 378 | m_chart->setTheme(chartTheme); |
|
379 | 379 | } |
|
380 | 380 | |
|
381 | 381 | DeclarativeChart::Theme DeclarativeChart::theme() |
|
382 | 382 | { |
|
383 | 383 | return (DeclarativeChart::Theme) m_chart->theme(); |
|
384 | 384 | } |
|
385 | 385 | |
|
386 | 386 | void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations) |
|
387 | 387 | { |
|
388 | 388 | QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations; |
|
389 | 389 | if (animationOptions != m_chart->animationOptions()) |
|
390 | 390 | m_chart->setAnimationOptions(animationOptions); |
|
391 | 391 | } |
|
392 | 392 | |
|
393 | 393 | DeclarativeChart::Animation DeclarativeChart::animationOptions() |
|
394 | 394 | { |
|
395 | 395 | if (m_chart->animationOptions().testFlag(QChart::AllAnimations)) |
|
396 | 396 | return DeclarativeChart::AllAnimations; |
|
397 | 397 | else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations)) |
|
398 | 398 | return DeclarativeChart::GridAxisAnimations; |
|
399 | 399 | else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations)) |
|
400 | 400 | return DeclarativeChart::SeriesAnimations; |
|
401 | 401 | else |
|
402 | 402 | return DeclarativeChart::NoAnimation; |
|
403 | 403 | } |
|
404 | 404 | |
|
405 | 405 | void DeclarativeChart::setTitle(QString title) |
|
406 | 406 | { |
|
407 | 407 | if (title != m_chart->title()) |
|
408 | 408 | m_chart->setTitle(title); |
|
409 | 409 | } |
|
410 | 410 | QString DeclarativeChart::title() |
|
411 | 411 | { |
|
412 | 412 | return m_chart->title(); |
|
413 | 413 | } |
|
414 | 414 | |
|
415 | 415 | QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series) |
|
416 | 416 | { |
|
417 | 417 | return m_chart->axisX(series); |
|
418 | 418 | } |
|
419 | 419 | |
|
420 | 420 | QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series) |
|
421 | 421 | { |
|
422 | 422 | return m_chart->axisY(series); |
|
423 | 423 | } |
|
424 | 424 | |
|
425 | 425 | QLegend *DeclarativeChart::legend() |
|
426 | 426 | { |
|
427 | 427 | return m_chart->legend(); |
|
428 | 428 | } |
|
429 | 429 | |
|
430 | 430 | void DeclarativeChart::setTitleColor(QColor color) |
|
431 | 431 | { |
|
432 | 432 | QBrush b = m_chart->titleBrush(); |
|
433 | 433 | if (color != b.color()) { |
|
434 | 434 | b.setColor(color); |
|
435 | 435 | m_chart->setTitleBrush(b); |
|
436 | 436 | emit titleColorChanged(color); |
|
437 | 437 | } |
|
438 | 438 | } |
|
439 | 439 | |
|
440 | 440 | QFont DeclarativeChart::titleFont() const |
|
441 | 441 | { |
|
442 | 442 | return m_chart->titleFont(); |
|
443 | 443 | } |
|
444 | 444 | |
|
445 | 445 | void DeclarativeChart::setTitleFont(const QFont& font) |
|
446 | 446 | { |
|
447 | 447 | m_chart->setTitleFont(font); |
|
448 | 448 | } |
|
449 | 449 | |
|
450 | 450 | QColor DeclarativeChart::titleColor() |
|
451 | 451 | { |
|
452 | 452 | return m_chart->titleBrush().color(); |
|
453 | 453 | } |
|
454 | 454 | |
|
455 | 455 | void DeclarativeChart::setBackgroundColor(QColor color) |
|
456 | 456 | { |
|
457 | 457 | QBrush b = m_chart->backgroundBrush(); |
|
458 | 458 | if (b.style() != Qt::SolidPattern || color != b.color()) { |
|
459 | 459 | b.setStyle(Qt::SolidPattern); |
|
460 | 460 | b.setColor(color); |
|
461 | 461 | m_chart->setBackgroundBrush(b); |
|
462 | 462 | emit backgroundColorChanged(); |
|
463 | 463 | } |
|
464 | 464 | } |
|
465 | 465 | |
|
466 | 466 | QColor DeclarativeChart::backgroundColor() |
|
467 | 467 | { |
|
468 | 468 | return m_chart->backgroundBrush().color(); |
|
469 | 469 | } |
|
470 | 470 | |
|
471 | 471 | int DeclarativeChart::count() |
|
472 | 472 | { |
|
473 | 473 | return m_chart->series().count(); |
|
474 | 474 | } |
|
475 | 475 | |
|
476 | 476 | void DeclarativeChart::setDropShadowEnabled(bool enabled) |
|
477 | 477 | { |
|
478 | 478 | if (enabled != m_chart->isDropShadowEnabled()) { |
|
479 | 479 | m_chart->setDropShadowEnabled(enabled); |
|
480 | 480 | dropShadowEnabledChanged(enabled); |
|
481 | 481 | } |
|
482 | 482 | } |
|
483 | 483 | |
|
484 | 484 | bool DeclarativeChart::dropShadowEnabled() |
|
485 | 485 | { |
|
486 | 486 | return m_chart->isDropShadowEnabled(); |
|
487 | 487 | } |
|
488 | 488 | |
|
489 | 489 | qreal DeclarativeChart::topMargin() |
|
490 | 490 | { |
|
491 | 491 | qWarning() << "ChartView.topMargin is deprecated. Use minimumMargins and plotArea instead."; |
|
492 | 492 | return m_chart->plotArea().top(); |
|
493 | 493 | } |
|
494 | 494 | |
|
495 | 495 | qreal DeclarativeChart::bottomMargin() |
|
496 | 496 | { |
|
497 | 497 | qWarning() << "ChartView.bottomMargin is deprecated. Use minimumMargins and plotArea instead."; |
|
498 | 498 | return m_chart->plotArea().bottom(); |
|
499 | 499 | } |
|
500 | 500 | |
|
501 | 501 | qreal DeclarativeChart::leftMargin() |
|
502 | 502 | { |
|
503 | 503 | qWarning() << "ChartView.leftMargin is deprecated. Use minimumMargins and plotArea instead."; |
|
504 | 504 | return m_chart->plotArea().left(); |
|
505 | 505 | } |
|
506 | 506 | |
|
507 | 507 | qreal DeclarativeChart::rightMargin() |
|
508 | 508 | { |
|
509 | 509 | qWarning() << "ChartView.rightMargin is deprecated. Use minimumMargins and plotArea instead."; |
|
510 | 510 | return m_chart->plotArea().right(); |
|
511 | 511 | } |
|
512 | 512 | |
|
513 | 513 | void DeclarativeChart::zoom(qreal factor) |
|
514 | 514 | { |
|
515 | 515 | m_chart->zoom(factor); |
|
516 | 516 | } |
|
517 | 517 | |
|
518 | 518 | void DeclarativeChart::scrollLeft(qreal pixels) |
|
519 | 519 | { |
|
520 | m_chart->scroll(pixels, 0); | |
|
520 | m_chart->scroll(-pixels, 0); | |
|
521 | 521 | } |
|
522 | 522 | |
|
523 | 523 | void DeclarativeChart::scrollRight(qreal pixels) |
|
524 | 524 | { |
|
525 |
m_chart->scroll( |
|
|
525 | m_chart->scroll(pixels, 0); | |
|
526 | 526 | } |
|
527 | 527 | |
|
528 | 528 | void DeclarativeChart::scrollUp(qreal pixels) |
|
529 | 529 | { |
|
530 | 530 | m_chart->scroll(0, pixels); |
|
531 | 531 | } |
|
532 | 532 | |
|
533 | 533 | void DeclarativeChart::scrollDown(qreal pixels) |
|
534 | 534 | { |
|
535 | 535 | m_chart->scroll(0, -pixels); |
|
536 | 536 | } |
|
537 | 537 | |
|
538 | 538 | QAbstractSeries *DeclarativeChart::series(int index) |
|
539 | 539 | { |
|
540 | 540 | if (index < m_chart->series().count()) { |
|
541 | 541 | return m_chart->series().at(index); |
|
542 | 542 | } |
|
543 | 543 | return 0; |
|
544 | 544 | } |
|
545 | 545 | |
|
546 | 546 | QAbstractSeries *DeclarativeChart::series(QString seriesName) |
|
547 | 547 | { |
|
548 | 548 | foreach(QAbstractSeries *series, m_chart->series()) { |
|
549 | 549 | if (series->name() == seriesName) |
|
550 | 550 | return series; |
|
551 | 551 | } |
|
552 | 552 | return 0; |
|
553 | 553 | } |
|
554 | 554 | |
|
555 | 555 | QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name) |
|
556 | 556 | { |
|
557 | 557 | QAbstractSeries *series = 0; |
|
558 | 558 | |
|
559 | 559 | switch (type) { |
|
560 | 560 | case DeclarativeChart::SeriesTypeLine: |
|
561 | 561 | series = new DeclarativeLineSeries(); |
|
562 | 562 | break; |
|
563 | 563 | case DeclarativeChart::SeriesTypeArea: |
|
564 | 564 | series = new DeclarativeAreaSeries(); |
|
565 | 565 | break; |
|
566 | 566 | case DeclarativeChart::SeriesTypeStackedBar: |
|
567 | 567 | series = new DeclarativeStackedBarSeries(); |
|
568 | 568 | break; |
|
569 | 569 | case DeclarativeChart::SeriesTypePercentBar: |
|
570 | 570 | series = new DeclarativePercentBarSeries(); |
|
571 | 571 | break; |
|
572 | 572 | case DeclarativeChart::SeriesTypeBar: |
|
573 | 573 | series = new DeclarativeBarSeries(); |
|
574 | 574 | break; |
|
575 | 575 | case DeclarativeChart::SeriesTypeHorizontalBar: |
|
576 | 576 | series = new DeclarativeHorizontalBarSeries(); |
|
577 | 577 | break; |
|
578 | 578 | case DeclarativeChart::SeriesTypeHorizontalPercentBar: |
|
579 | 579 | series = new DeclarativeHorizontalPercentBarSeries(); |
|
580 | 580 | break; |
|
581 | 581 | case DeclarativeChart::SeriesTypeHorizontalStackedBar: |
|
582 | 582 | series = new DeclarativeHorizontalStackedBarSeries(); |
|
583 | 583 | break; |
|
584 | 584 | case DeclarativeChart::SeriesTypePie: |
|
585 | 585 | series = new DeclarativePieSeries(); |
|
586 | 586 | break; |
|
587 | 587 | case DeclarativeChart::SeriesTypeScatter: |
|
588 | 588 | series = new DeclarativeScatterSeries(); |
|
589 | 589 | break; |
|
590 | 590 | case DeclarativeChart::SeriesTypeSpline: |
|
591 | 591 | series = new DeclarativeSplineSeries(); |
|
592 | 592 | break; |
|
593 | 593 | default: |
|
594 | 594 | qWarning() << "Illegal series type"; |
|
595 | 595 | } |
|
596 | 596 | |
|
597 | 597 | if (series) { |
|
598 | 598 | series->setName(name); |
|
599 | 599 | m_chart->addSeries(series); |
|
600 | 600 | createDefaultAxes(series); |
|
601 | 601 | } |
|
602 | 602 | |
|
603 | 603 | return series; |
|
604 | 604 | } |
|
605 | 605 | |
|
606 | 606 | void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series) |
|
607 | 607 | { |
|
608 | 608 | if (axis) |
|
609 | 609 | m_chart->setAxisX(axis, series); |
|
610 | 610 | } |
|
611 | 611 | |
|
612 | 612 | void DeclarativeChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series) |
|
613 | 613 | { |
|
614 | 614 | if (axis) |
|
615 | 615 | m_chart->setAxisY(axis, series); |
|
616 | 616 | } |
|
617 | 617 | |
|
618 | 618 | void DeclarativeChart::createDefaultAxes() |
|
619 | 619 | { |
|
620 | 620 | qWarning() << "ChartView.createDefaultAxes() is deprecated. Axes are created automatically."; |
|
621 | 621 | } |
|
622 | 622 | |
|
623 | 623 | void DeclarativeChart::createDefaultAxes(QAbstractSeries* series) |
|
624 | 624 | { |
|
625 | 625 | foreach (QAbstractSeries *s, m_chart->series()) { |
|
626 | 626 | // If there is already an x axis of the correct type, re-use it |
|
627 | 627 | if (!m_chart->axisX(series) && s != series && m_chart->axisX(s) |
|
628 | 628 | && m_chart->axisX(s)->type() == series->d_ptr->defaultAxisType(Qt::Horizontal)) |
|
629 | 629 | m_chart->setAxisX(m_chart->axisX(s), series); |
|
630 | 630 | |
|
631 | 631 | // If there is already a y axis of the correct type, re-use it |
|
632 | 632 | if (!m_chart->axisY(series) && s != series && m_chart->axisY(s) |
|
633 | 633 | && m_chart->axisY(s)->type() == series->d_ptr->defaultAxisType(Qt::Vertical)) |
|
634 | 634 | m_chart->setAxisY(m_chart->axisY(s), series); |
|
635 | 635 | } |
|
636 | 636 | |
|
637 | 637 | // If no x axis of correct type was found, create a new x axis based of default axis type |
|
638 | 638 | if (!m_chart->axisX(series)) { |
|
639 | 639 | switch (series->d_ptr->defaultAxisType(Qt::Horizontal)) { |
|
640 | 640 | case QAbstractAxis::AxisTypeValue: |
|
641 | 641 | m_chart->setAxisX(new QValueAxis(this), series); |
|
642 | 642 | break; |
|
643 | 643 | case QAbstractAxis::AxisTypeBarCategory: |
|
644 | 644 | m_chart->setAxisX(new QBarCategoryAxis(this), series); |
|
645 | 645 | break; |
|
646 | 646 | case QAbstractAxis::AxisTypeCategory: |
|
647 | 647 | m_chart->setAxisX(new QCategoryAxis(this), series); |
|
648 | 648 | break; |
|
649 | 649 | #ifndef QT_ON_ARM |
|
650 | 650 | case QAbstractAxis::AxisTypeDateTime: |
|
651 | 651 | m_chart->setAxisX(new QDateTimeAxis(this), series); |
|
652 | 652 | break; |
|
653 | 653 | #endif |
|
654 | 654 | default: |
|
655 | 655 | // Do nothing, assume AxisTypeNoAxis |
|
656 | 656 | break; |
|
657 | 657 | } |
|
658 | 658 | } |
|
659 | 659 | |
|
660 | 660 | // If no y axis of correct type was found, create a new y axis based of default axis type |
|
661 | 661 | if (!m_chart->axisY(series)) { |
|
662 | 662 | switch (series->d_ptr->defaultAxisType(Qt::Vertical)) { |
|
663 | 663 | case QAbstractAxis::AxisTypeValue: |
|
664 | 664 | m_chart->setAxisY(new QValueAxis(this), series); |
|
665 | 665 | break; |
|
666 | 666 | case QAbstractAxis::AxisTypeBarCategory: |
|
667 | 667 | m_chart->setAxisY(new QBarCategoryAxis(this), series); |
|
668 | 668 | break; |
|
669 | 669 | case QAbstractAxis::AxisTypeCategory: |
|
670 | 670 | m_chart->setAxisY(new QCategoryAxis(this), series); |
|
671 | 671 | break; |
|
672 | 672 | #ifndef QT_ON_ARM |
|
673 | 673 | case QAbstractAxis::AxisTypeDateTime: |
|
674 | 674 | m_chart->setAxisY(new QDateTimeAxis(this), series); |
|
675 | 675 | break; |
|
676 | 676 | #endif |
|
677 | 677 | default: |
|
678 | 678 | // Do nothing, assume AxisTypeNoAxis |
|
679 | 679 | break; |
|
680 | 680 | } |
|
681 | 681 | } |
|
682 | 682 | |
|
683 | 683 | //qDebug() << "axis for series" << series << "x:" << m_chart->axisX(series) << "y:" << m_chart->axisY(series); |
|
684 | 684 | } |
|
685 | 685 | |
|
686 | 686 | #include "moc_declarativechart.cpp" |
|
687 | 687 | |
|
688 | 688 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now