| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 or (at your option) any later version |
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include "boxdatareader.h" |
| 31 | |
| 32 | #include <algorithm> |
| 33 | |
| 34 | BoxDataReader::BoxDataReader(QIODevice *device) : |
| 35 | QTextStream(device) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void BoxDataReader::readFile(QIODevice *device) |
| 40 | { |
| 41 | QTextStream::setDevice(device); |
| 42 | } |
| 43 | |
| 44 | QBoxSet *BoxDataReader::readBox() |
| 45 | { |
| 46 | //! [1] |
| 47 | QString line = readLine(); |
| 48 | if (line.startsWith(s: "#" )) |
| 49 | return 0; |
| 50 | //! [1] |
| 51 | |
| 52 | //! [2] |
| 53 | QStringList strList = line.split(sep: QLatin1Char(' '), behavior: Qt::SkipEmptyParts); |
| 54 | //! [2] |
| 55 | |
| 56 | //! [3] |
| 57 | sortedList.clear(); |
| 58 | for (int i = 1; i < strList.count(); i++) |
| 59 | sortedList.append(t: strList.at(i).toDouble()); |
| 60 | |
| 61 | std::sort(first: sortedList.begin(), last: sortedList.end()); |
| 62 | //! [3] |
| 63 | |
| 64 | int count = sortedList.count(); |
| 65 | |
| 66 | //! [4] |
| 67 | QBoxSet *box = new QBoxSet(strList.first()); |
| 68 | box->setValue(index: QBoxSet::LowerExtreme, value: sortedList.first()); |
| 69 | box->setValue(index: QBoxSet::UpperExtreme, value: sortedList.last()); |
| 70 | box->setValue(index: QBoxSet::Median, value: findMedian(begin: 0, end: count)); |
| 71 | box->setValue(index: QBoxSet::LowerQuartile, value: findMedian(begin: 0, end: count / 2)); |
| 72 | box->setValue(index: QBoxSet::UpperQuartile, value: findMedian(begin: count / 2 + (count % 2), end: count)); |
| 73 | //! [4] |
| 74 | |
| 75 | return box; |
| 76 | } |
| 77 | |
| 78 | qreal BoxDataReader::findMedian(int begin, int end) |
| 79 | { |
| 80 | //! [5] |
| 81 | int count = end - begin; |
| 82 | if (count % 2) { |
| 83 | return sortedList.at(i: count / 2 + begin); |
| 84 | } else { |
| 85 | qreal right = sortedList.at(i: count / 2 + begin); |
| 86 | qreal left = sortedList.at(i: count / 2 - 1 + begin); |
| 87 | return (right + left) / 2.0; |
| 88 | } |
| 89 | //! [5] |
| 90 | } |
| 91 | |