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