##// END OF EJS Templates
Fixes and tests to Interval axis
Marek Rosa -
r1708:f5ae73793525
parent child
Show More
@@ -0,0 +1,5
1 !include( ../auto.pri ) {
2 error( "Couldn't find the auto.pri file!" )
3 }
4 HEADERS += ../qabstractaxis/tst_qabstractaxis.h
5 SOURCES += tst_qintervalsaxis.cpp ../qabstractaxis/tst_qabstractaxis.cpp
@@ -0,0 +1,299
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "../qabstractaxis/tst_qabstractaxis.h"
22 #include "qintervalsaxis.h"
23 #include <qlineseries.h>
24
25 class tst_QIntervalsAxis: public tst_QAbstractAxis
26 {
27 Q_OBJECT
28
29 public slots:
30 void initTestCase();
31 void cleanupTestCase();
32 void init();
33 void cleanup();
34
35 private slots:
36 void qintervalsaxis_data();
37 void qintervalsaxis();
38
39 void max_raw_data();
40 void max_raw();
41 void max_data();
42 void max();
43 void max_animation_data();
44 void max_animation();
45 void min_raw_data();
46 void min_raw();
47 void min_data();
48 void min();
49 void min_animation_data();
50 void min_animation();
51 void range_raw_data();
52 void range_raw();
53 void range_data();
54 void range();
55 void range_animation_data();
56 void range_animation();
57
58 void interval_data();
59 void interval();
60
61 private:
62 QIntervalsAxis* m_intervalsaxis;
63 QLineSeries* m_series;
64 };
65
66 void tst_QIntervalsAxis::initTestCase()
67 {
68 }
69
70 void tst_QIntervalsAxis::cleanupTestCase()
71 {
72 }
73
74 void tst_QIntervalsAxis::init()
75 {
76 m_intervalsaxis = new QIntervalsAxis();
77 m_series = new QLineSeries();
78 *m_series << QPointF(-100, -100) << QPointF(0, 0) << QPointF(100, 100);
79 tst_QAbstractAxis::init(m_intervalsaxis, m_series);
80 m_chart->addSeries(m_series);
81 m_chart->createDefaultAxes();
82 }
83
84 void tst_QIntervalsAxis::cleanup()
85 {
86 delete m_series;
87 delete m_intervalsaxis;
88 m_series = 0;
89 m_intervalsaxis = 0;
90 tst_QAbstractAxis::cleanup();
91 }
92
93 void tst_QIntervalsAxis::qintervalsaxis_data()
94 {
95 }
96
97 void tst_QIntervalsAxis::qintervalsaxis()
98 {
99 qabstractaxis();
100
101 QVERIFY(qFuzzyIsNull(m_intervalsaxis->max()));
102 QVERIFY(qFuzzyIsNull(m_intervalsaxis->min()));
103 QCOMPARE(m_intervalsaxis->type(), QAbstractAxis::AxisTypeIntervals);
104
105 m_chart->setAxisX(m_intervalsaxis, m_series);
106 m_view->show();
107 QTest::qWaitForWindowShown(m_view);
108
109 QVERIFY(!qFuzzyIsNull(m_intervalsaxis->max()));
110 QVERIFY(!qFuzzyIsNull(m_intervalsaxis->min()));
111 }
112
113 void tst_QIntervalsAxis::max_raw_data()
114 {
115 QTest::addColumn<qreal>("max");
116 QTest::newRow("1.0") << 1.0;
117 QTest::newRow("50.0") << 50.0;
118 QTest::newRow("101.0") << 101.0;
119 }
120
121 void tst_QIntervalsAxis::max_raw()
122 {
123 QFETCH(qreal, max);
124
125 QSignalSpy spy0(m_intervalsaxis, SIGNAL(maxChanged(qreal)));
126 QSignalSpy spy1(m_intervalsaxis, SIGNAL(minChanged(qreal)));
127 QSignalSpy spy2(m_intervalsaxis, SIGNAL(rangeChanged(qreal, qreal)));
128
129 m_intervalsaxis->setMax(max);
130 QVERIFY2(qFuzzyIsNull(m_intervalsaxis->max() - max), "Not equal");
131
132 QCOMPARE(spy0.count(), 1);
133 QCOMPARE(spy1.count(), 0);
134 QCOMPARE(spy2.count(), 1);
135 }
136
137 void tst_QIntervalsAxis::max_data()
138 {
139 max_raw_data();
140 }
141
142 void tst_QIntervalsAxis::max()
143 {
144 m_chart->setAxisX(m_intervalsaxis, m_series);
145 m_view->show();
146 QTest::qWaitForWindowShown(m_view);
147 max_raw();
148 }
149
150 void tst_QIntervalsAxis::max_animation_data()
151 {
152 max_data();
153 }
154
155 void tst_QIntervalsAxis::max_animation()
156 {
157 m_chart->setAnimationOptions(QChart::GridAxisAnimations);
158 max();
159 }
160
161 void tst_QIntervalsAxis::min_raw_data()
162 {
163 QTest::addColumn<qreal>("min");
164 QTest::newRow("-1.0") << -1.0;
165 QTest::newRow("-50.0") << -50.0;
166 QTest::newRow("-101.0") << -101.0;
167 }
168
169 void tst_QIntervalsAxis::min_raw()
170 {
171 QFETCH(qreal, min);
172
173 QSignalSpy spy0(m_intervalsaxis, SIGNAL(maxChanged(qreal)));
174 QSignalSpy spy1(m_intervalsaxis, SIGNAL(minChanged(qreal)));
175 QSignalSpy spy2(m_intervalsaxis, SIGNAL(rangeChanged(qreal, qreal)));
176
177 m_intervalsaxis->setMin(min);
178 QVERIFY2(qFuzzyIsNull(m_intervalsaxis->min() - min), "Not equal");
179
180 QCOMPARE(spy0.count(), 0);
181 QCOMPARE(spy1.count(), 1);
182 QCOMPARE(spy2.count(), 1);
183 }
184
185 void tst_QIntervalsAxis::min_data()
186 {
187 min_raw_data();
188 }
189
190 void tst_QIntervalsAxis::min()
191 {
192 m_chart->setAxisX(m_intervalsaxis, m_series);
193 m_view->show();
194 QTest::qWaitForWindowShown(m_view);
195 min_raw();
196 }
197
198 void tst_QIntervalsAxis::min_animation_data()
199 {
200 min_data();
201 }
202
203 void tst_QIntervalsAxis::min_animation()
204 {
205 m_chart->setAnimationOptions(QChart::GridAxisAnimations);
206 min();
207 }
208
209 void tst_QIntervalsAxis::range_raw_data()
210 {
211 QTest::addColumn<qreal>("min");
212 QTest::addColumn<qreal>("max");
213 QTest::newRow("1.0 - 101.0") << -1.0 << 101.0;
214 QTest::newRow("25.0 - 75.0") << 25.0 << 75.0;
215 QTest::newRow("101.0") << 40.0 << 60.0;
216 }
217
218 void tst_QIntervalsAxis::range_raw()
219 {
220 QFETCH(qreal, min);
221 QFETCH(qreal, max);
222
223 QSignalSpy spy0(m_intervalsaxis, SIGNAL(maxChanged(qreal)));
224 QSignalSpy spy1(m_intervalsaxis, SIGNAL(minChanged(qreal)));
225 QSignalSpy spy2(m_intervalsaxis, SIGNAL(rangeChanged(qreal, qreal)));
226
227 m_intervalsaxis->setRange(min, max);
228 QVERIFY2(qFuzzyIsNull(m_intervalsaxis->min() - min), "Min not equal");
229 QVERIFY2(qFuzzyIsNull(m_intervalsaxis->max() - max), "Max not equal");
230
231 QCOMPARE(spy0.count(), 1);
232 QCOMPARE(spy1.count(), 1);
233 QCOMPARE(spy2.count(), 1);
234 }
235
236 void tst_QIntervalsAxis::range_data()
237 {
238 range_raw_data();
239 }
240
241 void tst_QIntervalsAxis::range()
242 {
243 m_chart->setAxisX(m_intervalsaxis, m_series);
244 m_view->show();
245 QTest::qWaitForWindowShown(m_view);
246 range_raw();
247 }
248
249 void tst_QIntervalsAxis::range_animation_data()
250 {
251 range_data();
252 }
253
254 void tst_QIntervalsAxis::range_animation()
255 {
256 m_chart->setAnimationOptions(QChart::GridAxisAnimations);
257 range();
258 }
259
260 void tst_QIntervalsAxis::interval_data()
261 {
262 //
263 }
264
265 void tst_QIntervalsAxis::interval()
266 {
267 // append one correct interval
268 m_intervalsaxis->append("first", (qreal)45);
269 QCOMPARE(m_intervalsaxis->intervalMin("first"), (qreal)0);
270 QCOMPARE(m_intervalsaxis->intervalMax("first"), (qreal)45);
271
272 // append one more correct interval
273 m_intervalsaxis->append("second", (qreal)75);
274 QCOMPARE(m_intervalsaxis->intervalMin("second"), (qreal)45);
275 QCOMPARE(m_intervalsaxis->intervalMax("second"), (qreal)75);
276
277 // append one incorrect interval
278 m_intervalsaxis->append("third", (qreal)15);
279 QCOMPARE(m_intervalsaxis->count(), 2);
280 QCOMPARE(m_intervalsaxis->intervalMax(m_intervalsaxis->intervalsLabels().last()), (qreal)75);
281 // QCOMPARE(intervalMax("first"), (qreal)75);
282
283 // append one more correct interval
284 m_intervalsaxis->append("third", (qreal)100);
285 QCOMPARE(m_intervalsaxis->count(), 3);
286 QCOMPARE(m_intervalsaxis->intervalMin("third"), (qreal)75);
287 QCOMPARE(m_intervalsaxis->intervalMax("third"), (qreal)100);
288
289 // remove one interval
290 m_intervalsaxis->remove("first");
291 QCOMPARE(m_intervalsaxis->count(), 2);
292 QCOMPARE(m_intervalsaxis->intervalMin("second"), (qreal)0); // second interval should extend to firstInterval minimum
293 QCOMPARE(m_intervalsaxis->intervalMax("second"), (qreal)75);
294
295 }
296
297 QTEST_MAIN(tst_QIntervalsAxis)
298 #include "tst_qintervalsaxis.moc"
299
@@ -45,6 +45,10 QVector<qreal> ChartIntervalAxisX::calculateLayout() const
45 QIntervalsAxis *axis = qobject_cast<QIntervalsAxis *>(m_chartAxis);
45 QIntervalsAxis *axis = qobject_cast<QIntervalsAxis *>(m_chartAxis);
46 int tickCount = axis->intervalsLabels().count() + 1;
46 int tickCount = axis->intervalsLabels().count() + 1;
47 QVector<qreal> points;
47 QVector<qreal> points;
48
49 if (tickCount < 2)
50 return points;
51
48 points.resize(tickCount);
52 points.resize(tickCount);
49
53
50 qreal scale = m_rect.width() / axis->max();
54 qreal scale = m_rect.width() / axis->max();
@@ -45,6 +45,10 QVector<qreal> ChartIntervalAxisY::calculateLayout() const
45 QIntervalsAxis *axis = qobject_cast<QIntervalsAxis *>(m_chartAxis);
45 QIntervalsAxis *axis = qobject_cast<QIntervalsAxis *>(m_chartAxis);
46 int tickCount = axis->intervalsLabels().count() + 1;
46 int tickCount = axis->intervalsLabels().count() + 1;
47 QVector<qreal> points;
47 QVector<qreal> points;
48
49 if (tickCount < 2)
50 return points;
51
48 points.resize(tickCount);
52 points.resize(tickCount);
49
53
50 qreal scale = m_rect.height() / axis->max();
54 qreal scale = m_rect.height() / axis->max();
@@ -76,31 +76,31 QIntervalsAxis::QIntervalsAxis(QIntervalsAxisPrivate &d,QObject *parent):QValues
76 void QIntervalsAxis::append(const QString& intervalLabel, qreal interval)
76 void QIntervalsAxis::append(const QString& intervalLabel, qreal interval)
77 {
77 {
78 Q_D(QIntervalsAxis);
78 Q_D(QIntervalsAxis);
79
79 if (!d->m_intervals.contains(intervalLabel))
80 if (!d->m_intervals.contains(intervalLabel))
80 {
81 {
81 if(d->m_intervals.isEmpty()){
82 if(d->m_intervals.isEmpty()){
82 Range range(d->m_categoryMinimum,interval);
83 Range range(d->m_categoryMinimum,interval);
83 d->m_intervalsMap.insert(intervalLabel, range);
84 d->m_intervalsMap.insert(intervalLabel, range);
84 d->m_intervals.append(intervalLabel);
85 d->m_intervals.append(intervalLabel);
85 }else{
86 }else if (interval > intervalMax(d->m_intervals.last())){
86 Range range = d->m_intervalsMap.value(d->m_intervals.last());
87 Range range = d->m_intervalsMap.value(d->m_intervals.last());
87 d->m_intervalsMap.insert(intervalLabel, Range(range.second,interval));
88 d->m_intervalsMap.insert(intervalLabel, Range(range.second,interval));
88 d->m_intervals.append(intervalLabel);
89 d->m_intervals.append(intervalLabel);
89 }
90 }
90 // setRange(d->m_min,interval);
91 }
91 }
92 }
92 }
93
93
94 void QIntervalsAxis::setFisrtIntervalMinimum(qreal min)
94 void QIntervalsAxis::setFisrtIntervalMinimum(qreal min)
95 {
95 {
96 Q_D(QIntervalsAxis);
96 Q_D(QIntervalsAxis);
97 if(d->m_intervals.isEmpty()){
97 if(d->m_intervals.isEmpty()){
98 d->m_categoryMinimum = min;
98 d->m_categoryMinimum = min;
99 }else{
99 }else{
100 Range range = d->m_intervalsMap.value(d->m_intervals.first());
100 Range range = d->m_intervalsMap.value(d->m_intervals.first());
101 d->m_intervalsMap.insert(d->m_intervals.first(), Range(min, range.second));
101 d->m_intervalsMap.insert(d->m_intervals.first(), Range(min, range.second));
102 setRange(min, d->m_min);
102 // setRange(min, d->m_min);
103 }
103 }
104 }
104 }
105
105
106 qreal QIntervalsAxis::intervalMin(const QString& intervalLabel) const
106 qreal QIntervalsAxis::intervalMin(const QString& intervalLabel) const
@@ -120,8 +120,29 qreal QIntervalsAxis::intervalMax(const QString& intervalLabel) const
120 */
120 */
121 void QIntervalsAxis::remove(const QString &intervalLabel)
121 void QIntervalsAxis::remove(const QString &intervalLabel)
122 {
122 {
123 Q_UNUSED(intervalLabel);
123 Q_D(QIntervalsAxis);
124 //TODO
124 int labelIndex = d->m_intervals.indexOf(intervalLabel);
125
126 // check if such label exists
127 if (labelIndex != -1) {
128 d->m_intervals.removeAt(labelIndex);
129 d->m_intervalsMap.remove(intervalLabel);
130
131 // the range of the interval that follows (if exists) needs to be updated
132 if (labelIndex < d->m_intervals.count()) {
133 QString label = d->m_intervals.at(labelIndex);
134 Range range = d->m_intervalsMap.value(label);
135
136 // set the range
137 if (labelIndex == 0) {
138 range.first = d->m_categoryMinimum;
139 d->m_intervalsMap.insert(label, range);
140 } else {
141 range.first = d->m_intervalsMap.value(d->m_intervals.at(labelIndex - 1)).second;
142 d->m_intervalsMap.insert(label, range);
143 }
144 }
145 }
125 }
146 }
126
147
127 QStringList QIntervalsAxis::intervalsLabels()
148 QStringList QIntervalsAxis::intervalsLabels()
@@ -144,7 +165,7 int QIntervalsAxis::count() const
144 */
165 */
145 QAbstractAxis::AxisType QIntervalsAxis::type() const
166 QAbstractAxis::AxisType QIntervalsAxis::type() const
146 {
167 {
147 return AxisTypeCategories;
168 return QAbstractAxis::AxisTypeIntervals;
148 }
169 }
149
170
150 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
171 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -171,7 +192,6 void QIntervalsAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int coun
171 Q_UNUSED(count);
192 Q_UNUSED(count);
172 m_min = min;
193 m_min = min;
173 m_max = max;
194 m_max = max;
174 // m_ticksCount = count;
175 }
195 }
176
196
177 ChartAxis* QIntervalsAxisPrivate::createGraphics(ChartPresenter* presenter)
197 ChartAxis* QIntervalsAxisPrivate::createGraphics(ChartPresenter* presenter)
@@ -50,7 +50,8 public:
50 enum AxisType {
50 enum AxisType {
51 AxisTypeNoAxis = 0x0,
51 AxisTypeNoAxis = 0x0,
52 AxisTypeValues = 0x1,
52 AxisTypeValues = 0x1,
53 AxisTypeCategories = 0x2
53 AxisTypeCategories = 0x2,
54 AxisTypeIntervals = 0x3
54 };
55 };
55
56
56 Q_DECLARE_FLAGS(AxisTypes, AxisType)
57 Q_DECLARE_FLAGS(AxisTypes, AxisType)
@@ -20,7 +20,8 SUBDIRS += \
20 qhorizontalbarseries \
20 qhorizontalbarseries \
21 qhorizontalstackedbarseries \
21 qhorizontalstackedbarseries \
22 qhorizontalpercentbarseries \
22 qhorizontalpercentbarseries \
23 qvaluesaxis
23 qvaluesaxis \
24 qintervalsaxis
24
25
25 test_private:{
26 test_private:{
26 SUBDIRS += domain chartdataset
27 SUBDIRS += domain chartdataset
@@ -31,7 +31,7 void tst_QAbstractAxis::cleanupTestCase()
31 {
31 {
32 }
32 }
33
33
34 void tst_QAbstractAxis::init(QAbstractAxis* axis,QAbstractSeries* series)
34 void tst_QAbstractAxis::init(QAbstractAxis* axis, QAbstractSeries* series)
35 {
35 {
36 m_axis = axis;
36 m_axis = axis;
37 m_series = series;
37 m_series = series;
General Comments 0
You need to be logged in to leave comments. Login now