@@ -1,91 +1,94 | |||
|
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 <QApplication> |
|
22 | 22 | #include <QMainWindow> |
|
23 | 23 | #include <QChartView> |
|
24 | 24 | #include <QStackedBarSeries> |
|
25 | 25 | #include <QBarSet> |
|
26 | 26 | #include <QLegend> |
|
27 | 27 | #include <QCategoriesAxis> |
|
28 | 28 | |
|
29 | 29 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
30 | 30 | |
|
31 | 31 | int main(int argc, char *argv[]) |
|
32 | 32 | { |
|
33 | 33 | QApplication a(argc, argv); |
|
34 | 34 | |
|
35 | 35 | //![1] |
|
36 | 36 | QBarSet *set0 = new QBarSet("Jane"); |
|
37 | 37 | QBarSet *set1 = new QBarSet("John"); |
|
38 | 38 | QBarSet *set2 = new QBarSet("Axel"); |
|
39 | 39 | QBarSet *set3 = new QBarSet("Mary"); |
|
40 | 40 | QBarSet *set4 = new QBarSet("Samantha"); |
|
41 | 41 | |
|
42 | 42 | *set0 << 1 << 2 << 3 << 4 << 5 << 6; |
|
43 | 43 | *set1 << 5 << 0 << 0 << 4 << 0 << 7; |
|
44 | 44 | *set2 << 3 << 5 << 8 << 13 << 8 << 5; |
|
45 | 45 | *set3 << 5 << 6 << 7 << 3 << 4 << 5; |
|
46 | 46 | *set4 << 9 << 7 << 5 << 3 << 1 << 2; |
|
47 | 47 | //![1] |
|
48 | 48 | |
|
49 | 49 | //![2] |
|
50 | 50 | QStackedBarSeries* series = new QStackedBarSeries(); |
|
51 | 51 | series->append(set0); |
|
52 | 52 | series->append(set1); |
|
53 | 53 | series->append(set2); |
|
54 | 54 | series->append(set3); |
|
55 | 55 | series->append(set4); |
|
56 | 56 | //![2] |
|
57 | 57 | |
|
58 | 58 | //![3] |
|
59 | 59 | QChart* chart = new QChart(); |
|
60 | 60 | chart->addSeries(series); |
|
61 | 61 | chart->setTitle("Simple stackedbarchart example"); |
|
62 | 62 | //![3] |
|
63 | 63 | |
|
64 | 64 | //![4] |
|
65 | 65 | QStringList categories; |
|
66 | 66 | categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; |
|
67 | 67 | QCategoriesAxis* axis = new QCategoriesAxis(); |
|
68 | 68 | axis->append(categories); |
|
69 | 69 | chart->setAxisX(series,axis); |
|
70 | // axis->setMinCategory(QString("Feb")); | |
|
71 | // axis->setMaxCategory(QString("May")); | |
|
72 | // axis->setRange(QString("Feb"), QString("Apr")); | |
|
70 | 73 | //![4] |
|
71 | 74 | |
|
72 | 75 | //![5] |
|
73 | 76 | chart->legend()->setVisible(true); |
|
74 | 77 | chart->legend()->setAlignment(Qt::AlignBottom); |
|
75 | 78 | //![5] |
|
76 | 79 | |
|
77 | 80 | //![6] |
|
78 | 81 | QChartView* chartView = new QChartView(chart); |
|
79 | 82 | chartView->setRenderHint(QPainter::Antialiasing); |
|
80 | 83 | //![6] |
|
81 | 84 | |
|
82 | 85 | //![7] |
|
83 | 86 | QMainWindow window; |
|
84 | 87 | window.setCentralWidget(chartView); |
|
85 | 88 | window.resize(400, 300); |
|
86 | 89 | window.show(); |
|
87 | 90 | //![7] |
|
88 | 91 | |
|
89 | 92 | return a.exec(); |
|
90 | 93 | } |
|
91 | 94 |
@@ -1,280 +1,293 | |||
|
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 "qcategoriesaxis.h" |
|
22 | 22 | #include "qcategoriesaxis_p.h" |
|
23 | 23 | #include "chartcategoriesaxisx_p.h" |
|
24 | 24 | #include "chartcategoriesaxisy_p.h" |
|
25 | #include <qmath.h> | |
|
25 | 26 | |
|
26 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
27 | 28 | |
|
28 | 29 | QCategoriesAxis::QCategoriesAxis(QObject *parent): |
|
29 | 30 | QAbstractAxis(*new QCategoriesAxisPrivate(this),parent) |
|
30 | 31 | { |
|
31 | 32 | } |
|
32 | 33 | |
|
33 | 34 | QCategoriesAxis::~QCategoriesAxis() |
|
34 | 35 | { |
|
35 | 36 | } |
|
36 | 37 | |
|
37 | 38 | QCategoriesAxis::QCategoriesAxis(QCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent) |
|
38 | 39 | { |
|
39 | 40 | |
|
40 | 41 | } |
|
41 | 42 | |
|
42 | 43 | /*! |
|
43 | 44 | Appends \a categories to axis |
|
44 | 45 | */ |
|
45 | 46 | void QCategoriesAxis::append(const QStringList &categories) |
|
46 | 47 | { |
|
47 | 48 | Q_D(QCategoriesAxis); |
|
48 | 49 | d->m_categories.append(categories); |
|
49 | 50 | emit categoriesChanged(); |
|
50 | 51 | } |
|
51 | 52 | |
|
52 | 53 | /*! |
|
53 | 54 | Appends \a category to axis |
|
54 | 55 | */ |
|
55 | 56 | void QCategoriesAxis::append(const QString &category) |
|
56 | 57 | { |
|
57 | 58 | Q_D(QCategoriesAxis); |
|
58 | 59 | d->m_categories.append(category); |
|
59 | 60 | emit categoriesChanged(); |
|
60 | 61 | } |
|
61 | 62 | |
|
62 | 63 | /*! |
|
63 | 64 | Removes \a category from axis |
|
64 | 65 | */ |
|
65 | 66 | void QCategoriesAxis::remove(const QString &category) |
|
66 | 67 | { |
|
67 | 68 | Q_D(QCategoriesAxis); |
|
68 | 69 | if (d->m_categories.contains(category)) { |
|
69 | 70 | d->m_categories.removeAt(d->m_categories.indexOf(category)); |
|
70 | 71 | emit categoriesChanged(); |
|
71 | 72 | } |
|
72 | 73 | } |
|
73 | 74 | |
|
74 | 75 | /*! |
|
75 | 76 | Inserts \a category to axis at \a index |
|
76 | 77 | */ |
|
77 | 78 | void QCategoriesAxis::insert(int index, const QString &category) |
|
78 | 79 | { |
|
79 | 80 | Q_D(QCategoriesAxis); |
|
80 | 81 | d->m_categories.insert(index,category); |
|
81 | 82 | emit categoriesChanged(); |
|
82 | 83 | } |
|
83 | 84 | |
|
84 | 85 | /*! |
|
85 | 86 | Removes all categories. |
|
86 | 87 | */ |
|
87 | 88 | void QCategoriesAxis::clear() |
|
88 | 89 | { |
|
89 | 90 | Q_D(QCategoriesAxis); |
|
90 | 91 | d->m_categories.clear(); |
|
91 | 92 | emit categoriesChanged(); |
|
92 | 93 | } |
|
93 | 94 | |
|
94 | 95 | void QCategoriesAxis::setCategories(const QStringList &categories) |
|
95 | 96 | { |
|
96 | 97 | Q_D(QCategoriesAxis); |
|
97 | 98 | d->m_categories = categories; |
|
98 | 99 | emit categoriesChanged(); |
|
99 | 100 | } |
|
100 | 101 | |
|
101 | 102 | QStringList QCategoriesAxis::categories() |
|
102 | 103 | { |
|
103 | 104 | Q_D(QCategoriesAxis); |
|
104 | 105 | return d->m_categories; |
|
105 | 106 | } |
|
106 | 107 | |
|
107 | 108 | /*! |
|
108 | 109 | Returns number of categories. |
|
109 | 110 | */ |
|
110 | 111 | int QCategoriesAxis::count() const |
|
111 | 112 | { |
|
112 | 113 | Q_D(const QCategoriesAxis); |
|
113 | 114 | return d->m_categories.count(); |
|
114 | 115 | } |
|
115 | 116 | |
|
116 | 117 | /*! |
|
117 | 118 | Returns category at \a index. Index must be valid. |
|
118 | 119 | */ |
|
119 | 120 | QString QCategoriesAxis::at(int index) const |
|
120 | 121 | { |
|
121 | 122 | Q_D(const QCategoriesAxis); |
|
122 | 123 | return d->m_categories.at(index); |
|
123 | 124 | } |
|
124 | 125 | |
|
125 | 126 | /*! |
|
126 | 127 | Sets minimum category to \a minCategory. |
|
127 | 128 | */ |
|
128 | 129 | void QCategoriesAxis::setMinCategory(const QString& minCategory) |
|
129 | 130 | { |
|
130 | 131 | Q_D(QCategoriesAxis); |
|
131 | 132 | d->setMinCategory(minCategory); |
|
132 | 133 | } |
|
133 | 134 | |
|
134 | 135 | /*! |
|
135 | 136 | Returns minimum category. |
|
136 | 137 | */ |
|
137 | 138 | QString QCategoriesAxis::minCategory() const |
|
138 | 139 | { |
|
139 | 140 | Q_D(const QCategoriesAxis); |
|
140 | 141 | return d->m_minCategory; |
|
141 | 142 | } |
|
142 | 143 | |
|
143 | 144 | /*! |
|
144 | 145 | Sets maximum category to \a maxCategory. |
|
145 | 146 | */ |
|
146 | 147 | void QCategoriesAxis::setMaxCategory(const QString& maxCategory) |
|
147 | 148 | { |
|
148 | 149 | Q_D(QCategoriesAxis); |
|
149 | 150 | d->setMaxCategory(maxCategory); |
|
150 | 151 | } |
|
151 | 152 | |
|
152 | 153 | /*! |
|
153 | 154 | Returns maximum category |
|
154 | 155 | */ |
|
155 | 156 | QString QCategoriesAxis::maxCategory() const |
|
156 | 157 | { |
|
157 | 158 | Q_D(const QCategoriesAxis); |
|
158 | 159 | return d->m_maxCategory; |
|
159 | 160 | } |
|
160 | 161 | |
|
161 | 162 | /*! |
|
162 | 163 | Sets range from \a minCategory to \a maxCategory |
|
163 | 164 | */ |
|
164 | 165 | void QCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory) |
|
165 | 166 | { |
|
166 | 167 | Q_D(QCategoriesAxis); |
|
167 | 168 | d->setRangeCategory(minCategory,maxCategory); |
|
168 | 169 | } |
|
169 | 170 | |
|
170 | 171 | /*! |
|
171 | 172 | Returns the type of axis. |
|
172 | 173 | */ |
|
173 | 174 | QAbstractAxis::AxisType QCategoriesAxis::type() const |
|
174 | 175 | { |
|
175 | 176 | return AxisTypeCategories; |
|
176 | 177 | } |
|
177 | 178 | |
|
178 | 179 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
179 | 180 | |
|
180 | 181 | QCategoriesAxisPrivate::QCategoriesAxisPrivate(QCategoriesAxis* q): |
|
181 | 182 | QAbstractAxisPrivate(q) |
|
182 | 183 | { |
|
183 | 184 | |
|
184 | 185 | } |
|
185 | 186 | |
|
186 | 187 | QCategoriesAxisPrivate::~QCategoriesAxisPrivate() |
|
187 | 188 | { |
|
188 | 189 | |
|
189 | 190 | } |
|
190 | 191 | |
|
191 | 192 | void QCategoriesAxisPrivate::setMinCategory(const QString& minCategory) |
|
192 | 193 | { |
|
193 | 194 | // Convert the category to value |
|
194 | 195 | int minIndex = m_categories.indexOf(minCategory); |
|
195 | 196 | if (minIndex == -1) { |
|
196 | 197 | return; |
|
197 | 198 | } |
|
198 | setRange(minIndex,m_max); | |
|
199 | ||
|
200 | int maxIndex = m_max; | |
|
201 | if (minIndex > maxIndex) { | |
|
202 | maxIndex = m_categories.count()-1; | |
|
203 | } | |
|
204 | setRange(minIndex - 0.5, maxIndex + 0.5); | |
|
199 | 205 | } |
|
200 | 206 | |
|
201 | 207 | void QCategoriesAxisPrivate::setMaxCategory(const QString& maxCategory) |
|
202 | 208 | { |
|
203 | 209 | // Convert the category to value |
|
204 | 210 | int maxIndex = m_categories.indexOf(maxCategory); |
|
205 | 211 | if (maxIndex == -1) { |
|
206 | 212 | return; |
|
207 | 213 | } |
|
208 | setRange(m_min,maxIndex); | |
|
214 | if (maxIndex < m_min) { | |
|
215 | m_min = 0; | |
|
216 | } | |
|
217 | setRange(m_min - 0.5, maxIndex + 0.5); | |
|
209 | 218 | } |
|
210 | 219 | |
|
211 | 220 | void QCategoriesAxisPrivate::setRangeCategory(const QString& minCategory, const QString& maxCategory) |
|
212 | 221 | { |
|
213 | 222 | // TODO: |
|
214 | 223 | int minIndex = m_categories.indexOf(minCategory); |
|
215 | 224 | if (minIndex == -1) { |
|
216 | 225 | return; |
|
217 | 226 | } |
|
218 | 227 | int maxIndex = m_categories.indexOf(maxCategory); |
|
219 | 228 | if (maxIndex == -1) { |
|
220 | 229 | return; |
|
221 | 230 | } |
|
222 | setRange(minIndex,maxIndex); | |
|
231 | setRange(minIndex -0.5, maxIndex + 0.5); | |
|
223 | 232 | } |
|
224 | 233 | |
|
225 | 234 | void QCategoriesAxisPrivate::setMin(const qreal min) |
|
226 | 235 | { |
|
227 | 236 | setRange(min,m_max); |
|
228 | 237 | } |
|
229 | 238 | |
|
230 | 239 | void QCategoriesAxisPrivate::setMax(const qreal max) |
|
231 | 240 | { |
|
232 | 241 | setRange(m_min,max); |
|
233 | 242 | } |
|
234 | 243 | |
|
235 | 244 | void QCategoriesAxisPrivate::setRange(const qreal min, const qreal max) |
|
236 | 245 | { |
|
246 | if (max <= min) { | |
|
247 | // max must be greater than min | |
|
248 | return; | |
|
249 | } | |
|
237 | 250 | Q_Q(QCategoriesAxis); |
|
238 | 251 | bool changed = false; |
|
239 | 252 | if (!qFuzzyIsNull(m_min - min)) { |
|
240 | 253 | m_min = min; |
|
241 | 254 | changed = true; |
|
242 | 255 | } |
|
243 | 256 | |
|
244 | 257 | if (!qFuzzyIsNull(m_max - max)) { |
|
245 | 258 | m_max = max; |
|
246 | 259 | changed = true; |
|
247 | 260 | } |
|
248 | 261 | |
|
249 | 262 | if (changed) { |
|
250 |
emit this->changed(m_min, m_max, |
|
|
263 | emit this->changed(m_min, m_max, qCeil(m_max) -qCeil(m_min) +1, false); | |
|
251 | 264 | emit q->categoriesChanged(); |
|
252 | 265 | } |
|
253 | 266 | } |
|
254 | 267 | |
|
255 | 268 | int QCategoriesAxisPrivate::ticksCount() const |
|
256 | 269 | { |
|
257 | 270 | return m_categories.count()+1; |
|
258 | 271 | } |
|
259 | 272 | |
|
260 | 273 | void QCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count) |
|
261 | 274 | { |
|
262 | 275 | m_min = min; |
|
263 | 276 | m_max = max; |
|
264 | 277 | m_ticksCount = count; |
|
265 | 278 | } |
|
266 | 279 | |
|
267 | 280 | ChartAxis* QCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter) |
|
268 | 281 | { |
|
269 | 282 | Q_Q( QCategoriesAxis); |
|
270 | 283 | if(m_orientation == Qt::Vertical){ |
|
271 | 284 | return new ChartCategoriesAxisY(q,presenter); |
|
272 | 285 | }else{ |
|
273 | 286 | return new ChartCategoriesAxisX(q,presenter); |
|
274 | 287 | } |
|
275 | 288 | } |
|
276 | 289 | |
|
277 | 290 | #include "moc_qcategoriesaxis.cpp" |
|
278 | 291 | #include "moc_qcategoriesaxis_p.cpp" |
|
279 | 292 | |
|
280 | 293 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,315 +1,316 | |||
|
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 "chartdataset_p.h" |
|
22 | 22 | #include "qchart.h" |
|
23 | 23 | #include "qvaluesaxis.h" |
|
24 | 24 | #include "qvaluesaxis_p.h" |
|
25 | 25 | #include "qabstractseries_p.h" |
|
26 | 26 | #include "qbarseries.h" |
|
27 | 27 | #include "qstackedbarseries.h" |
|
28 | 28 | #include "qpercentbarseries.h" |
|
29 | 29 | #include "qpieseries.h" |
|
30 | 30 | |
|
31 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | 32 | |
|
33 | 33 | ChartDataSet::ChartDataSet(QChart *parent):QObject(parent), |
|
34 | 34 | m_domainIndex(0) |
|
35 | 35 | { |
|
36 | 36 | |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | ChartDataSet::~ChartDataSet() |
|
40 | 40 | { |
|
41 | 41 | removeAllSeries(); |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | void ChartDataSet::addSeries(QAbstractSeries* series) |
|
45 | 45 | { |
|
46 | 46 | QAbstractAxis* axis = m_seriesAxisXMap.value(series); |
|
47 | 47 | |
|
48 | 48 | if(axis) { |
|
49 | 49 | qWarning() << "Can not add series. Series already on the chart"; |
|
50 | 50 | return; |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | QAbstractAxis* axisX = series->d_ptr->createAxisX(this); |
|
54 | 54 | QAbstractAxis* axisY = series->d_ptr->createAxisY(this); |
|
55 | 55 | |
|
56 | 56 | series->setParent(this); // take ownership |
|
57 | 57 | |
|
58 | 58 | Domain* domain = new Domain(series); |
|
59 | 59 | |
|
60 | 60 | if(axisX){ |
|
61 | 61 | QObject::connect(axisX->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); |
|
62 | 62 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); |
|
63 | 63 | axisX->d_ptr->m_orientation=Qt::Horizontal; |
|
64 | 64 | emit axisAdded(axisX,domain); |
|
65 | 65 | m_seriesAxisXMap.insert(series,axisX); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | if(axisY){ |
|
69 | 69 | QObject::connect(axisY->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); |
|
70 | 70 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); |
|
71 | 71 | axisY->d_ptr->m_orientation=Qt::Vertical; |
|
72 | 72 | emit axisAdded(axisY,domain); |
|
73 | 73 | m_seriesAxisYMap.insert(series,axisY); |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | m_seriesDomainMap.insert(series,domain); |
|
77 | 77 | |
|
78 | 78 | series->d_ptr->scaleDomain(*domain); |
|
79 | 79 | |
|
80 | 80 | QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap); |
|
81 | 81 | |
|
82 | 82 | int key=0; |
|
83 | 83 | while (i.hasNext()) { |
|
84 | 84 | i.next(); |
|
85 | 85 | if(i.key()!=key) { |
|
86 | 86 | break; |
|
87 | 87 | } |
|
88 | 88 | key++; |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | m_indexSeriesMap.insert(key,series); |
|
92 | 92 | |
|
93 | 93 | series->d_ptr->m_chart = qobject_cast<QChart*>(parent()); |
|
94 | 94 | series->d_ptr->m_dataset = this; |
|
95 | 95 | |
|
96 | 96 | emit seriesAdded(series,domain); |
|
97 | 97 | |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | void ChartDataSet::removeSeries(QAbstractSeries* series) |
|
101 | 101 | { |
|
102 | 102 | Domain* domain = m_seriesDomainMap.take(series); |
|
103 | 103 | |
|
104 | 104 | if(!domain) { |
|
105 | 105 | qWarning()<<"Can not remove series. Series not found on the chart."; |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | emit seriesRemoved(series); |
|
109 | 109 | |
|
110 | 110 | delete domain; |
|
111 | 111 | domain = 0; |
|
112 | 112 | |
|
113 | 113 | int key = seriesIndex(series); |
|
114 | 114 | Q_ASSERT(key!=-1); |
|
115 | 115 | |
|
116 | 116 | m_indexSeriesMap.remove(key); |
|
117 | 117 | |
|
118 | 118 | series->setParent(0); |
|
119 | 119 | series->d_ptr->m_chart = 0; |
|
120 | 120 | series->d_ptr->m_dataset = 0; |
|
121 | 121 | |
|
122 | 122 | QAbstractAxis* axisX = m_seriesAxisXMap.take(series); |
|
123 | 123 | |
|
124 | 124 | if(axisX) { |
|
125 | 125 | QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values(); |
|
126 | 126 | int x = axesX.indexOf(axisX); |
|
127 | 127 | |
|
128 | 128 | if(x==-1) { |
|
129 | 129 | emit axisRemoved(axisX); |
|
130 | 130 | axisX->deleteLater(); |
|
131 | 131 | } |
|
132 | 132 | } |
|
133 | 133 | |
|
134 | 134 | QAbstractAxis* axisY = m_seriesAxisYMap.take(series); |
|
135 | 135 | |
|
136 | 136 | if(axisY) { |
|
137 | 137 | QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values(); |
|
138 | 138 | |
|
139 | 139 | int y = axesY.indexOf(axisY); |
|
140 | 140 | |
|
141 | 141 | if(y==-1) { |
|
142 | 142 | emit axisRemoved(axisY); |
|
143 | 143 | axisY->deleteLater(); |
|
144 | 144 | } |
|
145 | 145 | } |
|
146 | 146 | } |
|
147 | 147 | |
|
148 | 148 | void ChartDataSet::removeAllSeries() |
|
149 | 149 | { |
|
150 | 150 | QList<QAbstractSeries*> series = m_seriesDomainMap.keys(); |
|
151 | 151 | foreach(QAbstractSeries *s , series) { |
|
152 | 152 | removeSeries(s); |
|
153 | 153 | } |
|
154 | 154 | |
|
155 | 155 | Q_ASSERT(m_seriesAxisXMap.count()==0); |
|
156 | 156 | Q_ASSERT(m_seriesAxisXMap.count()==0); |
|
157 | 157 | Q_ASSERT(m_seriesDomainMap.count()==0); |
|
158 | 158 | |
|
159 | 159 | qDeleteAll(series); |
|
160 | 160 | } |
|
161 | 161 | |
|
162 | 162 | void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) |
|
163 | 163 | { |
|
164 | 164 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); |
|
165 | 165 | while (i.hasNext()) { |
|
166 | 166 | i.next(); |
|
167 | 167 | i.value()->zoomIn(rect,size); |
|
168 | 168 | } |
|
169 | 169 | } |
|
170 | 170 | |
|
171 | 171 | void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size) |
|
172 | 172 | { |
|
173 | 173 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); |
|
174 | 174 | while (i.hasNext()) { |
|
175 | 175 | i.next(); |
|
176 | 176 | i.value()->zoomOut(rect,size); |
|
177 | 177 | } |
|
178 | 178 | } |
|
179 | 179 | |
|
180 | 180 | int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type) |
|
181 | 181 | { |
|
182 | 182 | int count=0; |
|
183 | 183 | QMapIterator<QAbstractSeries*, QAbstractAxis*> i(m_seriesAxisXMap); |
|
184 | 184 | while (i.hasNext()) { |
|
185 | 185 | i.next(); |
|
186 | 186 | if(i.key()->type()==type) count++; |
|
187 | 187 | } |
|
188 | 188 | return count; |
|
189 | 189 | } |
|
190 | 190 | |
|
191 | 191 | int ChartDataSet::seriesIndex(QAbstractSeries *series) |
|
192 | 192 | { |
|
193 | 193 | QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap); |
|
194 | 194 | while (i.hasNext()) { |
|
195 | 195 | i.next(); |
|
196 | 196 | if (i.value() == series) |
|
197 | 197 | return i.key(); |
|
198 | 198 | } |
|
199 | 199 | return -1; |
|
200 | 200 | } |
|
201 | 201 | |
|
202 | 202 | QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const |
|
203 | 203 | { |
|
204 | 204 | if(series == 0) return m_seriesAxisXMap.begin().value(); |
|
205 | 205 | return m_seriesAxisXMap.value(series); |
|
206 | 206 | } |
|
207 | 207 | |
|
208 | 208 | QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const |
|
209 | 209 | { |
|
210 | 210 | if(series == 0) return m_seriesAxisYMap.begin().value(); |
|
211 | 211 | return m_seriesAxisYMap.value(series); |
|
212 | 212 | } |
|
213 | 213 | |
|
214 | 214 | void ChartDataSet::setAxisX(QAbstractSeries *series, QAbstractAxis *axis) |
|
215 | 215 | { |
|
216 | 216 | |
|
217 | 217 | Q_ASSERT(axis); |
|
218 | 218 | QAbstractAxis *oldAxis = m_seriesAxisXMap.take(series); |
|
219 | 219 | |
|
220 | 220 | if(!oldAxis) { |
|
221 | 221 | qWarning()<<"Series not found on the chart."; |
|
222 | 222 | return; |
|
223 | 223 | } |
|
224 | 224 | |
|
225 | 225 | if(axis->d_ptr->m_orientation==Qt::Vertical) { |
|
226 | 226 | qWarning()<<"Axis already defined as axis Y"; |
|
227 | 227 | return; |
|
228 | 228 | } |
|
229 | 229 | |
|
230 | 230 | QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values(); |
|
231 | 231 | int x = axesX.indexOf(oldAxis); |
|
232 | 232 | if(x==-1) { |
|
233 | 233 | emit axisRemoved(oldAxis); |
|
234 | 234 | oldAxis->deleteLater(); |
|
235 | 235 | } |
|
236 | 236 | |
|
237 | // TODO: should we take domain from axis (if it has min and max already defined) | |
|
237 | 238 | Domain* domain = m_seriesDomainMap.value(series); |
|
238 | 239 | Q_ASSERT(domain); |
|
239 | 240 | |
|
240 | 241 | QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); |
|
241 | 242 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); |
|
242 | 243 | |
|
243 | 244 | x = axesX.indexOf(axis); |
|
244 | 245 | if(x==-1) { |
|
245 | 246 | axis->d_ptr->m_orientation=Qt::Horizontal; |
|
246 | 247 | emit axisAdded(axis,domain); |
|
247 | 248 | } |
|
248 | 249 | |
|
249 | 250 | m_seriesAxisXMap.insert(series,axis); |
|
250 | 251 | } |
|
251 | 252 | |
|
252 | 253 | void ChartDataSet::setAxisY(QAbstractSeries *series, QAbstractAxis *axis) |
|
253 | 254 | { |
|
254 | 255 | Q_ASSERT(axis); |
|
255 | 256 | QAbstractAxis *oldAxis = m_seriesAxisYMap.take(series); |
|
256 | 257 | |
|
257 | 258 | if(!oldAxis) { |
|
258 | 259 | qWarning()<<"Series not found on the chart or axis is unsupported for given series type"; |
|
259 | 260 | return; |
|
260 | 261 | } |
|
261 | 262 | |
|
262 | 263 | if(axis->d_ptr->m_orientation==Qt::Horizontal) { |
|
263 | 264 | qWarning()<<"Axis already defined as axis X"; |
|
264 | 265 | return; |
|
265 | 266 | } |
|
266 | 267 | |
|
267 | 268 | QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values(); |
|
268 | 269 | int y = axesY.indexOf(oldAxis); |
|
269 | 270 | if(y==-1) { |
|
270 | 271 | emit axisRemoved(oldAxis); |
|
271 | 272 | oldAxis->deleteLater(); |
|
272 | 273 | } |
|
273 | 274 | |
|
274 | 275 | Domain* domain = m_seriesDomainMap.value(series); |
|
275 | 276 | Q_ASSERT(domain); |
|
276 | 277 | |
|
277 | 278 | QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); |
|
278 | 279 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); |
|
279 | 280 | |
|
280 | 281 | y = axesY.indexOf(axis); |
|
281 | 282 | if(y==-1) { |
|
282 | 283 | axis->d_ptr->m_orientation=Qt::Vertical; |
|
283 | 284 | emit axisAdded(axis,domain); |
|
284 | 285 | } |
|
285 | 286 | |
|
286 | 287 | m_seriesAxisYMap.insert(series,axis); |
|
287 | 288 | } |
|
288 | 289 | |
|
289 | 290 | Domain* ChartDataSet::domain(QAbstractSeries *series) const |
|
290 | 291 | { |
|
291 | 292 | return m_seriesDomainMap.value(series); |
|
292 | 293 | } |
|
293 | 294 | |
|
294 | 295 | void ChartDataSet::scrollDomain(qreal dx,qreal dy,const QSizeF& size) |
|
295 | 296 | { |
|
296 | 297 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); |
|
297 | 298 | while (i.hasNext()) { |
|
298 | 299 | i.next(); |
|
299 | 300 | i.value()->move(dx,dy,size); |
|
300 | 301 | } |
|
301 | 302 | } |
|
302 | 303 | |
|
303 | 304 | QList<QAbstractSeries*> ChartDataSet::series() const |
|
304 | 305 | { |
|
305 | 306 | return m_seriesAxisXMap.keys(); |
|
306 | 307 | } |
|
307 | 308 | |
|
308 | 309 | void ChartDataSet::updateSeries(QAbstractSeries *series) |
|
309 | 310 | { |
|
310 | 311 | emit seriesUpdated(series); |
|
311 | 312 | } |
|
312 | 313 | |
|
313 | 314 | #include "moc_chartdataset_p.cpp" |
|
314 | 315 | |
|
315 | 316 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now