##// END OF EJS Templates
visible property to barseries
sauimone -
r1284:38cef2676df3
parent child
Show More
@@ -1,213 +1,215
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 "barchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "qbarset.h"
24 24 #include "qbarset_p.h"
25 25 #include "qbarseries.h"
26 26 #include "qbarseries_p.h"
27 27 #include "qchart.h"
28 28 #include "chartpresenter_p.h"
29 29 #include "chartanimator_p.h"
30 30 #include "chartdataset_p.h"
31 31 #include <QPainter>
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
36 36 ChartItem(presenter),
37 37 m_series(series)
38 38 {
39 39 setFlag(ItemClipsChildrenToShape);
40 40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
41 41 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
42 42 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(labelsVisibleChanged(bool)));
43 43 setZValue(ChartPresenter::BarSeriesZValue);
44 44 dataChanged();
45 45 }
46 46
47 47 BarChartItem::~BarChartItem()
48 48 {
49 49 }
50 50
51 51 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 52 {
53 53 Q_UNUSED(painter);
54 54 Q_UNUSED(option);
55 55 Q_UNUSED(widget);
56 56 }
57 57
58 58 QRectF BarChartItem::boundingRect() const
59 59 {
60 60 return m_rect;
61 61 }
62 62
63 63 void BarChartItem::dataChanged()
64 64 {
65 65 foreach(QGraphicsItem *item, childItems()) {
66 66 delete item;
67 67 }
68 68
69 69 m_bars.clear();
70 70 m_labels.clear();
71 71 m_layout.clear();
72 72
73 73 bool labelsVisible = m_series->isLabelsVisible();
74 74
75 75 // Create new graphic items for bars
76 76 for (int c = 0; c < m_series->categoryCount(); c++) {
77 77 QString category = m_series->d_func()->categoryName(c);
78 78 for (int s = 0; s < m_series->barsetCount(); s++) {
79 79 QBarSet *set = m_series->d_func()->barsetAt(s);
80 80
81 81 // Bars
82 82 Bar *bar = new Bar(set,category,this);
83 83 m_bars.append(bar);
84 84 connect(bar, SIGNAL(clicked(QBarSet*,QString)), m_series, SIGNAL(clicked(QBarSet*,QString)));
85 85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
86 86 m_layout.append(QRectF(0, 0, 0, 0));
87 87
88 88 // Labels
89 89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
90 90 label->setVisible(labelsVisible);
91 91 m_labels.append(label);
92 92 }
93 93 }
94 94
95 95 // TODO: Is this the right place to call it?
96 96 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
97 97 }
98 98
99 99 QVector<QRectF> BarChartItem::calculateLayout()
100 100 {
101 101 QVector<QRectF> layout;
102 102
103 103 // Use temporary qreals for accurancy
104 104 qreal categoryCount = m_series->categoryCount();
105 105 qreal setCount = m_series->barsetCount();
106 bool barsVisible = m_series->isVisible();
106 107
107 108 // Domain:
108 109 qreal width = geometry().width();
109 110 qreal height = geometry().height();
110 111 qreal rangeY = m_domainMaxY - m_domainMinY;
111 112 qreal rangeX = m_domainMaxX - m_domainMinX;
112 113 qreal scaleY = (height / rangeY);
113 114 qreal scaleX = (width / rangeX);
114 115 qreal categoryWidth = width / categoryCount;
115 116 qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin();
116 117
117 118 int itemIndex(0);
118 119 for (int category = 0; category < categoryCount; category++) {
119 120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
120 121 for (int set = 0; set < setCount; set++) {
121 122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
122 123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
123 124 qreal barHeight = barSet->at(category).y() * scaleY;
124 125
125 126 Bar* bar = m_bars.at(itemIndex);
126 127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
127 128
128 129 layout.append(rect);
129 130 bar->setPen(barSet->pen());
130 131 bar->setBrush(barSet->brush());
132 bar->setVisible(barsVisible);
131 133
132 134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
133 135
134 136 if (!qFuzzyIsNull(barSet->at(category).y())) {
135 137 label->setText(QString::number(barSet->at(category).y()));
136 138 } else {
137 139 label->setText(QString(""));
138 140 }
139 141
140 142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
141 143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
142 144 label->setFont(barSet->labelFont());
143 145
144 146 itemIndex++;
145 147 }
146 148 }
147 149
148 150 return layout;
149 151 }
150 152
151 153 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
152 154 {
153 155 if (animator())
154 156 animator()->updateLayout(this, m_layout, layout);
155 157 else
156 158 setLayout(layout);
157 159 }
158 160
159 161 void BarChartItem::setLayout(const QVector<QRectF> &layout)
160 162 {
161 163 m_layout = layout;
162 164
163 165 for (int i=0; i < m_bars.count(); i++)
164 166 m_bars.at(i)->setRect(layout.at(i));
165 167
166 168 update();
167 169 }
168 170 //handlers
169 171
170 172 void BarChartItem::handleModelChanged()
171 173 {
172 174 // dataChanged();
173 175 presenter()->resetAllElements();
174 176 }
175 177
176 178 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
177 179 {
178 180 m_domainMinX = minX;
179 181 m_domainMaxX = maxX;
180 182 m_domainMinY = minY;
181 183 m_domainMaxY = maxY;
182 184 handleLayoutChanged();
183 185 }
184 186
185 187 void BarChartItem::handleGeometryChanged(const QRectF &rect)
186 188 {
187 189 prepareGeometryChange();
188 190 m_rect = rect;
189 191 handleLayoutChanged();
190 192 }
191 193
192 194 void BarChartItem::handleLayoutChanged()
193 195 {
194 196 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
195 197 // rect size zero.
196 198 return;
197 199 }
198 200 QVector<QRectF> layout = calculateLayout();
199 201 applyLayout(layout);
200 202 update();
201 203 }
202 204
203 205 void BarChartItem::labelsVisibleChanged(bool visible)
204 206 {
205 207 foreach (QGraphicsSimpleTextItem* label, m_labels) {
206 208 label->setVisible(visible);
207 209 }
208 210 update();
209 211 }
210 212
211 213 #include "moc_barchartitem_p.cpp"
212 214
213 215 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,536 +1,568
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 "domain_p.h"
26 26 #include "legendmarker_p.h"
27 27 #include "chartdataset_p.h"
28 28 #include "charttheme_p.h"
29 29 #include "chartanimator_p.h"
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 /*!
34 34 \class QBarSeries
35 35 \brief part of QtCommercial chart API.
36 36 \mainclass
37 37
38 38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
39 39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
40 40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
41 41 shows the x-values.
42 42
43 43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
44 44 \image examples_barchart.png
45 45
46 46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 47 */
48 48
49 49 /*!
50 50 \fn void QBarSeries::clicked(QBarSet *barset, QString category)
51 51
52 52 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category
53 53 contained by the series.
54 54 */
55 55
56 56 /*!
57 57 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
58 58
59 59 The signal is emitted if mouse is hovered on top of series.
60 60 Parameter \a barset is the pointer of barset, where hover happened.
61 61 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
62 62 */
63 63
64 64 /*!
65 65 Constructs empty QBarSeries.
66 66 QBarSeries is QObject which is a child of a \a parent.
67 67 */
68 68 QBarSeries::QBarSeries(QObject *parent) :
69 69 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
70 70 {
71 71 }
72 72
73 73 /*!
74 74 Destructs barseries and owned barsets.
75 75 */
76 76 QBarSeries::~QBarSeries()
77 77 {
78 78 Q_D(QBarSeries);
79 79 if(d->m_dataset){
80 80 d->m_dataset->removeSeries(this);
81 81 }
82 82 }
83 83
84 84 /*!
85 85 \internal
86 86 */
87 87 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
88 88 QAbstractSeries(d,parent)
89 89 {
90 90 }
91 91
92 92 /*!
93 93 Returns the type of series. Derived classes override this.
94 94 */
95 95 QAbstractSeries::SeriesType QBarSeries::type() const
96 96 {
97 97 return QAbstractSeries::SeriesTypeBar;
98 98 }
99 99
100 100 /*!
101 101 Sets the \a categories, which are used to to group the data.
102 102 */
103 103 void QBarSeries::setCategories(QBarCategories categories)
104 104 {
105 105 Q_D(QBarSeries);
106 106 d->setCategories(categories);
107 107 emit d->categoriesUpdated();
108 108 }
109 109
110 110 void QBarSeries::setBarMargin(qreal margin)
111 111 {
112 112 Q_D(QBarSeries);
113 113 d->setBarMargin(margin);
114 114 }
115 115
116 qreal QBarSeries::barMargin() const
117 {
118 Q_D(const QBarSeries);
119 return d->barMargin();
120 }
121
116 122 /*!
117 123 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
118 124 Returns true, if appending succeeded.
119 125
120 126 */
121 127 bool QBarSeries::append(QBarSet *set)
122 128 {
123 129 Q_D(QBarSeries);
124 130 return d->append(set);
125 131 }
126 132
127 133 /*!
128 134 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
129 135 Returns true, if set was removed.
130 136 */
131 137 bool QBarSeries::remove(QBarSet *set)
132 138 {
133 139 Q_D(QBarSeries);
134 140 return d->remove(set);
135 141 }
136 142
137 143 /*!
138 144 Adds a list of barsets to series. Takes ownership of \a sets.
139 145 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
140 146 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
141 147 and function returns false.
142 148 */
143 149 bool QBarSeries::append(QList<QBarSet* > sets)
144 150 {
145 151 Q_D(QBarSeries);
146 152 return d->append(sets);
147 153 }
148 154
149 155 /*!
150 156 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
151 157 */
152 158 bool QBarSeries::remove(QList<QBarSet* > sets)
153 159 {
154 160 Q_D(QBarSeries);
155 161 return d->remove(sets);
156 162 }
157 163
158 164 /*!
159 165 Returns number of sets in series.
160 166 */
161 167 int QBarSeries::barsetCount() const
162 168 {
163 169 Q_D(const QBarSeries);
164 170 return d->m_barSets.count();
165 171 }
166 172
167 173 /*!
168 174 Returns number of categories in series
169 175 */
170 176 int QBarSeries::categoryCount() const
171 177 {
172 178 Q_D(const QBarSeries);
173 179 return d->categoryCount();
174 180 }
175 181
176 182 /*!
177 183 Returns a list of sets in series. Keeps ownership of sets.
178 184 */
179 185 QList<QBarSet*> QBarSeries::barSets() const
180 186 {
181 187 Q_D(const QBarSeries);
182 188 return d->m_barSets;
183 189 }
184 190
185 191 /*!
186 192 Returns the bar categories of the series.
187 193 */
188 194 QBarCategories QBarSeries::categories() const
189 195 {
190 196 Q_D(const QBarSeries);
191 197 return d->categories();
192 198 }
193 199
200 void QBarSeries::setVisible(bool visible)
201 {
202 Q_D(QBarSeries);
203 d->setVisible(visible);
204 }
205
206 bool QBarSeries::isVisible() const
207 {
208 Q_D(const QBarSeries);
209 return d->isVisible();
210 }
211
194 212 /*!
195 213 Sets the visibility of labels in series to \a visible
196 214 */
197 215 void QBarSeries::setLabelsVisible(bool visible)
198 216 {
199 217 Q_D(QBarSeries);
200 218 if (d->m_labelsVisible != visible) {
201 219 d->m_labelsVisible = visible;
202 220 emit d->updatedBars();
203 221 }
204 222 }
205 223
206 224 bool QBarSeries::isLabelsVisible() const
207 225 {
208 226 Q_D(const QBarSeries);
209 227 return d->m_labelsVisible;
210 228 }
211 229
212 230 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
213 231
214 232 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
215 233 QAbstractSeriesPrivate(q),
216 234 m_barMargin(0.05), // Default value is 5% of category width
217 m_labelsVisible(false)
235 m_labelsVisible(false),
236 m_visible(true)
218 237 {
219 238 }
220 239
221 240 void QBarSeriesPrivate::setCategories(QBarCategories categories)
222 241 {
223 242 m_categories = categories;
224 243 }
225 244
226 245 void QBarSeriesPrivate::insertCategory(int index, const QString category)
227 246 {
228 247 m_categories.insert(index, category);
229 248 emit categoriesUpdated();
230 249 }
231 250
232 251 void QBarSeriesPrivate::removeCategory(int index)
233 252 {
234 253 m_categories.removeAt(index);
235 254 emit categoriesUpdated();
236 255 }
237 256
238 257 int QBarSeriesPrivate::categoryCount() const
239 258 {
240 259 if (m_categories.count() > 0) {
241 260 return m_categories.count();
242 261 }
243 262
244 263 // No categories defined. return count of longest set.
245 264 int count = 0;
246 265 for (int i=0; i<m_barSets.count(); i++) {
247 266 if (m_barSets.at(i)->count() > count) {
248 267 count = m_barSets.at(i)->count();
249 268 }
250 269 }
251 270
252 271 return count;
253 272 }
254 273
255 274 QBarCategories QBarSeriesPrivate::categories() const
256 275 {
257 276 if (m_categories.count() > 0) {
258 277 return m_categories;
259 278 }
260 279
261 280 // No categories defined. retun list of indices.
262 281 QBarCategories categories;
263 282
264 283 int count = categoryCount();
265 284 for (int i = 0; i < count; i++) {
266 285 categories.append(QString::number(i));
267 286 }
268 287 return categories;
269 288 }
270 289
271 290 void QBarSeriesPrivate::setBarMargin(qreal margin)
272 291 {
273 292 if (margin > 1.0) {
274 293 margin = 1.0;
275 294 } else if (margin < 0.0) {
276 295 margin = 0.0;
277 296 }
278 297
279 298 m_barMargin = margin;
280 299 emit updatedBars();
281 300 }
282 301
283 qreal QBarSeriesPrivate::barMargin()
302 qreal QBarSeriesPrivate::barMargin() const
284 303 {
285 304 return m_barMargin;
286 305 }
287 306
288 307 QBarSet* QBarSeriesPrivate::barsetAt(int index)
289 308 {
290 309 return m_barSets.at(index);
291 310 }
292 311
312 void QBarSeriesPrivate::setVisible(bool visible)
313 {
314 if (m_visible != visible) {
315 m_visible = visible;
316 emit updatedBars();
317 }
318 }
319
320 bool QBarSeriesPrivate::isVisible() const
321 {
322 return m_visible;
323 }
324
293 325 QString QBarSeriesPrivate::categoryName(int category)
294 326 {
295 327 if ((category >= 0) && (category < m_categories.count())) {
296 328 return m_categories.at(category);
297 329 }
298 330
299 331 return QString::number(category);
300 332 }
301 333
302 334 qreal QBarSeriesPrivate::min()
303 335 {
304 336 if (m_barSets.count() <= 0) {
305 337 return 0;
306 338 }
307 339 qreal min = INT_MAX;
308 340
309 341 for (int i = 0; i < m_barSets.count(); i++) {
310 342 int categoryCount = m_barSets.at(i)->count();
311 343 for (int j = 0; j < categoryCount; j++) {
312 344 qreal temp = m_barSets.at(i)->at(j).y();
313 345 if (temp < min)
314 346 min = temp;
315 347 }
316 348 }
317 349 return min;
318 350 }
319 351
320 352 qreal QBarSeriesPrivate::max()
321 353 {
322 354 if (m_barSets.count() <= 0) {
323 355 return 0;
324 356 }
325 357 qreal max = INT_MIN;
326 358
327 359 for (int i = 0; i < m_barSets.count(); i++) {
328 360 int categoryCount = m_barSets.at(i)->count();
329 361 for (int j = 0; j < categoryCount; j++) {
330 362 qreal temp = m_barSets.at(i)->at(j).y();
331 363 if (temp > max)
332 364 max = temp;
333 365 }
334 366 }
335 367
336 368 return max;
337 369 }
338 370
339 371 qreal QBarSeriesPrivate::valueAt(int set, int category)
340 372 {
341 373 if ((set < 0) || (set >= m_barSets.count())) {
342 374 // No set, no value.
343 375 return 0;
344 376 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
345 377 // No category, no value.
346 378 return 0;
347 379 }
348 380
349 381 return m_barSets.at(set)->at(category).y();
350 382 }
351 383
352 384 qreal QBarSeriesPrivate::percentageAt(int set, int category)
353 385 {
354 386 if ((set < 0) || (set >= m_barSets.count())) {
355 387 // No set, no value.
356 388 return 0;
357 389 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
358 390 // No category, no value.
359 391 return 0;
360 392 }
361 393
362 394 qreal value = m_barSets.at(set)->at(category).y();
363 395 qreal sum = categorySum(category);
364 396 if ( qFuzzyIsNull(sum) ) {
365 397 return 0;
366 398 }
367 399
368 400 return value / sum;
369 401 }
370 402
371 403 qreal QBarSeriesPrivate::categorySum(int category)
372 404 {
373 405 qreal sum(0);
374 406 int count = m_barSets.count(); // Count sets
375 407 for (int set = 0; set < count; set++) {
376 408 if (category < m_barSets.at(set)->count())
377 409 sum += m_barSets.at(set)->at(category).y();
378 410 }
379 411 return sum;
380 412 }
381 413
382 414 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
383 415 {
384 416 qreal sum(0);
385 417 int count = m_barSets.count(); // Count sets
386 418 for (int set = 0; set < count; set++) {
387 419 if (category < m_barSets.at(set)->count())
388 420 sum += qAbs(m_barSets.at(set)->at(category).y());
389 421 }
390 422 return sum;
391 423 }
392 424
393 425 qreal QBarSeriesPrivate::maxCategorySum()
394 426 {
395 427 qreal max = INT_MIN;
396 428 int count = categoryCount();
397 429 for (int i = 0; i < count; i++) {
398 430 qreal sum = categorySum(i);
399 431 if (sum > max)
400 432 max = sum;
401 433 }
402 434 return max;
403 435 }
404 436
405 437 void QBarSeriesPrivate::barsetChanged()
406 438 {
407 439 emit updatedBars();
408 440 }
409 441
410 442 void QBarSeriesPrivate::scaleDomain(Domain& domain)
411 443 {
412 444 qreal minX(domain.minX());
413 445 qreal minY(domain.minY());
414 446 qreal maxX(domain.maxX());
415 447 qreal maxY(domain.maxY());
416 448 int tickXCount(domain.tickXCount());
417 449 int tickYCount(domain.tickYCount());
418 450
419 451 qreal x = categoryCount();
420 452 qreal y = max();
421 453 minX = qMin(minX, x) - 0.5;
422 454 minY = qMin(minY, y);
423 455 maxX = qMax(maxX, x) - 0.5;
424 456 maxY = qMax(maxY, y);
425 457 tickXCount = x+1;
426 458
427 459 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
428 460 }
429 461
430 462 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
431 463 {
432 464 Q_Q(QBarSeries);
433 465
434 466 BarChartItem* bar = new BarChartItem(q,presenter);
435 467 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
436 468 presenter->animator()->addAnimation(bar);
437 469 }
438 470 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
439 471 return bar;
440 472
441 473 }
442 474
443 475 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
444 476 {
445 477 Q_Q(QBarSeries);
446 478 QList<LegendMarker*> markers;
447 479 foreach(QBarSet* set, q->barSets()) {
448 480 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
449 481 markers << marker;
450 482 }
451 483
452 484 return markers;
453 485 }
454 486
455 487 bool QBarSeriesPrivate::append(QBarSet *set)
456 488 {
457 489 Q_Q(QBarSeries);
458 490 if ((m_barSets.contains(set)) || (set == 0)) {
459 491 // Fail if set is already in list or set is null.
460 492 return false;
461 493 }
462 494 m_barSets.append(set);
463 495 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
464 496 if (m_dataset) {
465 497 m_dataset->updateSeries(q); // this notifies legend
466 498 }
467 499 emit restructuredBars(); // this notifies barchartitem
468 500 return true;
469 501 }
470 502
471 503 bool QBarSeriesPrivate::remove(QBarSet *set)
472 504 {
473 505 Q_Q(QBarSeries);
474 506 if (!m_barSets.contains(set)) {
475 507 // Fail if set is not in list
476 508 return false;
477 509 }
478 510 m_barSets.removeOne(set);
479 511 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
480 512 if (m_dataset) {
481 513 m_dataset->updateSeries(q); // this notifies legend
482 514 }
483 515 emit restructuredBars(); // this notifies barchartitem
484 516 return true;
485 517 }
486 518
487 519 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
488 520 {
489 521 Q_Q(QBarSeries);
490 522 foreach (QBarSet* set, sets) {
491 523 if ((set == 0) || (m_barSets.contains(set))) {
492 524 // Fail if any of the sets is null or is already appended.
493 525 return false;
494 526 }
495 527 if (sets.count(set) != 1) {
496 528 // Also fail if same set is more than once in given list.
497 529 return false;
498 530 }
499 531 }
500 532
501 533 foreach (QBarSet* set, sets) {
502 534 m_barSets.append(set);
503 535 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
504 536 }
505 537 if (m_dataset) {
506 538 m_dataset->updateSeries(q); // this notifies legend
507 539 }
508 540 emit restructuredBars(); // this notifies barchartitem
509 541 return true;
510 542 }
511 543
512 544 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
513 545 {
514 546 Q_Q(QBarSeries);
515 547 bool setsRemoved = false;
516 548 foreach (QBarSet* set, sets) {
517 549 if (m_barSets.contains(set)) {
518 550 m_barSets.removeOne(set);
519 551 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
520 552 setsRemoved = true;
521 553 }
522 554 }
523 555
524 556 if (setsRemoved) {
525 557 if (m_dataset) {
526 558 m_dataset->updateSeries(q); // this notifies legend
527 559 }
528 560 emit restructuredBars(); // this notifies barchartitem
529 561 }
530 562 return setsRemoved;
531 563 }
532 564
533 565 #include "moc_qbarseries.cpp"
534 566 #include "moc_qbarseries_p.cpp"
535 567
536 568 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,77 +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 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 typedef QStringList QBarCategories;
30 30
31 31 class QBarSet;
32 32 class BarCategory;
33 33 class QBarSeriesPrivate;
34 34
35 35 // Container for series
36 36 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
37 37 {
38 38 Q_OBJECT
39 39 public:
40 40 explicit QBarSeries(QObject *parent = 0);
41 41 virtual ~QBarSeries();
42 42
43 43 QAbstractSeries::SeriesType type() const;
44 44 void setCategories(QBarCategories categories);
45 45
46 46 void setBarMargin(qreal margin);
47 qreal barMargin() const;
47 48
48 49 bool append(QBarSet *set);
49 50 bool remove(QBarSet *set);
50 51 bool append(QList<QBarSet* > sets);
51 52 bool remove(QList<QBarSet* > sets);
52 53 int barsetCount() const;
53 54 int categoryCount() const;
54 55 QList<QBarSet*> barSets() const;
55 56 QBarCategories categories() const;
56 57
58 void setVisible(bool visible = true);
59 bool isVisible() const;
57 60 void setLabelsVisible(bool visible = true);
58 61 bool isLabelsVisible() const;
59 62
60 63 protected:
61 64 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
62 65
63 66 Q_SIGNALS:
64 67 void clicked(QBarSet *barset, QString category);
65 68 void hovered(QBarSet* barset, bool status);
66 69
67 70 protected:
68 71 Q_DECLARE_PRIVATE(QBarSeries)
69 72 friend class BarChartItem;
70 73 friend class PercentBarChartItem;
71 74 friend class StackedBarChartItem;
72 75 friend class GroupedBarChartItem;
73 76 };
74 77
75 78 QTCOMMERCIALCHART_END_NAMESPACE
76 79
77 80 #endif // BARSERIES_H
@@ -1,97 +1,101
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 QBARSERIES_P_H
31 31 #define QBARSERIES_P_H
32 32
33 33 #include "qbarseries.h"
34 34 #include "qabstractseries_p.h"
35 35 #include <QStringList>
36 36 #include <QAbstractSeries>
37 37
38 38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 39
40 40 class QBarModelMapper;
41 41
42 42 class QBarSeriesPrivate : public QAbstractSeriesPrivate
43 43 {
44 44 Q_OBJECT
45 45 public:
46 46 QBarSeriesPrivate(QBarSeries *parent);
47 47 void setCategories(QBarCategories categories);
48 48 void insertCategory(int index, const QString category);
49 49 void removeCategory(int index);
50 50 int categoryCount() const;
51 51 QBarCategories categories() const;
52 52
53 53 void setBarMargin(qreal margin);
54 qreal barMargin();
54 qreal barMargin() const;
55
56 void setVisible(bool visible);
57 bool isVisible() const;
55 58
56 59 void scaleDomain(Domain& domain);
57 60 Chart* createGraphics(ChartPresenter* presenter);
58 61 QList<LegendMarker*> createLegendMarker(QLegend* legend);
59 62
60 63 bool append(QBarSet *set);
61 64 bool remove(QBarSet *set);
62 65 bool append(QList<QBarSet* > sets);
63 66 bool remove(QList<QBarSet* > sets);
64 67
65 68 QBarSet* barsetAt(int index);
66 69 QString categoryName(int category);
67 70 qreal min();
68 71 qreal max();
69 72 qreal valueAt(int set, int category);
70 73 qreal percentageAt(int set, int category);
71 74 qreal categorySum(int category);
72 75 qreal absoluteCategorySum(int category);
73 76 qreal maxCategorySum();
74 77
75 78 Q_SIGNALS:
76 79 void clicked(QBarSet *barset, QString category);
77 80 void updatedBars();
78 81 void restructuredBars();
79 82 void categoriesUpdated();
80 83 void labelsVisibleChanged(bool visible);
81 84
82 85 private Q_SLOTS:
83 86 void barsetChanged();
84 87
85 88 protected:
86 89 QList<QBarSet *> m_barSets;
87 90 QBarCategories m_categories;
88 91 qreal m_barMargin;
89 92 bool m_labelsVisible;
93 bool m_visible;
90 94
91 95 private:
92 96 Q_DECLARE_PUBLIC(QBarSeries)
93 97 };
94 98
95 99 QTCOMMERCIALCHART_END_NAMESPACE
96 100
97 101 #endif // QBARSERIESPRIVATE_P_H
General Comments 0
You need to be logged in to leave comments. Login now