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