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