##// END OF EJS Templates
barchart: removed old model
sauimone -
r1005:a6e87336a436
parent child
Show More
@@ -1,50 +1,51
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 "drilldownchart.h"
22 22 #include <QChartAxis>
23 23
24 24 QTCOMMERCIALCHART_USE_NAMESPACE
25 25
26 26 DrilldownChart::DrilldownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 27 : QChart(parent, wFlags)
28 28 ,m_currentSeries(0)
29 29 {
30 30 }
31 31
32 32 void DrilldownChart::changeSeries(QAbstractSeries *series)
33 33 {
34 if (m_currentSeries)
34 if (m_currentSeries) {
35 35 removeSeries(m_currentSeries);
36 }
36 37 m_currentSeries = series;
37 38 addSeries(series);
38 39 setTitle(series->name());
39 40 axisY()->setNiceNumbersEnabled(true);
40 41 }
41 42
42 43 void DrilldownChart::handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button)
43 44 {
44 45 Q_UNUSED(barset)
45 46 Q_UNUSED(button)
46 47 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
47 48 changeSeries(series->drilldownSeries(category));
48 49 }
49 50
50 51 #include "moc_drilldownchart.cpp"
@@ -1,33 +1,31
1 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3
4 4 SOURCES += \
5 5 $$PWD/bar.cpp \
6 $$PWD/barchartmodel.cpp \
7 6 $$PWD/barchartitem.cpp \
8 7 $$PWD/percentbarchartitem.cpp \
9 8 $$PWD/qbarseries.cpp \
10 9 $$PWD/qbarset.cpp \
11 10 $$PWD/qpercentbarseries.cpp \
12 11 $$PWD/qstackedbarseries.cpp \
13 12 $$PWD/stackedbarchartitem.cpp \
14 13 $$PWD/barlabel.cpp
15 14
16 15 PRIVATE_HEADERS += \
17 16 $$PWD/bar_p.h \
18 $$PWD/barchartmodel_p.h \
19 17 $$PWD/barchartitem_p.h \
20 18 $$PWD/percentbarchartitem_p.h \
21 19 $$PWD/stackedbarchartitem_p.h \
22 20 $$PWD/barlabel_p.h \
23 21 $$PWD/qbarset_p.h \
24 22 $$PWD/qbarseries_p.h \
25 23 $$PWD/qstackedbarseries_p.h\
26 24 $$PWD/qpercentbarseries_p.h
27 25
28 26 PUBLIC_HEADERS += \
29 27 $$PWD/qbarseries.h \
30 28 $$PWD/qbarset.h \
31 29 $$PWD/qpercentbarseries.h \
32 30 $$PWD/qstackedbarseries.h
33 31
@@ -1,86 +1,84
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 BARCHARTITEM_H
22 22 #define BARCHARTITEM_H
23 23
24 24 #include "chartitem_p.h"
25 25 #include "qbarseries.h"
26 26 #include <QPen>
27 27 #include <QBrush>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class Bar;
32 32 class BarLabel;
33 33 class QChartAxisCategories;
34 34 class QChart;
35 35
36 36 //typedef QVector<QRectF> BarLayout;
37 37
38 38 class BarChartItem : public ChartItem
39 39 {
40 40 Q_OBJECT
41 41 public:
42 42 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
43 43 virtual ~BarChartItem();
44 44
45 45 public:
46 46 // From QGraphicsItem
47 47 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
48 48 QRectF boundingRect() const;
49 49
50 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
51 50 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
52 51
53 52 virtual QVector<QRectF> calculateLayout();
54 53 void applyLayout(const QVector<QRectF> &layout);
55 54 void setLayout(const QVector<QRectF> &layout);
56 55 void updateLayout(const QVector<QRectF> &layout);
57 56
58 57 QRectF geometry() const { return m_rect;}
59 58
60 59 public Q_SLOTS:
61 60 void handleModelChanged();
62 61 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
63 62 void handleGeometryChanged(const QRectF &size);
64 63 void handleLayoutChanged();
65 64
66 65 protected:
67 66
68 // TODO: consider these.
69 67 qreal m_domainMinX;
70 68 qreal m_domainMaxX;
71 69 qreal m_domainMinY;
72 70 qreal m_domainMaxY;
73 71
74 72 QRectF m_rect;
75 73 bool m_layoutSet; // True, if component has been laid out.
76 74 QVector<QRectF> m_layout;
77 75
78 76 // Not owned.
79 77 QBarSeries *m_series;
80 78 QList<Bar *> m_bars;
81 79 QList<BarLabel *> m_labels;
82 80 };
83 81
84 82 QTCOMMERCIALCHART_END_NAMESPACE
85 83
86 84 #endif // BARCHARTITEM_H
@@ -1,456 +1,512
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 #include "barchartmodel_p.h"
26 25 #include "domain_p.h"
27 26 #include "legendmarker_p.h"
28 27 #include "chartdataset_p.h"
29 28 #include "charttheme_p.h"
30 29 #include "chartanimator_p.h"
31 30
32 31 #include <QAbstractItemModel>
33 32 #include <QModelIndex>
34 33
35 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 35
37 36 /*!
38 37 \class QBarSeries
39 38 \brief part of QtCommercial chart API.
40 39 \mainclass
41 40
42 41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
43 42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
44 43 by QStringList.
45 44
46 45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
47 46 \image examples_barchart.png
48 47
49 48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
50 49 */
51 50
52 51 /*!
53 52 \fn void QBarSeries::clicked(QBarSet *barset, QString category, Qt::MouseButtons button)
54 53
55 54 The signal is emitted if the user clicks with a mouse \a button on top of QBarSet \a barset of category \a category
56 55 contained by the series.
57 56 */
58 57
59 58 /*!
60 59 \fn void QBarSeries::selected()
61 60
62 61 The signal is emitted if the user selects/deselects the series. The logic for storing selections should be
63 62 implemented by the user of QBarSeries API.
64 63 */
65 64
66 65 /*!
67 66 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
68 67
69 68 The signal is emitted if mouse is hovered on top of series.
70 69 Parameter \a barset is the pointer of barset, where hover happened.
71 70 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
72 71 */
73 72
74 73 /*!
75 74 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
76 75 QBarSeries is QObject which is a child of a \a parent.
77 76 */
78 77 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) :
79 78 QAbstractSeries(*new QBarSeriesPrivate(categories, this),parent)
80 79 {
81 80 }
82 81
83 82 /*!
84 83 Destructs barseries and owned barsets.
85 84 */
86 85 QBarSeries::~QBarSeries()
87 86 {
88 87 // NOTE: d_ptr destroyed by QObject
89 88 }
90 89
91 90 /*!
92 91 \internal
93 92 */
94 93 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
95 94 QAbstractSeries(d,parent)
96 95 {
97 96 }
98 97
99 98 /*!
100 99 Returns the type of series. Derived classes override this.
101 100 */
102 101 QAbstractSeries::QSeriesType QBarSeries::type() const
103 102 {
104 103 return QAbstractSeries::SeriesTypeBar;
105 104 }
106 105
107 106 /*!
108 107 Adds a set of bars to series. Takes ownership of \a set.
109 108 Connects the clicked(QString, Qt::MouseButtons) signal
110 109 of \a set to this series
111 110 */
112 111 void QBarSeries::appendBarSet(QBarSet *set)
113 112 {
114 113 Q_D(QBarSeries);
115 d->m_internalModel->appendBarSet(set);
114 d->m_barSets.append(set);
116 115 QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged()));
117 116 emit d->restructuredBars();
118 117 }
119 118
120 119 /*!
121 120 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
122 121 Disconnects the clicked(QString, Qt::MouseButtons) signal
123 122 of \a set from this series
124 123 */
125 124 void QBarSeries::removeBarSet(QBarSet *set)
126 125 {
127 126 Q_D(QBarSeries);
128 d->m_internalModel->removeBarSet(set);
129 emit d->restructuredBars();
127 if (d->m_barSets.contains(set)) {
128 d->m_barSets.removeOne(set);
129 emit d->restructuredBars();
130 }
130 131 }
131 132
132 133 /*!
133 134 Adds a list of barsets to series. Takes ownership of \a sets.
134 135 Connects the clicked(QString, Qt::MouseButtons) signals
135 136 of \a sets to this series
136 137 */
137 138 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
138 139 {
139 140 Q_D(QBarSeries);
140 141 foreach (QBarSet* barset, sets) {
141 d->m_internalModel->appendBarSet(barset);
142 d->m_barSets.append(barset);
142 143 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
143 144 }
144 145 emit d->restructuredBars();
145
146 146 }
147 147
148 148 /*!
149 149 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
150 150 Disconnects the clicked(QString, Qt::MouseButtons) signal
151 151 of \a sets from this series
152 152 */
153 153 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
154 154 {
155 155 Q_D(QBarSeries);
156 156
157 157 foreach (QBarSet* barset, sets) {
158 d->m_internalModel->removeBarSet(barset);
158 if (d->m_barSets.contains(barset)) {
159 d->m_barSets.removeOne(barset);
160 }
159 161 }
160 162 emit d->restructuredBars();
161 163 }
162 164
163 165 /*!
164 166 Inserts new \a set on the \a i position.
165 167 The barset that is currently at this postion is moved to postion i + 1
166 168 */
167 169 void QBarSeries::insertBarSet(int i, QBarSet *set)
168 170 {
169 171 Q_D(QBarSeries);
170 d->m_internalModel->insertBarSet(i, set);
172 d->m_barSets.insert(i,set);
171 173 emit d->barsetChanged();
172 174 }
173 175
174 176 /*!
175 177 Returns number of sets in series.
176 178 */
177 179 int QBarSeries::barsetCount() const
178 180 {
179 181 Q_D(const QBarSeries);
180 return d->m_internalModel->barsetCount();
182 return d->m_barSets.count();
181 183 }
182 184
183 185 /*!
184 186 Returns number of categories in series
185 187 */
186 188 int QBarSeries::categoryCount() const
187 189 {
188 190 Q_D(const QBarSeries);
189 return d->m_internalModel->categoryCount();
191 return d->m_categories.count();
190 192 }
191 193
192 194 /*!
193 195 Returns a list of sets in series. Keeps ownership of sets.
194 196 */
195 197 QList<QBarSet*> QBarSeries::barSets() const
196 198 {
197 199 Q_D(const QBarSeries);
198 return d->m_internalModel->barSets();
200 return d->m_barSets;
199 201 }
200 202
201 203 /*!
202 204 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
203 205 Sets the \a model to be used as a data source
204 206 */
205 207 bool QBarSeries::setModel(QAbstractItemModel *model)
206 208 {
207 209 Q_D(QBarSeries);
208 210 return d->setModel(model);
209 211 }
210 212
211 213 /*!
212 214 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
213 215 Sets column/row specified by \a categories to be used as a list of bar series categories.
214 216 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
215 217 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
216 218 All the columns/rows inbetween those two values are also used as data for bar sets.
217 219 The \a orientation parameter specifies whether the data is in columns or in rows.
218 220 */
219 221 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
220 222 {
221 223 Q_D(QBarSeries);
222 224 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
223 225 }
224 226
225 227 /*!
226 228 Returns the bar categories of the series.
227 229 */
228 230 QBarCategories QBarSeries::categories() const
229 231 {
230 232 Q_D(const QBarSeries);
231
232 QBarCategories categories;
233 int count = d->m_internalModel->categoryCount();
234 for (int i=1; i <= count; i++) {
235 categories.insert(i, d->m_internalModel->categoryName(i - 1));
236 }
237 return categories;
238
233 return d->m_categories;
239 234 }
240 235
241 236 /*!
242 237 Sets the visibility of labels in series to \a visible
243 238 */
244 239 void QBarSeries::setLabelsVisible(bool visible)
245 240 {
246 241 foreach (QBarSet* s, barSets()) {
247 242 s->setLabelsVisible(visible);
248 243 }
249 244 }
250 245
251 246 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
252 247
253 248 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) :
254 249 QAbstractSeriesPrivate(q),
255 m_internalModel(new BarChartModel(categories,this)),
250 m_categories(categories),
256 251 m_model(0),
257 252 m_mapCategories(-1),
258 253 m_mapBarBottom(-1),
259 254 m_mapBarTop(-1),
260 255 m_mapOrientation(Qt::Vertical)
261 256 {
262 257 }
263 258
264 259 QBarSet* QBarSeriesPrivate::barsetAt(int index)
265 260 {
266 return m_internalModel->barsetAt(index);
261 return m_barSets.at(index);
267 262 }
268 263
269 264 QString QBarSeriesPrivate::categoryName(int category)
270 265 {
271 return m_internalModel->categoryName(category);
266 return m_categories.at(category);
272 267 }
273 268
274 269 qreal QBarSeriesPrivate::min()
275 270 {
276 return m_internalModel->min();
271 if (m_barSets.count() <= 0) {
272 return 0;
273 }
274 qreal min = INT_MAX;
275
276 for (int i = 0; i < m_barSets.count(); i++) {
277 int categoryCount = m_barSets.at(i)->count();
278 for (int j = 0; j < categoryCount; j++) {
279 qreal temp = m_barSets.at(i)->at(j);
280 if (temp < min)
281 min = temp;
282 }
283 }
284 return min;
277 285 }
278 286
279 287 qreal QBarSeriesPrivate::max()
280 288 {
281 return m_internalModel->max();
289 if (m_barSets.count() <= 0) {
290 return 0;
291 }
292 qreal max = INT_MIN;
293
294 for (int i = 0; i < m_barSets.count(); i++) {
295 int categoryCount = m_barSets.at(i)->count();
296 for (int j = 0; j < categoryCount; j++) {
297 qreal temp = m_barSets.at(i)->at(j);
298 if (temp > max)
299 max = temp;
300 }
301 }
302
303 return max;
282 304 }
283 305
284 306 qreal QBarSeriesPrivate::valueAt(int set, int category)
285 307 {
286 return m_internalModel->valueAt(set, category);
308 if ((set < 0) || (set >= m_barSets.count())) {
309 // No set, no value.
310 return 0;
311 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
312 // No category, no value.
313 return 0;
314 }
315
316 return m_barSets.at(set)->at(category);
287 317 }
288 318
289 319 qreal QBarSeriesPrivate::percentageAt(int set, int category)
290 320 {
291 return m_internalModel->percentageAt(set, category);
321 if ((set < 0) || (set >= m_barSets.count())) {
322 // No set, no value.
323 return 0;
324 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
325 // No category, no value.
326 return 0;
327 }
328
329 qreal value = m_barSets.at(set)->at(category);
330 qreal sum = categorySum(category);
331 if ( qFuzzyIsNull(sum) ) {
332 return 0;
333 }
334
335 return value / sum;
292 336 }
293 337
294 338 qreal QBarSeriesPrivate::categorySum(int category)
295 339 {
296 return m_internalModel->categorySum(category);
340 qreal sum(0);
341 int count = m_barSets.count(); // Count sets
342 for (int set = 0; set < count; set++) {
343 if (category < m_barSets.at(set)->count())
344 sum += m_barSets.at(set)->at(category);
345 }
346 return sum;
297 347 }
298 348
299 349 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
300 350 {
301 return m_internalModel->absoluteCategorySum(category);
351 qreal sum(0);
352 int count = m_barSets.count(); // Count sets
353 for (int set = 0; set < count; set++) {
354 if (category < m_barSets.at(set)->count())
355 sum += qAbs(m_barSets.at(set)->at(category));
356 }
357 return sum;
302 358 }
303 359
304 360 qreal QBarSeriesPrivate::maxCategorySum()
305 361 {
306 return m_internalModel->maxCategorySum();
307 }
308
309 BarChartModel& QBarSeriesPrivate::modelInternal()
310 {
311 return *m_internalModel;
362 qreal max = INT_MIN;
363 int count = m_categories.count();
364 for (int i = 0; i < count; i++) {
365 qreal sum = categorySum(i);
366 if (sum > max)
367 max = sum;
368 }
369 return max;
312 370 }
313 371
314 372 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
315 373 {
316 374 // disconnect signals from old model
317 375 if(m_model)
318 376 {
319 377 disconnect(m_model, 0, this, 0);
320 378 m_mapCategories = -1;
321 379 m_mapBarBottom = -1;
322 380 m_mapBarTop = -1;
323 381 m_mapOrientation = Qt::Vertical;
324 382 }
325 383
326 384 // set new model
327 385 if(model)
328 386 {
329 387 m_model = model;
330 388 return true;
331 389 }
332 390 else
333 391 {
334 392 m_model = 0;
335 393 return false;
336 394 }
337 395 }
338 396
339 397 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
340 398 {
341 399 Q_Q(QBarSeries);
342 400
343 401 if (m_model == 0)
344 402 return;
345 403
346 404 m_mapCategories = categories;
347 405 m_mapBarBottom = bottomBoundry;
348 406 m_mapBarTop = topBoundry;
349 407 m_mapOrientation = orientation;
350 408
351 409 // connect the signals
352 410 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
353 411 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
354 412
355 413 // create the initial bars
356 delete m_internalModel;
414 m_categories.clear();
357 415 if (m_mapOrientation == Qt::Vertical) {
358 QStringList categories;
359 for (int k = 0; k < m_model->rowCount(); k++)
360 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
361 m_internalModel = new BarChartModel(categories, this);
416 for (int k = 0; k < m_model->rowCount(); k++) {
417 m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
418 }
362 419
363 420 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
364 421 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
365 422 for(int m = 0; m < m_model->rowCount(); m++)
366 423 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
367 424 q->appendBarSet(barSet);
368 425 }
369 426 } else {
370 QStringList categories;
371 for (int k = 0; k < m_model->columnCount(); k++)
372 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
373 m_internalModel = new BarChartModel(categories, this);
427 for (int k = 0; k < m_model->columnCount(); k++) {
428 m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
429 }
374 430
375 431 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
376 432 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
377 433 for(int m = 0; m < m_model->columnCount(); m++)
378 434 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
379 435 q->appendBarSet(barSet);
380 436 }
381 437 }
382 438 }
383 439
384 440 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
385 441 {
386 442 Q_UNUSED(bottomRight)
387 443
388 444 if (m_mapOrientation == Qt::Vertical)
389 445 {
390 446 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
391 447 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop)
392 448 barsetAt(topLeft.column() - m_mapBarBottom)->replace(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
393 449 }
394 450 else
395 451 {
396 452 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
397 453 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop)
398 454 barsetAt(topLeft.row() - m_mapBarBottom)->replace(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
399 455 }
400 456 }
401 457
402 458 void QBarSeriesPrivate::barsetChanged()
403 459 {
404 460 emit updatedBars();
405 461 }
406 462
407 463 void QBarSeriesPrivate::scaleDomain(Domain& domain)
408 464 {
409 465 qreal minX(domain.minX());
410 466 qreal minY(domain.minY());
411 467 qreal maxX(domain.maxX());
412 468 qreal maxY(domain.maxY());
413 469 int tickXCount(domain.tickXCount());
414 470 int tickYCount(domain.tickYCount());
415 471
416 qreal x = m_internalModel->categoryCount();
472 qreal x = m_categories.count();
417 473 qreal y = max();
418 474 minX = qMin(minX, x);
419 475 minY = qMin(minY, y);
420 476 maxX = qMax(maxX, x);
421 477 maxY = qMax(maxY, y);
422 478 tickXCount = x+1;
423 479
424 480 domain.setRangeX(minX,maxX,tickXCount);
425 481 domain.setRangeY(minY,maxY,tickYCount);
426 482 }
427 483
428 484 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
429 485 {
430 486 Q_Q(QBarSeries);
431 487
432 488 BarChartItem* bar = new BarChartItem(q,presenter);
433 489 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
434 490 presenter->animator()->addAnimation(bar);
435 491 }
436 492 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
437 493 return bar;
438 494
439 495 }
440 496
441 497 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
442 498 {
443 499 Q_Q(QBarSeries);
444 500 QList<LegendMarker*> markers;
445 501 foreach(QBarSet* set, q->barSets()) {
446 502 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
447 503 markers << marker;
448 504 }
449 505
450 506 return markers;
451 507 }
452 508
453 509 #include "moc_qbarseries.cpp"
454 510 #include "moc_qbarseries_p.cpp"
455 511
456 512 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,80 +1,79
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 class BarChartModel;
35 34 class BarCategory;
36 35 class QBarSeriesPrivate;
37 36
38 37 // Container for series
39 38 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
40 39 {
41 40 Q_OBJECT
42 41 public:
43 42 explicit QBarSeries(QBarCategories categories, QObject *parent = 0);
44 43 virtual ~QBarSeries();
45 44
46 45 QAbstractSeries::QSeriesType type() const;
47 46
48 47 void appendBarSet(QBarSet *set); // Takes ownership of set
49 48 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
50 49 void appendBarSets(QList<QBarSet* > sets);
51 50 void removeBarSets(QList<QBarSet* > sets);
52 51 void insertBarSet(int i, QBarSet *set);
53 52 int barsetCount() const;
54 53 int categoryCount() const;
55 54 QList<QBarSet*> barSets() const;
56 55 QBarCategories categories() const;
57 56
58 57 void setLabelsVisible(bool visible = true);
59 58
60 59 bool setModel(QAbstractItemModel *model);
61 60 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
62 61
63 62 protected:
64 63 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
65 64
66 65 Q_SIGNALS:
67 66 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
68 67 void selected();
69 68 void hovered(QBarSet* barset, bool status);
70 69
71 70 protected:
72 71 Q_DECLARE_PRIVATE(QBarSeries)
73 72 friend class BarChartItem;
74 73 friend class PercentBarChartItem;
75 74 friend class StackedBarChartItem;
76 75 };
77 76
78 77 QTCOMMERCIALCHART_END_NAMESPACE
79 78
80 79 #endif // BARSERIES_H
@@ -1,62 +1,63
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 BarChartModel& modelInternal();
37 36
38 37 Q_SIGNALS:
39 38 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
40 39 void selected();
41 40 void updatedBars();
42 41 void restructuredBars();
43 42
44 43 private Q_SLOTS:
45 44 // slots for updating bars when data in model changes
46 45 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
47 46 void barsetChanged();
48 47
49 48 protected:
50 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
49 QList<QBarSet *> m_barSets;
50 QBarCategories m_categories;
51
51 52 QAbstractItemModel* m_model;
52 53 int m_mapCategories;
53 54 int m_mapBarBottom;
54 55 int m_mapBarTop;
55 56 Qt::Orientation m_mapOrientation;
56 57 private:
57 58 Q_DECLARE_PUBLIC(QBarSeries)
58 59 };
59 60
60 61 QTCOMMERCIALCHART_END_NAMESPACE
61 62
62 63 #endif // QBARSERIESPRIVATE_P_H
@@ -1,110 +1,109
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 "qstackedbarseries.h"
22 22 #include "qstackedbarseries_p.h"
23 23 #include "stackedbarchartitem_p.h"
24 #include "barchartmodel_p.h"
25 24 #include "chartdataset_p.h"
26 25 #include "charttheme_p.h"
27 26 #include "chartanimator_p.h"
28 27
29 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 29
31 30 /*!
32 31 \class QStackedBarSeries
33 32 \brief part of QtCommercial chart API.
34 33 \mainclass
35 34
36 35 QStackedBarSeries represents a series of data shown as bars. All bars in same category are
37 36 stacked on top of each other. One QStackedBarSeries can contain multiple QBarSet data sets.
38 37 QStackedBarSeries groups the data from sets to categories, which are defined by QStringList.
39 38
40 39 See the \l {StackedbarChart Example} {stacked bar chart example} to learn how to create a stacked bar chart.
41 40 \image examples_stackedbarchart.png
42 41
43 42 \sa QBarSet, QPercentBarSeries, QBarSeries
44 43 */
45 44
46 45 /*!
47 46 \fn virtual QSeriesType QStackedBarSeries::type() const
48 47 \brief Returns type of series.
49 48 \sa QSeriesType
50 49 */
51 50
52 51 /*!
53 52 Constructs empty QStackedBarSeries. Parameter \a categories defines the categories for chart.
54 53 QStackedBarSeries is QObject which is a child of a \a parent.
55 54 */
56 55 QStackedBarSeries::QStackedBarSeries(QBarCategories categories, QObject *parent)
57 56 : QBarSeries(*new QStackedBarSeriesPrivate(categories,this), parent)
58 57 {
59 58 }
60 59
61 60 QAbstractSeries::QSeriesType QStackedBarSeries::type() const
62 61 {
63 62 return QAbstractSeries::SeriesTypeStackedBar;
64 63 }
65 64
66 65 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67 66
68 67 QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QBarCategories categories, QStackedBarSeries *q) : QBarSeriesPrivate(categories,q)
69 68 {
70 69
71 70 }
72 71
73 72 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
74 73 {
75 74 qreal minX(domain.minX());
76 75 qreal minY(domain.minY());
77 76 qreal maxX(domain.maxX());
78 77 qreal maxY(domain.maxY());
79 78 int tickXCount(domain.tickXCount());
80 79 int tickYCount(domain.tickYCount());
81 80
82 qreal x = m_internalModel->categoryCount();
81 qreal x = m_categories.count();
83 82 qreal y = maxCategorySum();
84 83 minX = qMin(minX, x);
85 84 minY = qMin(minY, y);
86 85 maxX = qMax(maxX, x);
87 86 maxY = qMax(maxY, y);
88 87 tickXCount = x+1;
89 88
90 89 domain.setRangeX(minX,maxX,tickXCount);
91 90 domain.setRangeY(minY,maxY,tickYCount);
92 91 }
93 92
94 93
95 94 Chart* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
96 95 {
97 96 Q_Q(QStackedBarSeries);
98 97
99 98 StackedBarChartItem* bar = new StackedBarChartItem(q,presenter);
100 99 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
101 100 presenter->animator()->addAnimation(bar);
102 101 }
103 102 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
104 103 return bar;
105 104 }
106 105
107 106 #include "moc_qstackedbarseries.cpp"
108 107
109 108 QTCOMMERCIALCHART_END_NAMESPACE
110 109
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now