@@ -1,379 +1,379 | |||
|
1 | 1 | #include "chartdataset_p.h" |
|
2 | 2 | #include "qchartaxis.h" |
|
3 | 3 | //series |
|
4 | 4 | #include "qlineseries.h" |
|
5 | 5 | #include "qbarseries.h" |
|
6 | 6 | #include "qstackedbarseries.h" |
|
7 | 7 | #include "qpercentbarseries.h" |
|
8 | 8 | #include "qpieseries.h" |
|
9 | 9 | #include "qscatterseries.h" |
|
10 | 10 | |
|
11 | 11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | 12 | |
|
13 | 13 | ChartDataSet::ChartDataSet(QObject *parent):QObject(parent), |
|
14 | 14 | m_axisX(new QChartAxis(this)), |
|
15 | 15 | m_axisY(new QChartAxis(this)), |
|
16 | 16 | m_domainIndex(0), |
|
17 | 17 | m_axisXInitialized(false) |
|
18 | 18 | { |
|
19 | 19 | } |
|
20 | 20 | |
|
21 | 21 | ChartDataSet::~ChartDataSet() |
|
22 | 22 | { |
|
23 | 23 | // TODO Auto-generated destructor stub |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | const Domain ChartDataSet::domain(QChartAxis *axisY) const |
|
27 | 27 | { |
|
28 | 28 | int i = m_domainMap.count(axisY); |
|
29 | 29 | if(i == 0){ |
|
30 | 30 | return Domain(); |
|
31 | 31 | } |
|
32 | 32 | i = i - m_domainIndex -1; |
|
33 | 33 | return m_domainMap.values(axisY).at(i); |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY) |
|
37 | 37 | { |
|
38 | 38 | // TODO: we should check the series not already added |
|
39 | 39 | |
|
40 | 40 | series->setParent(this); // take ownership |
|
41 | 41 | clearDomains(); |
|
42 | 42 | |
|
43 | 43 | if(axisY==0) axisY = m_axisY; |
|
44 | 44 | axisY->setParent(this); // take ownership |
|
45 | 45 | |
|
46 | 46 | QList<QSeries*> seriesList = m_seriesMap.values(axisY); |
|
47 | 47 | |
|
48 | 48 | QList<Domain> domainList = m_domainMap.values(axisY); |
|
49 | 49 | |
|
50 | 50 | Q_ASSERT(domainList.size()<=1); |
|
51 | 51 | |
|
52 | 52 | Domain domain; |
|
53 | 53 | |
|
54 | 54 | if(domainList.size()>0) domain = domainList.at(0); |
|
55 | 55 | |
|
56 | 56 | switch(series->type()) |
|
57 | 57 | { |
|
58 | 58 | case QSeries::SeriesTypeLine: { |
|
59 | 59 | |
|
60 | 60 | QLineSeries* xyseries = static_cast<QLineSeries*>(series); |
|
61 | 61 | |
|
62 | 62 | for (int i = 0; i < xyseries->count(); i++) |
|
63 | 63 | { |
|
64 | 64 | qreal x = xyseries->x(i); |
|
65 | 65 | qreal y = xyseries->y(i); |
|
66 | 66 | domain.m_minX = qMin(domain.m_minX,x); |
|
67 | 67 | domain.m_minY = qMin(domain.m_minY,y); |
|
68 | 68 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
69 | 69 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
70 | 70 | } |
|
71 | 71 | break; |
|
72 | 72 | } |
|
73 | 73 | case QSeries::SeriesTypeBar: { |
|
74 | 74 | qDebug() << "QChartSeries::SeriesTypeBar"; |
|
75 | 75 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
76 | 76 | qreal x = barSeries->categoryCount(); |
|
77 | 77 | qreal y = barSeries->max(); |
|
78 | 78 | domain.m_minX = qMin(domain.m_minX,x); |
|
79 | 79 | domain.m_minY = qMin(domain.m_minY,y); |
|
80 | 80 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
81 | 81 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
82 | 82 | break; |
|
83 | 83 | } |
|
84 | 84 | case QSeries::SeriesTypeStackedBar: { |
|
85 | 85 | qDebug() << "QChartSeries::SeriesTypeStackedBar"; |
|
86 | 86 | |
|
87 | 87 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
88 | 88 | qreal x = stackedBarSeries->categoryCount(); |
|
89 | 89 | qreal y = stackedBarSeries->maxCategorySum(); |
|
90 | 90 | domain.m_minX = qMin(domain.m_minX,x); |
|
91 | 91 | domain.m_minY = qMin(domain.m_minY,y); |
|
92 | 92 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
93 | 93 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
94 | 94 | break; |
|
95 | 95 | } |
|
96 | 96 | case QSeries::SeriesTypePercentBar: { |
|
97 | 97 | qDebug() << "QChartSeries::SeriesTypePercentBar"; |
|
98 | 98 | |
|
99 | 99 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
100 | 100 | qreal x = percentBarSeries->categoryCount(); |
|
101 | 101 | domain.m_minX = qMin(domain.m_minX,x); |
|
102 | 102 | domain.m_minY = 0; |
|
103 | 103 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
104 | 104 | domain.m_maxY = 100; |
|
105 | 105 | break; |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | case QSeries::SeriesTypePie: { |
|
109 | 109 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
110 | 110 | // TODO: domain stuff |
|
111 | 111 | break; |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | case QSeries::SeriesTypeScatter: { |
|
115 | 115 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
116 | 116 | Q_ASSERT(scatterSeries); |
|
117 | 117 | foreach (QPointF point, scatterSeries->data()) { |
|
118 | 118 | domain.m_minX = qMin(domain.m_minX, point.x()); |
|
119 | 119 | domain.m_maxX = qMax(domain.m_maxX, point.x()); |
|
120 | 120 | domain.m_minY = qMin(domain.m_minY, point.y()); |
|
121 | 121 | domain.m_maxY = qMax(domain.m_maxY, point.y()); |
|
122 | 122 | } |
|
123 | 123 | break; |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | default: { |
|
127 | 127 | qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; |
|
128 | 128 | return; |
|
129 | 129 | break; |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | } |
|
133 | 133 | |
|
134 | 134 | if(!m_domainMap.contains(axisY)) |
|
135 | 135 | { |
|
136 | 136 | emit axisAdded(axisY); |
|
137 | 137 | QObject::connect(axisY,SIGNAL(rangeChanged(QChartAxis*)),this,SLOT(handleRangeChanged(QChartAxis*))); |
|
138 | 138 | QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*))); |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | if(!m_axisXInitialized) |
|
142 | 142 | { |
|
143 | 143 | emit axisAdded(axisX()); |
|
144 | 144 | QObject::connect(axisX(),SIGNAL(rangeChanged(QChartAxis*)),this,SLOT(handleRangeChanged(QChartAxis*))); |
|
145 | 145 | QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*))); |
|
146 | 146 | m_axisXInitialized=true; |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | m_domainMap.replace(axisY,domain); |
|
150 | 150 | m_seriesMap.insert(axisY,series); |
|
151 | 151 | emit seriesAdded(series); |
|
152 | 152 | setDomain(m_domainIndex); |
|
153 | 153 | |
|
154 | 154 | } |
|
155 | 155 | |
|
156 | 156 | void ChartDataSet::removeSeries(QSeries* series) |
|
157 | 157 | { |
|
158 | 158 | QList<QChartAxis*> keys = m_seriesMap.uniqueKeys(); |
|
159 | 159 | foreach(QChartAxis* axis , keys) { |
|
160 | 160 | if(m_seriesMap.contains(axis,series)){ |
|
161 | 161 | emit seriesRemoved(series); |
|
162 | 162 | m_seriesMap.remove(axis,series); |
|
163 | 163 | //remove axis if no longer there |
|
164 | 164 | if(!m_seriesMap.contains(axis)){ |
|
165 | 165 | emit axisRemoved(axis); |
|
166 | 166 | m_domainMap.remove(axis); |
|
167 | 167 | if(axis != m_axisY) |
|
168 | 168 | delete axis; |
|
169 | 169 | } |
|
170 | 170 | series->setParent(0); |
|
171 | 171 | break; |
|
172 | 172 | } |
|
173 | 173 | } |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | void ChartDataSet::removeAllSeries() |
|
177 | 177 | { |
|
178 | 178 | QList<QChartAxis*> keys = m_seriesMap.uniqueKeys(); |
|
179 | 179 | foreach(QChartAxis* axis , keys) { |
|
180 | 180 | QList<QSeries*> seriesList = m_seriesMap.values(axis); |
|
181 | 181 | for(int i =0 ; i < seriesList.size();i++ ) |
|
182 | 182 | { |
|
183 | 183 | emit seriesRemoved(seriesList.at(i)); |
|
184 | 184 | delete(seriesList.at(i)); |
|
185 | 185 | } |
|
186 | 186 | m_seriesMap.remove(axis); |
|
187 | 187 | m_domainMap.remove(axis); |
|
188 | 188 | emit axisRemoved(axis); |
|
189 | 189 | if(axis != m_axisY) delete axis; |
|
190 | 190 | } |
|
191 | 191 | m_domainIndex=0; |
|
192 | 192 | } |
|
193 | 193 | |
|
194 | 194 | bool ChartDataSet::nextDomain() |
|
195 | 195 | { |
|
196 | 196 | int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1; |
|
197 | 197 | |
|
198 | 198 | if (m_domainIndex < limit) { |
|
199 | 199 | m_domainIndex++; |
|
200 | 200 | setDomain(m_domainIndex); |
|
201 | 201 | return true; |
|
202 | 202 | } |
|
203 | 203 | else { |
|
204 | 204 | return false; |
|
205 | 205 | } |
|
206 | 206 | } |
|
207 | 207 | |
|
208 | 208 | bool ChartDataSet::previousDomain() |
|
209 | 209 | { |
|
210 | 210 | if (m_domainIndex > 0) { |
|
211 | 211 | m_domainIndex--; |
|
212 | 212 | setDomain(m_domainIndex); |
|
213 | 213 | return true; |
|
214 | 214 | } |
|
215 | 215 | else { |
|
216 | 216 | return false; |
|
217 | 217 | } |
|
218 | 218 | } |
|
219 | 219 | |
|
220 | 220 | void ChartDataSet::setDomain(int index) |
|
221 | 221 | { |
|
222 | 222 | QList<QChartAxis*> domainList = m_domainMap.uniqueKeys(); |
|
223 | 223 | |
|
224 | 224 | if(domainList.count()==0) return; |
|
225 | 225 | |
|
226 | 226 | Domain domain; |
|
227 | 227 | |
|
228 | 228 | foreach (QChartAxis* axis , domainList) { |
|
229 | 229 | int i = m_domainMap.count(axis) - index -1; |
|
230 | 230 | Q_ASSERT(i>=0); |
|
231 | 231 | domain = m_domainMap.values(axis).at(i); |
|
232 | 232 | QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY); |
|
233 | 233 | QList<QSeries*> seriesList = m_seriesMap.values(axis); |
|
234 | 234 | foreach(QSeries* series, seriesList) { |
|
235 | 235 | emit seriesDomainChanged(series,domain); |
|
236 | 236 | } |
|
237 |
axis-> |
|
|
237 | axis->updateRange(domain.m_minY,domain.m_maxY); | |
|
238 | 238 | emit axisRangeChanged(axis,labels); |
|
239 | 239 | |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | 242 | QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX); |
|
243 |
axisX()-> |
|
|
243 | axisX()->updateRange(domain.m_minX,domain.m_maxY); | |
|
244 | 244 | emit axisRangeChanged(axisX(),labels); |
|
245 | 245 | } |
|
246 | 246 | |
|
247 | 247 | void ChartDataSet::clearDomains(int toIndex) |
|
248 | 248 | { |
|
249 | 249 | Q_ASSERT(toIndex>=0); |
|
250 | 250 | |
|
251 | 251 | m_domainIndex = toIndex; |
|
252 | 252 | |
|
253 | 253 | QList<QChartAxis*> keys = m_domainMap.uniqueKeys(); |
|
254 | 254 | |
|
255 | 255 | foreach (QChartAxis* key , keys) |
|
256 | 256 | { |
|
257 | 257 | QList<Domain> domains = m_domainMap.values(key); |
|
258 | 258 | m_domainMap.remove(key); |
|
259 | 259 | int i = domains.size() - toIndex - 1; |
|
260 | 260 | while(i--){ |
|
261 | 261 | domains.removeFirst(); |
|
262 | 262 | } |
|
263 | 263 | for(int j=domains.size()-1; j>=0 ;j--) |
|
264 | 264 | m_domainMap.insert(key,domains.at(j)); |
|
265 | 265 | } |
|
266 | 266 | } |
|
267 | 267 | |
|
268 | 268 | void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport) |
|
269 | 269 | { |
|
270 | 270 | Q_ASSERT(rect.isValid()); |
|
271 | 271 | Q_ASSERT(viewport.isValid()); |
|
272 | 272 | |
|
273 | 273 | clearDomains(m_domainIndex); |
|
274 | 274 | |
|
275 | 275 | QList<QChartAxis*> domainList = m_domainMap.uniqueKeys(); |
|
276 | 276 | |
|
277 | 277 | Domain domain; |
|
278 | 278 | |
|
279 | 279 | foreach (QChartAxis* axis , domainList){ |
|
280 | 280 | domain = m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height()); |
|
281 | 281 | m_domainMap.insert(axis,domain); |
|
282 | 282 | } |
|
283 | 283 | |
|
284 | 284 | setDomain(++m_domainIndex); |
|
285 | 285 | } |
|
286 | 286 | |
|
287 | 287 | QChartAxis* ChartDataSet::axisY(QSeries* series) const |
|
288 | 288 | { |
|
289 | 289 | if(series == 0) return m_axisY; |
|
290 | 290 | |
|
291 | 291 | QList<QChartAxis*> keys = m_seriesMap.uniqueKeys(); |
|
292 | 292 | |
|
293 | 293 | foreach(QChartAxis* axis , keys) { |
|
294 | 294 | if(m_seriesMap.contains(axis,series)){ |
|
295 | 295 | return axis; |
|
296 | 296 | } |
|
297 | 297 | } |
|
298 | 298 | return 0; |
|
299 | 299 | } |
|
300 | 300 | |
|
301 | 301 | QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max) |
|
302 | 302 | { |
|
303 | 303 | Q_ASSERT(max>=min); |
|
304 | 304 | |
|
305 | 305 | QStringList labels; |
|
306 | 306 | |
|
307 | 307 | int ticks = axis->ticksCount()-1; |
|
308 | 308 | |
|
309 | 309 | for(int i=0; i<= ticks; i++){ |
|
310 | 310 | qreal value = min + (i * (max - min)/ ticks); |
|
311 | 311 | QString label = axis->axisTickLabel(value); |
|
312 | 312 | if(label.isEmpty()){ |
|
313 | 313 | labels << QString::number(value); |
|
314 | 314 | }else{ |
|
315 | 315 | labels << label; |
|
316 | 316 | } |
|
317 | 317 | } |
|
318 | 318 | return labels; |
|
319 | 319 | } |
|
320 | 320 | |
|
321 | 321 | |
|
322 | 322 | void ChartDataSet::handleRangeChanged(QChartAxis* axis) |
|
323 | 323 | { |
|
324 | 324 | qreal min = axis->min(); |
|
325 | 325 | qreal max = axis->max(); |
|
326 | 326 | |
|
327 | 327 | if(axis==axisX()) { |
|
328 | 328 | |
|
329 | 329 | m_domainIndex=0; |
|
330 | 330 | |
|
331 | 331 | clearDomains(m_domainIndex); |
|
332 | 332 | |
|
333 | 333 | QList<QChartAxis*> domainList = m_domainMap.uniqueKeys(); |
|
334 | 334 | |
|
335 | 335 | foreach (QChartAxis* axis , domainList) { |
|
336 | 336 | |
|
337 | 337 | Q_ASSERT(m_domainMap.values(axis).size()==1); |
|
338 | 338 | |
|
339 | 339 | Domain domain = m_domainMap.value(axis); |
|
340 | 340 | domain.m_minX=min; |
|
341 | 341 | domain.m_maxX=max; |
|
342 | 342 | m_domainMap.replace(axis,domain); |
|
343 | 343 | } |
|
344 | 344 | |
|
345 | 345 | } |
|
346 | 346 | else { |
|
347 | 347 | |
|
348 | 348 | QList<Domain> domains = m_domainMap.values(axis); |
|
349 | 349 | m_domainMap.remove(axis); |
|
350 | 350 | |
|
351 | 351 | for(int i=0;i<domains.size();i++) |
|
352 | 352 | { |
|
353 | 353 | domains[i].m_minY=min; |
|
354 | 354 | domains[i].m_maxY=max; |
|
355 | 355 | } |
|
356 | 356 | |
|
357 | 357 | for(int j=domains.size()-1; j>=0;j--) |
|
358 | 358 | m_domainMap.insert(axis,domains.at(j)); |
|
359 | 359 | } |
|
360 | 360 | |
|
361 | 361 | setDomain(m_domainIndex); |
|
362 | 362 | } |
|
363 | 363 | |
|
364 | 364 | void ChartDataSet::handleTickChanged(QChartAxis* axis) |
|
365 | 365 | { |
|
366 | 366 | if(axis==axisX()){ |
|
367 | 367 | Domain domain = m_domainMap.value(axisY()); |
|
368 | 368 | QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX); |
|
369 | 369 | emit axisRangeChanged(axis,labels); |
|
370 | 370 | }else{ |
|
371 | 371 | Domain domain = m_domainMap.value(axis); |
|
372 | 372 | QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY); |
|
373 | 373 | emit axisRangeChanged(axis,labels); |
|
374 | 374 | } |
|
375 | 375 | } |
|
376 | 376 | |
|
377 | 377 | #include "moc_chartdataset_p.cpp" |
|
378 | 378 | |
|
379 | 379 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now