@@ -1,94 +1,94 | |||
|
1 | 1 | /*! |
|
2 | 2 | \page classes.html |
|
3 | 3 | \title QtCommercial Charts API |
|
4 | 4 | \keyword All Classes |
|
5 | 5 | |
|
6 | 6 | Charts API is build on top of Qt Graphics View Framework. Charts can be displayed as QGraphicsWidget using QChart class. However |
|
7 | 7 | there is also convenience class QChartView which is QWidget based. These lets quickly use QCharts as normal Qt widget. |
|
8 | 8 | The API supports following chart types: |
|
9 | 9 | |
|
10 | 10 | \list |
|
11 | 11 | \o area |
|
12 | 12 | \o line |
|
13 | 13 | \o bar |
|
14 | 14 | \o percent bar |
|
15 | 15 | \o pie |
|
16 | 16 | \o scatter |
|
17 | 17 | \o spline |
|
18 | 18 | \o stacked bar |
|
19 | 19 | \endlist |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | Each chart type is represented by QSeries derived class. To create given chart type users have to use instace of releted sereis class and add it to QChart/QChartView instance. |
|
23 | 23 | \code |
|
24 | 24 | QLineSeries* series = new QLineSeries(); |
|
25 | 25 | series->add(0, 6); |
|
26 | 26 | series->add(2, 4); |
|
27 | 27 | ... |
|
28 |
chart |
|
|
28 | chart->addSeries(series); | |
|
29 | 29 | \endcode |
|
30 | 30 | |
|
31 | 31 | \raw HTML |
|
32 | 32 | <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable"> |
|
33 | 33 | <tr> |
|
34 | 34 | <th class="titleheader" width="33%"> |
|
35 | 35 | List of classes |
|
36 | 36 | </th> |
|
37 | 37 | </tr> |
|
38 | 38 | <tr> |
|
39 | 39 | <td valign="top"> |
|
40 | 40 | <ul> |
|
41 | 41 | <li><a href="qareaseries.html">QAreaSeries</a></li> |
|
42 | 42 | <li><a href="qbarseries.html">QBarSeries</a></li> |
|
43 | 43 | <li><a href="qbarset.html">QBarSet</a></li> |
|
44 | 44 | <li><a href="qchart.html">QChart</a></li> |
|
45 | 45 | <li><a href="qaxis.html">QAxis</a></li> |
|
46 | 46 | <li><a href="qchartview.html">QChartView</a></li> |
|
47 | 47 | <li><a href="qlineseries.html">QLineSeries</a></li> |
|
48 | 48 | <li><a href="qpercentbarseries.html">QPercentBarSeries</a></li> |
|
49 | 49 | <li><a href="qpieseries.html">QPieSeries</a></li> |
|
50 | 50 | <li><a href="qpieslice.html">QPieSlice</a></li> |
|
51 | 51 | <li><a href="qscatterseries.html">QScatterSeries</a></li> |
|
52 | 52 | <li><a href="qabstractseries.html">QAbstractSeries</a></li> |
|
53 | 53 | <li><a href="qsplineseries.html">QSplineSeries</a></li> |
|
54 | 54 | <li><a href="qstackedbarseries.html">QStackedBarSeries</a></li> |
|
55 | 55 | <li><a href="qxyseries.html">QXYSeries</a></li> |
|
56 | 56 | <li><a href="qlegend.html">QLegend</a></li> |
|
57 | 57 | <li><a href="qpiemodelmapper.html">QPieModelMapper</a></li> |
|
58 | 58 | <ul> |
|
59 | 59 | <li><a href="qhpiemodelmapper.html">QHPieModelMapper</a></li> |
|
60 | 60 | <li><a href="qvpiemodelmapper.html">QVPieModelMapper</a></li> |
|
61 | 61 | </ul> |
|
62 | 62 | <li><a href="qbarmodelmapper.html">QBarModelMapper</a></li> |
|
63 | 63 | <ul> |
|
64 | 64 | <li><a href="qhbarmodelmapper.html">QHBarModelMapper</a></li> |
|
65 | 65 | <li><a href="qvbarmodelmapper.html">QVBarModelMapper</a></li> |
|
66 | 66 | </ul> |
|
67 | 67 | <li><a href="qxymodelmapper.html">QXYModelMapper</a></li> |
|
68 | 68 | <ul> |
|
69 | 69 | <li><a href="qhxymodelmapper.html">QHXYModelMapper</a></li> |
|
70 | 70 | <li><a href="qvxymodelmapper.html">QVXYModelMapper</a></li> |
|
71 | 71 | </ul> |
|
72 | 72 | </ul> |
|
73 | 73 | </td> |
|
74 | 74 | </tr> |
|
75 | 75 | </table> |
|
76 | 76 | |
|
77 | 77 | <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable"> |
|
78 | 78 | <tr> |
|
79 | 79 | <th class="titleheader" width="33%"> |
|
80 | 80 | Other files: |
|
81 | 81 | </th> |
|
82 | 82 | </tr> |
|
83 | 83 | <tr> |
|
84 | 84 | <td valign="top"> |
|
85 | 85 | <ul> |
|
86 | 86 | <li><a href="qchartglobal.html">QChartGlobal</a></li> |
|
87 | 87 | </ul> |
|
88 | 88 | </td> |
|
89 | 89 | </tr> |
|
90 | 90 | </table> |
|
91 | 91 | |
|
92 | 92 | \endraw |
|
93 | 93 | |
|
94 | 94 | */ |
@@ -1,616 +1,650 | |||
|
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 | \property QBarSeries::barMargin |
|
51 | 51 | \brief Defines the margin around bars. |
|
52 | 52 | |
|
53 | 53 | Value is from 0 to 1 and represents |
|
54 | 54 | percentage of margin compared to bars |
|
55 | 55 | */ |
|
56 | 56 | |
|
57 | 57 | /*! |
|
58 | 58 | \property QBarSeries::count |
|
59 | 59 | \brief Holds the number of sets in series. |
|
60 | 60 | */ |
|
61 | 61 | |
|
62 | 62 | /*! |
|
63 | 63 | \property QBarSeries::labelsVisible |
|
64 | 64 | \brief Defines the visibility of the labels in series |
|
65 | 65 | */ |
|
66 | 66 | |
|
67 | 67 | /*! |
|
68 | 68 | \fn void QBarSeries::clicked(QBarSet *barset, int index) |
|
69 | 69 | |
|
70 | 70 | The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset. |
|
71 | 71 | Clicked bar inside set is indexed by \a index |
|
72 | 72 | */ |
|
73 | 73 | |
|
74 | 74 | /*! |
|
75 | 75 | \fn void QBarSeries::hovered(QBarSet* barset, bool status) |
|
76 | 76 | |
|
77 | 77 | The signal is emitted if mouse is hovered on top of series. |
|
78 | 78 | Parameter \a barset is the pointer of barset, where hover happened. |
|
79 | 79 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
80 | 80 | */ |
|
81 | 81 | |
|
82 | 82 | /*! |
|
83 | 83 | \fn void QBarSeries::visibleChanged() |
|
84 | 84 | */ |
|
85 | 85 | |
|
86 | 86 | /*! |
|
87 | 87 | \fn void QBarSeries::labelsVisibleChanged() |
|
88 | 88 | |
|
89 | 89 | This signal is emitted when labels visibility have changed. |
|
90 | 90 | |
|
91 |
\sa |
|
|
91 | \sa isLabelsVisible(), setLabelsVisible() | |
|
92 | 92 | */ |
|
93 | 93 | |
|
94 | 94 | /*! |
|
95 | 95 | \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets) |
|
96 | 96 | |
|
97 | 97 | This signal is emitted when \a sets have been added to the series. |
|
98 | 98 | |
|
99 | 99 | \sa append(), insert() |
|
100 | 100 | */ |
|
101 | 101 | |
|
102 | 102 | /*! |
|
103 | 103 | \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets) |
|
104 | 104 | |
|
105 | 105 | This signal is emitted when \a sets have been removed from the series. |
|
106 | 106 | |
|
107 | 107 | \sa remove() |
|
108 | 108 | */ |
|
109 | 109 | |
|
110 | 110 | /*! |
|
111 | 111 | Constructs empty QBarSeries. |
|
112 | 112 | QBarSeries is QObject which is a child of a \a parent. |
|
113 | 113 | */ |
|
114 | 114 | QBarSeries::QBarSeries(QObject *parent) : |
|
115 | 115 | QAbstractSeries(*new QBarSeriesPrivate(this),parent) |
|
116 | 116 | { |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | /*! |
|
120 | 120 | Destructs barseries and owned barsets. |
|
121 | 121 | */ |
|
122 | 122 | QBarSeries::~QBarSeries() |
|
123 | 123 | { |
|
124 | 124 | Q_D(QBarSeries); |
|
125 | 125 | if(d->m_dataset){ |
|
126 | 126 | d->m_dataset->removeSeries(this); |
|
127 | 127 | } |
|
128 | 128 | } |
|
129 | 129 | |
|
130 | 130 | /*! |
|
131 | 131 | \internal |
|
132 | 132 | */ |
|
133 | 133 | QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) : |
|
134 | 134 | QAbstractSeries(d,parent) |
|
135 | 135 | { |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | /*! |
|
139 | 139 | Returns the type of series. Derived classes override this. |
|
140 | 140 | */ |
|
141 | 141 | QAbstractSeries::SeriesType QBarSeries::type() const |
|
142 | 142 | { |
|
143 | 143 | return QAbstractSeries::SeriesTypeBar; |
|
144 | 144 | } |
|
145 | 145 | |
|
146 | 146 | /*! |
|
147 | 147 | Sets the margin around bars. Parameter \a margin is from 0 to 1 and represents |
|
148 | 148 | percentage of margin compared to bars |
|
149 | 149 | */ |
|
150 | 150 | void QBarSeries::setBarMargin(qreal margin) |
|
151 | 151 | { |
|
152 | 152 | Q_D(QBarSeries); |
|
153 | 153 | d->setBarMargin(margin); |
|
154 | 154 | } |
|
155 | 155 | |
|
156 | 156 | /*! |
|
157 | 157 | Returns the margin around bars |
|
158 | 158 | */ |
|
159 | 159 | qreal QBarSeries::barMargin() const |
|
160 | 160 | { |
|
161 | 161 | Q_D(const QBarSeries); |
|
162 | 162 | return d->barMargin(); |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | /*! |
|
166 | 166 | 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. |
|
167 | 167 | Returns true, if appending succeeded. |
|
168 | 168 | |
|
169 | 169 | */ |
|
170 | 170 | bool QBarSeries::append(QBarSet *set) |
|
171 | 171 | { |
|
172 | 172 | Q_D(QBarSeries); |
|
173 | 173 | bool success = d->append(set); |
|
174 | 174 | if (success) { |
|
175 | 175 | QList<QBarSet*> sets; |
|
176 | 176 | sets.append(set); |
|
177 | 177 | emit barsetsAdded(sets); |
|
178 | 178 | } |
|
179 | 179 | return success; |
|
180 | 180 | } |
|
181 | 181 | |
|
182 | 182 | /*! |
|
183 | 183 | Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set. |
|
184 | 184 | Returns true, if set was removed. |
|
185 | 185 | */ |
|
186 | 186 | bool QBarSeries::remove(QBarSet *set) |
|
187 | 187 | { |
|
188 | 188 | Q_D(QBarSeries); |
|
189 | 189 | bool success = d->remove(set); |
|
190 | 190 | if (success) { |
|
191 | 191 | QList<QBarSet*> sets; |
|
192 | 192 | sets.append(set); |
|
193 | 193 | emit barsetsRemoved(sets); |
|
194 | 194 | } |
|
195 | 195 | return success; |
|
196 | 196 | } |
|
197 | 197 | |
|
198 | 198 | /*! |
|
199 | 199 | Adds a list of barsets to series. Takes ownership of \a sets. |
|
200 | 200 | Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series, |
|
201 | 201 | nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended |
|
202 | 202 | and function returns false. |
|
203 | 203 | */ |
|
204 | 204 | bool QBarSeries::append(QList<QBarSet* > sets) |
|
205 | 205 | { |
|
206 | 206 | Q_D(QBarSeries); |
|
207 | 207 | bool success = d->append(sets); |
|
208 | 208 | if (success) { |
|
209 | 209 | emit barsetsAdded(sets); |
|
210 | 210 | } |
|
211 | 211 | return success; |
|
212 | 212 | } |
|
213 | 213 | |
|
214 | 214 | /*! |
|
215 | 215 | Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets. |
|
216 | 216 | */ |
|
217 | 217 | bool QBarSeries::remove(QList<QBarSet* > sets) |
|
218 | 218 | { |
|
219 | 219 | Q_D(QBarSeries); |
|
220 | 220 | bool success = d->remove(sets); |
|
221 | 221 | if (success) { |
|
222 | 222 | emit barsetsRemoved(sets); |
|
223 | 223 | } |
|
224 | 224 | return success; |
|
225 | 225 | } |
|
226 | 226 | |
|
227 | 227 | /*! |
|
228 | Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended. | |
|
229 | Returns true, if inserting succeeded. | |
|
230 | ||
|
231 | */ | |
|
232 | bool QBarSeries::insert(int index, QBarSet *set) | |
|
233 | { | |
|
234 | Q_D(QBarSeries); | |
|
235 | bool success = d->insert(index, set); | |
|
236 | if (success) { | |
|
237 | QList<QBarSet*> sets; | |
|
238 | sets.append(set); | |
|
239 | emit barsetsAdded(sets); | |
|
240 | } | |
|
241 | return success; | |
|
242 | } | |
|
243 | ||
|
244 | /*! | |
|
228 | 245 | Removes all of the bar sets from the series |
|
229 | 246 | */ |
|
230 | 247 | void QBarSeries::clear() |
|
231 | 248 | { |
|
232 | 249 | Q_D(QBarSeries); |
|
233 | 250 | d->remove(barSets()); |
|
234 | 251 | } |
|
235 | 252 | |
|
236 | 253 | /*! |
|
237 | 254 | Returns number of sets in series. |
|
238 | 255 | */ |
|
239 | 256 | int QBarSeries::barsetCount() const |
|
240 | 257 | { |
|
241 | 258 | Q_D(const QBarSeries); |
|
242 | 259 | return d->m_barSets.count(); |
|
243 | 260 | } |
|
244 | 261 | |
|
245 | 262 | /*! |
|
246 | 263 | Returns a list of sets in series. Keeps ownership of sets. |
|
247 | 264 | */ |
|
248 | 265 | QList<QBarSet*> QBarSeries::barSets() const |
|
249 | 266 | { |
|
250 | 267 | Q_D(const QBarSeries); |
|
251 | 268 | return d->m_barSets; |
|
252 | 269 | } |
|
253 | 270 | |
|
254 | 271 | /*! |
|
255 | 272 | Sets the visibility of labels in series to \a visible |
|
256 | 273 | */ |
|
257 | 274 | void QBarSeries::setLabelsVisible(bool visible) |
|
258 | 275 | { |
|
259 | 276 | Q_D(QBarSeries); |
|
260 | 277 | if (d->m_labelsVisible != visible) { |
|
261 | 278 | d->setLabelsVisible(visible); |
|
262 | 279 | emit labelsVisibleChanged(); |
|
263 | 280 | } |
|
264 | 281 | } |
|
265 | 282 | |
|
266 | 283 | /*! |
|
267 | 284 | Returns the visibility of labels |
|
268 | 285 | */ |
|
269 |
bool QBarSeries:: |
|
|
286 | bool QBarSeries::isLabelsVisible() const | |
|
270 | 287 | { |
|
271 | 288 | Q_D(const QBarSeries); |
|
272 | 289 | return d->m_labelsVisible; |
|
273 | 290 | } |
|
274 | 291 | |
|
275 | 292 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
276 | 293 | |
|
277 | 294 | QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : |
|
278 | 295 | QAbstractSeriesPrivate(q), |
|
279 | 296 | m_barMargin(0.5), // Default value is 50% of category width |
|
280 | 297 | m_labelsVisible(false), |
|
281 | 298 | m_visible(true) |
|
282 | 299 | { |
|
283 | 300 | } |
|
284 | 301 | |
|
285 | 302 | void QBarSeriesPrivate::setCategories(QStringList categories) |
|
286 | 303 | { |
|
287 | 304 | m_categories = categories; |
|
288 | 305 | } |
|
289 | 306 | |
|
290 | 307 | void QBarSeriesPrivate::insertCategory(int index, const QString category) |
|
291 | 308 | { |
|
292 | 309 | m_categories.insert(index, category); |
|
293 | 310 | emit categoriesUpdated(); |
|
294 | 311 | } |
|
295 | 312 | |
|
296 | 313 | void QBarSeriesPrivate::removeCategory(int index) |
|
297 | 314 | { |
|
298 | 315 | m_categories.removeAt(index); |
|
299 | 316 | emit categoriesUpdated(); |
|
300 | 317 | } |
|
301 | 318 | |
|
302 | 319 | int QBarSeriesPrivate::categoryCount() const |
|
303 | 320 | { |
|
304 | 321 | if (m_categories.count() > 0) { |
|
305 | 322 | return m_categories.count(); |
|
306 | 323 | } |
|
307 | 324 | |
|
308 | 325 | // No categories defined. return count of longest set. |
|
309 | 326 | int count = 0; |
|
310 | 327 | for (int i=0; i<m_barSets.count(); i++) { |
|
311 | 328 | if (m_barSets.at(i)->count() > count) { |
|
312 | 329 | count = m_barSets.at(i)->count(); |
|
313 | 330 | } |
|
314 | 331 | } |
|
315 | 332 | |
|
316 | 333 | return count; |
|
317 | 334 | } |
|
318 | 335 | |
|
319 | 336 | QStringList QBarSeriesPrivate::categories() const |
|
320 | 337 | { |
|
321 | 338 | if (m_categories.count() > 0) { |
|
322 | 339 | return m_categories; |
|
323 | 340 | } |
|
324 | 341 | |
|
325 | 342 | // No categories defined. retun list of indices. |
|
326 | 343 | QStringList categories; |
|
327 | 344 | |
|
328 | 345 | int count = categoryCount(); |
|
329 | 346 | for (int i = 0; i < count; i++) { |
|
330 | 347 | categories.append(QString::number(i)); |
|
331 | 348 | } |
|
332 | 349 | return categories; |
|
333 | 350 | } |
|
334 | 351 | |
|
335 | 352 | void QBarSeriesPrivate::setBarMargin(qreal margin) |
|
336 | 353 | { |
|
337 | 354 | if (margin > 1.0) { |
|
338 | 355 | margin = 1.0; |
|
339 | 356 | } else if (margin < 0.0) { |
|
340 | 357 | margin = 0.0; |
|
341 | 358 | } |
|
342 | 359 | |
|
343 | 360 | m_barMargin = margin; |
|
344 | 361 | emit updatedBars(); |
|
345 | 362 | } |
|
346 | 363 | |
|
347 | 364 | qreal QBarSeriesPrivate::barMargin() const |
|
348 | 365 | { |
|
349 | 366 | return m_barMargin; |
|
350 | 367 | } |
|
351 | 368 | |
|
352 | 369 | QBarSet* QBarSeriesPrivate::barsetAt(int index) |
|
353 | 370 | { |
|
354 | 371 | return m_barSets.at(index); |
|
355 | 372 | } |
|
356 | 373 | |
|
357 | 374 | void QBarSeriesPrivate::setVisible(bool visible) |
|
358 | 375 | { |
|
359 | 376 | m_visible = visible; |
|
360 | 377 | emit updatedBars(); |
|
361 | 378 | } |
|
362 | 379 | |
|
363 | 380 | void QBarSeriesPrivate::setLabelsVisible(bool visible) |
|
364 | 381 | { |
|
365 | 382 | m_labelsVisible = visible; |
|
366 | 383 | emit labelsVisibleChanged(visible); |
|
367 | 384 | } |
|
368 | 385 | |
|
369 | 386 | QString QBarSeriesPrivate::categoryName(int category) |
|
370 | 387 | { |
|
371 | 388 | if ((category >= 0) && (category < m_categories.count())) { |
|
372 | 389 | return m_categories.at(category); |
|
373 | 390 | } |
|
374 | 391 | |
|
375 | 392 | return QString::number(category); |
|
376 | 393 | } |
|
377 | 394 | |
|
378 | 395 | qreal QBarSeriesPrivate::min() |
|
379 | 396 | { |
|
380 | 397 | if (m_barSets.count() <= 0) { |
|
381 | 398 | return 0; |
|
382 | 399 | } |
|
383 | 400 | qreal min = INT_MAX; |
|
384 | 401 | |
|
385 | 402 | for (int i = 0; i < m_barSets.count(); i++) { |
|
386 | 403 | int categoryCount = m_barSets.at(i)->count(); |
|
387 | 404 | for (int j = 0; j < categoryCount; j++) { |
|
388 | 405 | qreal temp = m_barSets.at(i)->at(j).y(); |
|
389 | 406 | if (temp < min) |
|
390 | 407 | min = temp; |
|
391 | 408 | } |
|
392 | 409 | } |
|
393 | 410 | return min; |
|
394 | 411 | } |
|
395 | 412 | |
|
396 | 413 | qreal QBarSeriesPrivate::max() |
|
397 | 414 | { |
|
398 | 415 | if (m_barSets.count() <= 0) { |
|
399 | 416 | return 0; |
|
400 | 417 | } |
|
401 | 418 | qreal max = INT_MIN; |
|
402 | 419 | |
|
403 | 420 | for (int i = 0; i < m_barSets.count(); i++) { |
|
404 | 421 | int categoryCount = m_barSets.at(i)->count(); |
|
405 | 422 | for (int j = 0; j < categoryCount; j++) { |
|
406 | 423 | qreal temp = m_barSets.at(i)->at(j).y(); |
|
407 | 424 | if (temp > max) |
|
408 | 425 | max = temp; |
|
409 | 426 | } |
|
410 | 427 | } |
|
411 | 428 | |
|
412 | 429 | return max; |
|
413 | 430 | } |
|
414 | 431 | |
|
415 | 432 | qreal QBarSeriesPrivate::valueAt(int set, int category) |
|
416 | 433 | { |
|
417 | 434 | if ((set < 0) || (set >= m_barSets.count())) { |
|
418 | 435 | // No set, no value. |
|
419 | 436 | return 0; |
|
420 | 437 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
421 | 438 | // No category, no value. |
|
422 | 439 | return 0; |
|
423 | 440 | } |
|
424 | 441 | |
|
425 | 442 | return m_barSets.at(set)->at(category).y(); |
|
426 | 443 | } |
|
427 | 444 | |
|
428 | 445 | qreal QBarSeriesPrivate::percentageAt(int set, int category) |
|
429 | 446 | { |
|
430 | 447 | if ((set < 0) || (set >= m_barSets.count())) { |
|
431 | 448 | // No set, no value. |
|
432 | 449 | return 0; |
|
433 | 450 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
434 | 451 | // No category, no value. |
|
435 | 452 | return 0; |
|
436 | 453 | } |
|
437 | 454 | |
|
438 | 455 | qreal value = m_barSets.at(set)->at(category).y(); |
|
439 | 456 | qreal sum = categorySum(category); |
|
440 | 457 | if ( qFuzzyIsNull(sum) ) { |
|
441 | 458 | return 0; |
|
442 | 459 | } |
|
443 | 460 | |
|
444 | 461 | return value / sum; |
|
445 | 462 | } |
|
446 | 463 | |
|
447 | 464 | qreal QBarSeriesPrivate::categorySum(int category) |
|
448 | 465 | { |
|
449 | 466 | qreal sum(0); |
|
450 | 467 | int count = m_barSets.count(); // Count sets |
|
451 | 468 | for (int set = 0; set < count; set++) { |
|
452 | 469 | if (category < m_barSets.at(set)->count()) |
|
453 | 470 | sum += m_barSets.at(set)->at(category).y(); |
|
454 | 471 | } |
|
455 | 472 | return sum; |
|
456 | 473 | } |
|
457 | 474 | |
|
458 | 475 | qreal QBarSeriesPrivate::absoluteCategorySum(int category) |
|
459 | 476 | { |
|
460 | 477 | qreal sum(0); |
|
461 | 478 | int count = m_barSets.count(); // Count sets |
|
462 | 479 | for (int set = 0; set < count; set++) { |
|
463 | 480 | if (category < m_barSets.at(set)->count()) |
|
464 | 481 | sum += qAbs(m_barSets.at(set)->at(category).y()); |
|
465 | 482 | } |
|
466 | 483 | return sum; |
|
467 | 484 | } |
|
468 | 485 | |
|
469 | 486 | qreal QBarSeriesPrivate::maxCategorySum() |
|
470 | 487 | { |
|
471 | 488 | qreal max = INT_MIN; |
|
472 | 489 | int count = categoryCount(); |
|
473 | 490 | for (int i = 0; i < count; i++) { |
|
474 | 491 | qreal sum = categorySum(i); |
|
475 | 492 | if (sum > max) |
|
476 | 493 | max = sum; |
|
477 | 494 | } |
|
478 | 495 | return max; |
|
479 | 496 | } |
|
480 | 497 | |
|
481 | 498 | void QBarSeriesPrivate::scaleDomain(Domain& domain) |
|
482 | 499 | { |
|
483 | 500 | qreal minX(domain.minX()); |
|
484 | 501 | qreal minY(domain.minY()); |
|
485 | 502 | qreal maxX(domain.maxX()); |
|
486 | 503 | qreal maxY(domain.maxY()); |
|
487 | 504 | int tickXCount(domain.tickXCount()); |
|
488 | 505 | int tickYCount(domain.tickYCount()); |
|
489 | 506 | |
|
490 | 507 | qreal x = categoryCount(); |
|
491 | 508 | qreal y = max(); |
|
492 | 509 | minX = qMin(minX, x) - 0.5; |
|
493 | 510 | minY = qMin(minY, y); |
|
494 | 511 | maxX = qMax(maxX, x) - 0.5; |
|
495 | 512 | maxY = qMax(maxY, y); |
|
496 | 513 | tickXCount = x+1; |
|
497 | 514 | |
|
498 | 515 | domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); |
|
499 | 516 | } |
|
500 | 517 | |
|
501 | 518 | Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter) |
|
502 | 519 | { |
|
503 | 520 | Q_Q(QBarSeries); |
|
504 | 521 | |
|
505 | 522 | BarChartItem* bar = new BarChartItem(q,presenter); |
|
506 | 523 | if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { |
|
507 | 524 | presenter->animator()->addAnimation(bar); |
|
508 | 525 | } |
|
509 | 526 | presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); |
|
510 | 527 | return bar; |
|
511 | 528 | |
|
512 | 529 | } |
|
513 | 530 | |
|
514 | 531 | QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend) |
|
515 | 532 | { |
|
516 | 533 | Q_Q(QBarSeries); |
|
517 | 534 | QList<LegendMarker*> markers; |
|
518 | 535 | foreach(QBarSet* set, q->barSets()) { |
|
519 | 536 | BarLegendMarker* marker = new BarLegendMarker(q,set,legend); |
|
520 | 537 | markers << marker; |
|
521 | 538 | } |
|
522 | 539 | |
|
523 | 540 | return markers; |
|
524 | 541 | } |
|
525 | 542 | |
|
526 | 543 | bool QBarSeriesPrivate::append(QBarSet *set) |
|
527 | 544 | { |
|
528 | 545 | Q_Q(QBarSeries); |
|
529 | 546 | if ((m_barSets.contains(set)) || (set == 0)) { |
|
530 | 547 | // Fail if set is already in list or set is null. |
|
531 | 548 | return false; |
|
532 | 549 | } |
|
533 | 550 | m_barSets.append(set); |
|
534 | 551 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
535 | 552 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
536 | 553 | if (m_dataset) { |
|
537 | 554 | m_dataset->updateSeries(q); // this notifies legend |
|
538 | 555 | } |
|
539 | 556 | emit restructuredBars(); // this notifies barchartitem |
|
540 | 557 | return true; |
|
541 | 558 | } |
|
542 | 559 | |
|
543 | 560 | bool QBarSeriesPrivate::remove(QBarSet *set) |
|
544 | 561 | { |
|
545 | 562 | Q_Q(QBarSeries); |
|
546 | 563 | if (!m_barSets.contains(set)) { |
|
547 | 564 | // Fail if set is not in list |
|
548 | 565 | return false; |
|
549 | 566 | } |
|
550 | 567 | m_barSets.removeOne(set); |
|
551 | 568 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
552 | 569 | QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
553 | 570 | if (m_dataset) { |
|
554 | 571 | m_dataset->updateSeries(q); // this notifies legend |
|
555 | 572 | } |
|
556 | 573 | emit restructuredBars(); // this notifies barchartitem |
|
557 | 574 | return true; |
|
558 | 575 | } |
|
559 | 576 | |
|
560 | 577 | bool QBarSeriesPrivate::append(QList<QBarSet* > sets) |
|
561 | 578 | { |
|
562 | 579 | Q_Q(QBarSeries); |
|
563 | 580 | foreach (QBarSet* set, sets) { |
|
564 | 581 | if ((set == 0) || (m_barSets.contains(set))) { |
|
565 | 582 | // Fail if any of the sets is null or is already appended. |
|
566 | 583 | return false; |
|
567 | 584 | } |
|
568 | 585 | if (sets.count(set) != 1) { |
|
569 | 586 | // Also fail if same set is more than once in given list. |
|
570 | 587 | return false; |
|
571 | 588 | } |
|
572 | 589 | } |
|
573 | 590 | |
|
574 | 591 | foreach (QBarSet* set, sets) { |
|
575 | 592 | m_barSets.append(set); |
|
576 | 593 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
577 | 594 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
578 | 595 | } |
|
579 | 596 | if (m_dataset) { |
|
580 | 597 | m_dataset->updateSeries(q); // this notifies legend |
|
581 | 598 | } |
|
582 | 599 | emit restructuredBars(); // this notifies barchartitem |
|
583 | 600 | return true; |
|
584 | 601 | } |
|
585 | 602 | |
|
586 | 603 | bool QBarSeriesPrivate::remove(QList<QBarSet* > sets) |
|
587 | 604 | { |
|
588 | 605 | Q_Q(QBarSeries); |
|
589 | 606 | foreach (QBarSet* set, sets) { |
|
590 | 607 | if ((set == 0) || (!m_barSets.contains(set))) { |
|
591 | 608 | // Fail if any of the sets is null or is not in series |
|
592 | 609 | return false; |
|
593 | 610 | } |
|
594 | 611 | if (sets.count(set) != 1) { |
|
595 | 612 | // Also fail if same set is more than once in given list. |
|
596 | 613 | return false; |
|
597 | 614 | } |
|
598 | 615 | } |
|
599 | 616 | |
|
600 | 617 | foreach (QBarSet* set, sets) { |
|
601 | 618 | m_barSets.removeOne(set); |
|
602 | 619 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
603 | 620 | QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
604 | 621 | } |
|
605 | 622 | |
|
606 | 623 | if (m_dataset) { |
|
607 | 624 | m_dataset->updateSeries(q); // this notifies legend |
|
608 | 625 | } |
|
609 | 626 | emit restructuredBars(); // this notifies barchartitem |
|
610 | 627 | return true; |
|
611 | 628 | } |
|
612 | 629 | |
|
630 | bool QBarSeriesPrivate::insert(int index, QBarSet *set) | |
|
631 | { | |
|
632 | Q_Q(QBarSeries); | |
|
633 | if ((m_barSets.contains(set)) || (set == 0)) { | |
|
634 | // Fail if set is already in list or set is null. | |
|
635 | return false; | |
|
636 | } | |
|
637 | m_barSets.insert(index, set); | |
|
638 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); | |
|
639 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); | |
|
640 | if (m_dataset) { | |
|
641 | m_dataset->updateSeries(q); // this notifies legend | |
|
642 | } | |
|
643 | emit restructuredBars(); // this notifies barchartitem | |
|
644 | return true; | |
|
645 | } | |
|
646 | ||
|
613 | 647 | #include "moc_qbarseries.cpp" |
|
614 | 648 | #include "moc_qbarseries_p.cpp" |
|
615 | 649 | |
|
616 | 650 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,82 +1,83 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #ifndef BARSERIES_H |
|
22 | 22 | #define BARSERIES_H |
|
23 | 23 | |
|
24 | 24 | #include <qabstractseries.h> |
|
25 | 25 | #include <QStringList> |
|
26 | 26 | |
|
27 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | 28 | |
|
29 | 29 | class QBarSet; |
|
30 | 30 | class QBarSeriesPrivate; |
|
31 | 31 | |
|
32 | 32 | // Container for series |
|
33 | 33 | class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries |
|
34 | 34 | { |
|
35 | 35 | Q_OBJECT |
|
36 | 36 | Q_PROPERTY(qreal barMargin READ barMargin WRITE setBarMargin) |
|
37 | 37 | Q_PROPERTY(int count READ barsetCount) |
|
38 |
Q_PROPERTY(bool labelsVisible READ |
|
|
38 | Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible) | |
|
39 | 39 | |
|
40 | 40 | public: |
|
41 | 41 | explicit QBarSeries(QObject *parent = 0); |
|
42 | 42 | virtual ~QBarSeries(); |
|
43 | 43 | |
|
44 | 44 | QAbstractSeries::SeriesType type() const; |
|
45 | 45 | |
|
46 | 46 | void setBarMargin(qreal margin); |
|
47 | 47 | qreal barMargin() const; |
|
48 | 48 | |
|
49 | 49 | bool append(QBarSet *set); |
|
50 | 50 | bool remove(QBarSet *set); |
|
51 | 51 | bool append(QList<QBarSet* > sets); |
|
52 | 52 | bool remove(QList<QBarSet* > sets); |
|
53 | bool insert(int index, QBarSet *set); | |
|
53 | 54 | int barsetCount() const; |
|
54 | 55 | QList<QBarSet*> barSets() const; |
|
55 | 56 | void clear(); |
|
56 | 57 | |
|
57 | 58 | void setLabelsVisible(bool visible = true); |
|
58 |
bool |
|
|
59 | bool isLabelsVisible() const; | |
|
59 | 60 | |
|
60 | 61 | protected: |
|
61 | 62 | explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0); |
|
62 | 63 | |
|
63 | 64 | Q_SIGNALS: |
|
64 | 65 | void clicked(QBarSet *barset, int index); |
|
65 | 66 | void hovered(QBarSet* barset, bool status); |
|
66 | 67 | void visibleChanged(); |
|
67 | 68 | void labelsVisibleChanged(); |
|
68 | 69 | |
|
69 | 70 | void barsetsAdded(QList<QBarSet*> sets); |
|
70 | 71 | void barsetsRemoved(QList<QBarSet*> sets); |
|
71 | 72 | |
|
72 | 73 | protected: |
|
73 | 74 | Q_DECLARE_PRIVATE(QBarSeries) |
|
74 | 75 | friend class BarChartItem; |
|
75 | 76 | friend class PercentBarChartItem; |
|
76 | 77 | friend class StackedBarChartItem; |
|
77 | 78 | friend class GroupedBarChartItem; |
|
78 | 79 | }; |
|
79 | 80 | |
|
80 | 81 | QTCOMMERCIALCHART_END_NAMESPACE |
|
81 | 82 | |
|
82 | 83 | #endif // BARSERIES_H |
@@ -1,99 +1,100 | |||
|
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 | 48 | void setCategories(QStringList categories); |
|
49 | 49 | void insertCategory(int index, const QString category); |
|
50 | 50 | void removeCategory(int index); |
|
51 | 51 | int categoryCount() const; |
|
52 | 52 | QStringList categories() const; |
|
53 | 53 | |
|
54 | 54 | void setBarMargin(qreal margin); |
|
55 | 55 | qreal barMargin() const; |
|
56 | 56 | |
|
57 | 57 | void setVisible(bool visible); |
|
58 | 58 | void setLabelsVisible(bool visible); |
|
59 | 59 | |
|
60 | 60 | void scaleDomain(Domain& domain); |
|
61 | 61 | Chart* createGraphics(ChartPresenter* presenter); |
|
62 | 62 | QList<LegendMarker*> createLegendMarker(QLegend* legend); |
|
63 | 63 | |
|
64 | 64 | bool append(QBarSet *set); |
|
65 | 65 | bool remove(QBarSet *set); |
|
66 | 66 | bool append(QList<QBarSet* > sets); |
|
67 | 67 | bool remove(QList<QBarSet* > sets); |
|
68 | bool insert(int index, QBarSet *set); | |
|
68 | 69 | |
|
69 | 70 | QBarSet* barsetAt(int index); |
|
70 | 71 | QString categoryName(int category); |
|
71 | 72 | qreal min(); |
|
72 | 73 | qreal max(); |
|
73 | 74 | qreal valueAt(int set, int category); |
|
74 | 75 | qreal percentageAt(int set, int category); |
|
75 | 76 | qreal categorySum(int category); |
|
76 | 77 | qreal absoluteCategorySum(int category); |
|
77 | 78 | qreal maxCategorySum(); |
|
78 | 79 | |
|
79 | 80 | Q_SIGNALS: |
|
80 | 81 | void clicked(QBarSet *barset, int index); |
|
81 | 82 | void updatedBars(); |
|
82 | 83 | void restructuredBars(); |
|
83 | 84 | void categoriesUpdated(); |
|
84 | 85 | void labelsVisibleChanged(bool visible); |
|
85 | 86 | |
|
86 | 87 | protected: |
|
87 | 88 | QList<QBarSet *> m_barSets; |
|
88 | 89 | QStringList m_categories; |
|
89 | 90 | qreal m_barMargin; |
|
90 | 91 | bool m_labelsVisible; |
|
91 | 92 | bool m_visible; |
|
92 | 93 | |
|
93 | 94 | private: |
|
94 | 95 | Q_DECLARE_PUBLIC(QBarSeries) |
|
95 | 96 | }; |
|
96 | 97 | |
|
97 | 98 | QTCOMMERCIALCHART_END_NAMESPACE |
|
98 | 99 | |
|
99 | 100 | #endif // QBARSERIESPRIVATE_P_H |
General Comments 0
You need to be logged in to leave comments.
Login now