##// END OF EJS Templates
QML: Added possibility to define axes when creating series
Tero Ahola -
r1960:bd364d589414
parent child
Show More
@@ -1,103 +1,116
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 import QtQuick 1.0
22 22 import QtCommercial.Chart 1.1
23 23
24 24 //![1]
25 25 ChartView {
26 26 id: chartView
27 27 animationOptions: ChartView.NoAnimation
28 theme: ChartView.ChartThemeDark
28 29
29 30 ValueAxis {
30 31 id: axisY1
31 32 min: -1
32 33 max: 4
33 34 }
34 35
35 36 ValueAxis {
36 37 id: axisY2
37 38 min: -10
38 39 max: 5
39 40 }
40 41
41 42 ValueAxis {
42 43 id: axisX
43 44 min: 0
44 45 max: 1000
45 46 }
46 47
47 48 LineSeries {
48 49 id: lineSeries1
49 50 name: "signal 1"
50 51 axisX: axisX
51 52 axisY: axisY1
52 53 }
53 54 LineSeries {
54 55 id: lineSeries2
55 56 name: "signal 2"
56 57 axisX: axisX
57 58 axisY: axisY2
58 59 }
59 60 // ...
60 61 //![1]
61 62
62 63 //![2]
63 64 Timer {
64 65 id: refreshTimer
65 66 interval: 1 / 60 * 1000 // 60 Hz
66 67 running: true
67 68 repeat: true
68 69 onTriggered: {
69 70 dataSource.update(chartView.series(0));
70 71 dataSource.update(chartView.series(1));
71 72 }
72 73 }
73 74 //![2]
74 75
76 //![3]
75 77 function changeSeriesType(type) {
76 chartView.series(1).destroy();
77 chartView.series(0).destroy();
78 var seriesCount = 2;
79 for (var i = 0; i < seriesCount; i++) {
80 var series;
81 if (type == "line") {
82 series = scopeView.createSeries(ChartView.SeriesTypeLine, "signal " + (i + 1));
83 } else if (type == "spline") {
84 series = chartView.createSeries(ChartView.SeriesTypeSpline, "signal " + (i + 1));
85 } else {
86 series = chartView.createSeries(ChartView.SeriesTypeScatter, "signal " + (i + 1));
87 series.markerSize = 3;
88 series.borderColor = "transparent";
89 }
78 chartView.removeAllSeries();
79
80 // Create two new series of the correct type. Axis x is the same for both of the series,
81 // but the series have their own y-axes to make it possible to control the y-offset
82 // of the "signal sources".
83 if (type == "line") {
84 scopeView.createSeries(ChartView.SeriesTypeLine, "signal 1", createAxis(0, 1000), createAxis(-1, 4));
85 scopeView.createSeries(ChartView.SeriesTypeLine, "signal 2", chartView.axisX(), createAxis(-10, 5));
86 } else if (type == "spline") {
87 scopeView.createSeries(ChartView.SeriesTypeSpline, "signal 1", createAxis(0, 1000), createAxis(-1, 4));
88 scopeView.createSeries(ChartView.SeriesTypeSpline, "signal 2", chartView.axisX(), createAxis(-10, 5));
89 } else {
90 var series1 = scopeView.createSeries(ChartView.SeriesTypeScatter, "signal 1", createAxis(0, 1000), createAxis(-1, 4));
91 series1.markerSize = 3;
92 series1.borderColor = "transparent";
93 var series2 = scopeView.createSeries(ChartView.SeriesTypeScatter, "signal 2", chartView.axisX(), createAxis(-10, 5));
94 series2.markerSize = 3;
95 series2.borderColor = "transparent";
90 96 }
91 97 }
92 98
99 function createAxis(min, max) {
100 // The following creates a ValueAxis object that can be then set as a x or y axis for a series
101 return Qt.createQmlObject("import QtQuick 1.1; import QtCommercial.Chart 1.1; ValueAxis { min: "
102 + min + "; max: " + max + " }", chartView);
103 }
104 //![3]
105
93 106 function setAnimations(enabled) {
94 107 if (enabled)
95 108 scopeView.animationOptions = ChartView.SeriesAnimations;
96 109 else
97 110 scopeView.animationOptions = ChartView.NoAnimation;
98 111 }
99 112
100 113 function changeRefreshRate(rate) {
101 114 refreshTimer.interval = 1 / Number(rate) * 1000;
102 115 }
103 116 }
1 NO CONTENT: modified file, binary diff hidden
@@ -1,27 +1,31
1 1 /*!
2 2 \example demos/qmloscilloscope
3 3 \title Oscilloscope
4 4
5 5 \image demos_qmloscilloscope.png
6 6
7 7 Oscilloscope application demonstrates how to use QtCommercial Charts QML api to implement an
8 8 application with strict performance requirements. The application uses generated data with
9 9 configurable characteristics to mimic a simple oscilloscope user interface. To find out the
10 10 actual screen refresh performance of the application, you can set QML_SHOW_FRAMERATE = 1 to
11 11 your run environment settings to get the framerate shown in the application output console.
12 12 I.e. go to Projects - Run - Run environment in Qt Creator and select Add. Then you can
13 13 experiment with the different configurable options of the demo application to find the
14 14 configuration that gives you the best performance in your environment.
15 15
16 16 The application window is shared by control and scope views:
17 17 \snippet ../demos/qmloscilloscope/qml/qmloscilloscope/main.qml 1
18 18 \snippet ../demos/qmloscilloscope/qml/qmloscilloscope/main.qml 2
19 19
20 20 ControlView implements the buttons used for configuring. ScopeView uses a ChartView to show
21 21 a chart with two line series:
22 22 \snippet ../demos/qmloscilloscope/qml/qmloscilloscope/ScopeView.qml 1
23 23
24 24 The data of the line series is updated with a QML timer. In a real life application the
25 25 updating could triggered with a signal from Qt C++ code.
26 26 \snippet ../demos/qmloscilloscope/qml/qmloscilloscope/ScopeView.qml 2
27
28 The oscilloscope also allows you to switch the type of the series used for visualizing the
29 signal sources. This is implemented by dynamically destroying and creating series:
30 \snippet ../demos/qmloscilloscope/qml/qmloscilloscope/ScopeView.qml 3
27 31 */
@@ -1,688 +1,704
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 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name)
168 Creates a series object of \a type to the chart. For example:
167 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name, AbstractAxis axisX, AbstractAxis axisY)
168 Creates a series object of \a type to the chart with name \a name, optional axis \a axisX and
169 optional axis \a axisY. For example:
169 170 \code
170 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series");
171 scatter.markerSize = 22;
172 scatter.append(1.1, 2.0);
171 // lineSeries is a LineSeries object that has already been added to the ChartView; re-use it's axes
172 var myAxisX = chartView.axisX(lineSeries);
173 var myAxisY = chartView.axisY(lineSeries);
174 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series", myAxisX, myAxisY);
173 175 \endcode
174 176 */
175 177
176 178 /*!
177 179 \qmlmethod ChartView::removeSeries(AbstractSeries series)
178 180 Removes the \a series from the chart. The series object is also destroyed.
179 181 */
180 182
181 183 /*!
182 184 \qmlmethod ChartView::removeAllSeries()
183 185 Removes all series from the chart. All the series objects are also destroyed.
184 186 */
185 187
186 188 /*!
189 \qmlmethod Axis ChartView::axisX(QAbstractSeries *series)
190 The x-axis of the series.
191 */
192
193 /*!
187 194 \qmlmethod Axis ChartView::axisY(QAbstractSeries *series)
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.
195 The y-axis of the series.
189 196 */
190 197
191 198 /*!
192 199 \qmlmethod ChartView::zoomY(real factor)
193 200 Zooms in by \a factor on the center of the chart.
194 201 */
195 202
196 203 /*!
197 204 \qmlmethod ChartView::scrollLeft(real pixels)
198 205 Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation.
199 206 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
200 207 */
201 208
202 209 /*!
203 210 \qmlmethod ChartView::scrollRight(real pixels)
204 211 Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation.
205 212 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
206 213 */
207 214
208 215 /*!
209 216 \qmlmethod ChartView::scrollUp(real pixels)
210 217 Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation.
211 218 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
212 219 */
213 220
214 221 /*!
215 222 \qmlmethod ChartView::scrollDown(real pixels)
216 223 Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation.
217 224 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
218 225 */
219 226
220 227 /*!
221 228 \qmlsignal ChartView::onPlotAreaChanged(rect plotArea)
222 229 The plot area of the chart has changed. This may happen for example, if you modify minimumMargins
223 230 or if you resize the chart, or if you modify font size related properties of the legend or chart
224 231 title.
225 232 */
226 233
227 234 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
228 235 : QDeclarativeItem(parent),
229 236 m_chart(new QChart(this))
230 237 {
231 238 setFlag(QGraphicsItem::ItemHasNoContents, false);
232 239 m_minMargins = new DeclarativeMargins(this);
233 240 connect(m_minMargins, SIGNAL(topChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int)));
234 241 connect(m_minMargins, SIGNAL(bottomChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int)));
235 242 connect(m_minMargins, SIGNAL(leftChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int)));
236 243 connect(m_minMargins, SIGNAL(rightChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int)));
237 244 }
238 245
239 246 void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right)
240 247 {
241 248 m_chart->setMinimumMargins(QMargins(left, top, right, bottom));
242 249 emit minimumMarginsChanged();
243 250 emit plotAreaChanged(m_chart->plotArea());
244 251 }
245 252
246 253 DeclarativeChart::~DeclarativeChart()
247 254 {
248 255 delete m_chart;
249 256 }
250 257
251 258 void DeclarativeChart::childEvent(QChildEvent *event)
252 259 {
253 260 if (event->type() == QEvent::ChildAdded) {
254 261 if (qobject_cast<QAbstractSeries *>(event->child())) {
255 262 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
256 263 }
257 264 }
258 265 }
259 266
260 267 void DeclarativeChart::componentComplete()
261 268 {
262 269 foreach(QObject *child, children()) {
263 270 if (qobject_cast<QAbstractSeries *>(child)) {
264 271 // Add series to the chart
265 272 QAbstractSeries *series = qobject_cast<QAbstractSeries *>(child);
266 273 m_chart->addSeries(series);
267 274
268 275 // Set optional user defined axes for the series and connect axis related signals
269 276 if (qobject_cast<DeclarativeLineSeries *>(child)) {
270 277 DeclarativeLineSeries *s = qobject_cast<DeclarativeLineSeries *>(child);
271 278 connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
272 279 connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
273 280 setAxisX(s->axisX(), s);
274 281 setAxisY(s->axisY(), s);
275 282 } else if (qobject_cast<DeclarativeSplineSeries *>(child)) {
276 283 DeclarativeSplineSeries *s = qobject_cast<DeclarativeSplineSeries *>(child);
277 284 connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
278 285 connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
279 286 setAxisX(s->axisX(), s);
280 287 setAxisY(s->axisY(), s);
281 288 } else if (qobject_cast<DeclarativeScatterSeries *>(child)) {
282 289 DeclarativeScatterSeries *s = qobject_cast<DeclarativeScatterSeries *>(child);
283 290 connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
284 291 connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
285 292 setAxisX(s->axisX(), s);
286 293 setAxisY(s->axisY(), s);
287 294 } else if (qobject_cast<DeclarativeAreaSeries *>(child)) {
288 295 DeclarativeAreaSeries *s = qobject_cast<DeclarativeAreaSeries *>(child);
289 296 connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
290 297 connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
291 298 setAxisX(s->axisX(), s);
292 299 setAxisY(s->axisY(), s);
293 300 } else if (qobject_cast<DeclarativeBarSeries *>(child)) {
294 301 DeclarativeBarSeries *s = qobject_cast<DeclarativeBarSeries *>(child);
295 302 connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
296 303 connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
297 304 setAxisX(s->axisX(), s);
298 305 setAxisY(s->axisY(), s);
299 306 } else if (qobject_cast<DeclarativeStackedBarSeries *>(child)) {
300 307 DeclarativeStackedBarSeries *s = qobject_cast<DeclarativeStackedBarSeries *>(child);
301 308 connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
302 309 connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
303 310 setAxisX(s->axisX(), s);
304 311 setAxisY(s->axisY(), s);
305 312 } else if (qobject_cast<DeclarativePercentBarSeries *>(child)) {
306 313 DeclarativePercentBarSeries *s = qobject_cast<DeclarativePercentBarSeries *>(child);
307 314 connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
308 315 connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
309 316 setAxisX(s->axisX(), s);
310 317 setAxisY(s->axisY(), s);
311 318 } else if (qobject_cast<DeclarativeHorizontalBarSeries *>(child)) {
312 319 DeclarativeHorizontalBarSeries *s = qobject_cast<DeclarativeHorizontalBarSeries *>(child);
313 320 connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
314 321 connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
315 322 setAxisX(s->axisX(), s);
316 323 setAxisY(s->axisY(), s);
317 324 } else if (qobject_cast<DeclarativeHorizontalStackedBarSeries *>(child)) {
318 325 DeclarativeHorizontalStackedBarSeries *s = qobject_cast<DeclarativeHorizontalStackedBarSeries *>(child);
319 326 connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
320 327 connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
321 328 setAxisX(s->axisX(), s);
322 329 setAxisY(s->axisY(), s);
323 330 } else if (qobject_cast<DeclarativeHorizontalPercentBarSeries *>(child)) {
324 331 DeclarativeHorizontalPercentBarSeries *s = qobject_cast<DeclarativeHorizontalPercentBarSeries *>(child);
325 332 connect(s, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
326 333 connect(s, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
327 334 setAxisX(s->axisX(), s);
328 335 setAxisY(s->axisY(), s);
329 336 }
330 337 }
331 338 }
332 339
333 340 // Create the missing axes for the series that cannot be painted without axes
334 341 foreach (QAbstractSeries *chartSeries, m_chart->series())
335 342 createDefaultAxes(chartSeries);
336 343
337 344 QDeclarativeItem::componentComplete();
338 345 }
339 346
340 347 void DeclarativeChart::handleAxisXSet(QAbstractAxis* axis)
341 348 {
342 349 // qDebug() << "DeclarativeChart::handleAxisXSet" << sender() << axis;
343 350 if (axis && qobject_cast<QAbstractSeries *>(sender()))
344 351 m_chart->setAxisX(axis, qobject_cast<QAbstractSeries *>(sender()));
345 352 else
346 353 qWarning() << "Trying to set axisX to null.";
347 354 }
348 355
349 356 void DeclarativeChart::handleAxisYSet(QAbstractAxis* axis)
350 357 {
351 358 // qDebug() << "DeclarativeChart::handleAxisYSet" << sender() << axis;
352 359 if (axis && qobject_cast<QAbstractSeries *>(sender()))
353 360 m_chart->setAxisY(axis, qobject_cast<QAbstractSeries *>(sender()));
354 361 else
355 362 qWarning() << "Trying to set axisY to null.";
356 363 }
357 364
358 365 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
359 366 {
360 367 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
361 368 if (newGeometry.isValid()) {
362 369 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
363 370 m_chart->resize(newGeometry.width(), newGeometry.height());
364 371 }
365 372 }
366 373 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
367 374
368 375 // It would be better to trigger the plotAreaChanged signal from QChart::plotAreaChanged or
369 376 // similar. Since that kind of a signal is not clearly needed in the C++ API the work-around is
370 377 // to implement it here for the QML API purposes.
371 378 emit plotAreaChanged(m_chart->plotArea());
372 379 }
373 380
374 381 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
375 382 {
376 383 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
377 384 if (chartTheme != m_chart->theme())
378 385 m_chart->setTheme(chartTheme);
379 386 }
380 387
381 388 DeclarativeChart::Theme DeclarativeChart::theme()
382 389 {
383 390 return (DeclarativeChart::Theme) m_chart->theme();
384 391 }
385 392
386 393 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
387 394 {
388 395 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
389 396 if (animationOptions != m_chart->animationOptions())
390 397 m_chart->setAnimationOptions(animationOptions);
391 398 }
392 399
393 400 DeclarativeChart::Animation DeclarativeChart::animationOptions()
394 401 {
395 402 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
396 403 return DeclarativeChart::AllAnimations;
397 404 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
398 405 return DeclarativeChart::GridAxisAnimations;
399 406 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
400 407 return DeclarativeChart::SeriesAnimations;
401 408 else
402 409 return DeclarativeChart::NoAnimation;
403 410 }
404 411
405 412 void DeclarativeChart::setTitle(QString title)
406 413 {
407 414 if (title != m_chart->title())
408 415 m_chart->setTitle(title);
409 416 }
410 417 QString DeclarativeChart::title()
411 418 {
412 419 return m_chart->title();
413 420 }
414 421
415 422 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
416 423 {
417 424 return m_chart->axisX(series);
418 425 }
419 426
420 427 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
421 428 {
422 429 return m_chart->axisY(series);
423 430 }
424 431
425 432 QLegend *DeclarativeChart::legend()
426 433 {
427 434 return m_chart->legend();
428 435 }
429 436
430 437 void DeclarativeChart::setTitleColor(QColor color)
431 438 {
432 439 QBrush b = m_chart->titleBrush();
433 440 if (color != b.color()) {
434 441 b.setColor(color);
435 442 m_chart->setTitleBrush(b);
436 443 emit titleColorChanged(color);
437 444 }
438 445 }
439 446
440 447 QFont DeclarativeChart::titleFont() const
441 448 {
442 449 return m_chart->titleFont();
443 450 }
444 451
445 452 void DeclarativeChart::setTitleFont(const QFont& font)
446 453 {
447 454 m_chart->setTitleFont(font);
448 455 }
449 456
450 457 QColor DeclarativeChart::titleColor()
451 458 {
452 459 return m_chart->titleBrush().color();
453 460 }
454 461
455 462 void DeclarativeChart::setBackgroundColor(QColor color)
456 463 {
457 464 QBrush b = m_chart->backgroundBrush();
458 465 if (b.style() != Qt::SolidPattern || color != b.color()) {
459 466 b.setStyle(Qt::SolidPattern);
460 467 b.setColor(color);
461 468 m_chart->setBackgroundBrush(b);
462 469 emit backgroundColorChanged();
463 470 }
464 471 }
465 472
466 473 QColor DeclarativeChart::backgroundColor()
467 474 {
468 475 return m_chart->backgroundBrush().color();
469 476 }
470 477
471 478 int DeclarativeChart::count()
472 479 {
473 480 return m_chart->series().count();
474 481 }
475 482
476 483 void DeclarativeChart::setDropShadowEnabled(bool enabled)
477 484 {
478 485 if (enabled != m_chart->isDropShadowEnabled()) {
479 486 m_chart->setDropShadowEnabled(enabled);
480 487 dropShadowEnabledChanged(enabled);
481 488 }
482 489 }
483 490
484 491 bool DeclarativeChart::dropShadowEnabled()
485 492 {
486 493 return m_chart->isDropShadowEnabled();
487 494 }
488 495
489 496 qreal DeclarativeChart::topMargin()
490 497 {
491 498 qWarning() << "ChartView.topMargin is deprecated. Use minimumMargins and plotArea instead.";
492 499 return m_chart->plotArea().top();
493 500 }
494 501
495 502 qreal DeclarativeChart::bottomMargin()
496 503 {
497 504 qWarning() << "ChartView.bottomMargin is deprecated. Use minimumMargins and plotArea instead.";
498 505 return m_chart->plotArea().bottom();
499 506 }
500 507
501 508 qreal DeclarativeChart::leftMargin()
502 509 {
503 510 qWarning() << "ChartView.leftMargin is deprecated. Use minimumMargins and plotArea instead.";
504 511 return m_chart->plotArea().left();
505 512 }
506 513
507 514 qreal DeclarativeChart::rightMargin()
508 515 {
509 516 qWarning() << "ChartView.rightMargin is deprecated. Use minimumMargins and plotArea instead.";
510 517 return m_chart->plotArea().right();
511 518 }
512 519
513 520 void DeclarativeChart::zoom(qreal factor)
514 521 {
515 522 m_chart->zoom(factor);
516 523 }
517 524
518 525 void DeclarativeChart::scrollLeft(qreal pixels)
519 526 {
520 527 m_chart->scroll(-pixels, 0);
521 528 }
522 529
523 530 void DeclarativeChart::scrollRight(qreal pixels)
524 531 {
525 532 m_chart->scroll(pixels, 0);
526 533 }
527 534
528 535 void DeclarativeChart::scrollUp(qreal pixels)
529 536 {
530 537 m_chart->scroll(0, pixels);
531 538 }
532 539
533 540 void DeclarativeChart::scrollDown(qreal pixels)
534 541 {
535 542 m_chart->scroll(0, -pixels);
536 543 }
537 544
538 545 QAbstractSeries *DeclarativeChart::series(int index)
539 546 {
540 547 if (index < m_chart->series().count()) {
541 548 return m_chart->series().at(index);
542 549 }
543 550 return 0;
544 551 }
545 552
546 553 QAbstractSeries *DeclarativeChart::series(QString seriesName)
547 554 {
548 555 foreach(QAbstractSeries *series, m_chart->series()) {
549 556 if (series->name() == seriesName)
550 557 return series;
551 558 }
552 559 return 0;
553 560 }
554 561
555 562 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
556 563 {
564 return createSeries(type, name, 0, 0);
565 }
566
567 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name, QAbstractAxis *axisX, QAbstractAxis *axisY)
568 {
557 569 QAbstractSeries *series = 0;
558 570
559 571 switch (type) {
560 572 case DeclarativeChart::SeriesTypeLine:
561 573 series = new DeclarativeLineSeries();
562 574 break;
563 575 case DeclarativeChart::SeriesTypeArea:
564 576 series = new DeclarativeAreaSeries();
565 577 break;
566 578 case DeclarativeChart::SeriesTypeStackedBar:
567 579 series = new DeclarativeStackedBarSeries();
568 580 break;
569 581 case DeclarativeChart::SeriesTypePercentBar:
570 582 series = new DeclarativePercentBarSeries();
571 583 break;
572 584 case DeclarativeChart::SeriesTypeBar:
573 585 series = new DeclarativeBarSeries();
574 586 break;
575 587 case DeclarativeChart::SeriesTypeHorizontalBar:
576 588 series = new DeclarativeHorizontalBarSeries();
577 589 break;
578 590 case DeclarativeChart::SeriesTypeHorizontalPercentBar:
579 591 series = new DeclarativeHorizontalPercentBarSeries();
580 592 break;
581 593 case DeclarativeChart::SeriesTypeHorizontalStackedBar:
582 594 series = new DeclarativeHorizontalStackedBarSeries();
583 595 break;
584 596 case DeclarativeChart::SeriesTypePie:
585 597 series = new DeclarativePieSeries();
586 598 break;
587 599 case DeclarativeChart::SeriesTypeScatter:
588 600 series = new DeclarativeScatterSeries();
589 601 break;
590 602 case DeclarativeChart::SeriesTypeSpline:
591 603 series = new DeclarativeSplineSeries();
592 604 break;
593 605 default:
594 606 qWarning() << "Illegal series type";
595 607 }
596 608
597 609 if (series) {
598 610 series->setName(name);
599 611 m_chart->addSeries(series);
612 // Set possible user defined axes
613 setAxisX(axisX, series);
614 setAxisY(axisY, series);
615 // Then create the missing axes
600 616 createDefaultAxes(series);
601 617 }
602 618
603 619 return series;
604 620 }
605 621
606 622 void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series)
607 623 {
608 624 if (axis)
609 625 m_chart->setAxisX(axis, series);
610 626 }
611 627
612 628 void DeclarativeChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series)
613 629 {
614 630 if (axis)
615 631 m_chart->setAxisY(axis, series);
616 632 }
617 633
618 634 void DeclarativeChart::createDefaultAxes()
619 635 {
620 636 qWarning() << "ChartView.createDefaultAxes() is deprecated. Axes are created automatically.";
621 637 }
622 638
623 639 void DeclarativeChart::createDefaultAxes(QAbstractSeries* series)
624 640 {
625 641 foreach (QAbstractSeries *s, m_chart->series()) {
626 642 // If there is already an x axis of the correct type, re-use it
627 643 if (!m_chart->axisX(series) && s != series && m_chart->axisX(s)
628 644 && m_chart->axisX(s)->type() == series->d_ptr->defaultAxisType(Qt::Horizontal))
629 645 m_chart->setAxisX(m_chart->axisX(s), series);
630 646
631 647 // If there is already a y axis of the correct type, re-use it
632 648 if (!m_chart->axisY(series) && s != series && m_chart->axisY(s)
633 649 && m_chart->axisY(s)->type() == series->d_ptr->defaultAxisType(Qt::Vertical))
634 650 m_chart->setAxisY(m_chart->axisY(s), series);
635 651 }
636 652
637 653 // If no x axis of correct type was found, create a new x axis based of default axis type
638 654 if (!m_chart->axisX(series)) {
639 655 switch (series->d_ptr->defaultAxisType(Qt::Horizontal)) {
640 656 case QAbstractAxis::AxisTypeValue:
641 657 m_chart->setAxisX(new QValueAxis(this), series);
642 658 break;
643 659 case QAbstractAxis::AxisTypeBarCategory:
644 660 m_chart->setAxisX(new QBarCategoryAxis(this), series);
645 661 break;
646 662 case QAbstractAxis::AxisTypeCategory:
647 663 m_chart->setAxisX(new QCategoryAxis(this), series);
648 664 break;
649 665 #ifndef QT_ON_ARM
650 666 case QAbstractAxis::AxisTypeDateTime:
651 667 m_chart->setAxisX(new QDateTimeAxis(this), series);
652 668 break;
653 669 #endif
654 670 default:
655 671 // Do nothing, assume AxisTypeNoAxis
656 672 break;
657 673 }
658 674 }
659 675
660 676 // If no y axis of correct type was found, create a new y axis based of default axis type
661 677 if (!m_chart->axisY(series)) {
662 678 switch (series->d_ptr->defaultAxisType(Qt::Vertical)) {
663 679 case QAbstractAxis::AxisTypeValue:
664 680 m_chart->setAxisY(new QValueAxis(this), series);
665 681 break;
666 682 case QAbstractAxis::AxisTypeBarCategory:
667 683 m_chart->setAxisY(new QBarCategoryAxis(this), series);
668 684 break;
669 685 case QAbstractAxis::AxisTypeCategory:
670 686 m_chart->setAxisY(new QCategoryAxis(this), series);
671 687 break;
672 688 #ifndef QT_ON_ARM
673 689 case QAbstractAxis::AxisTypeDateTime:
674 690 m_chart->setAxisY(new QDateTimeAxis(this), series);
675 691 break;
676 692 #endif
677 693 default:
678 694 // Do nothing, assume AxisTypeNoAxis
679 695 break;
680 696 }
681 697 }
682 698
683 699 //qDebug() << "axis for series" << series << "x:" << m_chart->axisX(series) << "y:" << m_chart->axisY(series);
684 700 }
685 701
686 702 #include "moc_declarativechart.cpp"
687 703
688 704 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,161 +1,162
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 #ifndef DECLARATIVECHART_H
22 22 #define DECLARATIVECHART_H
23 23
24 24 #include <QtCore/QtGlobal>
25 25 #include <QDeclarativeItem>
26 26 #include "qchart.h"
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 class DeclarativeMargins;
31 31
32 32 class DeclarativeChart : public QDeclarativeItem
33 33 {
34 34 Q_OBJECT
35 35 Q_PROPERTY(Theme theme READ theme WRITE setTheme)
36 36 Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions)
37 37 Q_PROPERTY(QString title READ title WRITE setTitle)
38 38 Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont)
39 39 Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged)
40 40 Q_PROPERTY(QLegend *legend READ legend)
41 41 Q_PROPERTY(int count READ count)
42 42 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
43 43 Q_PROPERTY(bool dropShadowEnabled READ dropShadowEnabled WRITE setDropShadowEnabled NOTIFY dropShadowEnabledChanged)
44 44 Q_PROPERTY(qreal topMargin READ topMargin)
45 45 Q_PROPERTY(qreal bottomMargin READ bottomMargin)
46 46 Q_PROPERTY(qreal leftMargin READ leftMargin)
47 47 Q_PROPERTY(qreal rightMargin READ rightMargin)
48 48 Q_PROPERTY(DeclarativeMargins *minimumMargins READ minimumMargins NOTIFY minimumMarginsChanged REVISION 1)
49 49 Q_PROPERTY(QRectF plotArea READ plotArea NOTIFY plotAreaChanged REVISION 1)
50 50 Q_ENUMS(Animation)
51 51 Q_ENUMS(Theme)
52 52 Q_ENUMS(SeriesType)
53 53
54 54 public:
55 55 // duplicating enums from QChart to make the QML api namings 1-to-1 with the C++ api
56 56 enum Theme {
57 57 ChartThemeLight = 0,
58 58 ChartThemeBlueCerulean,
59 59 ChartThemeDark,
60 60 ChartThemeBrownSand,
61 61 ChartThemeBlueNcs,
62 62 ChartThemeHighContrast,
63 63 ChartThemeBlueIcy
64 64 };
65 65
66 66 enum Animation {
67 67 NoAnimation = 0x0,
68 68 GridAxisAnimations = 0x1,
69 69 SeriesAnimations =0x2,
70 70 AllAnimations = 0x3
71 71 };
72 72
73 73 enum SeriesType {
74 74 SeriesTypeLine,
75 75 SeriesTypeArea,
76 76 SeriesTypeBar,
77 77 SeriesTypeStackedBar,
78 78 SeriesTypePercentBar,
79 79 SeriesTypePie,
80 80 SeriesTypeScatter,
81 81 SeriesTypeSpline,
82 82 SeriesTypeHorizontalBar,
83 83 SeriesTypeHorizontalStackedBar,
84 84 SeriesTypeHorizontalPercentBar
85 85 };
86 86
87 87 public:
88 88 DeclarativeChart(QDeclarativeItem *parent = 0);
89 89 ~DeclarativeChart();
90 90
91 91 public: // From QDeclarativeItem/QGraphicsItem
92 92 void childEvent(QChildEvent *event);
93 93 void componentComplete();
94 94 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
95 95
96 96 public:
97 97 void setTheme(DeclarativeChart::Theme theme);
98 98 DeclarativeChart::Theme theme();
99 99 void setAnimationOptions(DeclarativeChart::Animation animations);
100 100 DeclarativeChart::Animation animationOptions();
101 101 void setTitle(QString title);
102 102 QString title();
103 103 QLegend *legend();
104 104 QFont titleFont() const;
105 105 void setTitleFont(const QFont& font);
106 106 void setTitleColor(QColor color);
107 107 QColor titleColor();
108 108 void setBackgroundColor(QColor color);
109 109 QColor backgroundColor();
110 110 int count();
111 111 void setDropShadowEnabled(bool enabled);
112 112 bool dropShadowEnabled();
113 113 qreal topMargin();
114 114 qreal bottomMargin();
115 115 qreal leftMargin();
116 116 qreal rightMargin();
117 117 void createDefaultAxes(QAbstractSeries* series);
118 118 DeclarativeMargins *minimumMargins() { return m_minMargins; }
119 119 QRectF plotArea() { return m_chart->plotArea(); }
120 120
121 121 public:
122 122 Q_INVOKABLE QAbstractSeries *series(int index);
123 123 Q_INVOKABLE QAbstractSeries *series(QString seriesName);
124 124 Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name = "");
125 Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name, QAbstractAxis *axisX, QAbstractAxis *axisY);
125 126 Q_INVOKABLE void removeSeries(QAbstractSeries *series) { m_chart->removeSeries(series); }
126 127 Q_INVOKABLE void removeAllSeries() { m_chart->removeAllSeries(); }
127 128 Q_INVOKABLE void setAxisX(QAbstractAxis *axis, QAbstractSeries *series = 0);
128 129 Q_INVOKABLE void setAxisY(QAbstractAxis *axis, QAbstractSeries *series = 0);
129 130 Q_INVOKABLE void createDefaultAxes();
130 131 Q_INVOKABLE QAbstractAxis *axisX(QAbstractSeries *series = 0);
131 132 Q_INVOKABLE QAbstractAxis *axisY(QAbstractSeries *series = 0);
132 133 Q_INVOKABLE void zoom(qreal factor);
133 134 Q_INVOKABLE void scrollLeft(qreal pixels);
134 135 Q_INVOKABLE void scrollRight(qreal pixels);
135 136 Q_INVOKABLE void scrollUp(qreal pixels);
136 137 Q_INVOKABLE void scrollDown(qreal pixels);
137 138
138 139 Q_SIGNALS:
139 140 void axisLabelsChanged();
140 141 void titleColorChanged(QColor color);
141 142 void backgroundColorChanged();
142 143 void dropShadowEnabledChanged(bool enabled);
143 144 void minimumMarginsChanged();
144 145 void plotAreaChanged(QRectF plotArea);
145 146
146 147 public Q_SLOTS:
147 148 void changeMinimumMargins(int top, int bottom, int left, int right);
148 149 void handleAxisXSet(QAbstractAxis *axis);
149 150 void handleAxisYSet(QAbstractAxis *axis);
150 151
151 152 private:
152 153 // Extending QChart with DeclarativeChart is not possible because QObject does not support
153 154 // multi inheritance, so we now have a QChart as a member instead
154 155 QChart *m_chart;
155 156 //QMargins m_chartMargins;
156 157 DeclarativeMargins *m_minMargins;
157 158 };
158 159
159 160 QTCOMMERCIALCHART_END_NAMESPACE
160 161
161 162 #endif // DECLARATIVECHART_H
General Comments 0
You need to be logged in to leave comments. Login now