##// END OF EJS Templates
Reserve ordering of chart series internally....
Tero Ahola -
r825:d850de289019
parent child
Show More
@@ -1,345 +1,334
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartdataset_p.h"
21 #include "chartdataset_p.h"
22 #include "qchartaxis.h"
22 #include "qchartaxis.h"
23 //series
23 //series
24 #include "qlineseries.h"
24 #include "qlineseries.h"
25 #include "qareaseries.h"
25 #include "qareaseries.h"
26 #include "qbarseries.h"
26 #include "qbarseries.h"
27 #include "qstackedbarseries.h"
27 #include "qstackedbarseries.h"
28 #include "qpercentbarseries.h"
28 #include "qpercentbarseries.h"
29 #include "qpieseries.h"
29 #include "qpieseries.h"
30 #include "qscatterseries.h"
30 #include "qscatterseries.h"
31 #include "qsplineseries.h"
31 #include "qsplineseries.h"
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
35 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
36 m_axisX(new QChartAxis(this)),
36 m_axisX(new QChartAxis(this)),
37 m_axisY(new QChartAxis(this)),
37 m_axisY(new QChartAxis(this)),
38 m_domainIndex(0),
38 m_domainIndex(0),
39 m_axisXInitialized(false)
39 m_axisXInitialized(false)
40 {
40 {
41 }
41 }
42
42
43 ChartDataSet::~ChartDataSet()
43 ChartDataSet::~ChartDataSet()
44 {
44 {
45 }
45 }
46
46
47 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
47 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
48 {
48 {
49 if(axisY==0) axisY = m_axisY;
49 if(axisY==0) axisY = m_axisY;
50
50
51 QChartAxis* axis = m_seriesAxisMap.value(series);
51 if (seriesIndex(series) > -1) {
52
53 if(axis) {
54 qWarning() << "Can not add series. Series already on the chart";
52 qWarning() << "Can not add series. Series already on the chart";
55 return;
53 return;
56 }
54 }
57
55
58 if(!series->parent()){
56 if(!series->parent()){
59 series->setParent(this); // take ownership
57 series->setParent(this); // take ownership
60 };
58 };
61
59
62 if(!axisY->parent()){
60 if(!axisY->parent()){
63 axisY->setParent(this); // take ownership
61 axisY->setParent(this); // take ownership
64 }
62 }
65
63
66 Domain* domain = m_axisDomainMap.value(axisY);
64 Domain* domain = m_axisDomainMap.value(axisY);
67
65 if (!domain) {
68 if(!domain) {
69 domain = new Domain(axisY);
66 domain = new Domain(axisY);
70 QObject::connect(axisY,SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
67 QObject::connect(axisY,SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
71 QObject::connect(axisX(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
68 QObject::connect(axisX(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
72 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY,SLOT(handleAxisRangeChanged(qreal,qreal,int)));
69 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY,SLOT(handleAxisRangeChanged(qreal,qreal,int)));
73 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
70 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
74 //initialize
71 //initialize
75 m_axisDomainMap.insert(axisY,domain);
72 m_axisDomainMap.insert(axisY,domain);
76 emit axisAdded(axisY,domain);
73 emit axisAdded(axisY,domain);
77 }
74 }
78
75
79 if(!m_axisXInitialized){
76 if (!m_axisXInitialized) {
80 emit axisAdded(axisX(),domain);
77 emit axisAdded(axisX(), domain);
81 m_axisXInitialized=true;
78 m_axisXInitialized = true;
82 }
79 }
83
80
84 calculateDomain(series,domain);
81 calculateDomain(series, domain);
85
82
86 m_seriesAxisMap.insert(series,axisY);
83 m_seriesAxisList.append(QPair<QSeries*, QChartAxis*>(series, axisY));
87 emit seriesAdded(series,domain);
88
84
85 emit seriesAdded(series, domain);
89 }
86 }
90
87
91 void ChartDataSet::removeSeries(QSeries* series)
88 void ChartDataSet::removeSeries(QSeries* series)
92 {
89 {
93
90 int index = seriesIndex(series);
94 QChartAxis* axis = m_seriesAxisMap.value(series);
91 if (!index < 0) {
95
92 qWarning() << "Can not remove series. Series not found on the chart.";
96 if(!axis){
97 qWarning()<<"Can not remove series. Series not found on the chart.";
98 return;
93 return;
99 }
94 }
100 emit seriesRemoved(series);
101 m_seriesAxisMap.remove(series);
102
95
103 if(series->parent()==this){
96 // Remove the series and the axis from the container
97 QChartAxis* axis = m_seriesAxisList.at(index).second;
98 m_seriesAxisList.removeAt(index);
99
100 // Delete the series
101 emit seriesRemoved(series);
102 if (series->parent() == this) {
104 delete series;
103 delete series;
105 series=0;
104 series = 0;
106 }
105 }
107
106
108 QList<QChartAxis*> axes = m_seriesAxisMap.values();
107 // Check if the Y axis is still in use
109
108 bool yAxisInUse(false);
110 int i = axes.indexOf(axis);
109 for (int i(0); i < m_seriesAxisList.count(); i++) {
110 QPair<QSeries *, QChartAxis *> pair = m_seriesAxisList.at(i);
111 if (pair.second == axis)
112 yAxisInUse = true;
113 }
111
114
112 if(i==-1){
115 // Remove the Y axis if not in use
116 if (!yAxisInUse) {
113 Domain* domain = m_axisDomainMap.take(axis);
117 Domain* domain = m_axisDomainMap.take(axis);
114 emit axisRemoved(axis);
118 emit axisRemoved(axis);
115 if(axis!=axisY()){
119 if (axis != axisY()) {
116 if(axis->parent()==this){
120 // Delete the Y axis unless it is the default one
121 if (axis->parent() == this) {
117 delete axis;
122 delete axis;
118 axis=0;
123 axis = 0;
119 }
124 }
120 }
125 }
121 delete domain;
126 delete domain;
122 }
127 }
123
128
124 if(m_seriesAxisMap.values().size()==0)
129 // Remove the x axis in case there are no y-axes left
125 {
130 if (m_seriesAxisList.count() == 0) {
126 m_axisXInitialized=false;
131 m_axisXInitialized = false;
127 emit axisRemoved(axisX());
132 emit axisRemoved(axisX());
128 }
133 }
129 }
134 }
130
135
131 void ChartDataSet::removeAllSeries()
136 void ChartDataSet::removeAllSeries()
132 {
137 {
133
138 while (m_seriesAxisList.count()) {
134 QList<QSeries*> series = m_seriesAxisMap.keys();
139 QPair<QSeries *, QChartAxis *> pair = m_seriesAxisList.last();
135
140 removeSeries(pair.first);
136 foreach(QSeries* s , series) {
137 removeSeries(s);
138 }
141 }
139
142
140 Q_ASSERT(m_seriesAxisMap.count()==0);
143 Q_ASSERT(m_seriesAxisList.count() == 0);
141 Q_ASSERT(m_axisDomainMap.count()==0);
144 Q_ASSERT(m_axisDomainMap.count() == 0);
142
143 }
145 }
144
146
145 //to be removed with PIMPL
147 //to be removed with PIMPL
146 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain)
148 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain)
147 {
149 {
148 qreal minX(domain->minX());
150 qreal minX(domain->minX());
149 qreal minY(domain->minY());
151 qreal minY(domain->minY());
150 qreal maxX(domain->maxX());
152 qreal maxX(domain->maxX());
151 qreal maxY(domain->maxY());
153 qreal maxY(domain->maxY());
152 int tickXCount(domain->tickXCount());
154 int tickXCount(domain->tickXCount());
153 int tickYCount(domain->tickYCount());
155 int tickYCount(domain->tickYCount());
154
156
155
157
156 switch(series->type())
158 switch(series->type())
157 {
159 {
158 case QSeries::SeriesTypeLine:
160 case QSeries::SeriesTypeLine:
159 case QSeries::SeriesTypeSpline:
161 case QSeries::SeriesTypeSpline:
160 case QSeries::SeriesTypeScatter:
162 case QSeries::SeriesTypeScatter:
161 {
163 {
162
164
163 QXYSeries* xySeries = static_cast<QXYSeries*>(series);
165 QXYSeries* xySeries = static_cast<QXYSeries*>(series);
164
166
165 for (int i = 0; i < xySeries->count(); i++)
167 for (int i = 0; i < xySeries->count(); i++)
166 {
168 {
167 qreal x = xySeries->x(i);
169 qreal x = xySeries->x(i);
168 qreal y = xySeries->y(i);
170 qreal y = xySeries->y(i);
169 minX = qMin(minX, x);
171 minX = qMin(minX, x);
170 minY = qMin(minY, y);
172 minY = qMin(minY, y);
171 maxX = qMax(maxX, x);
173 maxX = qMax(maxX, x);
172 maxY = qMax(maxY, y);
174 maxY = qMax(maxY, y);
173 }
175 }
174 break;
176 break;
175 }
177 }
176 case QSeries::SeriesTypeArea: {
178 case QSeries::SeriesTypeArea: {
177
179
178 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
180 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
179 QLineSeries* upperSeries = areaSeries->upperSeries();
181 QLineSeries* upperSeries = areaSeries->upperSeries();
180 QLineSeries* lowerSeries = areaSeries->lowerSeries();
182 QLineSeries* lowerSeries = areaSeries->lowerSeries();
181
183
182 for (int i = 0; i < upperSeries->count(); i++)
184 for (int i = 0; i < upperSeries->count(); i++)
183 {
185 {
184 qreal x = upperSeries->x(i);
186 qreal x = upperSeries->x(i);
185 qreal y = upperSeries->y(i);
187 qreal y = upperSeries->y(i);
186 minX = qMin(minX, x);
188 minX = qMin(minX, x);
187 minY = qMin(minY, y);
189 minY = qMin(minY, y);
188 maxX = qMax(maxX, x);
190 maxX = qMax(maxX, x);
189 maxY = qMax(maxY, y);
191 maxY = qMax(maxY, y);
190 }
192 }
191 if(lowerSeries) {
193 if(lowerSeries) {
192 for (int i = 0; i < lowerSeries->count(); i++)
194 for (int i = 0; i < lowerSeries->count(); i++)
193 {
195 {
194 qreal x = lowerSeries->x(i);
196 qreal x = lowerSeries->x(i);
195 qreal y = lowerSeries->y(i);
197 qreal y = lowerSeries->y(i);
196 minX = qMin(minX, x);
198 minX = qMin(minX, x);
197 minY = qMin(minY, y);
199 minY = qMin(minY, y);
198 maxX = qMax(maxX, x);
200 maxX = qMax(maxX, x);
199 maxY = qMax(maxY, y);
201 maxY = qMax(maxY, y);
200 }}
202 }}
201 break;
203 break;
202 }
204 }
203 case QSeries::SeriesTypeBar: {
205 case QSeries::SeriesTypeBar: {
204
206
205 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
207 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
206 qreal x = barSeries->categoryCount();
208 qreal x = barSeries->categoryCount();
207 qreal y = barSeries->max();
209 qreal y = barSeries->max();
208 minX = qMin(minX, x);
210 minX = qMin(minX, x);
209 minY = qMin(minY, y);
211 minY = qMin(minY, y);
210 maxX = qMax(maxX, x);
212 maxX = qMax(maxX, x);
211 maxY = qMax(maxY, y);
213 maxY = qMax(maxY, y);
212 tickXCount = x+1;
214 tickXCount = x+1;
213 setupCategories(barSeries);
215 setupCategories(barSeries);
214 break;
216 break;
215 }
217 }
216 case QSeries::SeriesTypeStackedBar: {
218 case QSeries::SeriesTypeStackedBar: {
217
219
218 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
220 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
219 qreal x = stackedBarSeries->categoryCount();
221 qreal x = stackedBarSeries->categoryCount();
220 qreal y = stackedBarSeries->maxCategorySum();
222 qreal y = stackedBarSeries->maxCategorySum();
221 minX = qMin(minX, x);
223 minX = qMin(minX, x);
222 minY = qMin(minY, y);
224 minY = qMin(minY, y);
223 maxX = qMax(maxX, x);
225 maxX = qMax(maxX, x);
224 maxY = qMax(maxY, y);
226 maxY = qMax(maxY, y);
225 tickXCount = x+1;
227 tickXCount = x+1;
226 setupCategories(stackedBarSeries);
228 setupCategories(stackedBarSeries);
227 break;
229 break;
228 }
230 }
229 case QSeries::SeriesTypePercentBar: {
231 case QSeries::SeriesTypePercentBar: {
230
232
231 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
233 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
232 qreal x = percentBarSeries->categoryCount();
234 qreal x = percentBarSeries->categoryCount();
233 minX = qMin(minX, x);
235 minX = qMin(minX, x);
234 maxX = qMax(maxX, x);
236 maxX = qMax(maxX, x);
235 minY = 0;
237 minY = 0;
236 maxY = 100;
238 maxY = 100;
237 setupCategories(percentBarSeries);
239 setupCategories(percentBarSeries);
238 break;
240 break;
239 }
241 }
240
242
241 case QSeries::SeriesTypePie: {
243 case QSeries::SeriesTypePie: {
242 //QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
244 //QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
243 // TODO: domain stuff
245 // TODO: domain stuff
244 break;
246 break;
245 }
247 }
246
248
247
249
248 default: {
250 default: {
249 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
251 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
250 return;
252 return;
251 }
253 }
252
254
253 }
255 }
254
256
255 domain->setRangeX(minX,maxX,tickXCount);
257 domain->setRangeX(minX,maxX,tickXCount);
256 domain->setRangeY(minY,maxY,tickYCount);
258 domain->setRangeY(minY,maxY,tickYCount);
257 }
259 }
258
260
259
261
260 void ChartDataSet::setupCategories(QBarSeries* series)
262 void ChartDataSet::setupCategories(QBarSeries* series)
261 {
263 {
262 QChartAxisCategories* categories = axisX()->categories();
264 QChartAxisCategories* categories = axisX()->categories();
263 categories->clear();
265 categories->clear();
264 categories->insert(series->categories());
266 categories->insert(series->categories());
265 }
267 }
266
268
267 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
269 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
268 {
270 {
269 QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap);
271 QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap);
270 while (i.hasNext()) {
272 while (i.hasNext()) {
271 i.next();
273 i.next();
272 i.value()->zoomIn(rect,size);
274 i.value()->zoomIn(rect,size);
273 }
275 }
274 }
276 }
275
277
276 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
278 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
277 {
279 {
278 QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap);
280 QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap);
279 while (i.hasNext()) {
281 while (i.hasNext()) {
280 i.next();
282 i.next();
281 i.value()->zoomOut(rect,size);
283 i.value()->zoomOut(rect,size);
282 }
284 }
283 }
285 }
284
286
285 int ChartDataSet::seriesCount(QSeries::QSeriesType type)
287 int ChartDataSet::seriesIndex(QSeries *series) const
286 {
287 int count=0;
288 QMapIterator<QSeries*, QChartAxis*> i(m_seriesAxisMap);
289 while (i.hasNext()) {
290 i.next();
291 if(i.key()->type()==type) count++;
292 }
293 return count;
294 }
295
296 int ChartDataSet::seriesIndex(QSeries *series)
297 {
288 {
298 int count=-1;
289 for (int i(0); i < m_seriesAxisList.count(); i++) {
299 QMapIterator<QSeries*, QChartAxis*> i(m_seriesAxisMap);
290 QPair<QSeries *, QChartAxis *> pair = m_seriesAxisList.at(i);
300 while (i.hasNext()) {
291 if (pair.first == series)
301 i.next();
292 return i;
302 count++;
303 if (i.key() == series)
304 return count;
305 }
293 }
306 return count;
294 return -1;
307 }
295 }
308
296
309 QChartAxis* ChartDataSet::axisY(QSeries* series) const
297 QChartAxis* ChartDataSet::axisY(QSeries* series) const
310 {
298 {
311 if(series == 0) return m_axisY;
299 if (series == 0)
312 return m_seriesAxisMap.value(series);
300 return m_axisY;
301
302 return m_seriesAxisList.at(seriesIndex(series)).second;
313 }
303 }
314
304
315 Domain* ChartDataSet::domain(QSeries* series) const
305 Domain* ChartDataSet::domain(QSeries* series) const
316 {
306 {
317 QChartAxis* axis = m_seriesAxisMap.value(series);
307 QChartAxis* axis = m_seriesAxisList.at(seriesIndex(series)).second;
318 if(axis){
308 if (axis)
319 return m_axisDomainMap.value(axis);
309 return m_axisDomainMap.value(axis);
320 }else
310 return 0;
321 return 0;
322 }
311 }
323
312
324 Domain* ChartDataSet::domain(QChartAxis* axis) const
313 Domain* ChartDataSet::domain(QChartAxis* axis) const
325 {
314 {
326 if(!axis || axis==axisX()) {
315 if(!axis || axis==axisX()) {
327 return m_axisDomainMap.value(axisY());
316 return m_axisDomainMap.value(axisY());
328 }
317 }
329 else {
318 else {
330 return m_axisDomainMap.value(axis);
319 return m_axisDomainMap.value(axis);
331 }
320 }
332 }
321 }
333
322
334 void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size)
323 void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size)
335 {
324 {
336 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
325 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
337 while (i.hasNext()) {
326 while (i.hasNext()) {
338 i.next();
327 i.next();
339 i.value()->move(dx,dy,size);
328 i.value()->move(dx,dy,size);
340 }
329 }
341 }
330 }
342
331
343 #include "moc_chartdataset_p.cpp"
332 #include "moc_chartdataset_p.cpp"
344
333
345 QTCOMMERCIALCHART_END_NAMESPACE
334 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,89 +1,88
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTDATASET_P_H_
30 #ifndef CHARTDATASET_P_H_
31 #define CHARTDATASET_P_H_
31 #define CHARTDATASET_P_H_
32
32
33 #include "qseries.h"
33 #include "qseries.h"
34 #include "domain_p.h"
34 #include "domain_p.h"
35 #include <QVector>
35 #include <QVector>
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 class QChartAxis;
39 class QChartAxis;
40 class QBarSeries;
40 class QBarSeries;
41
41
42 class ChartDataSet : public QObject
42 class ChartDataSet : public QObject
43 {
43 {
44 Q_OBJECT
44 Q_OBJECT
45 public:
45 public:
46 ChartDataSet(QObject* parent=0);
46 ChartDataSet(QObject* parent=0);
47 virtual ~ChartDataSet();
47 virtual ~ChartDataSet();
48
48
49 void addSeries(QSeries* series,QChartAxis *axisY = 0);
49 void addSeries(QSeries* series,QChartAxis *axisY = 0);
50 void removeSeries(QSeries* series);
50 void removeSeries(QSeries* series);
51 void removeAllSeries();
51 void removeAllSeries();
52
52
53 void zoomInDomain(const QRectF& rect, const QSizeF& size);
53 void zoomInDomain(const QRectF& rect, const QSizeF& size);
54 void zoomOutDomain(const QRectF& rect, const QSizeF& size);
54 void zoomOutDomain(const QRectF& rect, const QSizeF& size);
55 void scrollDomain(int dx,int dy,const QSizeF& size);
55 void scrollDomain(int dx,int dy,const QSizeF& size);
56
56
57 int seriesCount(QSeries::QSeriesType type);
57 int seriesIndex(QSeries *series) const;
58 int seriesIndex(QSeries *series);
59
58
60 Domain* domain(QSeries* series) const;
59 Domain* domain(QSeries* series) const;
61 Domain* domain(QChartAxis* axis) const;
60 Domain* domain(QChartAxis* axis) const;
62
61
63 QChartAxis* axisX() const { return m_axisX; }
62 QChartAxis* axisX() const { return m_axisX; }
64 QChartAxis* axisY(QSeries* series = 0) const;
63 QChartAxis* axisY(QSeries* series = 0) const;
65
64
66 Q_SIGNALS:
65 Q_SIGNALS:
67 void seriesAdded(QSeries* series,Domain* domain);
66 void seriesAdded(QSeries* series,Domain* domain);
68 void seriesRemoved(QSeries* series);
67 void seriesRemoved(QSeries* series);
69 void axisAdded(QChartAxis* axis,Domain* domain);
68 void axisAdded(QChartAxis* axis,Domain* domain);
70 void axisRemoved(QChartAxis* axis);
69 void axisRemoved(QChartAxis* axis);
71
70
72 private:
71 private:
73 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
72 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
74 void calculateDomain(QSeries* series,Domain* domain);
73 void calculateDomain(QSeries* series,Domain* domain);
75 void setupCategories(QBarSeries* series);
74 void setupCategories(QBarSeries* series);
76
75
77 private:
76 private:
78 QMap<QSeries*, QChartAxis*> m_seriesAxisMap;
77 QList<QPair<QSeries*, QChartAxis*> > m_seriesAxisList;
79 QMap<QChartAxis*, Domain*> m_axisDomainMap;
78 QMap<QChartAxis*, Domain*> m_axisDomainMap;
80 QChartAxis* m_axisX;
79 QChartAxis* m_axisX;
81 QChartAxis* m_axisY;
80 QChartAxis* m_axisY;
82
81
83 int m_domainIndex;
82 int m_domainIndex;
84 bool m_axisXInitialized;
83 bool m_axisXInitialized;
85 };
84 };
86
85
87 QTCOMMERCIALCHART_END_NAMESPACE
86 QTCOMMERCIALCHART_END_NAMESPACE
88
87
89 #endif /* CHARTENGINE_P_H_ */
88 #endif /* CHARTENGINE_P_H_ */
@@ -1,490 +1,454
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qchartaxis.h>
22 #include <qchartaxis.h>
23 #include <qlineseries.h>
23 #include <qlineseries.h>
24 #include <private/chartdataset_p.h>
24 #include <private/chartdataset_p.h>
25 #include <private/domain_p.h>
25 #include <private/domain_p.h>
26
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
28
29 Q_DECLARE_METATYPE(Domain*)
29 Q_DECLARE_METATYPE(Domain*)
30 Q_DECLARE_METATYPE(QChartAxis*)
30 Q_DECLARE_METATYPE(QChartAxis*)
31 Q_DECLARE_METATYPE(QSeries*)
31 Q_DECLARE_METATYPE(QSeries*)
32 Q_DECLARE_METATYPE(QLineSeries*)
32 Q_DECLARE_METATYPE(QLineSeries*)
33
33
34 class tst_ChartDataSet: public QObject {
34 class tst_ChartDataSet: public QObject {
35
35
36 Q_OBJECT
36 Q_OBJECT
37
37
38 public Q_SLOTS:
38 public Q_SLOTS:
39 void initTestCase();
39 void initTestCase();
40 void cleanupTestCase();
40 void cleanupTestCase();
41 void init();
41 void init();
42 void cleanup();
42 void cleanup();
43
43
44 private Q_SLOTS:
44 private Q_SLOTS:
45 void chartdataset_data();
45 void chartdataset_data();
46 void chartdataset();
46 void chartdataset();
47 void addSeries_data();
47 void addSeries_data();
48 void addSeries();
48 void addSeries();
49 void removeSeries_data();
49 void removeSeries_data();
50 void removeSeries();
50 void removeSeries();
51 void removeAllSeries_data();
51 void removeAllSeries_data();
52 void removeAllSeries();
52 void removeAllSeries();
53 void axisY_data();
53 void axisY_data();
54 void axisY();
54 void axisY();
55 void seriesCount_data();
56 void seriesCount();
57 void seriesIndex_data();
55 void seriesIndex_data();
58 void seriesIndex();
56 void seriesIndex();
59 void domain_data();
57 void domain_data();
60 void domain();
58 void domain();
61 void zoomInDomain_data();
59 void zoomInDomain_data();
62 void zoomInDomain();
60 void zoomInDomain();
63 void zoomOutDomain_data();
61 void zoomOutDomain_data();
64 void zoomOutDomain();
62 void zoomOutDomain();
65 void scrollDomain_data();
63 void scrollDomain_data();
66 void scrollDomain();
64 void scrollDomain();
67 };
65 };
68
66
69 void tst_ChartDataSet::initTestCase()
67 void tst_ChartDataSet::initTestCase()
70 {
68 {
71 qRegisterMetaType<Domain*>();
69 qRegisterMetaType<Domain*>();
72 qRegisterMetaType<QChartAxis*>();
70 qRegisterMetaType<QChartAxis*>();
73 qRegisterMetaType<QSeries*>();
71 qRegisterMetaType<QSeries*>();
74 }
72 }
75
73
76 void tst_ChartDataSet::cleanupTestCase()
74 void tst_ChartDataSet::cleanupTestCase()
77 {
75 {
78 }
76 }
79
77
80 void tst_ChartDataSet::init()
78 void tst_ChartDataSet::init()
81 {
79 {
82 }
80 }
83
81
84 void tst_ChartDataSet::cleanup()
82 void tst_ChartDataSet::cleanup()
85 {
83 {
86 }
84 }
87
85
88 void tst_ChartDataSet::chartdataset_data()
86 void tst_ChartDataSet::chartdataset_data()
89 {
87 {
90 }
88 }
91
89
92 void tst_ChartDataSet::chartdataset()
90 void tst_ChartDataSet::chartdataset()
93 {
91 {
94 ChartDataSet dataSet;
92 ChartDataSet dataSet;
95 QVERIFY2(dataSet.axisX(), "Missing axisX.");
93 QVERIFY2(dataSet.axisX(), "Missing axisX.");
96 QVERIFY2(dataSet.axisY(), "Missing axisY.");
94 QVERIFY2(dataSet.axisY(), "Missing axisY.");
97 //check if not dangling pointer
95 //check if not dangling pointer
98 dataSet.axisX()->objectName();
96 dataSet.axisX()->objectName();
99 dataSet.axisY()->objectName();
97 dataSet.axisY()->objectName();
100 }
98 }
101
99
102 void tst_ChartDataSet::addSeries_data()
100 void tst_ChartDataSet::addSeries_data()
103 {
101 {
104 QTest::addColumn<QLineSeries*>("series0");
102 QTest::addColumn<QLineSeries*>("series0");
105 QTest::addColumn<QChartAxis*>("axis0");
103 QTest::addColumn<QChartAxis*>("axis0");
106 QTest::addColumn<QLineSeries*>("series1");
104 QTest::addColumn<QLineSeries*>("series1");
107 QTest::addColumn<QChartAxis*>("axis1");
105 QTest::addColumn<QChartAxis*>("axis1");
108 QTest::addColumn<QLineSeries*>("series2");
106 QTest::addColumn<QLineSeries*>("series2");
109 QTest::addColumn<QChartAxis*>("axis2");
107 QTest::addColumn<QChartAxis*>("axis2");
110 QTest::addColumn<int>("axisCount");
108 QTest::addColumn<int>("axisCount");
111
109
112 QLineSeries* series0 = new QLineSeries(this);
110 QLineSeries* series0 = new QLineSeries(this);
113 QLineSeries* series1 = new QLineSeries(this);
111 QLineSeries* series1 = new QLineSeries(this);
114 QLineSeries* series2 = new QLineSeries(this);
112 QLineSeries* series2 = new QLineSeries(this);
115
113
116 QChartAxis* axis0 = new QChartAxis(this);
114 QChartAxis* axis0 = new QChartAxis(this);
117 QChartAxis* axis1 = new QChartAxis(this);
115 QChartAxis* axis1 = new QChartAxis(this);
118 QChartAxis* axis2 = new QChartAxis(this);
116 QChartAxis* axis2 = new QChartAxis(this);
119
117
120 QTest::newRow("default axis Y: series0,series1,series2") << series0 << (QChartAxis*)0 << series1 << (QChartAxis*)0 << series2 << (QChartAxis*)0 << 2;
118 QTest::newRow("default axis Y: series0,series1,series2") << series0 << (QChartAxis*)0 << series1 << (QChartAxis*)0 << series2 << (QChartAxis*)0 << 2;
121 QTest::newRow("default axis Y: series0, axis 0: series1,series2") << series0 << (QChartAxis*)0 << series1 << axis0 << series2 << axis0 << 3;
119 QTest::newRow("default axis Y: series0, axis 0: series1,series2") << series0 << (QChartAxis*)0 << series1 << axis0 << series2 << axis0 << 3;
122 QTest::newRow("axis0: series0, axis1: series1, axis2: series2") << series0 << axis0 << series1 << axis1 << series2 << axis2 << 4;
120 QTest::newRow("axis0: series0, axis1: series1, axis2: series2") << series0 << axis0 << series1 << axis1 << series2 << axis2 << 4;
123 }
121 }
124
122
125 void tst_ChartDataSet::addSeries()
123 void tst_ChartDataSet::addSeries()
126 {
124 {
127 QFETCH(QLineSeries*, series0);
125 QFETCH(QLineSeries*, series0);
128 QFETCH(QChartAxis*, axis0);
126 QFETCH(QChartAxis*, axis0);
129 QFETCH(QLineSeries*, series1);
127 QFETCH(QLineSeries*, series1);
130 QFETCH(QChartAxis*, axis1);
128 QFETCH(QChartAxis*, axis1);
131 QFETCH(QLineSeries*, series2);
129 QFETCH(QLineSeries*, series2);
132 QFETCH(QChartAxis*, axis2);
130 QFETCH(QChartAxis*, axis2);
133 QFETCH(int, axisCount);
131 QFETCH(int, axisCount);
134
132
135 ChartDataSet dataSet;
133 ChartDataSet dataSet;
136
134
137 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
135 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
138 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
136 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
139 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
137 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
140 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
138 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
141
139
142 dataSet.addSeries(series0,axis0);
140 dataSet.addSeries(series0,axis0);
143 dataSet.addSeries(series1,axis1);
141 dataSet.addSeries(series1,axis1);
144 dataSet.addSeries(series2,axis2);
142 dataSet.addSeries(series2,axis2);
145
143
146 QCOMPARE(spy0.count(), axisCount);
144 QCOMPARE(spy0.count(), axisCount);
147 QCOMPARE(spy1.count(), 0);
145 QCOMPARE(spy1.count(), 0);
148 QCOMPARE(spy2.count(), 3);
146 QCOMPARE(spy2.count(), 3);
149 QCOMPARE(spy3.count(), 0);
147 QCOMPARE(spy3.count(), 0);
150
148
151 }
149 }
152
150
153 void tst_ChartDataSet::removeSeries_data()
151 void tst_ChartDataSet::removeSeries_data()
154 {
152 {
155 addSeries_data();
153 addSeries_data();
156 }
154 }
157
155
158 void tst_ChartDataSet::removeSeries()
156 void tst_ChartDataSet::removeSeries()
159 {
157 {
160 QFETCH(QLineSeries*, series0);
158 QFETCH(QLineSeries*, series0);
161 QFETCH(QChartAxis*, axis0);
159 QFETCH(QChartAxis*, axis0);
162 QFETCH(QLineSeries*, series1);
160 QFETCH(QLineSeries*, series1);
163 QFETCH(QChartAxis*, axis1);
161 QFETCH(QChartAxis*, axis1);
164 QFETCH(QLineSeries*, series2);
162 QFETCH(QLineSeries*, series2);
165 QFETCH(QChartAxis*, axis2);
163 QFETCH(QChartAxis*, axis2);
166 QFETCH(int, axisCount);
164 QFETCH(int, axisCount);
167
165
168 ChartDataSet dataSet;
166 ChartDataSet dataSet;
169
167
170 dataSet.addSeries(series0,axis0);
168 dataSet.addSeries(series0,axis0);
171 dataSet.addSeries(series1,axis1);
169 dataSet.addSeries(series1,axis1);
172 dataSet.addSeries(series2,axis2);
170 dataSet.addSeries(series2,axis2);
173
171
174 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
172 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
175 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
173 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
176 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
174 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
177 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
175 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
178
176
179 dataSet.removeSeries(series0);
177 dataSet.removeSeries(series0);
180 dataSet.removeSeries(series1);
178 dataSet.removeSeries(series1);
181 dataSet.removeSeries(series2);
179 dataSet.removeSeries(series2);
182
180
183 QCOMPARE(spy0.count(), 0);
181 QCOMPARE(spy0.count(), 0);
184 QCOMPARE(spy1.count(), axisCount);
182 QCOMPARE(spy1.count(), axisCount);
185 QCOMPARE(spy2.count(), 0);
183 QCOMPARE(spy2.count(), 0);
186 QCOMPARE(spy3.count(), 3);
184 QCOMPARE(spy3.count(), 3);
187 }
185 }
188
186
189 void tst_ChartDataSet::removeAllSeries_data()
187 void tst_ChartDataSet::removeAllSeries_data()
190 {
188 {
191 addSeries_data();
189 addSeries_data();
192 }
190 }
193
191
194 void tst_ChartDataSet::removeAllSeries()
192 void tst_ChartDataSet::removeAllSeries()
195 {
193 {
196 QFETCH(QLineSeries*, series0);
194 QFETCH(QLineSeries*, series0);
197 QFETCH(QChartAxis*, axis0);
195 QFETCH(QChartAxis*, axis0);
198 QFETCH(QLineSeries*, series1);
196 QFETCH(QLineSeries*, series1);
199 QFETCH(QChartAxis*, axis1);
197 QFETCH(QChartAxis*, axis1);
200 QFETCH(QLineSeries*, series2);
198 QFETCH(QLineSeries*, series2);
201 QFETCH(QChartAxis*, axis2);
199 QFETCH(QChartAxis*, axis2);
202 QFETCH(int, axisCount);
200 QFETCH(int, axisCount);
203
201
204 ChartDataSet dataSet;
202 ChartDataSet dataSet;
205
203
206 dataSet.addSeries(series0,axis0);
204 dataSet.addSeries(series0,axis0);
207 dataSet.addSeries(series1,axis1);
205 dataSet.addSeries(series1,axis1);
208 dataSet.addSeries(series2,axis2);
206 dataSet.addSeries(series2,axis2);
209
207
210 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
208 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
211 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
209 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
212 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
210 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
213 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
211 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
214
212
215 dataSet.removeAllSeries();
213 dataSet.removeAllSeries();
216
214
217 QCOMPARE(spy0.count(), 0);
215 QCOMPARE(spy0.count(), 0);
218 QCOMPARE(spy1.count(), axisCount);
216 QCOMPARE(spy1.count(), axisCount);
219 QCOMPARE(spy2.count(), 0);
217 QCOMPARE(spy2.count(), 0);
220 QCOMPARE(spy3.count(), 3);
218 QCOMPARE(spy3.count(), 3);
221 }
219 }
222
220
223
221
224 void tst_ChartDataSet::axisY_data()
222 void tst_ChartDataSet::axisY_data()
225 {
223 {
226 QTest::addColumn<QChartAxis*>("axis0");
224 QTest::addColumn<QChartAxis*>("axis0");
227 QTest::addColumn<QChartAxis*>("axis1");
225 QTest::addColumn<QChartAxis*>("axis1");
228 QTest::addColumn<QChartAxis*>("axis2");
226 QTest::addColumn<QChartAxis*>("axis2");
229 QTest::newRow("1 defualt, 2 optional") << (QChartAxis*)0 << new QChartAxis() << new QChartAxis();
227 QTest::newRow("1 defualt, 2 optional") << (QChartAxis*)0 << new QChartAxis() << new QChartAxis();
230 QTest::newRow("3 optional") << new QChartAxis() << new QChartAxis() << new QChartAxis();
228 QTest::newRow("3 optional") << new QChartAxis() << new QChartAxis() << new QChartAxis();
231 }
229 }
232
230
233 void tst_ChartDataSet::axisY()
231 void tst_ChartDataSet::axisY()
234 {
232 {
235 QFETCH(QChartAxis*, axis0);
233 QFETCH(QChartAxis*, axis0);
236 QFETCH(QChartAxis*, axis1);
234 QFETCH(QChartAxis*, axis1);
237 QFETCH(QChartAxis*, axis2);
235 QFETCH(QChartAxis*, axis2);
238
236
239 ChartDataSet dataSet;
237 ChartDataSet dataSet;
240
238
241 QChartAxis* defaultAxisY = dataSet.axisY();
239 QChartAxis* defaultAxisY = dataSet.axisY();
242
240
243 QVERIFY2(defaultAxisY, "Missing axisY.");
241 QVERIFY2(defaultAxisY, "Missing axisY.");
244
242
245 QLineSeries* series0 = new QLineSeries();
243 QLineSeries* series0 = new QLineSeries();
246 dataSet.addSeries(series0,axis0);
244 dataSet.addSeries(series0,axis0);
247
245
248 QLineSeries* series1 = new QLineSeries();
246 QLineSeries* series1 = new QLineSeries();
249 dataSet.addSeries(series1,axis1);
247 dataSet.addSeries(series1,axis1);
250
248
251 QLineSeries* series2 = new QLineSeries();
249 QLineSeries* series2 = new QLineSeries();
252 dataSet.addSeries(series2,axis2);
250 dataSet.addSeries(series2,axis2);
253
251
254 if(!axis0) axis0=defaultAxisY ;
252 if(!axis0) axis0=defaultAxisY ;
255 if(!axis1) axis1=defaultAxisY ;
253 if(!axis1) axis1=defaultAxisY ;
256 if(!axis2) axis2=defaultAxisY ;
254 if(!axis2) axis2=defaultAxisY ;
257
255
258 QVERIFY(dataSet.axisY(series0) == axis0);
256 QVERIFY(dataSet.axisY(series0) == axis0);
259 QVERIFY(dataSet.axisY(series1) == axis1);
257 QVERIFY(dataSet.axisY(series1) == axis1);
260 QVERIFY(dataSet.axisY(series2) == axis2);
258 QVERIFY(dataSet.axisY(series2) == axis2);
261
259
262 }
260 }
263
261
264 void tst_ChartDataSet::seriesCount_data()
265 {
266 addSeries_data();
267 }
268
269 void tst_ChartDataSet::seriesCount()
270 {
271 QFETCH(QLineSeries*, series0);
272 QFETCH(QChartAxis*, axis0);
273 QFETCH(QLineSeries*, series1);
274 QFETCH(QChartAxis*, axis1);
275 QFETCH(QLineSeries*, series2);
276 QFETCH(QChartAxis*, axis2);
277 QFETCH(int, axisCount);
278 Q_UNUSED(axisCount);
279
280 ChartDataSet dataSet;
281
282 dataSet.addSeries(series0, axis0);
283 dataSet.addSeries(series1, axis1);
284 dataSet.addSeries(series2, axis2);
285
286 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
287 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
288 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
289 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
290
291 QCOMPARE(dataSet.seriesCount(series0->type()),3);
292 QCOMPARE(spy0.count(), 0);
293 QCOMPARE(spy1.count(), 0);
294 QCOMPARE(spy2.count(), 0);
295 QCOMPARE(spy3.count(), 0);
296 }
297
298 void tst_ChartDataSet::seriesIndex_data()
262 void tst_ChartDataSet::seriesIndex_data()
299 {
263 {
300 addSeries_data();
264 addSeries_data();
301 }
265 }
302
266
303 void tst_ChartDataSet::seriesIndex()
267 void tst_ChartDataSet::seriesIndex()
304 {
268 {
305 QFETCH(QLineSeries*, series0);
269 QFETCH(QLineSeries*, series0);
306 QFETCH(QChartAxis*, axis0);
270 QFETCH(QChartAxis*, axis0);
307 QFETCH(QLineSeries*, series1);
271 QFETCH(QLineSeries*, series1);
308 QFETCH(QChartAxis*, axis1);
272 QFETCH(QChartAxis*, axis1);
309 QFETCH(QLineSeries*, series2);
273 QFETCH(QLineSeries*, series2);
310 QFETCH(QChartAxis*, axis2);
274 QFETCH(QChartAxis*, axis2);
311 QFETCH(int, axisCount);
275 QFETCH(int, axisCount);
312 Q_UNUSED(axisCount);
276 Q_UNUSED(axisCount);
313
277
314 ChartDataSet dataSet;
278 ChartDataSet dataSet;
315
279
316 dataSet.addSeries(series0, axis0);
280 dataSet.addSeries(series0, axis0);
317 dataSet.addSeries(series1, axis1);
281 dataSet.addSeries(series1, axis1);
318 dataSet.addSeries(series2, axis2);
282 dataSet.addSeries(series2, axis2);
319
283
320 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
284 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
321 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
285 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
322 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
286 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
323 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
287 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
324
288
325 int i0 = dataSet.seriesIndex(series0);
289 int i0 = dataSet.seriesIndex(series0);
326 int i1 = dataSet.seriesIndex(series1);
290 int i1 = dataSet.seriesIndex(series1);
327 int i2 = dataSet.seriesIndex(series2);
291 int i2 = dataSet.seriesIndex(series2);
328
292
329 QVERIFY(i1!=i2);
293 QVERIFY(i1!=i2);
330 QVERIFY(i0!=i2);
294 QVERIFY(i0!=i2);
331 QVERIFY(i0!=i1);
295 QVERIFY(i0!=i1);
332
296
333 QCOMPARE(spy0.count(), 0);
297 QCOMPARE(spy0.count(), 0);
334 QCOMPARE(spy1.count(), 0);
298 QCOMPARE(spy1.count(), 0);
335 QCOMPARE(spy2.count(), 0);
299 QCOMPARE(spy2.count(), 0);
336 QCOMPARE(spy3.count(), 0);
300 QCOMPARE(spy3.count(), 0);
337 }
301 }
338
302
339 void tst_ChartDataSet::domain_data()
303 void tst_ChartDataSet::domain_data()
340 {
304 {
341 addSeries_data();
305 addSeries_data();
342 }
306 }
343
307
344 void tst_ChartDataSet::domain()
308 void tst_ChartDataSet::domain()
345 {
309 {
346 QFETCH(QLineSeries*, series0);
310 QFETCH(QLineSeries*, series0);
347 QFETCH(QChartAxis*, axis0);
311 QFETCH(QChartAxis*, axis0);
348 QFETCH(QLineSeries*, series1);
312 QFETCH(QLineSeries*, series1);
349 QFETCH(QChartAxis*, axis1);
313 QFETCH(QChartAxis*, axis1);
350 QFETCH(QLineSeries*, series2);
314 QFETCH(QLineSeries*, series2);
351 QFETCH(QChartAxis*, axis2);
315 QFETCH(QChartAxis*, axis2);
352 QFETCH(int, axisCount);
316 QFETCH(int, axisCount);
353 Q_UNUSED(axisCount);
317 Q_UNUSED(axisCount);
354
318
355 ChartDataSet dataSet;
319 ChartDataSet dataSet;
356
320
357 dataSet.addSeries(series0, axis0);
321 dataSet.addSeries(series0, axis0);
358 dataSet.addSeries(series1, axis1);
322 dataSet.addSeries(series1, axis1);
359 dataSet.addSeries(series2, axis2);
323 dataSet.addSeries(series2, axis2);
360
324
361 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
325 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
362 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
326 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
363 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
327 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
364 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
328 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
365
329
366 QVERIFY(dataSet.domain(axis0)==dataSet.domain(series0));
330 QVERIFY(dataSet.domain(axis0)==dataSet.domain(series0));
367 QVERIFY(dataSet.domain(axis1)==dataSet.domain(series1));
331 QVERIFY(dataSet.domain(axis1)==dataSet.domain(series1));
368 QVERIFY(dataSet.domain(axis2)==dataSet.domain(series2));
332 QVERIFY(dataSet.domain(axis2)==dataSet.domain(series2));
369 QCOMPARE(spy0.count(), 0);
333 QCOMPARE(spy0.count(), 0);
370 QCOMPARE(spy1.count(), 0);
334 QCOMPARE(spy1.count(), 0);
371 QCOMPARE(spy2.count(), 0);
335 QCOMPARE(spy2.count(), 0);
372 QCOMPARE(spy3.count(), 0);
336 QCOMPARE(spy3.count(), 0);
373 }
337 }
374
338
375 void tst_ChartDataSet::zoomInDomain_data()
339 void tst_ChartDataSet::zoomInDomain_data()
376 {
340 {
377 addSeries_data();
341 addSeries_data();
378 }
342 }
379
343
380 void tst_ChartDataSet::zoomInDomain()
344 void tst_ChartDataSet::zoomInDomain()
381 {
345 {
382 QFETCH(QLineSeries*, series0);
346 QFETCH(QLineSeries*, series0);
383 QFETCH(QChartAxis*, axis0);
347 QFETCH(QChartAxis*, axis0);
384 QFETCH(QLineSeries*, series1);
348 QFETCH(QLineSeries*, series1);
385 QFETCH(QChartAxis*, axis1);
349 QFETCH(QChartAxis*, axis1);
386 QFETCH(QLineSeries*, series2);
350 QFETCH(QLineSeries*, series2);
387 QFETCH(QChartAxis*, axis2);
351 QFETCH(QChartAxis*, axis2);
388 QFETCH(int, axisCount);
352 QFETCH(int, axisCount);
389
353
390 Q_UNUSED(axisCount);
354 Q_UNUSED(axisCount);
391 ChartDataSet dataSet;
355 ChartDataSet dataSet;
392
356
393 dataSet.addSeries(series0, axis0);
357 dataSet.addSeries(series0, axis0);
394 dataSet.addSeries(series1, axis1);
358 dataSet.addSeries(series1, axis1);
395 dataSet.addSeries(series2, axis2);
359 dataSet.addSeries(series2, axis2);
396
360
397 Domain* domain0 = dataSet.domain(series0);
361 Domain* domain0 = dataSet.domain(series0);
398 Domain* domain1 = dataSet.domain(series1);
362 Domain* domain1 = dataSet.domain(series1);
399 Domain* domain2 = dataSet.domain(series2);
363 Domain* domain2 = dataSet.domain(series2);
400
364
401 QSignalSpy spy0(domain0, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
365 QSignalSpy spy0(domain0, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
402 QSignalSpy spy1(domain1, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
366 QSignalSpy spy1(domain1, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
403 QSignalSpy spy2(domain2, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
367 QSignalSpy spy2(domain2, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
404
368
405 dataSet.zoomInDomain(QRect(0,0,100,100),QSize(1000,1000));
369 dataSet.zoomInDomain(QRect(0,0,100,100),QSize(1000,1000));
406
370
407 QCOMPARE(spy0.count(), 1);
371 QCOMPARE(spy0.count(), 1);
408 QCOMPARE(spy1.count(), 1);
372 QCOMPARE(spy1.count(), 1);
409 QCOMPARE(spy2.count(), 1);
373 QCOMPARE(spy2.count(), 1);
410 }
374 }
411
375
412 void tst_ChartDataSet::zoomOutDomain_data()
376 void tst_ChartDataSet::zoomOutDomain_data()
413 {
377 {
414 addSeries_data();
378 addSeries_data();
415 }
379 }
416
380
417 void tst_ChartDataSet::zoomOutDomain()
381 void tst_ChartDataSet::zoomOutDomain()
418 {
382 {
419 QFETCH(QLineSeries*, series0);
383 QFETCH(QLineSeries*, series0);
420 QFETCH(QChartAxis*, axis0);
384 QFETCH(QChartAxis*, axis0);
421 QFETCH(QLineSeries*, series1);
385 QFETCH(QLineSeries*, series1);
422 QFETCH(QChartAxis*, axis1);
386 QFETCH(QChartAxis*, axis1);
423 QFETCH(QLineSeries*, series2);
387 QFETCH(QLineSeries*, series2);
424 QFETCH(QChartAxis*, axis2);
388 QFETCH(QChartAxis*, axis2);
425 QFETCH(int, axisCount);
389 QFETCH(int, axisCount);
426
390
427 Q_UNUSED(axisCount);
391 Q_UNUSED(axisCount);
428
392
429 ChartDataSet dataSet;
393 ChartDataSet dataSet;
430
394
431 dataSet.addSeries(series0, axis0);
395 dataSet.addSeries(series0, axis0);
432 dataSet.addSeries(series1, axis1);
396 dataSet.addSeries(series1, axis1);
433 dataSet.addSeries(series2, axis2);
397 dataSet.addSeries(series2, axis2);
434
398
435 Domain* domain0 = dataSet.domain(series0);
399 Domain* domain0 = dataSet.domain(series0);
436 Domain* domain1 = dataSet.domain(series1);
400 Domain* domain1 = dataSet.domain(series1);
437 Domain* domain2 = dataSet.domain(series2);
401 Domain* domain2 = dataSet.domain(series2);
438
402
439 QSignalSpy spy0(domain0, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
403 QSignalSpy spy0(domain0, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
440 QSignalSpy spy1(domain1, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
404 QSignalSpy spy1(domain1, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
441 QSignalSpy spy2(domain2, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
405 QSignalSpy spy2(domain2, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
442
406
443 dataSet.zoomOutDomain(QRect(0,0,100,100),QSize(1000,1000));
407 dataSet.zoomOutDomain(QRect(0,0,100,100),QSize(1000,1000));
444
408
445 QCOMPARE(spy0.count(), 1);
409 QCOMPARE(spy0.count(), 1);
446 QCOMPARE(spy1.count(), 1);
410 QCOMPARE(spy1.count(), 1);
447 QCOMPARE(spy2.count(), 1);
411 QCOMPARE(spy2.count(), 1);
448 }
412 }
449
413
450 void tst_ChartDataSet::scrollDomain_data()
414 void tst_ChartDataSet::scrollDomain_data()
451 {
415 {
452 addSeries_data();
416 addSeries_data();
453 }
417 }
454
418
455 void tst_ChartDataSet::scrollDomain()
419 void tst_ChartDataSet::scrollDomain()
456 {
420 {
457 QFETCH(QLineSeries*, series0);
421 QFETCH(QLineSeries*, series0);
458 QFETCH(QChartAxis*, axis0);
422 QFETCH(QChartAxis*, axis0);
459 QFETCH(QLineSeries*, series1);
423 QFETCH(QLineSeries*, series1);
460 QFETCH(QChartAxis*, axis1);
424 QFETCH(QChartAxis*, axis1);
461 QFETCH(QLineSeries*, series2);
425 QFETCH(QLineSeries*, series2);
462 QFETCH(QChartAxis*, axis2);
426 QFETCH(QChartAxis*, axis2);
463 QFETCH(int, axisCount);
427 QFETCH(int, axisCount);
464
428
465 Q_UNUSED(axisCount);
429 Q_UNUSED(axisCount);
466
430
467 ChartDataSet dataSet;
431 ChartDataSet dataSet;
468
432
469 dataSet.addSeries(series0, axis0);
433 dataSet.addSeries(series0, axis0);
470 dataSet.addSeries(series1, axis1);
434 dataSet.addSeries(series1, axis1);
471 dataSet.addSeries(series2, axis2);
435 dataSet.addSeries(series2, axis2);
472
436
473 Domain* domain0 = dataSet.domain(series0);
437 Domain* domain0 = dataSet.domain(series0);
474 Domain* domain1 = dataSet.domain(series1);
438 Domain* domain1 = dataSet.domain(series1);
475 Domain* domain2 = dataSet.domain(series2);
439 Domain* domain2 = dataSet.domain(series2);
476
440
477 QSignalSpy spy0(domain0, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
441 QSignalSpy spy0(domain0, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
478 QSignalSpy spy1(domain1, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
442 QSignalSpy spy1(domain1, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
479 QSignalSpy spy2(domain2, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
443 QSignalSpy spy2(domain2, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
480
444
481 dataSet.scrollDomain(10,10,QSize(1000,1000));
445 dataSet.scrollDomain(10,10,QSize(1000,1000));
482
446
483 QCOMPARE(spy0.count(), 1);
447 QCOMPARE(spy0.count(), 1);
484 QCOMPARE(spy1.count(), 1);
448 QCOMPARE(spy1.count(), 1);
485 QCOMPARE(spy2.count(), 1);
449 QCOMPARE(spy2.count(), 1);
486 }
450 }
487
451
488 QTEST_MAIN(tst_ChartDataSet)
452 QTEST_MAIN(tst_ChartDataSet)
489 #include "tst_chartdataset.moc"
453 #include "tst_chartdataset.moc"
490
454
@@ -1,325 +1,328
1 #include "mainwidget.h"
1 #include "mainwidget.h"
2 #include "dataseriedialog.h"
2 #include "dataseriedialog.h"
3 #include "qchartview.h"
3 #include "qchartview.h"
4 #include "qpieseries.h"
4 #include "qpieseries.h"
5 #include "qscatterseries.h"
5 #include "qscatterseries.h"
6 #include "qlineseries.h"
6 #include "qlineseries.h"
7 #include <qareaseries.h>
7 #include <qareaseries.h>
8 #include <qsplineseries.h>
8 #include <qsplineseries.h>
9 #include <qbarset.h>
9 #include <qbarset.h>
10 #include <qbarseries.h>
10 #include <qbarseries.h>
11 #include <qstackedbarseries.h>
11 #include <qstackedbarseries.h>
12 #include <qpercentbarseries.h>
12 #include <qpercentbarseries.h>
13 #include <QPushButton>
13 #include <QPushButton>
14 #include <QComboBox>
14 #include <QComboBox>
15 #include <QSpinBox>
15 #include <QSpinBox>
16 #include <QCheckBox>
16 #include <QCheckBox>
17 #include <QGridLayout>
17 #include <QGridLayout>
18 #include <QHBoxLayout>
18 #include <QHBoxLayout>
19 #include <QLabel>
19 #include <QLabel>
20 #include <QSpacerItem>
20 #include <QSpacerItem>
21 #include <QMessageBox>
21 #include <QMessageBox>
22 #include <cmath>
22 #include <cmath>
23 #include <QDebug>
23 #include <QDebug>
24 #include <QStandardItemModel>
24 #include <QStandardItemModel>
25
25
26
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
28
29 MainWidget::MainWidget(QWidget *parent) :
29 MainWidget::MainWidget(QWidget *parent) :
30 QWidget(parent),
30 QWidget(parent),
31 m_addSerieDialog(0),
31 m_addSerieDialog(0),
32 m_chart(0)
32 m_chart(0)
33 {
33 {
34 m_chart = new QChart();
34 m_chart = new QChart();
35
35
36 // Grid layout for the controls for configuring the chart widget
36 // Grid layout for the controls for configuring the chart widget
37 QGridLayout *grid = new QGridLayout();
37 QGridLayout *grid = new QGridLayout();
38 QPushButton *addSeriesButton = new QPushButton("Add series");
38 QPushButton *addSeriesButton = new QPushButton("Add series");
39 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
39 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
40 grid->addWidget(addSeriesButton, 0, 1);
40 grid->addWidget(addSeriesButton, 0, 1);
41 initBackroundCombo(grid);
41 initBackroundCombo(grid);
42 initScaleControls(grid);
42 initScaleControls(grid);
43 initThemeCombo(grid);
43 initThemeCombo(grid);
44 initCheckboxes(grid);
44 initCheckboxes(grid);
45
45
46 // add row with empty label to make all the other rows static
46 // add row with empty label to make all the other rows static
47 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
47 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
48 grid->setRowStretch(grid->rowCount() - 1, 1);
48 grid->setRowStretch(grid->rowCount() - 1, 1);
49
49
50 // Create chart view with the chart
50 // Create chart view with the chart
51 m_chartView = new QChartView(m_chart, this);
51 m_chartView = new QChartView(m_chart, this);
52 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
52 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
53
53
54 // Another grid layout as a main layout
54 // Another grid layout as a main layout
55 QGridLayout *mainLayout = new QGridLayout();
55 QGridLayout *mainLayout = new QGridLayout();
56 mainLayout->addLayout(grid, 0, 0);
56 mainLayout->addLayout(grid, 0, 0);
57 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
57 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
58 setLayout(mainLayout);
58 setLayout(mainLayout);
59 }
59 }
60
60
61 // Combo box for selecting the chart's background
61 // Combo box for selecting the chart's background
62 void MainWidget::initBackroundCombo(QGridLayout *grid)
62 void MainWidget::initBackroundCombo(QGridLayout *grid)
63 {
63 {
64 QComboBox *backgroundCombo = new QComboBox(this);
64 QComboBox *backgroundCombo = new QComboBox(this);
65 backgroundCombo->addItem("Color");
65 backgroundCombo->addItem("Color");
66 backgroundCombo->addItem("Gradient");
66 backgroundCombo->addItem("Gradient");
67 backgroundCombo->addItem("Image");
67 backgroundCombo->addItem("Image");
68 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
68 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
69 this, SLOT(backgroundChanged(int)));
69 this, SLOT(backgroundChanged(int)));
70
70
71 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
71 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
72 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
72 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
73 }
73 }
74
74
75 // Scale related controls (auto-scale vs. manual min-max values)
75 // Scale related controls (auto-scale vs. manual min-max values)
76 void MainWidget::initScaleControls(QGridLayout *grid)
76 void MainWidget::initScaleControls(QGridLayout *grid)
77 {
77 {
78 m_autoScaleCheck = new QCheckBox("Automatic scaling");
78 m_autoScaleCheck = new QCheckBox("Automatic scaling");
79 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
79 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
80 // Allow setting also non-sense values (like -2147483648 and 2147483647)
80 // Allow setting also non-sense values (like -2147483648 and 2147483647)
81 m_xMinSpin = new QSpinBox();
81 m_xMinSpin = new QSpinBox();
82 m_xMinSpin->setMinimum(INT_MIN);
82 m_xMinSpin->setMinimum(INT_MIN);
83 m_xMinSpin->setMaximum(INT_MAX);
83 m_xMinSpin->setMaximum(INT_MAX);
84 m_xMinSpin->setValue(0);
84 m_xMinSpin->setValue(0);
85 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
85 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
86 m_xMaxSpin = new QSpinBox();
86 m_xMaxSpin = new QSpinBox();
87 m_xMaxSpin->setMinimum(INT_MIN);
87 m_xMaxSpin->setMinimum(INT_MIN);
88 m_xMaxSpin->setMaximum(INT_MAX);
88 m_xMaxSpin->setMaximum(INT_MAX);
89 m_xMaxSpin->setValue(10);
89 m_xMaxSpin->setValue(10);
90 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
90 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
91 m_yMinSpin = new QSpinBox();
91 m_yMinSpin = new QSpinBox();
92 m_yMinSpin->setMinimum(INT_MIN);
92 m_yMinSpin->setMinimum(INT_MIN);
93 m_yMinSpin->setMaximum(INT_MAX);
93 m_yMinSpin->setMaximum(INT_MAX);
94 m_yMinSpin->setValue(0);
94 m_yMinSpin->setValue(0);
95 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
95 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
96 m_yMaxSpin = new QSpinBox();
96 m_yMaxSpin = new QSpinBox();
97 m_yMaxSpin->setMinimum(INT_MIN);
97 m_yMaxSpin->setMinimum(INT_MIN);
98 m_yMaxSpin->setMaximum(INT_MAX);
98 m_yMaxSpin->setMaximum(INT_MAX);
99 m_yMaxSpin->setValue(10);
99 m_yMaxSpin->setValue(10);
100 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
100 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
101
101
102 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
102 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
103 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
103 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
104 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
104 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
105 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
105 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
106 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
106 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
107 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
107 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
108 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
108 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
109 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
109 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
110 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
110 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
111
111
112 m_autoScaleCheck->setChecked(true);
112 m_autoScaleCheck->setChecked(true);
113 }
113 }
114
114
115 // Combo box for selecting theme
115 // Combo box for selecting theme
116 void MainWidget::initThemeCombo(QGridLayout *grid)
116 void MainWidget::initThemeCombo(QGridLayout *grid)
117 {
117 {
118 QComboBox *chartTheme = new QComboBox();
118 QComboBox *chartTheme = new QComboBox();
119 chartTheme->addItem("Default");
119 chartTheme->addItem("Default");
120 chartTheme->addItem("Light");
120 chartTheme->addItem("Light");
121 chartTheme->addItem("Blue Cerulean");
121 chartTheme->addItem("Blue Cerulean");
122 chartTheme->addItem("Dark");
122 chartTheme->addItem("Dark");
123 chartTheme->addItem("Brown Sand");
123 chartTheme->addItem("Brown Sand");
124 chartTheme->addItem("Blue NCS");
124 chartTheme->addItem("Blue NCS");
125 chartTheme->addItem("High Contrast");
125 chartTheme->addItem("High Contrast");
126 chartTheme->addItem("Blue Icy");
126 chartTheme->addItem("Blue Icy");
127 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
127 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
128 this, SLOT(changeChartTheme(int)));
128 this, SLOT(changeChartTheme(int)));
129 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
129 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
130 grid->addWidget(chartTheme, 8, 1);
130 grid->addWidget(chartTheme, 8, 1);
131 }
131 }
132
132
133 // Different check boxes for customizing chart
133 // Different check boxes for customizing chart
134 void MainWidget::initCheckboxes(QGridLayout *grid)
134 void MainWidget::initCheckboxes(QGridLayout *grid)
135 {
135 {
136 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
136 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
137 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
137 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
138 // connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
138 // connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
139 zoomCheckBox->setChecked(true);
139 zoomCheckBox->setChecked(true);
140 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
140 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
141
141
142 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
142 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
143 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
143 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
144 aliasCheckBox->setChecked(false);
144 aliasCheckBox->setChecked(false);
145 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
145 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
146 }
146 }
147
147
148 void MainWidget::antiAliasToggled(bool enabled)
148 void MainWidget::antiAliasToggled(bool enabled)
149 {
149 {
150 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
150 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
151 }
151 }
152
152
153 void MainWidget::addSeries()
153 void MainWidget::addSeries()
154 {
154 {
155 if (!m_addSerieDialog) {
155 if (!m_addSerieDialog) {
156 m_addSerieDialog = new DataSerieDialog(this);
156 m_addSerieDialog = new DataSerieDialog(this);
157 connect(m_addSerieDialog, SIGNAL(accepted(QString, int, int, QString, bool)),
157 connect(m_addSerieDialog, SIGNAL(accepted(QString, int, int, QString, bool)),
158 this, SLOT(addSeries(QString, int, int, QString, bool)));
158 this, SLOT(addSeries(QString, int, int, QString, bool)));
159 }
159 }
160 m_addSerieDialog->exec();
160 m_addSerieDialog->exec();
161 }
161 }
162
162
163 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
163 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
164 {
164 {
165 // TODO: dataCharacteristics
165 // TODO: dataCharacteristics
166 QList<RealList> testData;
166 QList<RealList> testData;
167 for (int j(0); j < columnCount; j++) {
167 for (int j(0); j < columnCount; j++) {
168 QList <qreal> newColumn;
168 QList <qreal> newColumn;
169 for (int i(0); i < rowCount; i++) {
169 for (int i(0); i < rowCount; i++) {
170 if (dataCharacteristics == "Sin") {
170 if (dataCharacteristics == "Sin") {
171 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
171 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
172 } else if (dataCharacteristics == "Sin + random") {
172 } else if (dataCharacteristics == "Sin + random") {
173 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
173 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
174 } else if (dataCharacteristics == "Random") {
174 } else if (dataCharacteristics == "Random") {
175 newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX);
175 newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX);
176 } else if (dataCharacteristics == "Linear") {
176 } else if (dataCharacteristics == "Linear") {
177 //newColumn.append(i * (j + 1.0));
177 //newColumn.append(i * (j + 1.0));
178 // TODO: temporary hack to make pie work; prevent zero values:
178 // TODO: temporary hack to make pie work; prevent zero values:
179 newColumn.append(i * (j + 1.0) + 0.1);
179 newColumn.append(i * (j + 1.0) + 0.1);
180 } else { // "constant"
180 } else { // "constant"
181 newColumn.append((j + 1.0));
181 newColumn.append((j + 1.0));
182 }
182 }
183 }
183 }
184 testData.append(newColumn);
184 testData.append(newColumn);
185 }
185 }
186 return testData;
186 return testData;
187 }
187 }
188
188
189 QStringList MainWidget::generateLabels(int count)
189 QStringList MainWidget::generateLabels(int count)
190 {
190 {
191 QStringList result;
191 QStringList result;
192 for (int i(0); i < count; i++)
192 for (int i(0); i < count; i++)
193 result.append("label" + QString::number(i));
193 result.append("label" + QString::number(i));
194 return result;
194 return result;
195 }
195 }
196
196
197 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
197 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
198 {
198 {
199 qDebug() << "addSeries: " << seriesName
199 qDebug() << "addSeries: " << seriesName
200 << " columnCount: " << columnCount
200 << " columnCount: " << columnCount
201 << " rowCount: " << rowCount
201 << " rowCount: " << rowCount
202 << " dataCharacteristics: " << dataCharacteristics
202 << " dataCharacteristics: " << dataCharacteristics
203 << " labels enabled: " << labelsEnabled;
203 << " labels enabled: " << labelsEnabled;
204 m_defaultSeriesName = seriesName;
204 m_defaultSeriesName = seriesName;
205
205
206 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
206 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
207
207
208 // Line series and scatter series use similar data
208 // Line series and scatter series use similar data
209 if (seriesName == "Line") {
209 if (seriesName == "Line") {
210 for (int j(0); j < data.count(); j ++) {
210 for (int j(0); j < data.count(); j ++) {
211 QList<qreal> column = data.at(j);
211 QList<qreal> column = data.at(j);
212 QLineSeries *series = new QLineSeries();
212 QLineSeries *series = new QLineSeries();
213 series->setName("line" + QString::number(j));
213 for (int i(0); i < column.count(); i++)
214 for (int i(0); i < column.count(); i++)
214 series->append(i, column.at(i));
215 series->append(i, column.at(i));
215 m_chart->addSeries(series);
216 m_chart->addSeries(series);
216 }
217 }
217 } else if (seriesName == "Area") {
218 } else if (seriesName == "Area") {
218 // TODO: lower series for the area?
219 // TODO: lower series for the area?
219 for (int j(0); j < data.count(); j ++) {
220 for (int j(0); j < data.count(); j ++) {
220 QList<qreal> column = data.at(j);
221 QList<qreal> column = data.at(j);
221 QLineSeries *lineSeries = new QLineSeries();
222 QLineSeries *lineSeries = new QLineSeries();
222 for (int i(0); i < column.count(); i++)
223 for (int i(0); i < column.count(); i++)
223 lineSeries->append(i, column.at(i));
224 lineSeries->append(i, column.at(i));
224 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
225 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
226 areaSeries->setName("area" + QString::number(j));
225 m_chart->addSeries(areaSeries);
227 m_chart->addSeries(areaSeries);
226 }
228 }
227 } else if (seriesName == "Scatter") {
229 } else if (seriesName == "Scatter") {
228 for (int j(0); j < data.count(); j++) {
230 for (int j(0); j < data.count(); j++) {
229 QList<qreal> column = data.at(j);
231 QList<qreal> column = data.at(j);
230 QScatterSeries *series = new QScatterSeries();
232 QScatterSeries *series = new QScatterSeries();
233 series->setName("scatter" + QString::number(j));
231 for (int i(0); i < column.count(); i++)
234 for (int i(0); i < column.count(); i++)
232 series->append(i, column.at(i));
235 series->append(i, column.at(i));
233 m_chart->addSeries(series);
236 m_chart->addSeries(series);
234 }
237 }
235 } else if (seriesName == "Pie") {
238 } else if (seriesName == "Pie") {
236 QStringList labels = generateLabels(rowCount);
239 QStringList labels = generateLabels(rowCount);
237 for (int j(0); j < data.count(); j++) {
240 for (int j(0); j < data.count(); j++) {
238 QPieSeries *series = new QPieSeries();
241 QPieSeries *series = new QPieSeries();
239 QList<qreal> column = data.at(j);
242 QList<qreal> column = data.at(j);
240 for (int i(0); i < column.count(); i++)
243 for (int i(0); i < column.count(); i++)
241 series->append(column.at(i), labels.at(i));
244 series->append(column.at(i), labels.at(i));
242 m_chart->addSeries(series);
245 m_chart->addSeries(series);
243 }
246 }
244 } else if (seriesName == "Bar"
247 } else if (seriesName == "Bar"
245 || seriesName == "Stacked bar"
248 || seriesName == "Stacked bar"
246 || seriesName == "Percent bar") {
249 || seriesName == "Percent bar") {
247 QStringList category;
250 QStringList category;
248 QStringList labels = generateLabels(rowCount);
251 QStringList labels = generateLabels(rowCount);
249 foreach(QString label, labels)
252 foreach(QString label, labels)
250 category << label;
253 category << label;
251 QBarSeries* series = 0;
254 QBarSeries* series = 0;
252 if (seriesName == "Bar")
255 if (seriesName == "Bar")
253 series = new QBarSeries(category, this);
256 series = new QBarSeries(category, this);
254 else if (seriesName == "Stacked bar")
257 else if (seriesName == "Stacked bar")
255 series = new QStackedBarSeries(category, this);
258 series = new QStackedBarSeries(category, this);
256 else
259 else
257 series = new QPercentBarSeries(category, this);
260 series = new QPercentBarSeries(category, this);
258
261
259 for (int j(0); j < data.count(); j++) {
262 for (int j(0); j < data.count(); j++) {
260 QList<qreal> column = data.at(j);
263 QList<qreal> column = data.at(j);
261 QBarSet *set = new QBarSet("set" + QString::number(j));
264 QBarSet *set = new QBarSet("set" + QString::number(j));
262 for (int i(0); i < column.count(); i++)
265 for (int i(0); i < column.count(); i++)
263 *set << column.at(i);
266 *set << column.at(i);
264 series->appendBarSet(set);
267 series->appendBarSet(set);
265 }
268 }
266
269
267 // TODO: new implementation of setFloatingValuesEnabled with signals
270 // TODO: new implementation of setFloatingValuesEnabled with signals
268 //series->setFloatingValuesEnabled(true);
271 //series->setFloatingValuesEnabled(true);
269 series->setToolTipEnabled(true);
272 series->setToolTipEnabled(true);
270 m_chart->addSeries(series);
273 m_chart->addSeries(series);
271 } else if (seriesName == "Spline") {
274 } else if (seriesName == "Spline") {
272 for (int j(0); j < data.count(); j ++) {
275 for (int j(0); j < data.count(); j ++) {
273 QList<qreal> column = data.at(j);
276 QList<qreal> column = data.at(j);
274 QSplineSeries *series = new QSplineSeries();
277 QSplineSeries *series = new QSplineSeries();
275 for (int i(0); i < column.count(); i++)
278 for (int i(0); i < column.count(); i++)
276 series->append(i, column.at(i));
279 series->append(i, column.at(i));
277 m_chart->addSeries(series);
280 m_chart->addSeries(series);
278 }
281 }
279 }
282 }
280 }
283 }
281
284
282 void MainWidget::backgroundChanged(int itemIndex)
285 void MainWidget::backgroundChanged(int itemIndex)
283 {
286 {
284 qDebug() << "backgroundChanged: " << itemIndex;
287 qDebug() << "backgroundChanged: " << itemIndex;
285 }
288 }
286
289
287 void MainWidget::autoScaleChanged(int value)
290 void MainWidget::autoScaleChanged(int value)
288 {
291 {
289 if (value) {
292 if (value) {
290 // TODO: enable auto scaling
293 // TODO: enable auto scaling
291 } else {
294 } else {
292 // TODO: set scaling manually (and disable auto scaling)
295 // TODO: set scaling manually (and disable auto scaling)
293 }
296 }
294
297
295 m_xMinSpin->setEnabled(!value);
298 m_xMinSpin->setEnabled(!value);
296 m_xMaxSpin->setEnabled(!value);
299 m_xMaxSpin->setEnabled(!value);
297 m_yMinSpin->setEnabled(!value);
300 m_yMinSpin->setEnabled(!value);
298 m_yMaxSpin->setEnabled(!value);
301 m_yMaxSpin->setEnabled(!value);
299 }
302 }
300
303
301 void MainWidget::xMinChanged(int value)
304 void MainWidget::xMinChanged(int value)
302 {
305 {
303 qDebug() << "xMinChanged: " << value;
306 qDebug() << "xMinChanged: " << value;
304 }
307 }
305
308
306 void MainWidget::xMaxChanged(int value)
309 void MainWidget::xMaxChanged(int value)
307 {
310 {
308 qDebug() << "xMaxChanged: " << value;
311 qDebug() << "xMaxChanged: " << value;
309 }
312 }
310
313
311 void MainWidget::yMinChanged(int value)
314 void MainWidget::yMinChanged(int value)
312 {
315 {
313 qDebug() << "yMinChanged: " << value;
316 qDebug() << "yMinChanged: " << value;
314 }
317 }
315
318
316 void MainWidget::yMaxChanged(int value)
319 void MainWidget::yMaxChanged(int value)
317 {
320 {
318 qDebug() << "yMaxChanged: " << value;
321 qDebug() << "yMaxChanged: " << value;
319 }
322 }
320
323
321 void MainWidget::changeChartTheme(int themeIndex)
324 void MainWidget::changeChartTheme(int themeIndex)
322 {
325 {
323 qDebug() << "changeChartTheme: " << themeIndex;
326 qDebug() << "changeChartTheme: " << themeIndex;
324 m_chart->setTheme((QChart::ChartTheme) themeIndex);
327 m_chart->setTheme((QChart::ChartTheme) themeIndex);
325 }
328 }
General Comments 0
You need to be logged in to leave comments. Login now