@@ -1,244 +1,262 | |||
|
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 "declarativebarseries.h" |
|
22 | 22 | #include "declarativechart.h" |
|
23 | 23 | #include <QBarSet> |
|
24 | 24 | #include <QVBarModelMapper> |
|
25 | 25 | #include <QHBarModelMapper> |
|
26 | 26 | |
|
27 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | 28 | |
|
29 | 29 | DeclarativeBarSet::DeclarativeBarSet(QObject *parent) : |
|
30 | 30 | QBarSet("", parent) |
|
31 | 31 | { |
|
32 | 32 | connect(this, SIGNAL(valuesAdded(int,int)), this, SLOT(handleCountChanged(int, int))); |
|
33 | 33 | connect(this, SIGNAL(valuesRemoved(int,int)), this, SLOT(handleCountChanged(int, int))); |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | void DeclarativeBarSet::handleCountChanged(int index, int count) |
|
37 | 37 | { |
|
38 | 38 | Q_UNUSED(index) |
|
39 | 39 | Q_UNUSED(count) |
|
40 | 40 | emit countChanged(QBarSet::count()); |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | QVariantList DeclarativeBarSet::values() |
|
44 | 44 | { |
|
45 | 45 | QVariantList values; |
|
46 | 46 | for (int i(0); i < count(); i++) |
|
47 | 47 | values.append(QVariant(at(i))); |
|
48 | 48 | return values; |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | void DeclarativeBarSet::setValues(QVariantList values) |
|
52 | 52 | { |
|
53 | 53 | while (count()) |
|
54 | 54 | remove(count() - 1); |
|
55 | 55 | |
|
56 | 56 | for (int i(0); i < values.count(); i++) { |
|
57 | 57 | if (values.at(i).canConvert(QVariant::Double)) |
|
58 | 58 | append(values[i].toDouble()); |
|
59 | 59 | } |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) : |
|
63 | 63 | QBarSeries(parent) |
|
64 | 64 | { |
|
65 | connect(this, SIGNAL(barsetsAdded(QList<QBarSet*>)), this, SLOT(handleAdded(QList<QBarSet*>))); | |
|
66 | connect(this, SIGNAL(barsetsRemoved(QList<QBarSet*>)), this, SLOT(handleRemoved(QList<QBarSet*>))); | |
|
67 | } | |
|
68 | ||
|
69 | void DeclarativeBarSeries::handleAdded(QList<QBarSet* > barsets) | |
|
70 | { | |
|
71 | foreach(QBarSet *b, barsets) { | |
|
72 | DeclarativeBarSet *barset = qobject_cast<DeclarativeBarSet *>(b); | |
|
73 | emit added(barset); | |
|
74 | } | |
|
75 | } | |
|
76 | ||
|
77 | void DeclarativeBarSeries::handleRemoved(QList<QBarSet* > barsets) | |
|
78 | { | |
|
79 | foreach(QBarSet *b, barsets) { | |
|
80 | DeclarativeBarSet *barset = qobject_cast<DeclarativeBarSet *>(b); | |
|
81 | emit removed(barset); | |
|
82 | } | |
|
65 | 83 | } |
|
66 | 84 | |
|
67 | 85 | void DeclarativeBarSeries::classBegin() |
|
68 | 86 | { |
|
69 | 87 | } |
|
70 | 88 | |
|
71 | 89 | void DeclarativeBarSeries::componentComplete() |
|
72 | 90 | { |
|
73 | 91 | foreach(QObject *child, children()) { |
|
74 | 92 | if (qobject_cast<DeclarativeBarSet *>(child)) { |
|
75 | 93 | QBarSeries::append(qobject_cast<DeclarativeBarSet *>(child)); |
|
76 | 94 | } else if(qobject_cast<QVBarModelMapper *>(child)) { |
|
77 | 95 | QVBarModelMapper *mapper = qobject_cast<QVBarModelMapper *>(child); |
|
78 | 96 | mapper->setSeries(this); |
|
79 | 97 | } else if(qobject_cast<QHBarModelMapper *>(child)) { |
|
80 | 98 | QHBarModelMapper *mapper = qobject_cast<QHBarModelMapper *>(child); |
|
81 | 99 | mapper->setSeries(this); |
|
82 | 100 | } |
|
83 | 101 | } |
|
84 | 102 | } |
|
85 | 103 | |
|
86 | 104 | QDeclarativeListProperty<QObject> DeclarativeBarSeries::seriesChildren() |
|
87 | 105 | { |
|
88 | 106 | return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeBarSeries::appendSeriesChildren); |
|
89 | 107 | } |
|
90 | 108 | |
|
91 | 109 | void DeclarativeBarSeries::appendSeriesChildren(QDeclarativeListProperty<QObject> * list, QObject *element) |
|
92 | 110 | { |
|
93 | 111 | // Empty implementation; the children are parsed in componentComplete instead |
|
94 | 112 | Q_UNUSED(list); |
|
95 | 113 | Q_UNUSED(element); |
|
96 | 114 | } |
|
97 | 115 | |
|
98 | 116 | DeclarativeBarSet *DeclarativeBarSeries::at(int index) |
|
99 | 117 | { |
|
100 | 118 | QList<QBarSet*> setList = barSets(); |
|
101 | 119 | if (index < setList.count()) |
|
102 | 120 | return qobject_cast<DeclarativeBarSet *>(setList[index]); |
|
103 | 121 | |
|
104 | 122 | return 0; |
|
105 | 123 | } |
|
106 | 124 | |
|
107 | 125 | DeclarativeGroupedBarSeries::DeclarativeGroupedBarSeries(QDeclarativeItem *parent) : |
|
108 | 126 | QGroupedBarSeries(parent) |
|
109 | 127 | { |
|
110 | 128 | } |
|
111 | 129 | |
|
112 | 130 | void DeclarativeGroupedBarSeries::classBegin() |
|
113 | 131 | { |
|
114 | 132 | } |
|
115 | 133 | |
|
116 | 134 | void DeclarativeGroupedBarSeries::componentComplete() |
|
117 | 135 | { |
|
118 | 136 | foreach(QObject *child, children()) { |
|
119 | 137 | if (qobject_cast<DeclarativeBarSet *>(child)) { |
|
120 | 138 | QBarSeries::append(qobject_cast<DeclarativeBarSet *>(child)); |
|
121 | 139 | } else if(qobject_cast<QVBarModelMapper *>(child)) { |
|
122 | 140 | QVBarModelMapper *mapper = qobject_cast<QVBarModelMapper *>(child); |
|
123 | 141 | mapper->setSeries(this); |
|
124 | 142 | } else if(qobject_cast<QHBarModelMapper *>(child)) { |
|
125 | 143 | QHBarModelMapper *mapper = qobject_cast<QHBarModelMapper *>(child); |
|
126 | 144 | mapper->setSeries(this); |
|
127 | 145 | } |
|
128 | 146 | } |
|
129 | 147 | } |
|
130 | 148 | |
|
131 | 149 | QDeclarativeListProperty<QObject> DeclarativeGroupedBarSeries::seriesChildren() |
|
132 | 150 | { |
|
133 | 151 | return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeBarSeries::appendSeriesChildren); |
|
134 | 152 | } |
|
135 | 153 | |
|
136 | 154 | void DeclarativeGroupedBarSeries::appendSeriesChildren(QDeclarativeListProperty<QObject> * list, QObject *element) |
|
137 | 155 | { |
|
138 | 156 | // Empty implementation; the children are parsed in componentComplete instead |
|
139 | 157 | Q_UNUSED(list); |
|
140 | 158 | Q_UNUSED(element); |
|
141 | 159 | } |
|
142 | 160 | |
|
143 | 161 | DeclarativeBarSet *DeclarativeGroupedBarSeries::at(int index) |
|
144 | 162 | { |
|
145 | 163 | QList<QBarSet*> setList = barSets(); |
|
146 | 164 | if (index < setList.count()) |
|
147 | 165 | return qobject_cast<DeclarativeBarSet *>(setList[index]); |
|
148 | 166 | |
|
149 | 167 | return 0; |
|
150 | 168 | } |
|
151 | 169 | |
|
152 | 170 | DeclarativeStackedBarSeries::DeclarativeStackedBarSeries(QDeclarativeItem *parent) : |
|
153 | 171 | QStackedBarSeries(parent) |
|
154 | 172 | { |
|
155 | 173 | } |
|
156 | 174 | |
|
157 | 175 | void DeclarativeStackedBarSeries::classBegin() |
|
158 | 176 | { |
|
159 | 177 | } |
|
160 | 178 | |
|
161 | 179 | void DeclarativeStackedBarSeries::componentComplete() |
|
162 | 180 | { |
|
163 | 181 | foreach(QObject *child, children()) { |
|
164 | 182 | if (qobject_cast<DeclarativeBarSet *>(child)) { |
|
165 | 183 | QBarSeries::append(qobject_cast<DeclarativeBarSet *>(child)); |
|
166 | 184 | } else if(qobject_cast<QVBarModelMapper *>(child)) { |
|
167 | 185 | QVBarModelMapper *mapper = qobject_cast<QVBarModelMapper *>(child); |
|
168 | 186 | mapper->setSeries(this); |
|
169 | 187 | } else if(qobject_cast<QHBarModelMapper *>(child)) { |
|
170 | 188 | QHBarModelMapper *mapper = qobject_cast<QHBarModelMapper *>(child); |
|
171 | 189 | mapper->setSeries(this); |
|
172 | 190 | } |
|
173 | 191 | } |
|
174 | 192 | } |
|
175 | 193 | |
|
176 | 194 | QDeclarativeListProperty<QObject> DeclarativeStackedBarSeries::seriesChildren() |
|
177 | 195 | { |
|
178 | 196 | return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeBarSeries::appendSeriesChildren); |
|
179 | 197 | } |
|
180 | 198 | |
|
181 | 199 | void DeclarativeStackedBarSeries::appendSeriesChildren(QDeclarativeListProperty<QObject> * list, QObject *element) |
|
182 | 200 | { |
|
183 | 201 | // Empty implementation; the children are parsed in componentComplete instead |
|
184 | 202 | Q_UNUSED(list); |
|
185 | 203 | Q_UNUSED(element); |
|
186 | 204 | } |
|
187 | 205 | |
|
188 | 206 | DeclarativeBarSet *DeclarativeStackedBarSeries::at(int index) |
|
189 | 207 | { |
|
190 | 208 | QList<QBarSet*> setList = barSets(); |
|
191 | 209 | if (index < setList.count()) |
|
192 | 210 | return qobject_cast<DeclarativeBarSet *>(setList[index]); |
|
193 | 211 | |
|
194 | 212 | return 0; |
|
195 | 213 | } |
|
196 | 214 | |
|
197 | 215 | DeclarativePercentBarSeries::DeclarativePercentBarSeries(QDeclarativeItem *parent) : |
|
198 | 216 | QPercentBarSeries(parent) |
|
199 | 217 | { |
|
200 | 218 | } |
|
201 | 219 | |
|
202 | 220 | void DeclarativePercentBarSeries::classBegin() |
|
203 | 221 | { |
|
204 | 222 | } |
|
205 | 223 | |
|
206 | 224 | void DeclarativePercentBarSeries::componentComplete() |
|
207 | 225 | { |
|
208 | 226 | foreach(QObject *child, children()) { |
|
209 | 227 | if (qobject_cast<DeclarativeBarSet *>(child)) { |
|
210 | 228 | QBarSeries::append(qobject_cast<DeclarativeBarSet *>(child)); |
|
211 | 229 | } else if(qobject_cast<QVBarModelMapper *>(child)) { |
|
212 | 230 | QVBarModelMapper *mapper = qobject_cast<QVBarModelMapper *>(child); |
|
213 | 231 | mapper->setSeries(this); |
|
214 | 232 | } else if(qobject_cast<QHBarModelMapper *>(child)) { |
|
215 | 233 | QHBarModelMapper *mapper = qobject_cast<QHBarModelMapper *>(child); |
|
216 | 234 | mapper->setSeries(this); |
|
217 | 235 | } |
|
218 | 236 | } |
|
219 | 237 | } |
|
220 | 238 | |
|
221 | 239 | QDeclarativeListProperty<QObject> DeclarativePercentBarSeries::seriesChildren() |
|
222 | 240 | { |
|
223 | 241 | return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeBarSeries::appendSeriesChildren); |
|
224 | 242 | } |
|
225 | 243 | |
|
226 | 244 | void DeclarativePercentBarSeries::appendSeriesChildren(QDeclarativeListProperty<QObject> * list, QObject *element) |
|
227 | 245 | { |
|
228 | 246 | // Empty implementation; the children are parsed in componentComplete instead |
|
229 | 247 | Q_UNUSED(list); |
|
230 | 248 | Q_UNUSED(element); |
|
231 | 249 | } |
|
232 | 250 | |
|
233 | 251 | DeclarativeBarSet *DeclarativePercentBarSeries::at(int index) |
|
234 | 252 | { |
|
235 | 253 | QList<QBarSet*> setList = barSets(); |
|
236 | 254 | if (index < setList.count()) |
|
237 | 255 | return qobject_cast<DeclarativeBarSet *>(setList[index]); |
|
238 | 256 | |
|
239 | 257 | return 0; |
|
240 | 258 | } |
|
241 | 259 | |
|
242 | 260 | #include "moc_declarativebarseries.cpp" |
|
243 | 261 | |
|
244 | 262 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,140 +1,146 | |||
|
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 DECLARATIVEBARSERIES_H |
|
22 | 22 | #define DECLARATIVEBARSERIES_H |
|
23 | 23 | |
|
24 | 24 | #include "qchartglobal.h" |
|
25 | 25 | #include <QGroupedBarSeries> |
|
26 | 26 | #include <QStackedBarSeries> |
|
27 | 27 | #include <QPercentBarSeries> |
|
28 | 28 | #include <QBarSet> |
|
29 | 29 | #include <QDeclarativeItem> |
|
30 | 30 | #include <QDeclarativeParserStatus> |
|
31 | 31 | |
|
32 | 32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
33 | 33 | |
|
34 | 34 | class QChart; |
|
35 | 35 | |
|
36 | 36 | class DeclarativeBarSet : public QBarSet |
|
37 | 37 | { |
|
38 | 38 | Q_OBJECT |
|
39 | 39 | Q_PROPERTY(QVariantList values READ values WRITE setValues) |
|
40 | 40 | Q_PROPERTY(int count READ count NOTIFY countChanged) |
|
41 | 41 | |
|
42 | 42 | public: |
|
43 | 43 | explicit DeclarativeBarSet(QObject *parent = 0); |
|
44 | 44 | QVariantList values(); |
|
45 | 45 | void setValues(QVariantList values); |
|
46 | 46 | |
|
47 | 47 | public: // From QBarSet |
|
48 | 48 | Q_INVOKABLE void append(qreal value) { QBarSet::append(value); } |
|
49 | 49 | Q_INVOKABLE void append(qreal x, qreal y) { QBarSet::append(QPointF(x, y)); } |
|
50 | 50 | |
|
51 | 51 | Q_SIGNALS: |
|
52 | 52 | void countChanged(int count); |
|
53 | 53 | |
|
54 | 54 | private Q_SLOTS: |
|
55 | 55 | void handleCountChanged(int index, int count); |
|
56 | 56 | }; |
|
57 | 57 | |
|
58 | 58 | class DeclarativeBarSeries : public QBarSeries, public QDeclarativeParserStatus |
|
59 | 59 | { |
|
60 | 60 | Q_OBJECT |
|
61 | 61 | Q_INTERFACES(QDeclarativeParserStatus) |
|
62 | 62 | Q_PROPERTY(QDeclarativeListProperty<QObject> seriesChildren READ seriesChildren) |
|
63 | 63 | Q_CLASSINFO("DefaultProperty", "seriesChildren") |
|
64 | 64 | |
|
65 | 65 | public: |
|
66 | 66 | explicit DeclarativeBarSeries(QDeclarativeItem *parent = 0); |
|
67 | 67 | QDeclarativeListProperty<QObject> seriesChildren(); |
|
68 | 68 | Q_INVOKABLE DeclarativeBarSet *at(int index); |
|
69 | 69 | |
|
70 | 70 | public: // from QDeclarativeParserStatus |
|
71 | 71 | void classBegin(); |
|
72 | 72 | void componentComplete(); |
|
73 | 73 | |
|
74 | Q_SIGNALS: | |
|
75 | void added(DeclarativeBarSet *barset); | |
|
76 | void removed(DeclarativeBarSet *barset); | |
|
77 | ||
|
74 | 78 | public Q_SLOTS: |
|
75 | 79 | static void appendSeriesChildren(QDeclarativeListProperty<QObject> *list, QObject *element); |
|
80 | void handleAdded(QList<QBarSet* > barsets); | |
|
81 | void handleRemoved(QList<QBarSet* > barsets); | |
|
76 | 82 | }; |
|
77 | 83 | |
|
78 | 84 | class DeclarativeGroupedBarSeries : public QGroupedBarSeries, public QDeclarativeParserStatus |
|
79 | 85 | { |
|
80 | 86 | Q_OBJECT |
|
81 | 87 | Q_INTERFACES(QDeclarativeParserStatus) |
|
82 | 88 | Q_PROPERTY(QDeclarativeListProperty<QObject> seriesChildren READ seriesChildren) |
|
83 | 89 | Q_CLASSINFO("DefaultProperty", "seriesChildren") |
|
84 | 90 | |
|
85 | 91 | public: |
|
86 | 92 | explicit DeclarativeGroupedBarSeries(QDeclarativeItem *parent = 0); |
|
87 | 93 | QDeclarativeListProperty<QObject> seriesChildren(); |
|
88 | 94 | Q_INVOKABLE DeclarativeBarSet *at(int index); |
|
89 | 95 | |
|
90 | 96 | public: // from QDeclarativeParserStatus |
|
91 | 97 | void classBegin(); |
|
92 | 98 | void componentComplete(); |
|
93 | 99 | |
|
94 | 100 | public Q_SLOTS: |
|
95 | 101 | static void appendSeriesChildren(QDeclarativeListProperty<QObject> *list, QObject *element); |
|
96 | 102 | }; |
|
97 | 103 | |
|
98 | 104 | class DeclarativeStackedBarSeries : public QStackedBarSeries, public QDeclarativeParserStatus |
|
99 | 105 | { |
|
100 | 106 | Q_OBJECT |
|
101 | 107 | Q_INTERFACES(QDeclarativeParserStatus) |
|
102 | 108 | Q_PROPERTY(QDeclarativeListProperty<QObject> seriesChildren READ seriesChildren) |
|
103 | 109 | Q_CLASSINFO("DefaultProperty", "seriesChildren") |
|
104 | 110 | |
|
105 | 111 | public: |
|
106 | 112 | explicit DeclarativeStackedBarSeries(QDeclarativeItem *parent = 0); |
|
107 | 113 | QDeclarativeListProperty<QObject> seriesChildren(); |
|
108 | 114 | Q_INVOKABLE DeclarativeBarSet *at(int index); |
|
109 | 115 | |
|
110 | 116 | public: // from QDeclarativeParserStatus |
|
111 | 117 | void classBegin(); |
|
112 | 118 | void componentComplete(); |
|
113 | 119 | |
|
114 | 120 | public Q_SLOTS: |
|
115 | 121 | static void appendSeriesChildren(QDeclarativeListProperty<QObject> *list, QObject *element); |
|
116 | 122 | }; |
|
117 | 123 | |
|
118 | 124 | class DeclarativePercentBarSeries : public QPercentBarSeries, public QDeclarativeParserStatus |
|
119 | 125 | { |
|
120 | 126 | Q_OBJECT |
|
121 | 127 | Q_INTERFACES(QDeclarativeParserStatus) |
|
122 | 128 | Q_PROPERTY(QDeclarativeListProperty<QObject> seriesChildren READ seriesChildren) |
|
123 | 129 | Q_CLASSINFO("DefaultProperty", "seriesChildren") |
|
124 | 130 | |
|
125 | 131 | public: |
|
126 | 132 | explicit DeclarativePercentBarSeries(QDeclarativeItem *parent = 0); |
|
127 | 133 | QDeclarativeListProperty<QObject> seriesChildren(); |
|
128 | 134 | Q_INVOKABLE DeclarativeBarSet *at(int index); |
|
129 | 135 | |
|
130 | 136 | public: // from QDeclarativeParserStatus |
|
131 | 137 | void classBegin(); |
|
132 | 138 | void componentComplete(); |
|
133 | 139 | |
|
134 | 140 | public Q_SLOTS: |
|
135 | 141 | static void appendSeriesChildren(QDeclarativeListProperty<QObject> *list, QObject *element); |
|
136 | 142 | }; |
|
137 | 143 | |
|
138 | 144 | QTCOMMERCIALCHART_END_NAMESPACE |
|
139 | 145 | |
|
140 | 146 | #endif // DECLARATIVEBARSERIES_H |
@@ -1,680 +1,688 | |||
|
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 Series for creating a bar chart |
|
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 | \qmlclass BarSeries QBarSeries |
|
50 | 50 | |
|
51 | 51 | \beginfloatleft |
|
52 | 52 | \image demos_qmlchart6.png |
|
53 | 53 | \endfloat |
|
54 | 54 | \clearfloat |
|
55 | 55 | |
|
56 | 56 | The following QML shows how to create a simple bar chart: |
|
57 | 57 | \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1 |
|
58 | 58 | */ |
|
59 | 59 | |
|
60 | 60 | /*! |
|
61 | 61 | \property QBarSeries::barWidth |
|
62 | 62 | The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars |
|
63 | 63 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
64 | 64 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
65 | 65 | Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. |
|
66 | 66 | \sa QGroupedBarSeries |
|
67 | 67 | */ |
|
68 | 68 | /*! |
|
69 | 69 | \qmlproperty real BarSeries::barWidth |
|
70 | 70 | The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars |
|
71 | 71 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
72 | 72 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
73 | 73 | Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. |
|
74 | 74 | */ |
|
75 | 75 | |
|
76 | 76 | /*! |
|
77 | 77 | \property QBarSeries::count |
|
78 | 78 | Holds the number of sets in series. |
|
79 | 79 | */ |
|
80 | 80 | /*! |
|
81 | 81 | \qmlproperty int BarSeries::count |
|
82 | 82 | Holds the number of sets in series. |
|
83 | 83 | */ |
|
84 | 84 | |
|
85 | 85 | /*! |
|
86 | 86 | \property QBarSeries::labelsVisible |
|
87 | 87 | Defines the visibility of the labels in series |
|
88 | 88 | */ |
|
89 | 89 | /*! |
|
90 | 90 | \qmlproperty bool BarSeries::labelsVisible |
|
91 | 91 | Defines the visibility of the labels in series |
|
92 | 92 | */ |
|
93 | 93 | |
|
94 | 94 | /*! |
|
95 | 95 | \fn void QBarSeries::clicked(QBarSet *barset, int index) |
|
96 | 96 | |
|
97 | 97 | The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset. |
|
98 | 98 | Clicked bar inside set is indexed by \a index |
|
99 | 99 | */ |
|
100 | 100 | /*! |
|
101 | 101 | \qmlsignal BarSeries::onClicked(BarSet barset, int index) |
|
102 | 102 | |
|
103 | 103 | The signal is emitted if the user clicks with a mouse on top of BarSet. |
|
104 | 104 | Clicked bar inside set is indexed by \a index |
|
105 | 105 | */ |
|
106 | 106 | |
|
107 | 107 | /*! |
|
108 | 108 | \fn void QBarSeries::hovered(QBarSet* barset, bool status) |
|
109 | 109 | |
|
110 | 110 | The signal is emitted if mouse is hovered on top of series. |
|
111 | 111 | Parameter \a barset is the pointer of barset, where hover happened. |
|
112 | 112 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
113 | 113 | */ |
|
114 | 114 | /*! |
|
115 | 115 | \qmlsignal BarSeries::onHovered(BarSet barset, bool status) |
|
116 | 116 | |
|
117 | 117 | The signal is emitted if mouse is hovered on top of series. |
|
118 | 118 | Parameter \a barset is the pointer of barset, where hover happened. |
|
119 | 119 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
120 | 120 | */ |
|
121 | 121 | |
|
122 | 122 | /*! |
|
123 | 123 | \fn void QBarSeries::countChanged() |
|
124 | 124 | This signal is emitted when barset count has been changed, for example by append or remove. |
|
125 | 125 | */ |
|
126 | 126 | /*! |
|
127 | 127 | \qmlsignal BarSeries::countChanged() |
|
128 | 128 | This signal is emitted when barset count has been changed, for example by append or remove. |
|
129 | 129 | */ |
|
130 | 130 | |
|
131 | 131 | /*! |
|
132 | 132 | \fn void QBarSeries::labelsVisibleChanged() |
|
133 | 133 | This signal is emitted when labels visibility have changed. |
|
134 | 134 | \sa isLabelsVisible(), setLabelsVisible() |
|
135 | 135 | */ |
|
136 | 136 | |
|
137 | 137 | /*! |
|
138 | 138 | \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets) |
|
139 | 139 | This signal is emitted when \a sets have been added to the series. |
|
140 | 140 | \sa append(), insert() |
|
141 | 141 | */ |
|
142 | /*! | |
|
143 | \qmlsignal BarSeries::added(BarSet barset) | |
|
144 | Emitted when \a barset has been added to the series. | |
|
145 | */ | |
|
142 | 146 | |
|
143 | 147 | /*! |
|
144 | 148 | \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets) |
|
145 | 149 | This signal is emitted when \a sets have been removed from the series. |
|
146 | 150 | \sa remove() |
|
147 | 151 | */ |
|
152 | /*! | |
|
153 | \qmlsignal BarSeries::removed(BarSet barset) | |
|
154 | Emitted when \a barset has been removed from the series. | |
|
155 | */ | |
|
148 | 156 | |
|
149 | 157 | /*! |
|
150 | 158 | Constructs empty QBarSeries. |
|
151 | 159 | QBarSeries is QObject which is a child of a \a parent. |
|
152 | 160 | */ |
|
153 | 161 | QBarSeries::QBarSeries(QObject *parent) : |
|
154 | 162 | QAbstractSeries(*new QBarSeriesPrivate(this),parent) |
|
155 | 163 | { |
|
156 | 164 | } |
|
157 | 165 | |
|
158 | 166 | /*! |
|
159 | 167 | Destructs barseries and owned barsets. |
|
160 | 168 | */ |
|
161 | 169 | QBarSeries::~QBarSeries() |
|
162 | 170 | { |
|
163 | 171 | Q_D(QBarSeries); |
|
164 | 172 | if(d->m_dataset){ |
|
165 | 173 | d->m_dataset->removeSeries(this); |
|
166 | 174 | } |
|
167 | 175 | } |
|
168 | 176 | |
|
169 | 177 | /*! |
|
170 | 178 | \internal |
|
171 | 179 | */ |
|
172 | 180 | QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) : |
|
173 | 181 | QAbstractSeries(d,parent) |
|
174 | 182 | { |
|
175 | 183 | } |
|
176 | 184 | |
|
177 | 185 | /*! |
|
178 | 186 | Returns the type of series. Derived classes override this. |
|
179 | 187 | */ |
|
180 | 188 | QAbstractSeries::SeriesType QBarSeries::type() const |
|
181 | 189 | { |
|
182 | 190 | return QAbstractSeries::SeriesTypeBar; |
|
183 | 191 | } |
|
184 | 192 | |
|
185 | 193 | /*! |
|
186 | 194 | Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars |
|
187 | 195 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
188 | 196 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
189 | 197 | Note that with \link QGroupedBarSeries \endlink this value means the width of one group of bars instead of just one bar. |
|
190 | 198 | */ |
|
191 | 199 | void QBarSeries::setBarWidth(qreal width) |
|
192 | 200 | { |
|
193 | 201 | Q_D(QBarSeries); |
|
194 | 202 | d->setBarWidth(width); |
|
195 | 203 | } |
|
196 | 204 | |
|
197 | 205 | /*! |
|
198 | 206 | Returns the width of the bars of the series. |
|
199 | 207 | \sa setBarWidth() |
|
200 | 208 | */ |
|
201 | 209 | qreal QBarSeries::barWidth() const |
|
202 | 210 | { |
|
203 | 211 | Q_D(const QBarSeries); |
|
204 | 212 | return d->barWidth(); |
|
205 | 213 | } |
|
206 | 214 | |
|
207 | 215 | /*! |
|
208 | 216 | 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. |
|
209 | 217 | Returns true, if appending succeeded. |
|
210 | 218 | */ |
|
211 | 219 | bool QBarSeries::append(QBarSet *set) |
|
212 | 220 | { |
|
213 | 221 | Q_D(QBarSeries); |
|
214 | 222 | bool success = d->append(set); |
|
215 | 223 | if (success) { |
|
216 | 224 | QList<QBarSet*> sets; |
|
217 | 225 | sets.append(set); |
|
218 | 226 | emit barsetsAdded(sets); |
|
219 | 227 | emit countChanged(); |
|
220 | 228 | } |
|
221 | 229 | return success; |
|
222 | 230 | } |
|
223 | 231 | |
|
224 | 232 | /*! |
|
225 | 233 | Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set. |
|
226 | 234 | Returns true, if set was removed. |
|
227 | 235 | */ |
|
228 | 236 | bool QBarSeries::remove(QBarSet *set) |
|
229 | 237 | { |
|
230 | 238 | Q_D(QBarSeries); |
|
231 | 239 | bool success = d->remove(set); |
|
232 | 240 | if (success) { |
|
233 | 241 | QList<QBarSet*> sets; |
|
234 | 242 | sets.append(set); |
|
235 | 243 | emit barsetsRemoved(sets); |
|
236 | 244 | emit countChanged(); |
|
237 | 245 | } |
|
238 | 246 | return success; |
|
239 | 247 | } |
|
240 | 248 | |
|
241 | 249 | /*! |
|
242 | 250 | Adds a list of barsets to series. Takes ownership of \a sets. |
|
243 | 251 | Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series, |
|
244 | 252 | nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended |
|
245 | 253 | and function returns false. |
|
246 | 254 | */ |
|
247 | 255 | bool QBarSeries::append(QList<QBarSet* > sets) |
|
248 | 256 | { |
|
249 | 257 | Q_D(QBarSeries); |
|
250 | 258 | bool success = d->append(sets); |
|
251 | 259 | if (success) { |
|
252 | 260 | emit barsetsAdded(sets); |
|
253 | 261 | emit countChanged(); |
|
254 | 262 | } |
|
255 | 263 | return success; |
|
256 | 264 | } |
|
257 | 265 | |
|
258 | 266 | /*! |
|
259 | 267 | 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. |
|
260 | 268 | Returns true, if inserting succeeded. |
|
261 | 269 | |
|
262 | 270 | */ |
|
263 | 271 | bool QBarSeries::insert(int index, QBarSet *set) |
|
264 | 272 | { |
|
265 | 273 | Q_D(QBarSeries); |
|
266 | 274 | bool success = d->insert(index, set); |
|
267 | 275 | if (success) { |
|
268 | 276 | QList<QBarSet*> sets; |
|
269 | 277 | sets.append(set); |
|
270 | 278 | emit barsetsAdded(sets); |
|
271 | 279 | emit countChanged(); |
|
272 | 280 | } |
|
273 | 281 | return success; |
|
274 | 282 | } |
|
275 | 283 | |
|
276 | 284 | /*! |
|
277 | 285 | Removes all of the bar sets from the series |
|
278 | 286 | */ |
|
279 | 287 | void QBarSeries::clear() |
|
280 | 288 | { |
|
281 | 289 | Q_D(QBarSeries); |
|
282 | 290 | QList<QBarSet *> sets = barSets(); |
|
283 | 291 | bool success = d->remove(sets); |
|
284 | 292 | if (success) { |
|
285 | 293 | emit barsetsRemoved(sets); |
|
286 | 294 | emit countChanged(); |
|
287 | 295 | } |
|
288 | 296 | } |
|
289 | 297 | |
|
290 | 298 | /*! |
|
291 | 299 | Returns number of sets in series. |
|
292 | 300 | */ |
|
293 | 301 | int QBarSeries::count() const |
|
294 | 302 | { |
|
295 | 303 | Q_D(const QBarSeries); |
|
296 | 304 | return d->m_barSets.count(); |
|
297 | 305 | } |
|
298 | 306 | |
|
299 | 307 | /*! |
|
300 | 308 | Returns a list of sets in series. Keeps ownership of sets. |
|
301 | 309 | */ |
|
302 | 310 | QList<QBarSet*> QBarSeries::barSets() const |
|
303 | 311 | { |
|
304 | 312 | Q_D(const QBarSeries); |
|
305 | 313 | return d->m_barSets; |
|
306 | 314 | } |
|
307 | 315 | |
|
308 | 316 | /*! |
|
309 | 317 | Sets the visibility of labels in series to \a visible |
|
310 | 318 | */ |
|
311 | 319 | void QBarSeries::setLabelsVisible(bool visible) |
|
312 | 320 | { |
|
313 | 321 | Q_D(QBarSeries); |
|
314 | 322 | if (d->m_labelsVisible != visible) { |
|
315 | 323 | d->setLabelsVisible(visible); |
|
316 | 324 | emit labelsVisibleChanged(); |
|
317 | 325 | } |
|
318 | 326 | } |
|
319 | 327 | |
|
320 | 328 | /*! |
|
321 | 329 | Returns the visibility of labels |
|
322 | 330 | */ |
|
323 | 331 | bool QBarSeries::isLabelsVisible() const |
|
324 | 332 | { |
|
325 | 333 | Q_D(const QBarSeries); |
|
326 | 334 | return d->m_labelsVisible; |
|
327 | 335 | } |
|
328 | 336 | |
|
329 | 337 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
330 | 338 | |
|
331 | 339 | QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : |
|
332 | 340 | QAbstractSeriesPrivate(q), |
|
333 | 341 | m_barWidth(0.5), // Default value is 50% of category width |
|
334 | 342 | m_labelsVisible(false), |
|
335 | 343 | m_visible(true) |
|
336 | 344 | { |
|
337 | 345 | } |
|
338 | 346 | |
|
339 | 347 | int QBarSeriesPrivate::categoryCount() const |
|
340 | 348 | { |
|
341 | 349 | // No categories defined. return count of longest set. |
|
342 | 350 | int count = 0; |
|
343 | 351 | for (int i=0; i<m_barSets.count(); i++) { |
|
344 | 352 | if (m_barSets.at(i)->count() > count) { |
|
345 | 353 | count = m_barSets.at(i)->count(); |
|
346 | 354 | } |
|
347 | 355 | } |
|
348 | 356 | |
|
349 | 357 | return count; |
|
350 | 358 | } |
|
351 | 359 | |
|
352 | 360 | void QBarSeriesPrivate::setBarWidth(qreal width) |
|
353 | 361 | { |
|
354 | 362 | if (width < 0.0) { |
|
355 | 363 | width = 0.0; |
|
356 | 364 | } |
|
357 | 365 | m_barWidth = width; |
|
358 | 366 | emit updatedBars(); |
|
359 | 367 | } |
|
360 | 368 | |
|
361 | 369 | qreal QBarSeriesPrivate::barWidth() const |
|
362 | 370 | { |
|
363 | 371 | return m_barWidth; |
|
364 | 372 | } |
|
365 | 373 | |
|
366 | 374 | QBarSet* QBarSeriesPrivate::barsetAt(int index) |
|
367 | 375 | { |
|
368 | 376 | return m_barSets.at(index); |
|
369 | 377 | } |
|
370 | 378 | |
|
371 | 379 | void QBarSeriesPrivate::setVisible(bool visible) |
|
372 | 380 | { |
|
373 | 381 | m_visible = visible; |
|
374 | 382 | emit updatedBars(); |
|
375 | 383 | } |
|
376 | 384 | |
|
377 | 385 | void QBarSeriesPrivate::setLabelsVisible(bool visible) |
|
378 | 386 | { |
|
379 | 387 | m_labelsVisible = visible; |
|
380 | 388 | emit labelsVisibleChanged(visible); |
|
381 | 389 | } |
|
382 | 390 | |
|
383 | 391 | qreal QBarSeriesPrivate::min() |
|
384 | 392 | { |
|
385 | 393 | if (m_barSets.count() <= 0) { |
|
386 | 394 | return 0; |
|
387 | 395 | } |
|
388 | 396 | qreal min = INT_MAX; |
|
389 | 397 | |
|
390 | 398 | for (int i = 0; i < m_barSets.count(); i++) { |
|
391 | 399 | int categoryCount = m_barSets.at(i)->count(); |
|
392 | 400 | for (int j = 0; j < categoryCount; j++) { |
|
393 | 401 | qreal temp = m_barSets.at(i)->at(j).y(); |
|
394 | 402 | if (temp < min) |
|
395 | 403 | min = temp; |
|
396 | 404 | } |
|
397 | 405 | } |
|
398 | 406 | return min; |
|
399 | 407 | } |
|
400 | 408 | |
|
401 | 409 | qreal QBarSeriesPrivate::max() |
|
402 | 410 | { |
|
403 | 411 | if (m_barSets.count() <= 0) { |
|
404 | 412 | return 0; |
|
405 | 413 | } |
|
406 | 414 | qreal max = INT_MIN; |
|
407 | 415 | |
|
408 | 416 | for (int i = 0; i < m_barSets.count(); i++) { |
|
409 | 417 | int categoryCount = m_barSets.at(i)->count(); |
|
410 | 418 | for (int j = 0; j < categoryCount; j++) { |
|
411 | 419 | qreal temp = m_barSets.at(i)->at(j).y(); |
|
412 | 420 | if (temp > max) |
|
413 | 421 | max = temp; |
|
414 | 422 | } |
|
415 | 423 | } |
|
416 | 424 | |
|
417 | 425 | return max; |
|
418 | 426 | } |
|
419 | 427 | |
|
420 | 428 | qreal QBarSeriesPrivate::valueAt(int set, int category) |
|
421 | 429 | { |
|
422 | 430 | if ((set < 0) || (set >= m_barSets.count())) { |
|
423 | 431 | // No set, no value. |
|
424 | 432 | return 0; |
|
425 | 433 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
426 | 434 | // No category, no value. |
|
427 | 435 | return 0; |
|
428 | 436 | } |
|
429 | 437 | |
|
430 | 438 | return m_barSets.at(set)->at(category).y(); |
|
431 | 439 | } |
|
432 | 440 | |
|
433 | 441 | qreal QBarSeriesPrivate::percentageAt(int set, int category) |
|
434 | 442 | { |
|
435 | 443 | if ((set < 0) || (set >= m_barSets.count())) { |
|
436 | 444 | // No set, no value. |
|
437 | 445 | return 0; |
|
438 | 446 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
439 | 447 | // No category, no value. |
|
440 | 448 | return 0; |
|
441 | 449 | } |
|
442 | 450 | |
|
443 | 451 | qreal value = m_barSets.at(set)->at(category).y(); |
|
444 | 452 | qreal sum = categorySum(category); |
|
445 | 453 | if ( qFuzzyIsNull(sum) ) { |
|
446 | 454 | return 0; |
|
447 | 455 | } |
|
448 | 456 | |
|
449 | 457 | return value / sum; |
|
450 | 458 | } |
|
451 | 459 | |
|
452 | 460 | qreal QBarSeriesPrivate::categorySum(int category) |
|
453 | 461 | { |
|
454 | 462 | qreal sum(0); |
|
455 | 463 | int count = m_barSets.count(); // Count sets |
|
456 | 464 | for (int set = 0; set < count; set++) { |
|
457 | 465 | if (category < m_barSets.at(set)->count()) |
|
458 | 466 | sum += m_barSets.at(set)->at(category).y(); |
|
459 | 467 | } |
|
460 | 468 | return sum; |
|
461 | 469 | } |
|
462 | 470 | |
|
463 | 471 | qreal QBarSeriesPrivate::absoluteCategorySum(int category) |
|
464 | 472 | { |
|
465 | 473 | qreal sum(0); |
|
466 | 474 | int count = m_barSets.count(); // Count sets |
|
467 | 475 | for (int set = 0; set < count; set++) { |
|
468 | 476 | if (category < m_barSets.at(set)->count()) |
|
469 | 477 | sum += qAbs(m_barSets.at(set)->at(category).y()); |
|
470 | 478 | } |
|
471 | 479 | return sum; |
|
472 | 480 | } |
|
473 | 481 | |
|
474 | 482 | qreal QBarSeriesPrivate::maxCategorySum() |
|
475 | 483 | { |
|
476 | 484 | qreal max = INT_MIN; |
|
477 | 485 | int count = categoryCount(); |
|
478 | 486 | for (int i = 0; i < count; i++) { |
|
479 | 487 | qreal sum = categorySum(i); |
|
480 | 488 | if (sum > max) |
|
481 | 489 | max = sum; |
|
482 | 490 | } |
|
483 | 491 | return max; |
|
484 | 492 | } |
|
485 | 493 | |
|
486 | 494 | qreal QBarSeriesPrivate::minX() |
|
487 | 495 | { |
|
488 | 496 | if (m_barSets.count() <= 0) { |
|
489 | 497 | return 0; |
|
490 | 498 | } |
|
491 | 499 | qreal min = INT_MAX; |
|
492 | 500 | |
|
493 | 501 | for (int i = 0; i < m_barSets.count(); i++) { |
|
494 | 502 | int categoryCount = m_barSets.at(i)->count(); |
|
495 | 503 | for (int j = 0; j < categoryCount; j++) { |
|
496 | 504 | qreal temp = m_barSets.at(i)->at(j).x(); |
|
497 | 505 | if (temp < min) |
|
498 | 506 | min = temp; |
|
499 | 507 | } |
|
500 | 508 | } |
|
501 | 509 | return min; |
|
502 | 510 | } |
|
503 | 511 | |
|
504 | 512 | qreal QBarSeriesPrivate::maxX() |
|
505 | 513 | { |
|
506 | 514 | if (m_barSets.count() <= 0) { |
|
507 | 515 | return 0; |
|
508 | 516 | } |
|
509 | 517 | qreal max = INT_MIN; |
|
510 | 518 | |
|
511 | 519 | for (int i = 0; i < m_barSets.count(); i++) { |
|
512 | 520 | int categoryCount = m_barSets.at(i)->count(); |
|
513 | 521 | for (int j = 0; j < categoryCount; j++) { |
|
514 | 522 | qreal temp = m_barSets.at(i)->at(j).x(); |
|
515 | 523 | if (temp > max) |
|
516 | 524 | max = temp; |
|
517 | 525 | } |
|
518 | 526 | } |
|
519 | 527 | |
|
520 | 528 | return max; |
|
521 | 529 | } |
|
522 | 530 | |
|
523 | 531 | |
|
524 | 532 | void QBarSeriesPrivate::scaleDomain(Domain& domain) |
|
525 | 533 | { |
|
526 | 534 | qreal minX(domain.minX()); |
|
527 | 535 | qreal minY(domain.minY()); |
|
528 | 536 | qreal maxX(domain.maxX()); |
|
529 | 537 | qreal maxY(domain.maxY()); |
|
530 | 538 | int tickXCount(domain.tickXCount()); |
|
531 | 539 | int tickYCount(domain.tickYCount()); |
|
532 | 540 | |
|
533 | 541 | qreal seriesMinX = this->minX(); |
|
534 | 542 | qreal seriesMaxX = this->maxX(); |
|
535 | 543 | qreal y = max(); |
|
536 | 544 | minX = qMin(minX, seriesMinX - 0.5); |
|
537 | 545 | minY = qMin(minY, y); |
|
538 | 546 | maxX = qMax(maxX, seriesMaxX + 0.5); |
|
539 | 547 | maxY = qMax(maxY, y); |
|
540 | 548 | tickXCount = categoryCount()+1; |
|
541 | 549 | |
|
542 | 550 | domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); |
|
543 | 551 | } |
|
544 | 552 | |
|
545 | 553 | Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter) |
|
546 | 554 | { |
|
547 | 555 | Q_Q(QBarSeries); |
|
548 | 556 | |
|
549 | 557 | BarChartItem* bar = new BarChartItem(q,presenter); |
|
550 | 558 | if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { |
|
551 | 559 | presenter->animator()->addAnimation(bar); |
|
552 | 560 | } |
|
553 | 561 | presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); |
|
554 | 562 | return bar; |
|
555 | 563 | |
|
556 | 564 | } |
|
557 | 565 | |
|
558 | 566 | QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend) |
|
559 | 567 | { |
|
560 | 568 | Q_Q(QBarSeries); |
|
561 | 569 | QList<LegendMarker*> markers; |
|
562 | 570 | foreach(QBarSet* set, q->barSets()) { |
|
563 | 571 | BarLegendMarker* marker = new BarLegendMarker(q,set,legend); |
|
564 | 572 | markers << marker; |
|
565 | 573 | } |
|
566 | 574 | |
|
567 | 575 | return markers; |
|
568 | 576 | } |
|
569 | 577 | |
|
570 | 578 | bool QBarSeriesPrivate::append(QBarSet *set) |
|
571 | 579 | { |
|
572 | 580 | Q_Q(QBarSeries); |
|
573 | 581 | if ((m_barSets.contains(set)) || (set == 0)) { |
|
574 | 582 | // Fail if set is already in list or set is null. |
|
575 | 583 | return false; |
|
576 | 584 | } |
|
577 | 585 | m_barSets.append(set); |
|
578 | 586 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
579 | 587 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
580 | 588 | emit restructuredBars(); // this notifies barchartitem |
|
581 | 589 | if (m_dataset) { |
|
582 | 590 | m_dataset->updateSeries(q); // this notifies legend |
|
583 | 591 | } |
|
584 | 592 | return true; |
|
585 | 593 | } |
|
586 | 594 | |
|
587 | 595 | bool QBarSeriesPrivate::remove(QBarSet *set) |
|
588 | 596 | { |
|
589 | 597 | Q_Q(QBarSeries); |
|
590 | 598 | if (!m_barSets.contains(set)) { |
|
591 | 599 | // Fail if set is not in list |
|
592 | 600 | return false; |
|
593 | 601 | } |
|
594 | 602 | m_barSets.removeOne(set); |
|
595 | 603 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
596 | 604 | QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
597 | 605 | emit restructuredBars(); // this notifies barchartitem |
|
598 | 606 | if (m_dataset) { |
|
599 | 607 | m_dataset->updateSeries(q); // this notifies legend |
|
600 | 608 | } |
|
601 | 609 | return true; |
|
602 | 610 | } |
|
603 | 611 | |
|
604 | 612 | bool QBarSeriesPrivate::append(QList<QBarSet* > sets) |
|
605 | 613 | { |
|
606 | 614 | Q_Q(QBarSeries); |
|
607 | 615 | foreach (QBarSet* set, sets) { |
|
608 | 616 | if ((set == 0) || (m_barSets.contains(set))) { |
|
609 | 617 | // Fail if any of the sets is null or is already appended. |
|
610 | 618 | return false; |
|
611 | 619 | } |
|
612 | 620 | if (sets.count(set) != 1) { |
|
613 | 621 | // Also fail if same set is more than once in given list. |
|
614 | 622 | return false; |
|
615 | 623 | } |
|
616 | 624 | } |
|
617 | 625 | |
|
618 | 626 | foreach (QBarSet* set, sets) { |
|
619 | 627 | m_barSets.append(set); |
|
620 | 628 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
621 | 629 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
622 | 630 | } |
|
623 | 631 | emit restructuredBars(); // this notifies barchartitem |
|
624 | 632 | if (m_dataset) { |
|
625 | 633 | m_dataset->updateSeries(q); // this notifies legend |
|
626 | 634 | } |
|
627 | 635 | return true; |
|
628 | 636 | } |
|
629 | 637 | |
|
630 | 638 | bool QBarSeriesPrivate::remove(QList<QBarSet* > sets) |
|
631 | 639 | { |
|
632 | 640 | Q_Q(QBarSeries); |
|
633 | 641 | if (sets.count() == 0) { |
|
634 | 642 | return false; |
|
635 | 643 | } |
|
636 | 644 | foreach (QBarSet* set, sets) { |
|
637 | 645 | if ((set == 0) || (!m_barSets.contains(set))) { |
|
638 | 646 | // Fail if any of the sets is null or is not in series |
|
639 | 647 | return false; |
|
640 | 648 | } |
|
641 | 649 | if (sets.count(set) != 1) { |
|
642 | 650 | // Also fail if same set is more than once in given list. |
|
643 | 651 | return false; |
|
644 | 652 | } |
|
645 | 653 | } |
|
646 | 654 | |
|
647 | 655 | foreach (QBarSet* set, sets) { |
|
648 | 656 | m_barSets.removeOne(set); |
|
649 | 657 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
650 | 658 | QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
651 | 659 | } |
|
652 | 660 | |
|
653 | 661 | emit restructuredBars(); // this notifies barchartitem |
|
654 | 662 | if (m_dataset) { |
|
655 | 663 | m_dataset->updateSeries(q); // this notifies legend |
|
656 | 664 | } |
|
657 | 665 | return true; |
|
658 | 666 | } |
|
659 | 667 | |
|
660 | 668 | bool QBarSeriesPrivate::insert(int index, QBarSet *set) |
|
661 | 669 | { |
|
662 | 670 | Q_Q(QBarSeries); |
|
663 | 671 | if ((m_barSets.contains(set)) || (set == 0)) { |
|
664 | 672 | // Fail if set is already in list or set is null. |
|
665 | 673 | return false; |
|
666 | 674 | } |
|
667 | 675 | m_barSets.insert(index, set); |
|
668 | 676 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
669 | 677 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
670 | 678 | emit restructuredBars(); // this notifies barchartitem |
|
671 | 679 | if (m_dataset) { |
|
672 | 680 | m_dataset->updateSeries(q); // this notifies legend |
|
673 | 681 | } |
|
674 | 682 | return true; |
|
675 | 683 | } |
|
676 | 684 | |
|
677 | 685 | #include "moc_qbarseries.cpp" |
|
678 | 686 | #include "moc_qbarseries_p.cpp" |
|
679 | 687 | |
|
680 | 688 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,54 +1,58 | |||
|
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 | import QtQuick 1.0 |
|
22 | 22 | import QtCommercial.Chart 1.0 |
|
23 | 23 | |
|
24 | 24 | ChartView { |
|
25 | 25 | title: "Bar series" |
|
26 | 26 | anchors.fill: parent |
|
27 | 27 | theme: ChartView.ChartThemeLight |
|
28 | 28 | legend.alignment: Qt.AlignBottom |
|
29 | 29 | axisXLabels: ["0", "2007", "1", "2008", "2", "2009", "3", "2010", "4", "2011", "5", "2012"] |
|
30 | 30 | |
|
31 | 31 | property variant series: barSeries |
|
32 | 32 | |
|
33 | 33 | BarSeries { |
|
34 | 34 | id: barSeries |
|
35 | 35 | name: "bar" |
|
36 | 36 | BarSet { label: "Bob"; values: [4, 7, 3, 10, 5, 6] |
|
37 | 37 | onClicked: console.log("barset.onClicked: " + index); |
|
38 | 38 | onHovered: console.log("barset.onHovered: " + status); |
|
39 | 39 | onColorChanged: console.log("barset.onColorChanged: " + color); |
|
40 | 40 | onBorderColorChanged: console.log("barset.onBorderColorChanged: " + color); |
|
41 | 41 | onLabelColorChanged: console.log("barset.onLabelColorChanged: " + color); |
|
42 | 42 | onCountChanged: console.log("barset.onCountChanged: " + count); |
|
43 | 43 | } |
|
44 | 44 | BarSet { label: "Susan"; values: [5, 1, 2, 4, 1, 8] } |
|
45 | 45 | BarSet { label: "James"; values: [3, 5, 8, 5, 4, 7] } |
|
46 | 46 | |
|
47 | 47 | onNameChanged: console.log("barSeries.onNameChanged: " + series.name); |
|
48 | 48 | onVisibleChanged: console.log("barSeries.onVisibleChanged: " + series.visible); |
|
49 | 49 | onClicked: console.log("barSeries.onClicked: " + barset + " " + index); |
|
50 | 50 | onHovered: console.log("barSeries.onHovered: " + barset + " " + status); |
|
51 | 51 | onLabelsVisibleChanged: console.log("barSeries.onLabelsVisibleChanged: " + series.labelsVisible); |
|
52 | 52 | onCountChanged: console.log("barSeries.onCountChanged: " + count); |
|
53 | onBarsetsAdded: console.log("barSeries.onBarsetsAdded: " + sets); // There is no point in this signal on QML side | |
|
54 | onAdded: console.log("barSeries.onBarsetAdded: " + barset); | |
|
55 | onBarsetsRemoved: console.log("barSeries.onBarsetsRemoved: " + sets); // There is no point in this signal on QML side | |
|
56 | onRemoved: console.log("barSeries.onBarsetRemoved: " + barset); | |
|
53 | 57 | } |
|
54 | 58 | } |
@@ -1,66 +1,70 | |||
|
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 | import QtQuick 1.0 |
|
22 | 22 | import QtCommercial.Chart 1.0 |
|
23 | 23 | |
|
24 | 24 | Flow { |
|
25 | 25 | id: flow |
|
26 | 26 | spacing: 5 |
|
27 | 27 | flow: Flow.TopToBottom |
|
28 | 28 | property variant series |
|
29 | 29 | |
|
30 | 30 | Button { |
|
31 | 31 | text: "visible" |
|
32 | 32 | onClicked: series.visible = !series.visible; |
|
33 | 33 | } |
|
34 | 34 | Button { |
|
35 | 35 | text: "labels visible" |
|
36 | 36 | onClicked: series.labelsVisible = !series.labelsVisible; |
|
37 | 37 | } |
|
38 | 38 | Button { |
|
39 | 39 | text: "bar width +" |
|
40 | 40 | onClicked: series.barWidth += 0.1; |
|
41 | 41 | } |
|
42 | 42 | Button { |
|
43 | 43 | text: "bar width -" |
|
44 | 44 | onClicked: series.barWidth -= 0.1; |
|
45 | 45 | } |
|
46 | 46 | Button { |
|
47 | text: "remove set" | |
|
48 | onClicked: series.remove(series.count - 1); | |
|
49 | } | |
|
50 | Button { | |
|
47 | 51 | text: "set 1 color" |
|
48 | 52 | onClicked: series.at(0).color = main.nextColor(); |
|
49 | 53 | } |
|
50 | 54 | Button { |
|
51 | 55 | text: "set 1 border color" |
|
52 | 56 | onClicked: series.at(0).borderColor = main.nextColor(); |
|
53 | 57 | } |
|
54 | 58 | Button { |
|
55 | 59 | text: "set 1 label color" |
|
56 | 60 | onClicked: series.at(0).labelColor = main.nextColor(); |
|
57 | 61 | } |
|
58 | 62 | Button { |
|
59 | 63 | text: "set 1 font size +" |
|
60 | 64 | onClicked: series.at(0).labelFont.pixelSize += 1; |
|
61 | 65 | } |
|
62 | 66 | Button { |
|
63 | 67 | text: "set 1 font size -" |
|
64 | 68 | onClicked: series.at(0).labelFont.pixelSize -= 1; |
|
65 | 69 | } |
|
66 | 70 | } |
General Comments 0
You need to be logged in to leave comments.
Login now