##// END OF EJS Templates
QBoxSet changed from list to table mode. New example for file reading....
Mika Salmela -
r2508:25def4b65f99
parent child
Show More
@@ -0,0 +1,59
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "boxdatareader.h"
22
23 #include <QDebug>
24
25 BoxDataReader::BoxDataReader(QIODevice *fileHandle) :
26 QTextStream(fileHandle)
27 {
28 }
29
30 QBoxSet* BoxDataReader::readBox()
31 {
32 QString line = readLine();
33 if (line.startsWith("#"))
34 return 0;
35
36 QStringList strList = line.split(" ", QString::SkipEmptyParts);
37 QList<qreal> sortedList;
38 foreach (QString str, strList) {
39 sortedList.append(str.toDouble());
40 }
41
42 qSort(sortedList.begin(), sortedList.end());
43 qDebug() << "sortedList = " << sortedList;
44
45 int count = sortedList.count();
46 int median = (int)(count / 2);
47 int lowerQ = (int)(median / 2);
48 int upperQ = median + lowerQ + 1;
49
50 QBoxSet *box = new QBoxSet();
51 box->setLowerExtreme(sortedList.at(0));
52 box->setLowerQuartile(sortedList.at(lowerQ));
53 box->setMedian(sortedList.at(median));
54 box->setUpperQuartile(sortedList.at(upperQ));
55 box->setUpperExtreme(sortedList.last());
56
57 return box;
58 }
59
@@ -0,0 +1,41
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef BOXDATAREADER_H
22 #define BOXDATAREADER_H
23
24 #include <QTextStream>
25 #include <QBoxSet>
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
29 class BoxDataReader : public QTextStream
30 {
31 public:
32 explicit BoxDataReader(QIODevice *fileHandle);
33 QBoxSet* readBox();
34
35 protected:
36 qreal findMedian(int begin, int end);
37
38 private:
39 };
40
41 #endif // BOXDATAREADER_H
@@ -0,0 +1,5
1 <RCC>
2 <qresource prefix="/">
3 <file alias="stock">stock_data.txt</file>
4 </qresource>
5 </RCC>
@@ -0,0 +1,5
1 1.0 1.2 2.0 2.4 2.5 3.0 3.5 3.7 4.0 4.7 5.0
2 4.6 4.7 5.2 7.3 8.4 8.8 9.1 8.3 7.4 6.4 5.3
3 3.5 7.2 5.4 6.3 7.4 8.3 8.8 3.3 5.4 5.7 3.9
4 #0 1 2 3 4 5 6 7 8 9 10
5 # * * *
@@ -3,4 +3,11
3 3 }
4 4
5 5 TARGET = boxplotchart
6 SOURCES += main.cpp
6 SOURCES += main.cpp \
7 boxdatareader.cpp
8
9 RESOURCES += \
10 boxplotdata.qrc
11
12 HEADERS += \
13 boxdatareader.h
@@ -27,104 +27,79
27 27 #include <QBarCategoryAxis>
28 28 #include <QLineSeries>
29 29 #include <QScatterSeries>
30 #include <QTextStream>
30 31
31 32 #include <QBrush>
32 33 #include <QColor>
33 34
35 #include "boxdatareader.h"
36
37 #include <QDebug>
38
34 39 QTCOMMERCIALCHART_USE_NAMESPACE
35 40
36 41 int main(int argc, char *argv[])
37 42 {
38 43 QApplication a(argc, argv);
39
40 //![1]
41 QBoxSet *set0 = new QBoxSet();
42 QBoxSet *set1 = new QBoxSet();
43 QBoxSet *set2 = new QBoxSet();
44 QBoxSet *set3 = new QBoxSet();
45 QBoxSet *set4 = new QBoxSet();
46 QBoxSet *set5 = new QBoxSet();
47 QBoxSet *set6 = new QBoxSet();
48 QBoxSet *set7 = new QBoxSet();
49 QBoxSet *set8 = new QBoxSet();
50 QBoxSet *set9 = new QBoxSet();
51 QBoxSet *set10 = new QBoxSet();
52 QBoxSet *set11 = new QBoxSet();
53
54 // low bot med top upp
55 *set0 << 3 << 4 << 4.4 << 6 << 7;
56 *set1 << 5 << 6 << 7.5 << 8 << 12;
57 *set2 << 3 << 5 << 5.7 << 8 << 9;
58 *set3 << 5 << 6 << 6.8 << 7 << 8;
59 *set4 << 4 << 5 << 5.2 << 6 << 7;
60 *set5 << 4 << 7 << 8.2 << 9 << 10;
61 *set6 << 2.5 << 5 << 5.4 << 6 << 7;
62 *set7 << 5 << 6.3 << 7.5 << 8 << 12;
63 *set8 << 2.6 << 5.1 << 5.7 << 8 << 9;
64 *set9 << 3.1 << 5.8 << 6.8 << 7 << 8;
65 *set10 << 4.2 << 5 << 5.8 << 6 << 7;
66 *set11 << 4.7 << 7 << 8.2 << 9 << 10;
67
68 44 //![1]
69 45
70 46 //![2]
71 47 QBoxPlotSeries *series = new QBoxPlotSeries();
72 series->append(set0);
73 series->append(set1);
74 series->append(set2);
75 series->append(set3);
76 series->append(set4);
77 series->append(set5);
78 series->append(set6);
79 series->append(set7);
80 series->append(set8);
81 series->append(set9);
82 series->append(set10);
83 series->append(set11);
84 48 series->setName("Box & Whiskers");
85 49 //![2]
86 50
87 QLineSeries *lineSeries = new QLineSeries();
88 lineSeries->append(0, 4.4);
89 lineSeries->append(1, 7.5);
90 lineSeries->append(2, 5.7);
91 lineSeries->append(3, 6.8);
92 lineSeries->append(4, 5.2);
93 lineSeries->append(5, 8.2);
94 lineSeries->append(6, 5.4);
95 lineSeries->append(7, 7.5);
96 lineSeries->append(8, 5.7);
97 lineSeries->append(9, 6.8);
98 lineSeries->append(10, 5.2);
99 lineSeries->append(11, 8.2);
100 lineSeries->setName("Medians");
101
102 QScatterSeries *scatterSeries = new QScatterSeries();
103 scatterSeries->setName("Outliers");
104 scatterSeries->setMarkerShape(QScatterSeries::MarkerShapeCircle);
105 scatterSeries->setMarkerSize(7.0);
106 scatterSeries->setBrush(QBrush(Qt::white));
107 scatterSeries->setPen(QPen(Qt::black, 1.0));
108 scatterSeries->append(1, 4);
109 scatterSeries->append(1, 4.1);
110 scatterSeries->append(1, 4.2);
111 scatterSeries->append(1, 4.3);
112 *scatterSeries << QPointF(3, 8.5) << QPointF(3, 8.6);
51 QFile stockData(":stock");
52 if (!stockData.open(QIODevice::ReadOnly | QIODevice::Text)) {
53 return 1;
54 }
55
56 BoxDataReader dataReader(&stockData);
57 while (!dataReader.atEnd()) {
58 QBoxSet *set = dataReader.readBox();
59 if (set)
60 series->append(set);
61 }
62
63 // QTextStream stream(&stockData);
64 // while (!stream.atEnd()) {
65 // QString line = stream.readLine();
66 // if (line.startsWith("#"))
67 // continue;
68 // QStringList strList = line.split(" ", QString::SkipEmptyParts);
69 // QList<qreal> realList;
70 // foreach (QString str, strList) {
71 // realList.append(str.toDouble());
72 // }
73
74 // qSort(realList.begin(), realList.end());
75
76 // int count = realList.count();
77 // int median = (int)(count / 2);
78 // int lowerQ = (int)(median / 2);
79 // int upperQ = median + lowerQ + 1;
80 // QBoxSet *set = new QBoxSet();
81
82 // set->setLowerExtreme(realList.at(0));
83 // set->setLowerQuartile(realList.at(lowerQ));
84 // set->setMedian(realList.at(median));
85 // set->setUpperQuartile(realList.at(upperQ));
86 // set->setUpperExtreme(realList.last());
87
88 // series->append(set);
89 // }
113 90
114 91 //![3]
115 92 QChart *chart = new QChart();
116 93 chart->addSeries(series);
117 chart->addSeries(lineSeries);
118 chart->addSeries(scatterSeries);
119 94 chart->setTitle("Simple boxplotchart example");
120 95 chart->setAnimationOptions(QChart::SeriesAnimations);
121 96 //![3]
122 97
123 98 //![4]
124 QStringList categories;
125 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
99 //QStringList categories;
100 //categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
126 101 QBarCategoryAxis *axis = new QBarCategoryAxis();
127 axis->append(categories);
102 //axis->append(categories);
128 103 chart->createDefaultAxes();
129 104 chart->setAxisX(axis, series);
130 105 //![4]
@@ -22,6 +22,8
22 22 #include "qboxset_p.h"
23 23 #include "charthelpers_p.h"
24 24
25 #include <QDebug> //TODO: remove on release
26
25 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 28
27 29 /*!
@@ -57,10 +59,10 QBoxSet::~QBoxSet()
57 59 */
58 60 void QBoxSet::append(const qreal value)
59 61 {
60 int index = d_ptr->m_values.count();
62 //int index = d_ptr->m_values.count();
61 63 d_ptr->append(value);
62 64
63 emit valuesAdded(index, 1);
65 emit valuesAdded(d_ptr->m_valuesCount, 1);
64 66 }
65 67
66 68 /*!
@@ -70,9 +72,9 void QBoxSet::append(const qreal value)
70 72 */
71 73 void QBoxSet::append(const QList<qreal> &values)
72 74 {
73 int index = d_ptr->m_values.count();
75 //int index = d_ptr->m_values.count();
74 76 d_ptr->append(values);
75 emit valuesAdded(index, values.count());
77 emit valuesAdded(d_ptr->m_valuesCount, values.count());
76 78 }
77 79
78 80 /*!
@@ -81,6 +83,7 void QBoxSet::append(const QList<qreal> &values)
81 83 void QBoxSet::setLowerExtreme(const qreal value)
82 84 {
83 85 d_ptr->replace(QBoxSetPrivate::PosLowerExtreme, value);
86 emit d_ptr->restructuredBox();
84 87 emit valueChanged(QBoxSetPrivate::PosLowerExtreme);
85 88 }
86 89
@@ -89,7 +92,7 void QBoxSet::setLowerExtreme(const qreal value)
89 92 */
90 93 qreal QBoxSet::lowerExtreme()
91 94 {
92 return d_ptr->m_values.at(QBoxSetPrivate::PosLowerExtreme);
95 return d_ptr->m_values[QBoxSetPrivate::PosLowerExtreme];
93 96 }
94 97
95 98 /*!
@@ -98,6 +101,7 qreal QBoxSet::lowerExtreme()
98 101 void QBoxSet::setLowerQuartile(const qreal value)
99 102 {
100 103 d_ptr->replace(QBoxSetPrivate::PosLowerQuartile, value);
104 emit d_ptr->restructuredBox();
101 105 emit valueChanged(QBoxSetPrivate::PosLowerQuartile);
102 106 }
103 107
@@ -106,7 +110,7 void QBoxSet::setLowerQuartile(const qreal value)
106 110 */
107 111 qreal QBoxSet::lowerQuartile()
108 112 {
109 return d_ptr->m_values.at(QBoxSetPrivate::PosLowerQuartile);
113 return d_ptr->m_values[QBoxSetPrivate::PosLowerQuartile];
110 114 }
111 115
112 116 /*!
@@ -115,6 +119,7 qreal QBoxSet::lowerQuartile()
115 119 void QBoxSet::setMedian(const qreal value)
116 120 {
117 121 d_ptr->replace(QBoxSetPrivate::PosMedian, value);
122 emit d_ptr->restructuredBox();
118 123 emit valueChanged(QBoxSetPrivate::PosMedian);
119 124 }
120 125
@@ -123,7 +128,7 void QBoxSet::setMedian(const qreal value)
123 128 */
124 129 qreal QBoxSet::median()
125 130 {
126 return d_ptr->m_values.at(QBoxSetPrivate::PosMedian);
131 return d_ptr->m_values[QBoxSetPrivate::PosMedian];
127 132 }
128 133
129 134 /*!
@@ -132,6 +137,7 qreal QBoxSet::median()
132 137 void QBoxSet::setUpperQuartile(const qreal value)
133 138 {
134 139 d_ptr->replace(QBoxSetPrivate::PosUpperQuartile, value);
140 emit d_ptr->restructuredBox();
135 141 emit valueChanged(QBoxSetPrivate::PosUpperQuartile);
136 142 }
137 143
@@ -140,7 +146,7 void QBoxSet::setUpperQuartile(const qreal value)
140 146 */
141 147 qreal QBoxSet::upperQuartile()
142 148 {
143 return d_ptr->m_values.at(QBoxSetPrivate::PosUpperQuartile);
149 return d_ptr->m_values[QBoxSetPrivate::PosUpperQuartile];
144 150 }
145 151
146 152 /*!
@@ -148,8 +154,9 qreal QBoxSet::upperQuartile()
148 154 */
149 155 void QBoxSet::setUpperExtreme(const qreal value)
150 156 {
151 d_ptr->replace(QBoxSetPrivate::PosUpperQuartile, value);
152 emit valueChanged(QBoxSetPrivate::PosUpperQuartile);
157 d_ptr->replace(QBoxSetPrivate::PosUpperExtreme, value);
158 emit d_ptr->restructuredBox();
159 emit valueChanged(QBoxSetPrivate::PosUpperExtreme);
153 160 }
154 161
155 162 /*!
@@ -157,7 +164,7 void QBoxSet::setUpperExtreme(const qreal value)
157 164 */
158 165 qreal QBoxSet::upperExtreme()
159 166 {
160 return d_ptr->m_values.at(QBoxSetPrivate::PosUpperExtreme);
167 return d_ptr->m_values[QBoxSetPrivate::PosUpperExtreme];
161 168 }
162 169
163 170 /*!
@@ -173,7 +180,6 QBoxSet &QBoxSet::operator << (const qreal &value)
173 180 /*!
174 181 Inserts new \a value on the \a index position.
175 182 The value that is currently at this postion is moved to postion index + 1
176 \sa remove()
177 183 */
178 184 void QBoxSet::insert(const int index, const qreal value)
179 185 {
@@ -182,23 +188,11 void QBoxSet::insert(const int index, const qreal value)
182 188 }
183 189
184 190 /*!
185 Removes \a count number of values from the set starting at \a index.
186 \sa insert()
187 */
188 void QBoxSet::remove(const int index, const int count)
189 {
190 int removedCount = d_ptr->remove(index, count);
191 if (removedCount > 0)
192 emit valuesRemoved(index, removedCount);
193 return;
194 }
195
196 /*!
197 191 Sets a new value \a value to set, indexed by \a index
198 192 */
199 193 void QBoxSet::replace(const int index, const qreal value)
200 194 {
201 if (index >= 0 && index < d_ptr->m_values.count()) {
195 if (index >= 0 && index < 5) {
202 196 d_ptr->replace(index, value);
203 197 emit valueChanged(index);
204 198 }
@@ -211,9 +205,9 void QBoxSet::replace(const int index, const qreal value)
211 205 */
212 206 qreal QBoxSet::at(const int index) const
213 207 {
214 if (index < 0 || index >= d_ptr->m_values.count())
208 if (index < 0 || index >= 5)
215 209 return 0;
216 return d_ptr->m_values.at(index);
210 return d_ptr->m_values[index];
217 211 }
218 212
219 213 /*!
@@ -230,7 +224,7 qreal QBoxSet::operator [](const int index) const
230 224 */
231 225 int QBoxSet::count() const
232 226 {
233 return d_ptr->m_values.count();
227 return d_ptr->m_valuesCount;
234 228 }
235 229
236 230 /*!
@@ -326,9 +320,12 void QBoxSet::setBorderColor(QColor color)
326 320
327 321 QBoxSetPrivate::QBoxSetPrivate(QBoxSet *parent) : QObject(parent),
328 322 q_ptr(parent),
323 m_valuesCount(5),
324 m_appendCount(0),
329 325 m_pen(QPen(Qt::NoPen)),
330 326 m_brush(QBrush(Qt::NoBrush))
331 327 {
328 m_values = new qreal[m_valuesCount];
332 329 }
333 330
334 331 QBoxSetPrivate::~QBoxSetPrivate()
@@ -337,8 +334,8 QBoxSetPrivate::~QBoxSetPrivate()
337 334
338 335 void QBoxSetPrivate::append(qreal value)
339 336 {
340 if (isValidValue(value)) {
341 m_values.append(value);
337 if (isValidValue(value) && m_appendCount < m_valuesCount) {
338 m_values[m_appendCount++] = value;
342 339 emit restructuredBox();
343 340 }
344 341 }
@@ -346,8 +343,8 void QBoxSetPrivate::append(qreal value)
346 343 void QBoxSetPrivate::append(QList<qreal> values)
347 344 {
348 345 for (int i = 0; i < values.count(); i++) {
349 if (isValidValue(values.at(i)))
350 m_values.append(values.at(i));
346 if (isValidValue(values.at(i)) && m_appendCount < m_valuesCount)
347 m_values[m_appendCount++] = values.at(i);
351 348 }
352 349 emit restructuredBox();
353 350 }
@@ -355,40 +352,24 void QBoxSetPrivate::append(QList<qreal> values)
355 352 void QBoxSetPrivate::insert(const int index, const qreal value)
356 353 {
357 354 if (isValidValue(value)) {
358 m_values.insert(index, value);
355 for (int i = 4; i > index; i--)
356 m_values[i] = m_values[i - 1];
357 m_values[index] = value;
359 358 emit restructuredBox();
360 359 }
361 360 }
362 361
363 int QBoxSetPrivate::remove(const int index, const int count)
364 {
365 int removeCount = count;
366
367 if ((index < 0) || (m_values.count() == 0))
368 return 0; // Invalid index or not values in list, remove nothing.
369 else if ((index + count) > m_values.count())
370 removeCount = m_values.count() - index; // Trying to remove more items than list has. Limit amount to be removed.
371
372 int c = 0;
373 while (c < removeCount) {
374 m_values.removeAt(index);
375 c++;
376 }
377 emit restructuredBox();
378 return removeCount;
379 }
380
381 362 void QBoxSetPrivate::replace(const int index, const qreal value)
382 363 {
383 m_values.replace(index, value);
364 m_values[index] = value;
384 365 emit updatedLayout();
385 366 }
386 367
387 368 qreal QBoxSetPrivate::value(const int index)
388 369 {
389 if (index < 0 || index >= m_values.count())
370 if (index < 0 || index >= m_valuesCount)
390 371 return 0;
391 return m_values.at(index);
372 return m_values[index];
392 373 }
393 374
394 375 #include "moc_qboxset.cpp"
@@ -73,7 +73,10 Q_SIGNALS:
73 73 public:
74 74 QBoxSet * const q_ptr;
75 75 //QString m_label;
76 QList<qreal> m_values;
76 //QList<qreal> m_values;
77 const int m_valuesCount;
78 qreal *m_values;
79 int m_appendCount;
77 80 QPen m_pen;
78 81 QBrush m_brush;
79 82 QBrush m_labelBrush;
General Comments 0
You need to be logged in to leave comments. Login now