##// END OF EJS Templates
Removed all the unfinished model functionality from XYSeries and BarSeries
Marek Rosa -
r990:2f31b8379e5f
parent child
Show More
@@ -1,89 +1,89
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativebarseries.h"
22 22 #include "declarativechart.h"
23 23 #include "qchart.h"
24 24 #include "qbarseries.h"
25 25 #include "qbarset.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) :
30 30 QDeclarativeItem(parent)
31 31 {
32 32 setFlag(QGraphicsItem::ItemHasNoContents, false);
33 33 }
34 34
35 35 void DeclarativeBarSeries::componentComplete()
36 36 {
37 37 if (!m_series) {
38 38 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
39 39
40 40 if (declarativeChart) {
41 41 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
42 42 Q_ASSERT(chart);
43 43
44 44 // QStringList categories;
45 45 // categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
46 46 // m_series = new QBarSeries(categories);
47 47 // m_series = new QBarSeries(m_categories);
48 48 m_series = new QBarSeries(m_categories);
49 49
50 50 // TODO: use data from model
51 51 QBarSet *set0 = new QBarSet("Bub");
52 52 QBarSet *set1 = new QBarSet("Bob");
53 53 QBarSet *set2 = new QBarSet("Guybrush");
54 54
55 55 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
56 56 *set1 << 5 << 1 << 2 << 4 << 1 << 7;
57 57 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
58 58
59 59 m_series->appendBarSet(set0);
60 60 m_series->appendBarSet(set1);
61 61 m_series->appendBarSet(set2);
62 62
63 63 chart->addSeries(m_series);
64 64 }
65 65 }
66 66 }
67 67
68 void DeclarativeBarSeries::setBarCategories(QStringList categories)
68 void DeclarativeBarSeries::setBarCategories(QStringList /*categories*/)
69 69 {
70 m_categories = categories;
71 if (m_series) {
72 // Replace categories of the QBarSeries with the new categories
73 for (int i(0); i < m_categories.count(); i++) {
74 if (m_series->categories().at(i) != m_categories.at(i))
75 m_series->insertCategory(m_series->categoryCount(), m_categories.at(i));
76 }
77 while (m_series->categoryCount() > m_categories.count())
78 m_series->removeCategory(m_series->categoryCount() - 1);
79 }
70 // m_categories = categories;
71 // if (m_series) {
72 // // Replace categories of the QBarSeries with the new categories
73 // for (int i(0); i < m_categories.count(); i++) {
74 // if (m_series->categories().at(i) != m_categories.at(i))
75 // m_series->insertCategory(m_series->categoryCount(), m_categories.at(i));
76 // }
77 // while (m_series->categoryCount() > m_categories.count())
78 // m_series->removeCategory(m_series->categoryCount() - 1);
79 // }
80 80 }
81 81
82 82 QStringList DeclarativeBarSeries::barCategories()
83 83 {
84 84 return m_categories;
85 85 }
86 86
87 87 #include "moc_declarativebarseries.cpp"
88 88
89 89 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,527 +1,454
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qbarseries.h"
22 22 #include "qbarseries_p.h"
23 23 #include "qbarset.h"
24 24 #include "qbarset_p.h"
25 25 #include "barchartmodel_p.h"
26 26 #include "domain_p.h"
27 27 #include "legendmarker_p.h"
28 28 #include "chartdataset_p.h"
29 29 #include "charttheme_p.h"
30 30 #include "chartanimator_p.h"
31 31
32 32 #include <QAbstractItemModel>
33 33 #include <QModelIndex>
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 /*!
38 38 \class QBarSeries
39 39 \brief part of QtCommercial chart API.
40 40
41 41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
42 42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
43 43 by QStringList.
44 44
45 45 \mainclass
46 46
47 47 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
48 48 */
49 49
50 50 /*!
51 51 \fn void QBarSeries::clicked(QBarSet *barset, QString category, Qt::MouseButtons button)
52 52
53 53 The signal is emitted if the user clicks with a mouse \a button on top of QBarSet \a barset of category \a category
54 54 contained by the series.
55 55 */
56 56
57 57 /*!
58 58 \fn void QBarSeries::selected()
59 59
60 60 The signal is emitted if the user selects/deselects the series. The logic for storing selections should be
61 61 implemented by the user of QBarSeries API.
62 62 */
63 63
64 64 /*!
65 65 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
66 66
67 67 The signal is emitted if mouse is hovered on top of series.
68 68 Parameter \a barset is the pointer of barset, where hover happened.
69 69 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
70 70 */
71 71
72 72 /*!
73 73 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
74 74 QBarSeries is QObject which is a child of a \a parent.
75 75 */
76 76 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) :
77 77 QAbstractSeries(*new QBarSeriesPrivate(categories, this),parent)
78 78 {
79 79 }
80 80
81 81 /*!
82 82 Destructs barseries and owned barsets.
83 83 */
84 84 QBarSeries::~QBarSeries()
85 85 {
86 86 // NOTE: d_ptr destroyed by QObject
87 87 }
88 88
89 89 /*!
90 90 \internal
91 91 */
92 92 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
93 93 QAbstractSeries(d,parent)
94 94 {
95 95 }
96 96
97 97 /*!
98 98 Returns the type of series. Derived classes override this.
99 99 */
100 100 QAbstractSeries::QSeriesType QBarSeries::type() const
101 101 {
102 102 return QAbstractSeries::SeriesTypeBar;
103 103 }
104 104
105 105 /*!
106 106 Adds a set of bars to series. Takes ownership of \a set.
107 107 Connects the clicked(QString, Qt::MouseButtons) signal
108 108 of \a set to this series
109 109 */
110 110 void QBarSeries::appendBarSet(QBarSet *set)
111 111 {
112 112 Q_D(QBarSeries);
113 113 d->m_internalModel->appendBarSet(set);
114 114 QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged()));
115 115 emit d->restructuredBars();
116 116 }
117 117
118 118 /*!
119 119 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
120 120 Disconnects the clicked(QString, Qt::MouseButtons) signal
121 121 of \a set from this series
122 122 */
123 123 void QBarSeries::removeBarSet(QBarSet *set)
124 124 {
125 125 Q_D(QBarSeries);
126 126 d->m_internalModel->removeBarSet(set);
127 127 emit d->restructuredBars();
128 128 }
129 129
130 130 /*!
131 131 Adds a list of barsets to series. Takes ownership of \a sets.
132 132 Connects the clicked(QString, Qt::MouseButtons) signals
133 133 of \a sets to this series
134 134 */
135 135 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
136 136 {
137 137 Q_D(QBarSeries);
138 138 foreach (QBarSet* barset, sets) {
139 139 d->m_internalModel->appendBarSet(barset);
140 140 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
141 141 }
142 142 emit d->restructuredBars();
143 143
144 144 }
145 145
146 146 /*!
147 147 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
148 148 Disconnects the clicked(QString, Qt::MouseButtons) signal
149 149 of \a sets from this series
150 150 */
151 151 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
152 152 {
153 153 Q_D(QBarSeries);
154 154
155 155 foreach (QBarSet* barset, sets) {
156 156 d->m_internalModel->removeBarSet(barset);
157 157 }
158 158 emit d->restructuredBars();
159 159 }
160 160
161 161 /*!
162 162 Inserts new \a set on the \a i position.
163 163 The barset that is currently at this postion is moved to postion i + 1
164 164 */
165 165 void QBarSeries::insertBarSet(int i, QBarSet *set)
166 166 {
167 167 Q_D(QBarSeries);
168 168 d->m_internalModel->insertBarSet(i, set);
169 169 emit d->barsetChanged();
170 170 }
171 171
172 172 /*!
173 Inserts new \a category on the \a i position.
174 The category that is currently at this postion is moved to postion i + 1
175 \sa removeCategory()
176 */
177 void QBarSeries::insertCategory(int i, QString category)
178 {
179 Q_D(QBarSeries);
180 d->m_internalModel->insertCategory(i, category);
181 }
182
183 /*!
184 Removes the category specified by \a i
185 \sa insertCategory()
186 */
187 void QBarSeries::removeCategory(int i)
188 {
189 Q_D(QBarSeries);
190 d->m_internalModel->removeCategory(i);
191 }
192
193 /*!
194 173 Returns number of sets in series.
195 174 */
196 175 int QBarSeries::barsetCount() const
197 176 {
198 177 Q_D(const QBarSeries);
199 178 return d->m_internalModel->barsetCount();
200 179 }
201 180
202 181 /*!
203 182 Returns number of categories in series
204 183 */
205 184 int QBarSeries::categoryCount() const
206 185 {
207 186 Q_D(const QBarSeries);
208 187 return d->m_internalModel->categoryCount();
209 188 }
210 189
211 190 /*!
212 191 Returns a list of sets in series. Keeps ownership of sets.
213 192 */
214 193 QList<QBarSet*> QBarSeries::barSets() const
215 194 {
216 195 Q_D(const QBarSeries);
217 196 return d->m_internalModel->barSets();
218 197 }
219 198
220 199 /*!
221 200 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
222 201 Sets the \a model to be used as a data source
223 202 */
224 203 bool QBarSeries::setModel(QAbstractItemModel *model)
225 204 {
226 205 Q_D(QBarSeries);
227 206 return d->setModel(model);
228 207 }
229 208
230 209 /*!
231 210 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
232 211 Sets column/row specified by \a categories to be used as a list of bar series categories.
233 212 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
234 213 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
235 214 All the columns/rows inbetween those two values are also used as data for bar sets.
236 215 The \a orientation parameter specifies whether the data is in columns or in rows.
237 216 */
238 217 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
239 218 {
240 219 Q_D(QBarSeries);
241 220 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
242 221 }
243 222
244 223 /*!
245 224 Returns the bar categories of the series.
246 225 */
247 226 QBarCategories QBarSeries::categories() const
248 227 {
249 228 Q_D(const QBarSeries);
250 229
251 230 QBarCategories categories;
252 231 int count = d->m_internalModel->categoryCount();
253 232 for (int i=1; i <= count; i++) {
254 categories.insert(i, d->m_internalModel->categoryName(i - 1));
233 categories.insert(i, d->m_internalModel->categoryName(i - 1));
255 234 }
256 235 return categories;
257 236
258 237 }
259 238
260 239 /*!
261 240 Sets the visibility of labels in series to \a visible
262 241 */
263 242 void QBarSeries::setLabelsVisible(bool visible)
264 243 {
265 244 foreach (QBarSet* s, barSets()) {
266 245 s->setLabelsVisible(visible);
267 246 }
268 247 }
269 248
270 249 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
271 250
272 251 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) :
273 252 QAbstractSeriesPrivate(q),
274 253 m_internalModel(new BarChartModel(categories,this)),
275 254 m_model(0),
276 255 m_mapCategories(-1),
277 256 m_mapBarBottom(-1),
278 257 m_mapBarTop(-1),
279 m_mapFirst(0),
280 m_mapCount(0),
281 258 m_mapOrientation(Qt::Vertical)
282 259 {
283 260 }
284 261
285 262 QBarSet* QBarSeriesPrivate::barsetAt(int index)
286 263 {
287 264 return m_internalModel->barsetAt(index);
288 265 }
289 266
290 267 QString QBarSeriesPrivate::categoryName(int category)
291 268 {
292 269 return m_internalModel->categoryName(category);
293 270 }
294 271
295 272 qreal QBarSeriesPrivate::min()
296 273 {
297 274 return m_internalModel->min();
298 275 }
299 276
300 277 qreal QBarSeriesPrivate::max()
301 278 {
302 279 return m_internalModel->max();
303 280 }
304 281
305 282 qreal QBarSeriesPrivate::valueAt(int set, int category)
306 283 {
307 284 return m_internalModel->valueAt(set, category);
308 285 }
309 286
310 287 qreal QBarSeriesPrivate::percentageAt(int set, int category)
311 288 {
312 289 return m_internalModel->percentageAt(set, category);
313 290 }
314 291
315 292 qreal QBarSeriesPrivate::categorySum(int category)
316 293 {
317 294 return m_internalModel->categorySum(category);
318 295 }
319 296
320 297 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
321 298 {
322 299 return m_internalModel->absoluteCategorySum(category);
323 300 }
324 301
325 302 qreal QBarSeriesPrivate::maxCategorySum()
326 303 {
327 304 return m_internalModel->maxCategorySum();
328 305 }
329 306
330 307 BarChartModel& QBarSeriesPrivate::modelInternal()
331 308 {
332 309 return *m_internalModel;
333 310 }
334 311
335 312 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
336 313 {
337 314 // disconnect signals from old model
338 315 if(m_model)
339 316 {
340 317 disconnect(m_model, 0, this, 0);
341 318 m_mapCategories = -1;
342 319 m_mapBarBottom = -1;
343 320 m_mapBarTop = -1;
344 m_mapFirst = 0;
345 m_mapCount = 0;
346 321 m_mapOrientation = Qt::Vertical;
347 322 }
348 323
349 324 // set new model
350 325 if(model)
351 326 {
352 327 m_model = model;
353 328 return true;
354 329 }
355 330 else
356 331 {
357 332 m_model = 0;
358 333 return false;
359 334 }
360 335 }
361 336
362 337 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
363 338 {
364 339 Q_Q(QBarSeries);
365 340
366 341 if (m_model == 0)
367 342 return;
368 343
369 344 m_mapCategories = categories;
370 345 m_mapBarBottom = bottomBoundry;
371 346 m_mapBarTop = topBoundry;
372 // m_mapFirst = 1;
373 347 m_mapOrientation = orientation;
374 348
375 349 // connect the signals
376 if (m_mapOrientation == Qt::Vertical) {
377 m_mapCount = m_model->rowCount() - m_mapFirst;
378 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
379 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
380 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)),
381 this, SLOT(modelDataAdded(QModelIndex,int,int)));
382 connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
383 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
384 } else {
385 m_mapCount = m_model->columnCount() - m_mapFirst;
386 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
387 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
388 connect(m_model,SIGNAL(columnsInserted(QModelIndex,int,int)),
389 this, SLOT(modelDataAdded(QModelIndex,int,int)));
390 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
391 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
392 }
350 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
351 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
393 352
394 353 // create the initial bars
395 354 delete m_internalModel;
396 355 if (m_mapOrientation == Qt::Vertical) {
397 356 QStringList categories;
398 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
357 for (int k = 0; k < m_model->rowCount(); k++)
399 358 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
400 359 m_internalModel = new BarChartModel(categories, this);
401 360
402 361 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
403 362 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
404 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
363 for(int m = 0; m < m_model->rowCount(); m++)
405 364 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
406 365 q->appendBarSet(barSet);
407 366 }
408 367 } else {
409 368 QStringList categories;
410 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
369 for (int k = 0; k < m_model->columnCount(); k++)
411 370 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
412 371 m_internalModel = new BarChartModel(categories, this);
413 372
414 373 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
415 374 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
416 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
375 for(int m = 0; m < m_model->columnCount(); m++)
417 376 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
418 377 q->appendBarSet(barSet);
419 378 }
420 379 }
421 380 }
422 381
423 382 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
424 383 {
425 384 Q_UNUSED(bottomRight)
426 385
427 386 if (m_mapOrientation == Qt::Vertical)
428 387 {
429 388 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
430 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
431 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
389 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop)
390 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
432 391 }
433 392 else
434 393 {
435 394 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
436 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
437 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
438 }
439 }
440
441 void QBarSeriesPrivate::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
442 {
443 Q_Q(QBarSeries);
444
445 if (m_mapOrientation == Qt::Vertical) {
446 q->insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
447 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
448 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
449 }
450 } else {
451 q->insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
452 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
453 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
454 }
455 }
456 emit restructuredBars();
457 }
458
459 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
460 {
461 Q_Q(QBarSeries);
462 Q_UNUSED(parent)
463 Q_UNUSED(end)
464
465 q->removeCategory(start - m_mapFirst);
466 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
467 {
468 barsetAt(i)->removeValue(start - m_mapFirst);
395 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop)
396 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
469 397 }
470 emit restructuredBars();
471 398 }
472 399
473 400 void QBarSeriesPrivate::barsetChanged()
474 401 {
475 402 emit updatedBars();
476 403 }
477 404
478 405 void QBarSeriesPrivate::scaleDomain(Domain& domain)
479 406 {
480 407 qreal minX(domain.minX());
481 408 qreal minY(domain.minY());
482 409 qreal maxX(domain.maxX());
483 410 qreal maxY(domain.maxY());
484 411 int tickXCount(domain.tickXCount());
485 412 int tickYCount(domain.tickYCount());
486 413
487 414 qreal x = m_internalModel->categoryCount();
488 415 qreal y = max();
489 416 minX = qMin(minX, x);
490 417 minY = qMin(minY, y);
491 418 maxX = qMax(maxX, x);
492 419 maxY = qMax(maxY, y);
493 420 tickXCount = x+1;
494 421
495 422 domain.setRangeX(minX,maxX,tickXCount);
496 423 domain.setRangeY(minY,maxY,tickYCount);
497 424 }
498 425
499 426 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
500 427 {
501 428 Q_Q(QBarSeries);
502 429
503 430 BarChartItem* bar = new BarChartItem(q,presenter);
504 431 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
505 432 presenter->animator()->addAnimation(bar);
506 433 }
507 434 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
508 435 return bar;
509 436
510 437 }
511 438
512 439 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
513 440 {
514 441 Q_Q(QBarSeries);
515 442 QList<LegendMarker*> markers;
516 443 foreach(QBarSet* set, q->barSets()) {
517 444 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
518 445 markers << marker;
519 446 }
520 447
521 448 return markers;
522 449 }
523 450
524 451 #include "moc_qbarseries.cpp"
525 452 #include "moc_qbarseries_p.cpp"
526 453
527 454 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,82 +1,80
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef BARSERIES_H
22 22 #define BARSERIES_H
23 23
24 24 #include <qabstractseries.h>
25 25 #include <QStringList>
26 26
27 27 class QModelIndex;
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 typedef QStringList QBarCategories;
32 32
33 33 class QBarSet;
34 34 class BarChartModel;
35 35 class BarCategory;
36 36 class QBarSeriesPrivate;
37 37
38 38 // Container for series
39 39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
40 40 {
41 41 Q_OBJECT
42 42 public:
43 43 explicit QBarSeries(QBarCategories categories, QObject *parent = 0);
44 44 virtual ~QBarSeries();
45 45
46 46 QAbstractSeries::QSeriesType type() const;
47 47
48 48 void appendBarSet(QBarSet *set); // Takes ownership of set
49 49 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
50 50 void appendBarSets(QList<QBarSet* > sets);
51 51 void removeBarSets(QList<QBarSet* > sets);
52 52 void insertBarSet(int i, QBarSet *set);
53 void insertCategory(int i, QString category);
54 void removeCategory(int i);
55 53 int barsetCount() const;
56 54 int categoryCount() const;
57 55 QList<QBarSet*> barSets() const;
58 56 QBarCategories categories() const;
59 57
60 58 void setLabelsVisible(bool visible = true);
61 59
62 60 bool setModel(QAbstractItemModel *model);
63 61 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
64 62
65 63 protected:
66 64 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
67 65
68 66 Q_SIGNALS:
69 67 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
70 68 void selected();
71 69 void hovered(QBarSet* barset, bool status);
72 70
73 71 protected:
74 72 Q_DECLARE_PRIVATE(QBarSeries)
75 73 friend class BarChartItem;
76 74 friend class PercentBarChartItem;
77 75 friend class StackedBarChartItem;
78 76 };
79 77
80 78 QTCOMMERCIALCHART_END_NAMESPACE
81 79
82 80 #endif // BARSERIES_H
@@ -1,66 +1,62
1 1 #ifndef QBARSERIES_P_H
2 2 #define QBARSERIES_P_H
3 3
4 4 #include "qbarseries.h"
5 5 #include "qabstractseries_p.h"
6 6 #include <QStringList>
7 7 #include <QAbstractSeries>
8 8
9 9 class QModelIndex;
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 // Container for series
14 14 class QBarSeriesPrivate : public QAbstractSeriesPrivate
15 15 {
16 16 Q_OBJECT
17 17 public:
18 18 QBarSeriesPrivate(QBarCategories categories, QBarSeries *parent);
19 19
20 20 void scaleDomain(Domain& domain);
21 21 Chart* createGraphics(ChartPresenter* presenter);
22 22 QList<LegendMarker*> createLegendMarker(QLegend* legend);
23 23
24 24 bool setModel(QAbstractItemModel *model);
25 25 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
26 26
27 27 QBarSet* barsetAt(int index);
28 28 QString categoryName(int category);
29 29 qreal min();
30 30 qreal max();
31 31 qreal valueAt(int set, int category);
32 32 qreal percentageAt(int set, int category);
33 33 qreal categorySum(int category);
34 34 qreal absoluteCategorySum(int category);
35 35 qreal maxCategorySum();
36 36 BarChartModel& modelInternal();
37 37
38 38 Q_SIGNALS:
39 39 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
40 40 void selected();
41 41 void updatedBars();
42 42 void restructuredBars();
43 43
44 44 private Q_SLOTS:
45 45 // slots for updating bars when data in model changes
46 46 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
47 void modelDataAdded(QModelIndex parent, int start, int end);
48 void modelDataRemoved(QModelIndex parent, int start, int end);
49 47 void barsetChanged();
50 48
51 49 protected:
52 50 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
53 51 QAbstractItemModel* m_model;
54 52 int m_mapCategories;
55 53 int m_mapBarBottom;
56 54 int m_mapBarTop;
57 int m_mapFirst;
58 int m_mapCount;
59 55 Qt::Orientation m_mapOrientation;
60 56 private:
61 57 Q_DECLARE_PUBLIC(QBarSeries)
62 58 };
63 59
64 60 QTCOMMERCIALCHART_END_NAMESPACE
65 61
66 62 #endif // QBARSERIESPRIVATE_P_H
@@ -1,236 +1,205
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qsplineseries.h"
22 22 #include "qsplineseries_p.h"
23 23 #include "splinechartitem_p.h"
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26 #include "chartanimator_p.h"
27 27
28 28 /*!
29 29 \class QSplineSeries
30 30 \brief Series type used to store data needed to draw a spline.
31 31
32 32 QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline
33 33 Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn.
34 34 */
35 35
36 36 /*!
37 37 \fn QSeriesType QSplineSeries::type() const
38 38 Returns the type of the series
39 39 */
40 40
41 41 /*!
42 42 \fn QSeriesType QSplineSeries::controlPoint(int index) const
43 43 Returns the control point specified by \a index
44 44 */
45 45
46 46 QTCOMMERCIALCHART_BEGIN_NAMESPACE
47 47
48 48 /*!
49 49 Constructs empty series object which is a child of \a parent.
50 50 When series object is added to QChartView or QChart instance then the ownerships is transferred.
51 51 */
52 52
53 53 QSplineSeries::QSplineSeries(QObject *parent) :
54 54 QLineSeries(*new QSplineSeriesPrivate(this),parent)
55 55 {
56 56 }
57 57
58 58 QAbstractSeries::QSeriesType QSplineSeries::type() const
59 59 {
60 60 return QAbstractSeries::SeriesTypeSpline;
61 61 }
62 62
63 63 QPointF QSplineSeries::controlPoint(int index) const
64 64 {
65 65 Q_D(const QSplineSeries);
66 66 return d->m_controlPoints[index];
67 67 }
68 68
69 void QSplineSeries::setModelMappingRange(int first, int count)
70 {
71 Q_D(QSplineSeries);
72 QLineSeries::setModelMappingRange(first, count);
73 d->calculateControlPoints();
74 }
75
76 69 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77 70
78 71 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
79 72 {
80 73 QObject::connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints()));
81 74 QObject::connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints()));
82 75 QObject::connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints()));
83 76 };
84 77
85 78 /*!
86 79 \internal
87 80 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
88 81 */
89 82 void QSplineSeriesPrivate::calculateControlPoints()
90 83 {
91 84
92 85 Q_Q(QSplineSeries);
93 86 // Based on http://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit
94 87 // CPOL License
95 88
96 89 int n = q->count() - 1;
97 90 if (n == 1)
98 91 { // Special case: Bezier curve should be a straight line.
99 92 // firstControlPoints = new Point[1];
100 93 // 3P1 = 2P0 + P3
101 94 m_controlPoints.append(QPointF((2 * q->x(0) + q->x(1)) / 3, (2 * q->y(0) + q->y(1)) / 3));
102 95
103 96 // P2 = 2P1 P0
104 97 m_controlPoints.append(QPointF(2 * m_controlPoints[0].x() - q->x(0), 2 * m_controlPoints[0].y() - q->y(0)));
105 98 return;
106 99 }
107 100
108 101 // Calculate first Bezier control points
109 102 // Right hand side vector
110 103 // Set of equations for P0 to Pn points.
111 104 //
112 105 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
113 106 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
114 107 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
115 108 // | . . . . . . . . . . . . | | ... | | ... |
116 109 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
117 110 // | . . . . . . . . . . . . | | ... | | ... |
118 111 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
119 112 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
120 113 //
121 114 QList<qreal> rhs;
122 115 rhs.append(q->x(0) + 2 * q->x(1));
123 116
124 117 // Set right hand side X values
125 118 for (int i = 1; i < n - 1; ++i)
126 119 rhs.append(4 * q->x(i) + 2 * q->x(i + 1));
127 120
128 121 rhs.append((8 * q->x(n - 1) + q->x(n)) / 2.0);
129 122 // Get first control points X-values
130 123 QList<qreal> xControl = getFirstControlPoints(rhs);
131 124 rhs[0] = q->y(0) + 2 * q->y(1);
132 125
133 126 // Set right hand side Y values
134 127 for (int i = 1; i < n - 1; ++i)
135 128 rhs[i] = 4 * q->y(i) + 2 * q->y(i + 1);
136 129
137 130 rhs[n - 1] = (8 * q->y(n - 1) + q->y(n)) / 2.0;
138 131 // Get first control points Y-values
139 132 QList<qreal> yControl = getFirstControlPoints(rhs);
140 133
141 134 // Fill output arrays.
142 135 for (int i = 0; i < n; ++i) {
143 136 // First control point
144 137 m_controlPoints.append(QPointF(xControl[i], yControl[i]));
145 138 // Second control point
146 139 if (i < n - 1)
147 140 m_controlPoints.append(QPointF(2 * q->x(i + 1) - xControl[i + 1], 2 * q->y(i + 1) - yControl[i + 1]));
148 141 else
149 142 m_controlPoints.append(QPointF((q->x(n) + xControl[n - 1]) / 2, (q->y(n) + yControl[n - 1]) / 2));
150 143 }
151 144 }
152 145
153 146 /*!
154 147 \internal
155 148 */
156 149 QList<qreal> QSplineSeriesPrivate::getFirstControlPoints(QList<qreal> rhs)
157 150 {
158 151 QList<qreal> x; // Solution vector.
159 152 QList<qreal> tmp; // Temp workspace.
160 153
161 154 qreal b = 2.0;
162 155 x.append(rhs[0] / b);
163 156 tmp.append(0);
164 157 for (int i = 1; i < rhs.size(); i++) {
165 158 // Decomposition and forward substitution.
166 159 tmp.append(1 / b);
167 160 b = (i < rhs.size() - 1 ? 4.0 : 3.5) - tmp[i];
168 161 x.append((rhs[i] - x[i - 1]) / b);
169 162 }
170 163 for (int i = 1; i < rhs.size(); i++)
171 164 x[rhs.size() - i - 1] -= tmp[rhs.size() - i] * x[rhs.size() - i]; // Backsubstitution.
172 165
173 166 return x;
174 167 }
175 168
176 169 /*!
177 170 \internal
178 171 Updates the control points, besed on currently avaiable knots.
179 172 */
180 173 void QSplineSeriesPrivate::updateControlPoints()
181 174 {
182 175 Q_Q(QSplineSeries);
183 176 if (q->count() > 1) {
184 177 m_controlPoints.clear();
185 178 calculateControlPoints();
186 179 }
187 180 }
188 181
189 /*//!
190 \fn bool QSplineSeries::setModel(QAbstractItemModel *model)
191 Sets the \a model to be used as a data source
192 \sa setModelMapping(), setModelMappingRange()
193 */
194 //bool QSplineSeries::setModel(QAbstractItemModel* model)
195 //{
196 // QXYSeries::setModel(model);
197 //// calculateControlPoints();
198 // return true;
199 //}
200
201 /*//!
202 \fn bool QSplineSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
203 Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used
204 as a data source for y coordinate. The \a orientation parameter specifies whether the data
205 is in columns or in rows.
206 */
207 //void QSplineSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
208 //{
209 // QLineSeries::setModelMapping(modelX, modelY, orientation);
210 //// calculateControlPoints();
211 //}
212
213 182 /*!
214 183 \fn bool QSplineSeries::setModelMappingRange(int first, int count)
215 184 Allows limiting the model mapping.
216 185 Parameter \a first specifies which element of the model should be used as a first one of the series.
217 186 Parameter \a count specifies how many elements should be mapped. If count is not specified (defaults to -1)
218 187 then all the items following \a first item in a model are used.
219 188 \sa setModel(), setModelMapping()
220 189 */
221 190
222 191 Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
223 192 {
224 193 Q_Q(QSplineSeries);
225 194 SplineChartItem* spline = new SplineChartItem(q,presenter);
226 195 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
227 196 presenter->animator()->addAnimation(spline);
228 197 }
229 198 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
230 199 return spline;
231 200 }
232 201
233 202 #include "moc_qsplineseries.cpp"
234 203 #include "moc_qsplineseries_p.cpp"
235 204
236 205 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,53 +1,52
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QSPLINESERIES_H
22 22 #define QSPLINESERIES_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <qlineseries.h>
26 26 #include <QList>
27 27 #include <QPointF>
28 28 #include <QtGlobal>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 class QSplineSeriesPrivate;
33 33
34 34 class QTCOMMERCIALCHART_EXPORT QSplineSeries : public QLineSeries
35 35 {
36 36 Q_OBJECT
37 37 public:
38 38
39 39 explicit QSplineSeries(QObject *parent = 0);
40 40 QAbstractSeries::QSeriesType type() const;
41 41
42 42 QPointF controlPoint(int index) const;
43 void setModelMappingRange(int first, int count);
44 43
45 44 private:
46 45 Q_DECLARE_PRIVATE(QSplineSeries);
47 46 Q_DISABLE_COPY(QSplineSeries);
48 47 friend class SplineChartItem;
49 48 };
50 49
51 50 QTCOMMERCIALCHART_END_NAMESPACE
52 51
53 52 #endif // QSPLINESERIES_H
@@ -1,670 +1,465
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qxyseries.h"
22 22 #include "qxyseries_p.h"
23 23 #include "domain_p.h"
24 24 #include "legendmarker_p.h"
25 25 #include <QAbstractItemModel>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 /*!
30 30 \class QXYSeries
31 31 \brief The QXYSeries class is a base class for line, spline and scatter series.
32 32 */
33 33
34 34 /*!
35 35 \fn QPen QXYSeries::pen() const
36 36 \brief Returns pen used to draw points for series.
37 37 \sa setPen()
38 38 */
39 39
40 40 /*!
41 41 \fn QBrush QXYSeries::brush() const
42 42 \brief Returns brush used to draw points for series.
43 43 \sa setBrush()
44 44 */
45 45
46 46 /*!
47 47 \fn void QXYSeries::clicked(const QPointF& point)
48 48 \brief Signal is emitted when user clicks the \a point on chart.
49 49 */
50 50
51 51 /*!
52 52 \fn void QXYSeries::selected()
53 53
54 54 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
55 55 implemented by the user of QXYSeries API.
56 56 */
57 57
58 58 /*!
59 59 \fn void QXYSeriesPrivate::pointReplaced(int index)
60 60 \brief \internal \a index
61 61 */
62 62
63 63 /*!
64 64 \fn void QXYSeriesPrivate::pointAdded(int index)
65 65 \brief \internal \a index
66 66 */
67 67
68 68 /*!
69 69 \fn void QXYSeriesPrivate::pointRemoved(int index)
70 70 \brief \internal \a index
71 71 */
72 72
73 73 /*!
74 74 \fn void QXYSeriesPrivate::updated()
75 75 \brief \internal
76 76 */
77 77
78 78 /*!
79 \fn int QXYSeries::mapFirst() const
80 Returns the index of the model's item that is used as a first one for the series.
81 \sa mapCount()
82 */
83
84 /*!
85 \fn int QXYSeries::mapCount() const
86 Returns the number of the items that are taken from the model.
87 If -1 it means all the items of the model following the first one are used.
88 \sa mapFirst()
89 */
90
91 /*!
92 79 \internal
93 80
94 81 Constructs empty series object which is a child of \a parent.
95 82 When series object is added to QChartView or QChart instance ownerships is transferred.
96 83 */
97 84 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
98 85 {
99 86
100 87 }
101 88 /*!
102 89 Destroys the object. Series added to QChartView or QChart instances are owned by those,
103 90 and are deleted when mentioned object are destroyed.
104 91 */
105 92 QXYSeries::~QXYSeries()
106 93 {
107 94 }
108 95
109 96 /*!
110 97 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
111 98 */
112 99 void QXYSeries::append(qreal x,qreal y)
113 100 {
114 101 Q_D(QXYSeries);
115 102 Q_ASSERT(d->m_x.size() == d->m_y.size());
116 103 d->m_x<<x;
117 104 d->m_y<<y;
118 105 emit d->pointAdded(d->m_x.size()-1);
119 106 }
120 107
121 108 /*!
122 109 This is an overloaded function.
123 110 Adds data \a point to the series. Points are connected with lines on the chart.
124 111 */
125 112 void QXYSeries::append(const QPointF &point)
126 113 {
127 114 append(point.x(),point.y());
128 115 }
129 116
130 117 /*!
131 118 This is an overloaded function.
132 119 Adds list of data \a points to the series. Points are connected with lines on the chart.
133 120 */
134 121 void QXYSeries::append(const QList<QPointF> points)
135 122 {
136 123 foreach(const QPointF& point , points) {
137 124 append(point.x(),point.y());
138 125 }
139 126 }
140 127
141 128 /*!
142 129 Modifies \a y value for given \a x a value.
143 130 */
144 131 void QXYSeries::replace(qreal x,qreal y)
145 132 {
146 133 Q_D(QXYSeries);
147 134 int index = d->m_x.indexOf(x);
148 135 d->m_x[index] = x;
149 136 d->m_y[index] = y;
150 137 emit d->pointReplaced(index);
151 138 }
152 139
153 140 /*!
154 141 This is an overloaded function.
155 142 Replaces current y value of for given \a point x value with \a point y value.
156 143 */
157 144 void QXYSeries::replace(const QPointF &point)
158 145 {
159 146 replace(point.x(),point.y());
160 147 }
161 148
162 149 /*!
163 150 Removes first \a x value and related y value.
164 151 */
165 152 void QXYSeries::remove(qreal x)
166 153 {
167 154 Q_D(QXYSeries);
168 155 int index = d->m_x.indexOf(x);
169 156
170 157 if (index == -1) return;
171 158
172 159 d->m_x.remove(index);
173 160 d->m_y.remove(index);
174 161
175 162 emit d->pointRemoved(index);
176 163 }
177 164
178 165 /*!
179 166 Removes current \a x and \a y value.
180 167 */
181 168 void QXYSeries::remove(qreal x,qreal y)
182 169 {
183 170 Q_D(QXYSeries);
184 171 int index =-1;
185 172 do {
186 173 index = d->m_x.indexOf(x,index+1);
187 174 } while (index !=-1 && d->m_y.at(index)!=y);
188 175
189 176 if (index==-1) return;
190 177
191 178 d->m_x.remove(index);
192 179 d->m_y.remove(index);
193 180 emit d->pointRemoved(index);
194 181 }
195 182
196 183 /*!
197 184 Removes current \a point x value. Note \a point y value is ignored.
198 185 */
199 186 void QXYSeries::remove(const QPointF &point)
200 187 {
201 188 remove(point.x(),point.y());
202 189 }
203 190
204 191 /*!
205 192 Removes all data points from the series.
206 193 */
207 194 void QXYSeries::removeAll()
208 195 {
209 196 Q_D(QXYSeries);
210 197 d->m_x.clear();
211 198 d->m_y.clear();
212 199 }
213 200
214 201 /*!
215 202 \internal \a pos
216 203 */
217 204 qreal QXYSeries::x(int pos) const
218 205 {
219 206 Q_D(const QXYSeries);
220 207 if (d->m_model) {
221 208 if (d->m_mapOrientation == Qt::Vertical)
222 209 // consecutive data is read from model's column
223 return d->m_model->data(d->m_model->index(pos + d->m_mapFirst, d->m_mapX), Qt::DisplayRole).toDouble();
210 return d->m_model->data(d->m_model->index(pos, d->m_mapX), Qt::DisplayRole).toDouble();
224 211 else
225 212 // consecutive data is read from model's row
226 return d->m_model->data(d->m_model->index(d->m_mapX, pos + d->m_mapFirst), Qt::DisplayRole).toDouble();
213 return d->m_model->data(d->m_model->index(d->m_mapX, pos), Qt::DisplayRole).toDouble();
227 214 } else {
228 215 // model is not specified, return the data from series' internal data store
229 216 return d->m_x.at(pos);
230 217 }
231 218 }
232 219
233 220 /*!
234 221 \internal \a pos
235 222 */
236 223 qreal QXYSeries::y(int pos) const
237 224 {
238 225 Q_D(const QXYSeries);
239 226 if (d->m_model) {
240 227 if (d->m_mapOrientation == Qt::Vertical)
241 228 // consecutive data is read from model's column
242 return d->m_model->data(d->m_model->index(pos + d->m_mapFirst, d->m_mapY), Qt::DisplayRole).toDouble();
229 return d->m_model->data(d->m_model->index(pos, d->m_mapY), Qt::DisplayRole).toDouble();
243 230 else
244 231 // consecutive data is read from model's row
245 return d->m_model->data(d->m_model->index(d->m_mapY, pos + d->m_mapFirst), Qt::DisplayRole).toDouble();
232 return d->m_model->data(d->m_model->index(d->m_mapY, pos), Qt::DisplayRole).toDouble();
246 233 } else {
247 234 // model is not specified, return the data from series' internal data store
248 235 return d->m_y.at(pos);
249 236 }
250 237 }
251 238
252 239 /*!
253 240 Returns number of data points within series.
254 241 */
255 242 int QXYSeries::count() const
256 243 {
257 244 Q_D(const QXYSeries);
258 245
259 246 Q_ASSERT(d->m_x.size() == d->m_y.size());
260 247
261 248 if (d->m_model) {
262 249 if (d->m_mapOrientation == Qt::Vertical) {
263 // data is in a column. Return the number of mapped items if the model's column have enough items
264 // or the number of items that can be mapped
265 if (d->m_mapLimited)
266 return qMin(d->m_mapCount, qMax(d->m_model->rowCount() - d->m_mapFirst, 0));
267 else
268 return qMax(d->m_model->rowCount() - d->m_mapFirst, 0);
250 // data is in a column. Return the number of mapped items
251 return d->m_model->rowCount();
269 252 } else {
270 // data is in a row. Return the number of mapped items if the model's row have enough items
271 // or the number of items that can be mapped
272 if (d->m_mapLimited)
273 return qMin(d->m_mapCount, qMax(d->m_model->columnCount() - d->m_mapFirst, 0));
274 else
275 return qMax(d->m_model->columnCount() - d->m_mapFirst, 0);
253 // data is in a row. Return the number of mapped items
254 return d->m_model->columnCount();
276 255 }
277 256 }
278 257
279 258 // model is not specified, return the number of points in the series internal data store
280 259 return d->m_x.size();
281 260 }
282 261
283 262 /*!
284 263 Returns the data points of the series.
285 264 */
286 265 QList<QPointF> QXYSeries::data()
287 266 {
288 267 Q_D(QXYSeries);
289 268 QList<QPointF> data;
290 269 for (int i(0); i < d->m_x.count() && i < d->m_y.count(); i++)
291 270 data.append(QPointF(d->m_x.at(i), d->m_y.at(i)));
292 271 return data;
293 272 }
294 273
295 274
296 275 /*!
297 276 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
298 277 pen from chart theme is used.
299 278 \sa QChart::setTheme()
300 279 */
301 280 void QXYSeries::setPen(const QPen &pen)
302 281 {
303 282 Q_D(QXYSeries);
304 283 if (d->m_pen!=pen) {
305 284 d->m_pen = pen;
306 285 emit d->updated();
307 286 }
308 287 }
309 288
310 289 QPen QXYSeries::pen() const
311 290 {
312 291 Q_D(const QXYSeries);
313 292 return d->m_pen;
314 293 }
315 294
316 295 /*!
317 296 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
318 297 from chart theme setting is used.
319 298 \sa QChart::setTheme()
320 299 */
321 300 void QXYSeries::setBrush(const QBrush &brush)
322 301 {
323 302 Q_D(QXYSeries);
324 303 if (d->m_brush!=brush) {
325 304 d->m_brush = brush;
326 305 emit d->updated();
327 306 }
328 307 }
329 308
330 309 QBrush QXYSeries::brush() const
331 310 {
332 311 Q_D(const QXYSeries);
333 312 return d->m_brush;
334 313 }
335 314
336 315
337 316 /*!
338 317 Sets if data points are \a visible and should be drawn on line.
339 318 */
340 319 void QXYSeries::setPointsVisible(bool visible)
341 320 {
342 321 Q_D(QXYSeries);
343 322 if (d->m_pointsVisible != visible){
344 323 d->m_pointsVisible = visible;
345 324 emit d->updated();
346 325 }
347 326 }
348 327
349 328 /*!
350 329 Returns true if drawing the data points of the series is enabled.
351 330 */
352 331 bool QXYSeries::pointsVisible() const
353 332 {
354 333 Q_D(const QXYSeries);
355 334 return d->m_pointsVisible;
356 335 }
357 336
358 337
359 338 /*!
360 339 Stream operator for adding a data \a point to the series.
361 340 \sa append()
362 341 */
363 342 QXYSeries& QXYSeries::operator<< (const QPointF &point)
364 343 {
365 344 append(point);
366 345 return *this;
367 346 }
368 347
369 348
370 349 /*!
371 350 Stream operator for adding a list of \a points to the series.
372 351 \sa append()
373 352 */
374 353
375 354 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
376 355 {
377 356 append(points);
378 357 return *this;
379 358 }
380 359
381 360 /*!
382 361 \internal
383 362 */
384 363 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
385 364 {
386 365 Q_UNUSED(bottomRight)
387 366 Q_D(QXYSeries);
388 367 if (d->m_mapOrientation == Qt::Vertical) {
389 if (topLeft.row() >= d->m_mapFirst && (!d->m_mapLimited || topLeft.row() < d->m_mapFirst + d->m_mapCount))
390 emit d->pointReplaced(topLeft.row() - d->m_mapFirst);
391 } else {
392 if (topLeft.column() >= d->m_mapFirst && (!d->m_mapLimited || topLeft.column() < d->m_mapFirst + d->m_mapCount))
393 emit d->pointReplaced(topLeft.column() - d->m_mapFirst);
394 }
395 }
396
397 /*!
398 \internal
399 */
400 void QXYSeries::modelDataAboutToBeAdded(QModelIndex parent, int start, int end)
401 {
402 Q_UNUSED(parent)
403 // Q_UNUSED(end)
404 Q_D(QXYSeries);
405 if (d->m_mapLimited) {
406 if (start >= d->m_mapFirst + d->m_mapCount) {
407 // the added data is below mapped area
408 // therefore it has no relevance
409 return;
410 } else {
411 // the added data is in the mapped area or before it and update is needed
412
413 // check how many mapped items there is currently (before new items are added)
414 // if the number of items currently is equal the m_mapCount then some needs to be removed from xychartitem
415 // internal storage before new ones can be added
416
417 int itemsToRemove = qMin(count() - qMax(start - d->m_mapFirst, 0), end - start + 1);
418 if (d->m_mapCount == count()) {
419 for (int i = 0; i < itemsToRemove; i++)
420 emit d->pointRemoved(qMin(end, count()) - i);
421 }
422 }
423 } else {
424 // map is not limited (it includes all the items starting from m_mapFirst till the end of model)
425 // nothing to do
426 // emit pointAdded(qMax(start - m_mapFirst, 0));
427 }
428 }
429
430 /*!
431 \internal
432 */
433 void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end)
434 {
435 Q_UNUSED(parent)
436 // Q_UNUSED(end)
437 Q_D(QXYSeries);
438 if (d->m_mapLimited) {
439 if (start >= d->m_mapFirst + d->m_mapCount) {
440 // the added data is below mapped area
441 // therefore it has no relevance
442 return;
443 } else {
444 // the added data is in the mapped area or before it
445 // update needed
446 if (count() > 0) {
447 int toBeAdded = qMin(d->m_mapCount - (start - d->m_mapFirst), end - start + 1);
448 for (int i = 0; i < toBeAdded; i++)
449 if (start + i >= d->m_mapFirst)
450 emit d->pointAdded(start + i);
451 }
452 }
453 } else {
454 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
455 for (int i = 0; i < end - start + 1; i++)
456 emit d->pointAdded(start + i);
457 }
458 }
459
460 /*!
461 \internal
462 */
463 void QXYSeries::modelDataAboutToBeRemoved(QModelIndex parent, int start, int end)
464 {
465 Q_UNUSED(parent)
466 // Q_UNUSED(end)
467 Q_D(QXYSeries);
468 if (d->m_mapLimited) {
469 if (start >= d->m_mapFirst + d->m_mapCount) {
470 // the removed data is below mapped area
471 // therefore it has no relevance
472 return;
473 } else {
474 // the removed data is in the mapped area or before it
475 // update needed
476
477 // check how many items need to be removed from the xychartitem storage
478 // the number equals the number of items that are removed and that lay before
479 // or in the mapped area. Items that lay beyond the map do not count
480 // the max is the current number of items in storage (count())
481 int itemsToRemove = qMin(count(), qMin(end, d->m_mapFirst + d->m_mapCount - 1) - start + 1);
482 for (int i = 0; i < itemsToRemove; i++)
483 emit d->pointRemoved(start);
484 }
485 } else {
486 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
487 for (int i = 0; i < end - start + 1; i++)
488 emit d->pointRemoved(start);
489 }
490 }
491
492 /*!
493 \internal
494 */
495 void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end)
496 {
497
498 Q_UNUSED(parent)
499 Q_UNUSED(end)
500 Q_D(QXYSeries);
501 // how many items there were before data was removed
502 // int oldCount = count() - 1;
503
504 if (d->m_mapLimited) {
505 if (start >= d->m_mapFirst + d->m_mapCount) {
506 // the removed data is below mapped area
507 // therefore it has no relevance
508 return;
509 } else {
510 // if the current items count in the whole model is bigger than the index of the last item
511 // that was removed than it means there are some extra items available
512
513 int removedItemsCount = qMin(count(), qMin(end, d->m_mapFirst + d->m_mapCount - 1) - start + 1);
514 int extraItemsAvailable = 0;
515 if (d->m_mapOrientation == Qt::Vertical) {
516 extraItemsAvailable = qMax(d->m_model->rowCount() + (end - start + 1) - qMax(end + 1, d->m_mapFirst + d->m_mapCount), 0);
517 } else {
518 extraItemsAvailable = qMax(d->m_model->columnCount() + (end - start + 1) - qMax(end + 1, d->m_mapFirst + d->m_mapCount), 0);
519 }
520
521 // if there are excess items available (below the mapped area) use them to repopulate mapped area
522 int toBeAdded = qMin(extraItemsAvailable, removedItemsCount);
523 for (int k = 0; k < toBeAdded; k++)
524 emit d->pointAdded(d->m_mapFirst + d->m_mapCount - removedItemsCount + k);
525 }
368 if (topLeft.column() == d->m_mapX || topLeft.column() == d->m_mapY)
369 emit d->pointReplaced(topLeft.row());
526 370 } else {
527 // data was removed from XYSeries interal storage. Nothing more to do
371 if (topLeft.row() == d->m_mapX || topLeft.row() == d->m_mapY)
372 emit d->pointReplaced(topLeft.column());
528 373 }
529 374 }
530 375
531 376 /*!
532 377 \fn bool QXYSeries::setModel(QAbstractItemModel *model)
533 378 Sets the \a model to be used as a data source
534 379 \sa setModelMapping(), setModelMappingRange()
535 380 */
536 381 bool QXYSeries::setModel(QAbstractItemModel *model)
537 382 {
538 383 Q_D(QXYSeries);
539 384 // disconnect signals from old model
540 385 if (d->m_model) {
541 386 QObject::disconnect(d->m_model, 0, this, 0);
542 387 d->m_mapX = -1;
543 388 d->m_mapY = -1;
544 d->m_mapFirst = 0;
545 d->m_mapCount = 0;
546 d->m_mapLimited = false;
547 389 d->m_mapOrientation = Qt::Vertical;
548 390 }
549 391
550 392 // set new model
551 393 if (model) {
552 394 d->m_model = model;
553 395 return true;
554 396 } else {
555 397 d->m_model = 0;
556 398 return false;
557 399 }
558 400 }
559 401
560 402 /*!
561 403 \fn bool QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
562 404 Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used
563 405 as a data source for y coordinate. The \a orientation parameter specifies whether the data
564 406 is in columns or in rows.
565 407 \sa setModel(), setModelMappingRange()
566 408 */
567 409 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
568 410 {
569 411 Q_D(QXYSeries);
570 412 if (d->m_model == 0)
571 413 return;
572 414 d->m_mapX = modelX;
573 415 d->m_mapY = modelY;
574 d->m_mapFirst = 0;
575 416 d->m_mapOrientation = orientation;
576 if (d->m_mapOrientation == Qt::Vertical) {
577 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
578 connect(d->m_model,SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(modelDataAboutToBeAdded(QModelIndex,int,int)));
579 connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
580 connect(d->m_model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(modelDataAboutToBeRemoved(QModelIndex,int,int)));
581 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
582 } else {
583 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
584 connect(d->m_model,SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(modelDataAboutToBeAdded(QModelIndex,int,int)));
585 connect(d->m_model,SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
586 connect(d->m_model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(modelDataAboutToBeRemoved(QModelIndex,int,int)));
587 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
588 }
589 }
590
591 /*!
592 \fn bool QXYSeries::setModelMappingRange(int first, int count)
593 Allows limiting the model mapping.
594 Parameter \a first specifies which element of the model should be used as a first one of the series.
595 Parameter \a count specifies how many elements should be mapped. If count is not specified (defaults to -1)
596 then all the items following \a first item in a model are used.
597 \sa setModel(), setModelMapping()
598 */
599 void QXYSeries::setModelMappingRange(int first, int count)
600 {
601 Q_D(QXYSeries);
602 d->m_mapFirst = first;
603 if (count == 0) {
604 d->m_mapLimited = false;
605 } else {
606 d->m_mapCount = count;
607 d->m_mapLimited = true;
608 }
609 }
610
611 int QXYSeries::mapFirst() const
612 {
613 Q_D(const QXYSeries);
614 return d->m_mapFirst;
615 }
616
617 int QXYSeries::mapCount() const
618 {
619 Q_D(const QXYSeries);
620 return d->m_mapCount;
417 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
621 418 }
622 419
623 420 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
624 421
422
625 423 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) : QAbstractSeriesPrivate(q),
626 424 m_mapX(-1),
627 425 m_mapY(-1),
628 m_mapFirst(0),
629 m_mapCount(0),
630 m_mapLimited(false),
631 426 m_mapOrientation( Qt::Vertical),
632 427 m_pointsVisible(false)
633 428 {
634 429 }
635 430
636 431 void QXYSeriesPrivate::scaleDomain(Domain& domain)
637 432 {
638 433 qreal minX(domain.minX());
639 434 qreal minY(domain.minY());
640 435 qreal maxX(domain.maxX());
641 436 qreal maxY(domain.maxY());
642 437 int tickXCount(domain.tickXCount());
643 438 int tickYCount(domain.tickYCount());
644 439
645 440 Q_Q(QXYSeries);
646 441 for (int i = 0; i < q->count(); i++)
647 442 {
648 443 qreal x = q->x(i);
649 444 qreal y = q->y(i);
650 445 minX = qMin(minX, x);
651 446 minY = qMin(minY, y);
652 447 maxX = qMax(maxX, x);
653 448 maxY = qMax(maxY, y);
654 449 }
655 450
656 451 domain.setRangeX(minX,maxX,tickXCount);
657 452 domain.setRangeY(minY,maxY,tickYCount);
658 453 }
659 454
660 455 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
661 456 {
662 457 Q_Q(QXYSeries);
663 458 QList<LegendMarker*> list;
664 459 return list << new XYLegendMarker(q,legend);
665 460 }
666 461
667 462 #include "moc_qxyseries.cpp"
668 463 #include "moc_qxyseries_p.cpp"
669 464
670 465 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,97 +1,89
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QXYSERIES_H
22 22 #define QXYSERIES_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <qabstractseries.h>
26 26 #include <QPen>
27 27 #include <QBrush>
28 28
29 29 class QModelIndex;
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 class QXYSeriesPrivate;
34 34
35 35 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
36 36 {
37 37 Q_OBJECT
38 38 protected:
39 39 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
40 40 ~QXYSeries();
41 41
42 42 public:
43 43 void append(qreal x, qreal y);
44 44 void append(const QPointF &point);
45 45 void append(const QList<QPointF> points);
46 46 void replace(qreal x,qreal y);
47 47 void replace(const QPointF &point);
48 48 void remove(qreal x);
49 49 void remove(qreal x, qreal y);
50 50 void remove(const QPointF &point);
51 51 void removeAll();
52 52
53 53 int count() const;
54 54 qreal x(int pos) const;
55 55 qreal y(int pos) const;
56 56 QList<QPointF> data();
57 57
58 58 QXYSeries& operator << (const QPointF &point);
59 59 QXYSeries& operator << (const QList<QPointF> points);
60 60
61 61 void setPen(const QPen &pen);
62 62 QPen pen() const;
63 63
64 64 void setBrush(const QBrush &brush);
65 65 QBrush brush() const;
66 66
67 67 void setPointsVisible(bool visible = true);
68 68 bool pointsVisible() const;
69 69
70 70 bool setModel(QAbstractItemModel *model);
71
72 71 virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
73 virtual void setModelMappingRange(int first, int count = 0);
74 int mapFirst() const;
75 int mapCount() const;
76 72
77 73 private Q_SLOTS:
78 74 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
79 void modelDataAboutToBeAdded(QModelIndex parent, int start, int end);
80 void modelDataAdded(QModelIndex parent, int start, int end);
81 void modelDataAboutToBeRemoved(QModelIndex parent, int start, int end);
82 void modelDataRemoved(QModelIndex parent, int start, int end);
83 75
84 76 Q_SIGNALS:
85 77 void clicked(const QPointF &point);
86 78 void selected();
87 79
88 80 private:
89 81 Q_DECLARE_PRIVATE(QXYSeries);
90 82 Q_DISABLE_COPY(QXYSeries);
91 83 friend class XYLegendMarker;
92 84 friend class XYChartItem;
93 85 };
94 86
95 87 QTCOMMERCIALCHART_END_NAMESPACE
96 88
97 89 #endif
@@ -1,79 +1,75
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QXYSERIES_P_H
31 31 #define QXYSERIES_P_H
32 32
33 33 #include "qabstractseries_p.h"
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 class QXYSeries;
38 38
39 39 class QXYSeriesPrivate: public QAbstractSeriesPrivate
40 40 {
41 41 Q_OBJECT
42 42
43 43 public:
44 44 QXYSeriesPrivate(QXYSeries* q);
45 45
46 46 void scaleDomain(Domain& domain);
47 47 QList<LegendMarker*> createLegendMarker(QLegend* legend);
48 48
49 49 Q_SIGNALS:
50 50 void updated();
51 51 void pointReplaced(int index);
52 52 void pointRemoved(int index);
53 53 void pointAdded(int index);
54 54
55 55 protected:
56 56 QVector<qreal> m_x;
57 57 QVector<qreal> m_y;
58 58
59 59 QPen m_pen;
60 60 QBrush m_brush;
61 61
62 62 int m_mapX;
63 63 int m_mapY;
64 int m_mapFirst;
65 int m_mapCount;
66 bool m_mapLimited;
67 64 Qt::Orientation m_mapOrientation;
68 int tempItemsRemoved;
69 65 bool m_pointsVisible;
70 66
71 67 private:
72 68 Q_DECLARE_PUBLIC(QXYSeries);
73 69 friend class QScatterSeries;
74 70
75 71 };
76 72
77 73 QTCOMMERCIALCHART_END_NAMESPACE
78 74
79 75 #endif
@@ -1,202 +1,178
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "xychartitem_p.h"
22 22 #include "qxyseries.h"
23 23 #include "qxyseries_p.h"
24 24 #include "chartpresenter_p.h"
25 25 #include "chartanimator_p.h"
26 26 #include <QPainter>
27 27 #include <QGraphicsSceneMouseEvent>
28 28
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 //TODO: optimize : remove points which are not visible
33 33
34 34 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
35 35 m_minX(0),
36 36 m_maxX(0),
37 37 m_minY(0),
38 38 m_maxY(0),
39 39 m_series(series)
40 40 {
41 41 connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
42 42 connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
43 43 connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
44 44 connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
45 45 }
46 46
47 47 QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
48 48 {
49 49 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
50 50 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
51 51 qreal x = (point.x() - m_minX)* deltaX;
52 52 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
53 53 return QPointF(x,y);
54 54 }
55 55
56 56
57 57 QPointF XYChartItem::calculateGeometryPoint(int index) const
58 58 {
59 59 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
60 60 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
61 61 qreal x = (m_series->x(index) - m_minX)* deltaX;
62 62 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
63 63 return QPointF(x,y);
64 64 }
65 65
66 66 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
67 67 {
68 68 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
69 69 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
70 70
71 71 QVector<QPointF> points;
72 72 points.reserve(m_series->count());
73 73 for (int i = 0; i < m_series->count(); ++i) {
74 74 qreal x = (m_series->x(i) - m_minX)* deltaX;
75 75 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
76 76 points << QPointF(x,y);
77 77 }
78 78 return points;
79 79 }
80 80
81 81 QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
82 82 {
83 83 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
84 84 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
85 85 qreal x = point.x()/deltaX +m_minX;
86 86 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
87 87 return QPointF(x,y);
88 88 }
89 89
90 90 void XYChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
91 91 {
92 92 if (animator()) {
93 93 animator()->updateLayout(this,oldPoints,newPoints,index);
94 94 } else {
95 95 setLayout(newPoints);
96 96 }
97 97 }
98 98
99 99 void XYChartItem::setLayout(QVector<QPointF> &points)
100 100 {
101 101 m_points = points;
102 102 update();
103 103 }
104 104
105 105 //handlers
106 106
107 107 void XYChartItem::handlePointAdded(int index)
108 108 {
109 109 QVector<QPointF> points = m_points;
110 110 QPointF point;
111 if (m_series->model()) {
112 point = calculateGeometryPoint(index - m_series->mapFirst());
113 if (index < m_series->mapFirst()) {
114 points.insert(0, point);
115 updateLayout(m_points, points, 0);
116 }
117 else {
118 points.insert(index - m_series->mapFirst(), point);
119 updateLayout(m_points, points, index - m_series->mapFirst());
120 }
121 }
122 else {
123 // this checks do not work correctly if model is set
124 Q_ASSERT(index<m_series->count());
125 Q_ASSERT(index>=0);
126 point = calculateGeometryPoint(index);
127 points.insert(index, point);
128 updateLayout(m_points, points, index);
129 }
111 Q_ASSERT(index<m_series->count());
112 Q_ASSERT(index>=0);
113 point = calculateGeometryPoint(index);
114 points.insert(index, point);
115 updateLayout(m_points, points, index);
130 116 update();
131 117 }
132 118 void XYChartItem::handlePointRemoved(int index)
133 119 {
134 120 QVector<QPointF> points = m_points;
135 if (m_series->model()) {
136 if (index < m_series->mapFirst())
137 points.remove(0);
138 else
139 points.remove(index - m_series->mapFirst());
140 updateLayout(m_points, points, index - m_series->mapFirst());
141 }
142 else {
143 // this checks do not work correctly if model is set
144 Q_ASSERT(index<m_series->count() + 1);
145 Q_ASSERT(index>=0);
146 points.remove(index);
147 updateLayout(m_points, points, index);
148 }
121 Q_ASSERT(index<m_series->count() + 1);
122 Q_ASSERT(index>=0);
123 points.remove(index);
124 updateLayout(m_points, points, index);
149 125 update();
150 126 }
151 127
152 128 void XYChartItem::handlePointReplaced(int index)
153 129 {
154 130 Q_ASSERT(index<m_series->count());
155 131 Q_ASSERT(index>=0);
156 132 QPointF point = calculateGeometryPoint(index);
157 133 QVector<QPointF> points = m_points;
158 134 points.replace(index,point);
159 135 updateLayout(m_points,points,index);
160 136 update();
161 137 }
162 138
163 139 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
164 140 {
165 141 m_minX=minX;
166 142 m_maxX=maxX;
167 143 m_minY=minY;
168 144 m_maxY=maxY;
169 145
170 146 if (isEmpty()) return;
171 147 QVector<QPointF> points = calculateGeometryPoints();
172 148 updateLayout(m_points,points);
173 149 update();
174 150 }
175 151
176 152 void XYChartItem::handleGeometryChanged(const QRectF &rect)
177 153 {
178 154 Q_ASSERT(rect.isValid());
179 155 m_size=rect.size();
180 156 m_clipRect=rect.translated(-rect.topLeft());
181 157 setPos(rect.topLeft());
182 158
183 159 if (isEmpty()) return;
184 160 QVector<QPointF> points = calculateGeometryPoints();
185 161 updateLayout(m_points,points);
186 162 update();
187 163 }
188 164
189 165
190 166 bool XYChartItem::isEmpty()
191 167 {
192 168 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY);
193 169 }
194 170
195 171 void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
196 172 {
197 173 emit clicked(calculateDomainPoint(event->pos()));
198 174 }
199 175
200 176 #include "moc_xychartitem_p.cpp"
201 177
202 178 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,336 +1,336
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "tablewidget.h"
22 22 #include <QGridLayout>
23 23 #include <QTableView>
24 24 #include <QStyledItemDelegate>
25 25 #include <QLineSeries>
26 26 #include <QSplineSeries>
27 27 #include <QScatterSeries>
28 28 #include "customtablemodel.h"
29 29 #include <QPieSeries>
30 30 #include <QPieSlice>
31 31 #include <QAreaSeries>
32 32 #include <QBarSeries>
33 33 #include <QBarSet>
34 34 #include <QPushButton>
35 35 #include <QRadioButton>
36 36 #include <QLabel>
37 37 #include <QSpinBox>
38 38 #include <QTime>
39 39
40 40 TableWidget::TableWidget(QWidget *parent)
41 41 : QWidget(parent)
42 42 {
43 43 setGeometry(100, 100, 1000, 600);
44 44 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
45 45 // create simple model for storing data
46 46 // user's table data model
47 47 m_model = new CustomTableModel;
48 48 m_tableView = new QTableView;
49 49 m_tableView->setModel(m_model);
50 50 m_tableView->setMinimumHeight(300);
51 51 // tableView->setMinimumSize(340, 480);
52 52 // tableView->setItemDelegate(new QStyledItemDelegate);
53 53 m_chart = new QChart;
54 54 m_chartView = new QChartView(m_chart);
55 55 m_chartView->setRenderHint(QPainter::Antialiasing);
56 56 m_chartView->setMinimumSize(640, 480);
57 57
58 58 // add, remove data buttons
59 59 QPushButton* addRowAboveButton = new QPushButton("Add row above");
60 60 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
61 61
62 62 QPushButton* addRowBelowButton = new QPushButton("Add row below");
63 63 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
64 64
65 65 QPushButton* removeRowButton = new QPushButton("Remove row");
66 66 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
67 67
68 68 QLabel *spinBoxLabel = new QLabel("Rows affected:");
69 69
70 70 // spin box for setting number of affected items (add, remove)
71 71 m_linesCountSpinBox = new QSpinBox;
72 72 m_linesCountSpinBox->setRange(1, 10);
73 73 m_linesCountSpinBox->setValue(1);
74 74
75 75 // buttons layout
76 76 QVBoxLayout* buttonsLayout = new QVBoxLayout;
77 77 buttonsLayout->addWidget(spinBoxLabel);
78 78 buttonsLayout->addWidget(m_linesCountSpinBox);
79 79 buttonsLayout->addWidget(addRowAboveButton);
80 80 buttonsLayout->addWidget(addRowBelowButton);
81 81 buttonsLayout->addWidget(removeRowButton);
82 82 buttonsLayout->addStretch();
83 83
84 84 // chart type radio buttons
85 85 m_lineRadioButton = new QRadioButton("Line");
86 86 m_splineRadioButton = new QRadioButton("Spline");
87 87 m_scatterRadioButton = new QRadioButton("Scatter");
88 88 m_pieRadioButton = new QRadioButton("Pie");
89 89 m_areaRadioButton = new QRadioButton("Area");
90 90 m_barRadioButton = new QRadioButton("Bar");
91 91
92 92 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
93 93 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
94 94 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
95 95 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
96 96 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
97 97 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
98 98 m_lineRadioButton->setChecked(true);
99 99
100 100 // radio buttons layout
101 101 QVBoxLayout* radioLayout = new QVBoxLayout;
102 102 radioLayout->addWidget(m_lineRadioButton);
103 103 radioLayout->addWidget(m_splineRadioButton);
104 104 radioLayout->addWidget(m_scatterRadioButton);
105 105 radioLayout->addWidget(m_pieRadioButton);
106 106 radioLayout->addWidget(m_areaRadioButton);
107 107 radioLayout->addWidget(m_barRadioButton);
108 108 radioLayout->addStretch();
109 109
110 110 // create main layout
111 111 QGridLayout* mainLayout = new QGridLayout;
112 112 mainLayout->addLayout(buttonsLayout, 1, 1);
113 113 mainLayout->addLayout(radioLayout, 2, 1);
114 114 mainLayout->addWidget(m_tableView, 1, 0);
115 115 mainLayout->addWidget(m_chartView, 2, 0);
116 116 setLayout(mainLayout);
117 117 m_lineRadioButton->setFocus();
118 118 }
119 119
120 120 void TableWidget::addRowAbove()
121 121 {
122 122 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
123 123
124 124 }
125 125
126 126 void TableWidget::addRowBelow()
127 127 {
128 128 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
129 129
130 130 }
131 131
132 132 void TableWidget::removeRow()
133 133 {
134 134 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
135 135 }
136 136
137 137 void TableWidget::updateChartType(bool toggle)
138 138 {
139 139 // this if is needed, so that the function is only called once.
140 140 // For the radioButton that was enabled.
141 141 if (toggle) {
142 142 m_chart->removeAllSeries();
143 143
144 144 // renable axes of the chart (pie hides them)
145 145 // x axis
146 146 QChartAxis *axis = m_chart->axisX();
147 147 axis->setAxisVisible(true);
148 148 axis->setGridLineVisible(true);
149 149 axis->setLabelsVisible(true);
150 150
151 151 // y axis
152 152 axis = m_chart->axisY();
153 153 axis->setAxisVisible(true);
154 154 axis->setGridLineVisible(true);
155 155 axis->setLabelsVisible(true);
156 156
157 157 m_model->clearMapping();
158 158
159 159 QString seriesColorHex = "#000000";
160 160 QPen pen;
161 161 pen.setWidth(2);
162 162
163 163 if (m_lineRadioButton->isChecked())
164 164 {
165 165 // series 1
166 166 m_series = new QLineSeries;
167 167 m_series->setModel(m_model);
168 168 m_series->setModelMapping(0,1, Qt::Vertical);
169 m_series->setModelMappingRange(1, 4);
169 // m_series->setModelMappingRange(1, 4);
170 170 m_chart->addSeries(m_series);
171 171 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
172 172 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
173 173
174 174 // series 2
175 175 m_series = new QLineSeries;
176 176 m_series->setModel(m_model);
177 177 m_series->setModelMapping(2,3, Qt::Vertical);
178 178 m_chart->addSeries(m_series);
179 179 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
180 180 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
181 181
182 182 // series 3
183 183 m_series = new QLineSeries;
184 184 m_series->setModel(m_model);
185 185 m_series->setModelMapping(4,5, Qt::Vertical);
186 m_series->setModelMappingRange(2, 0);
186 // m_series->setModelMappingRange(2, 0);
187 187 m_chart->addSeries(m_series);
188 188 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
189 189 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
190 190 }
191 191 else if (m_splineRadioButton->isChecked())
192 192 {
193 193 // series 1
194 194 m_series = new QSplineSeries;
195 195 m_series->setModel(m_model);
196 196 m_series->setModelMapping(0,1, Qt::Vertical);
197 m_series->setModelMappingRange(1, 4);
197 // m_series->setModelMappingRange(1, 4);
198 198 // series->setModelMapping(0,1, Qt::Horizontal);
199 199 m_chart->addSeries(m_series);
200 200 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
201 201 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
202 202
203 203 // series 2
204 204 m_series = new QSplineSeries;
205 205 m_series->setModel(m_model);
206 206 m_series->setModelMapping(2,3, Qt::Vertical);
207 m_series->setModelMappingRange(0, 0);
207 // m_series->setModelMappingRange(0, 0);
208 208 // series->setModelMapping(2,3, Qt::Horizontal);
209 209 m_chart->addSeries(m_series);
210 210 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
211 211 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
212 212
213 213 // series 3
214 214 m_series = new QSplineSeries;
215 215 m_series->setModel(m_model);
216 216 m_series->setModelMapping(4,5, Qt::Vertical);
217 m_series->setModelMappingRange(2, 0);
217 // m_series->setModelMappingRange(2, 0);
218 218 // series->setModelMapping(4,5, Qt::Horizontal);
219 219 m_chart->addSeries(m_series);
220 220 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
221 221 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
222 222 }
223 223 else if (m_scatterRadioButton->isChecked())
224 224 {
225 225 // series 1
226 226 m_series = new QScatterSeries;
227 227 m_series->setModel(m_model);
228 228 m_series->setModelMapping(0,1, Qt::Vertical);
229 m_series->setModelMappingRange(2, 0);
229 // m_series->setModelMappingRange(2, 0);
230 230 // series->setModelMapping(0,1, Qt::Horizontal);
231 231 m_chart->addSeries(m_series);
232 232
233 233 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
234 234 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
235 235
236 236 // series 2
237 237 m_series = new QScatterSeries;
238 238 m_series->setModel(m_model);
239 239 m_series->setModelMapping(2,3, Qt::Vertical);
240 m_series->setModelMappingRange(1, 6);
240 // m_series->setModelMappingRange(1, 6);
241 241 // series->setModelMapping(2,3, Qt::Horizontal);
242 242 m_chart->addSeries(m_series);
243 243
244 244 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
245 245 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
246 246
247 247 // series 3
248 248 m_series = new QScatterSeries;
249 249 m_series->setModel(m_model);
250 250 m_series->setModelMapping(4,5, Qt::Vertical);
251 251 // series->setModelMapping(4,5, Qt::Horizontal);
252 252 m_chart->addSeries(m_series);
253 253 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
254 254 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
255 255 }
256 256 else if (m_pieRadioButton->isChecked())
257 257 {
258 258 // pie 1
259 259 QPieSeries* pieSeries = new QPieSeries;
260 260 pieSeries->setModel(m_model);
261 261 pieSeries->setModelMapping(0,0, Qt::Vertical);
262 262 pieSeries->setLabelsVisible(true);
263 263 pieSeries->setPieSize(0.4);
264 264 pieSeries->setHorizontalPosition(0.2);
265 265 pieSeries->setVerticalPosition(0.35);
266 266
267 267 m_chart->addSeries(pieSeries);
268 268 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
269 269 m_model->addMapping(seriesColorHex, QRect(0, 0, 1, 1000));
270 270
271 271 // pie 2
272 272 pieSeries = new QPieSeries;
273 273 pieSeries->setModel(m_model);
274 274 pieSeries->setModelMapping(1,1, Qt::Vertical);
275 275 pieSeries->setLabelsVisible(true);
276 276 pieSeries->setPieSize(0.4);
277 277 pieSeries->setHorizontalPosition(0.8);
278 278 pieSeries->setVerticalPosition(0.35);
279 279 m_chart->addSeries(pieSeries);
280 280 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
281 281 m_model->addMapping(seriesColorHex, QRect(1, 0, 1, 1000));
282 282
283 283 // pie 3
284 284 pieSeries = new QPieSeries;
285 285 pieSeries->setModel(m_model);
286 286 pieSeries->setModelMapping(2,2, Qt::Vertical);
287 287 pieSeries->setLabelsVisible(true);
288 288 pieSeries->setPieSize(0.4);
289 289 pieSeries->setHorizontalPosition(0.5);
290 290 pieSeries->setVerticalPosition(0.65);
291 291 m_chart->addSeries(pieSeries);
292 292 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
293 293 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
294 294 }
295 295 else if (m_areaRadioButton->isChecked())
296 296 {
297 297 QLineSeries* upperLineSeries = new QLineSeries;
298 298 upperLineSeries->setModel(m_model);
299 299 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
300 upperLineSeries->setModelMappingRange(1, 5);
300 // upperLineSeries->setModelMappingRange(1, 5);
301 301 QLineSeries* lowerLineSeries = new QLineSeries;
302 302 lowerLineSeries->setModel(m_model);
303 303 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
304 304 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
305 305 m_chart->addSeries(areaSeries);
306 306 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
307 307 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
308 308 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
309 309 }
310 310 else if (m_barRadioButton->isChecked())
311 311 {
312 312 QBarSeries* barSeries = new QBarSeries(QStringList());
313 313 barSeries->setModel(m_model);
314 314 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
315 315 m_chart->addSeries(barSeries);
316 316 QList<QBarSet*> barsets = barSeries->barSets();
317 317 for (int i = 0; i < barsets.count(); i++) {
318 318 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
319 319 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
320 320 }
321 321 }
322 322
323 323
324 324 m_chart->axisX()->setRange(0, 500);
325 325 m_chart->axisY()->setRange(0, 120);
326 326
327 327 // repaint table view colors
328 328 m_tableView->repaint();
329 329 m_tableView->setFocus();
330 330 }
331 331 }
332 332
333 333 TableWidget::~TableWidget()
334 334 {
335 335
336 336 }
General Comments 0
You need to be logged in to leave comments. Login now