##// END OF EJS Templates
Added zooming functions to ChartView...
Titta Heikkala -
r2788:67af712ca4a0
parent child
Show More
@@ -0,0 +1,158
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
7 ** This file is part of the Qt Charts module.
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
13 **
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
16 **
17 ****************************************************************************/
18
19 import QtQuick 2.0
20 import QtTest 1.0
21 import QtCharts 2.0
22
23 Rectangle {
24 width: 400
25 height: 300
26
27 TestCase {
28 id: tc1
29 name: "tst_qml-qtquicktest ChartView Functions"
30 when: windowShown
31
32 function test_chartViewSeriesAndAxes() {
33 // Create XY series
34 var line = chartView.createSeries(ChartView.SeriesTypeLine, "line");
35 verify(line != null && line != undefined);
36 var spline = chartView.createSeries(ChartView.SeriesTypeSpline, "spline");
37 verify(spline != null && spline != undefined);
38 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter");
39 verify(scatter != null && scatter != undefined);
40
41 // Create a series with specific axes
42 var line2 = chartView.createSeries(ChartView.SeriesTypeLine, "line2", chartView.axisX(line), chartView.axisY(line));
43
44 // Check that all the XY series use the same axes
45 verify(chartView.axisX(line) != null);
46 verify(chartView.axisY(line) != null);
47 compare(chartView.axisX(line), chartView.axisX(line2));
48 compare(chartView.axisY(line), chartView.axisY(line2));
49 compare(chartView.axisX(line), chartView.axisX(spline));
50 compare(chartView.axisY(line), chartView.axisY(spline));
51 compare(chartView.axisX(line), chartView.axisX(scatter));
52 compare(chartView.axisY(line), chartView.axisY(scatter));
53
54 var bar = chartView.createSeries(ChartView.SeriesTypeBar, "bar");
55 verify(bar != null && bar != undefined);
56 var stackedbar = chartView.createSeries(ChartView.SeriesTypeStackedBar, "stackedbar");
57 verify(stackedbar != null && stackedbar != undefined);
58 var percentbar = chartView.createSeries(ChartView.SeriesTypePercentBar, "percentbar");
59 verify(percentbar != null && percentbar != undefined);
60 var horizontalbar = chartView.createSeries(ChartView.SeriesTypeHorizontalBar, "horizontalbar");
61 verify(horizontalbar != null && horizontalbar != undefined);
62 var horizontalstackedbar = chartView.createSeries(ChartView.SeriesTypeHorizontalStackedBar, "horizontalstackedbar");
63 verify(horizontalstackedbar != null && horizontalstackedbar != undefined);
64 var horizontalpercentbar = chartView.createSeries(ChartView.SeriesTypeHorizontalPercentBar, "horizontalpercentbar");
65 verify(horizontalpercentbar != null && horizontalpercentbar != undefined);
66 var area = chartView.createSeries(ChartView.SeriesTypeArea, "area");
67 verify(area != null && area != undefined);
68
69 // Remove all series
70 chartView.removeAllSeries();
71 compare(chartView.count, 0);
72 }
73
74 function test_chartViewRange() {
75 // Set initial values
76 chartView.createSeries(ChartView.SeriesTypeLine, "line");
77 verify(chartView.axisX() != null);
78 verify(chartView.axisY() != null);
79 chartView.axisX().min = 1.0;
80 chartView.axisX().max = 2.0;
81 chartView.axisY().min = 1.0;
82 chartView.axisY().max = 2.0;
83
84 var xMax = chartView.axisX().max;
85 var xMin = chartView.axisX().min;
86 var yMax = chartView.axisY().max;
87 var yMin = chartView.axisY().min;
88
89 // zoom x 2.5
90 chartView.zoom(1.5);
91 verify(chartView.axisX().max < xMax);
92 verify(chartView.axisX().min > xMin);
93 verify(chartView.axisY().max < yMax);
94 verify(chartView.axisY().min > yMin);
95 xMax = chartView.axisX().max;
96 xMin = chartView.axisX().min;
97 yMax = chartView.axisY().max;
98 yMin = chartView.axisY().min;
99
100 // zoom x 0.5
101 chartView.zoom(0.5);
102 verify(chartView.axisX().max > xMax);
103 verify(chartView.axisX().min < xMin);
104 verify(chartView.axisY().max > yMax);
105 verify(chartView.axisY().min < yMin);
106 xMax = chartView.axisX().max;
107 xMin = chartView.axisX().min;
108 yMax = chartView.axisY().max;
109 yMin = chartView.axisY().min;
110
111 // Scroll up
112 chartView.scrollUp(10);
113 compare(chartView.axisX().max, xMax);
114 compare(chartView.axisX().min, xMin);
115 verify(chartView.axisY().max > yMax);
116 verify(chartView.axisY().min > yMin);
117 xMax = chartView.axisX().max;
118 xMin = chartView.axisX().min;
119 yMax = chartView.axisY().max;
120 yMin = chartView.axisY().min;
121
122 // Scroll down
123 chartView.scrollDown(10);
124 compare(chartView.axisX().max, xMax);
125 compare(chartView.axisX().min, xMin);
126 verify(chartView.axisY().max < yMax);
127 verify(chartView.axisY().min < yMin);
128 xMax = chartView.axisX().max;
129 xMin = chartView.axisX().min;
130 yMax = chartView.axisY().max;
131 yMin = chartView.axisY().min;
132
133 // Scroll left
134 chartView.scrollLeft(10);
135 verify(chartView.axisX().max < xMax);
136 verify(chartView.axisX().min < xMin);
137 compare(chartView.axisY().max, yMax);
138 compare(chartView.axisY().min, yMin);
139 xMax = chartView.axisX().max;
140 xMin = chartView.axisX().min;
141 yMax = chartView.axisY().max;
142 yMin = chartView.axisY().min;
143
144 // Scroll right
145 chartView.scrollRight(10);
146 verify(chartView.axisX().max > xMax);
147 verify(chartView.axisX().min > xMin);
148 compare(chartView.axisY().max, yMax);
149 compare(chartView.axisY().min, yMin);
150 }
151 }
152
153 ChartView {
154 id: chartView
155 anchors.fill: parent
156 title: "Chart"
157 }
158 }
@@ -305,6 +305,7 public:
305 qmlRegisterType<DeclarativeCategoryAxis, 1>(uri, 2, 1, "CategoryAxis");
305 qmlRegisterType<DeclarativeCategoryAxis, 1>(uri, 2, 1, "CategoryAxis");
306 qmlRegisterUncreatableType<QAbstractAxis>(uri, 2, 1, "AbstractAxis",
306 qmlRegisterUncreatableType<QAbstractAxis>(uri, 2, 1, "AbstractAxis",
307 QLatin1String("Trying to create uncreatable: AbstractAxis. Use specific types of axis instead."));
307 QLatin1String("Trying to create uncreatable: AbstractAxis. Use specific types of axis instead."));
308 qmlRegisterType<DeclarativeChart, 5>(uri, 2, 1, "ChartView");
308 }
309 }
309
310
310 };
311 };
@@ -227,6 +227,32 QT_CHARTS_BEGIN_NAMESPACE
227 /*!
227 /*!
228 \qmlmethod ChartView::zoom(real factor)
228 \qmlmethod ChartView::zoom(real factor)
229 Zooms in by \a factor on the center of the chart.
229 Zooms in by \a factor on the center of the chart.
230
231 A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.
232 */
233
234 /*!
235 \qmlmethod ChartView::zoomIn()
236 Zooms in the view by a factor of two.
237 */
238
239 /*!
240 \qmlmethod ChartView::zoomIn(rect rectangle)
241 Zooms in the view to a maximum level at which \a rectangle is still fully visible.
242 \note This is not supported for polar charts.
243 */
244
245 /*!
246 \qmlmethod ChartView::zoomOut()
247 Zooms out the view by a factor of two.
248 */
249
250 /*!
251 \qmlmethod ChartView::zoomReset()
252 Resets the series domains to what they were before any zoom method was called.
253 Note that this will also reset any scrolls and explicit axis range settings done between
254 the first zoom operation and calling this method. If no zoom operation has been
255 done, this method does nothing.
230 */
256 */
231
257
232 /*!
258 /*!
@@ -798,6 +824,26 void DeclarativeChart::zoom(qreal factor)
798 m_chart->zoom(factor);
824 m_chart->zoom(factor);
799 }
825 }
800
826
827 void DeclarativeChart::zoomIn()
828 {
829 m_chart->zoomIn();
830 }
831
832 void DeclarativeChart::zoomIn(const QRectF &rectangle)
833 {
834 m_chart->zoomIn(rectangle);
835 }
836
837 void DeclarativeChart::zoomOut()
838 {
839 m_chart->zoomOut();
840 }
841
842 void DeclarativeChart::zoomReset()
843 {
844 m_chart->zoomReset();
845 }
846
801 void DeclarativeChart::scrollLeft(qreal pixels)
847 void DeclarativeChart::scrollLeft(qreal pixels)
802 {
848 {
803 m_chart->scroll(-pixels, 0);
849 m_chart->scroll(-pixels, 0);
@@ -163,6 +163,10 public:
163 Q_INVOKABLE QAbstractAxis *axisX(QAbstractSeries *series = 0);
163 Q_INVOKABLE QAbstractAxis *axisX(QAbstractSeries *series = 0);
164 Q_INVOKABLE QAbstractAxis *axisY(QAbstractSeries *series = 0);
164 Q_INVOKABLE QAbstractAxis *axisY(QAbstractSeries *series = 0);
165 Q_INVOKABLE void zoom(qreal factor);
165 Q_INVOKABLE void zoom(qreal factor);
166 Q_REVISION(5) Q_INVOKABLE void zoomIn();
167 Q_REVISION(5) Q_INVOKABLE void zoomIn(const QRectF &rectangle);
168 Q_REVISION(5) Q_INVOKABLE void zoomOut();
169 Q_REVISION(5) Q_INVOKABLE void zoomReset();
166 Q_INVOKABLE void scrollLeft(qreal pixels);
170 Q_INVOKABLE void scrollLeft(qreal pixels);
167 Q_INVOKABLE void scrollRight(qreal pixels);
171 Q_INVOKABLE void scrollRight(qreal pixels);
168 Q_INVOKABLE void scrollUp(qreal pixels);
172 Q_INVOKABLE void scrollUp(qreal pixels);
@@ -929,9 +929,10 Module {
929 "QtCharts/ChartView 1.1",
929 "QtCharts/ChartView 1.1",
930 "QtCharts/ChartView 1.2",
930 "QtCharts/ChartView 1.2",
931 "QtCharts/ChartView 1.3",
931 "QtCharts/ChartView 1.3",
932 "QtCharts/ChartView 2.0"
932 "QtCharts/ChartView 2.0",
933 "QtCharts/ChartView 2.1"
933 ]
934 ]
934 exportMetaObjectRevisions: [0, 1, 2, 3, 4]
935 exportMetaObjectRevisions: [0, 1, 2, 3, 4, 5]
935 Enum {
936 Enum {
936 name: "Theme"
937 name: "Theme"
937 values: {
938 values: {
@@ -1098,6 +1099,14 Module {
1098 name: "zoom"
1099 name: "zoom"
1099 Parameter { name: "factor"; type: "double" }
1100 Parameter { name: "factor"; type: "double" }
1100 }
1101 }
1102 Method { name: "zoomIn"; revision: 5 }
1103 Method {
1104 name: "zoomIn"
1105 revision: 5
1106 Parameter { name: "rectangle"; type: "QRectF" }
1107 }
1108 Method { name: "zoomOut"; revision: 5 }
1109 Method { name: "zoomReset"; revision: 5 }
1101 Method {
1110 Method {
1102 name: "scrollLeft"
1111 name: "scrollLeft"
1103 Parameter { name: "pixels"; type: "double" }
1112 Parameter { name: "pixels"; type: "double" }
@@ -18,7 +18,7
18
18
19 import QtQuick 2.0
19 import QtQuick 2.0
20 import QtTest 1.0
20 import QtTest 1.0
21 import QtCharts 2.0
21 import QtCharts 2.1
22
22
23 Rectangle {
23 Rectangle {
24 width: 400
24 width: 400
@@ -28,6 +28,7 Rectangle {
28 id: tc1
28 id: tc1
29 name: "tst_qml-qtquicktest ChartView Functions"
29 name: "tst_qml-qtquicktest ChartView Functions"
30 when: windowShown
30 when: windowShown
31 property rect zoomRect
31
32
32 function test_chartViewSeriesAndAxes() {
33 function test_chartViewSeriesAndAxes() {
33 // Create XY series
34 // Create XY series
@@ -81,6 +82,11 Rectangle {
81 chartView.axisY().min = 1.0;
82 chartView.axisY().min = 1.0;
82 chartView.axisY().max = 2.0;
83 chartView.axisY().max = 2.0;
83
84
85 var xMaxOriginal = chartView.axisX().max;
86 var xMinOriginal = chartView.axisX().min;
87 var yMaxOriginal = chartView.axisY().max;
88 var yMinOriginal = chartView.axisY().min;
89
84 var xMax = chartView.axisX().max;
90 var xMax = chartView.axisX().max;
85 var xMin = chartView.axisX().min;
91 var xMin = chartView.axisX().min;
86 var yMax = chartView.axisY().max;
92 var yMax = chartView.axisY().max;
@@ -108,6 +114,62 Rectangle {
108 yMax = chartView.axisY().max;
114 yMax = chartView.axisY().max;
109 yMin = chartView.axisY().min;
115 yMin = chartView.axisY().min;
110
116
117 // zoom x -1
118 // negative value has no effect
119 chartView.zoom(-1);
120 verify(chartView.axisX().max === xMax);
121 verify(chartView.axisX().min === xMin);
122 verify(chartView.axisY().max === yMax);
123 verify(chartView.axisY().min === yMin);
124
125 // zoom in
126 chartView.zoomIn();
127 verify(chartView.axisX().max < xMax);
128 verify(chartView.axisX().min > xMin);
129 verify(chartView.axisY().max < yMax);
130 verify(chartView.axisY().min > yMin);
131 xMax = chartView.axisX().max;
132 xMin = chartView.axisX().min;
133 yMax = chartView.axisY().max;
134 yMin = chartView.axisY().min;
135
136 // zoom in rect
137 zoomRect.x = 100;
138 zoomRect.y = 100;
139 zoomRect.width = 120;
140 zoomRect.height = 120;
141 chartView.zoomIn(zoomRect);
142 verify(chartView.axisX().max < xMax);
143 verify(chartView.axisX().min > xMin);
144 verify(chartView.axisY().max < yMax);
145 verify(chartView.axisY().min > yMin);
146 xMax = chartView.axisX().max;
147 xMin = chartView.axisX().min;
148 yMax = chartView.axisY().max;
149 yMin = chartView.axisY().min;
150
151 // zoom out
152 chartView.zoomOut();
153 verify(chartView.axisX().max > xMax);
154 verify(chartView.axisX().min < xMin);
155 verify(chartView.axisY().max > yMax);
156 verify(chartView.axisY().min < yMin);
157 xMax = chartView.axisX().max;
158 xMin = chartView.axisX().min;
159 yMax = chartView.axisY().max;
160 yMin = chartView.axisY().min;
161
162 // zoom reset
163 chartView.zoomReset();
164 verify(chartView.axisX().max === xMaxOriginal);
165 verify(chartView.axisX().min === xMinOriginal);
166 verify(chartView.axisY().max === yMaxOriginal);
167 verify(chartView.axisY().min === yMinOriginal);
168 xMax = chartView.axisX().max;
169 xMin = chartView.axisX().min;
170 yMax = chartView.axisY().max;
171 yMin = chartView.axisY().min;
172
111 // Scroll up
173 // Scroll up
112 chartView.scrollUp(10);
174 chartView.scrollUp(10);
113 compare(chartView.axisX().max, xMax);
175 compare(chartView.axisX().max, xMax);
General Comments 0
You need to be logged in to leave comments. Login now