boxdatareader.cpp
79 lines
| 2.2 KiB
| text/x-c
|
CppLexer
Mika Salmela
|
r2548 | /**************************************************************************** | ||
** | ||||
Titta Heikkala
|
r2688 | ** Copyright (C) 2014 Digia Plc | ||
Mika Salmela
|
r2548 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
Miikka Heikkinen
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
Mika Salmela
|
r2548 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
Miikka Heikkinen
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
Mika Salmela
|
r2548 | ** Software or, alternatively, in accordance with the terms contained in | ||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "boxdatareader.h" | ||||
BoxDataReader::BoxDataReader(QIODevice *device) : | ||||
QTextStream(device) | ||||
{ | ||||
} | ||||
Mika Salmela
|
r2556 | void BoxDataReader::readFile(QIODevice *device) | ||
Mika Salmela
|
r2548 | { | ||
Mika Salmela
|
r2556 | QTextStream::setDevice(device); | ||
} | ||||
QBoxSet *BoxDataReader::readBox() | ||||
{ | ||||
//! [1] | ||||
Mika Salmela
|
r2548 | QString line = readLine(); | ||
if (line.startsWith("#")) | ||||
return 0; | ||||
Mika Salmela
|
r2556 | //! [1] | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | //! [2] | ||
Mika Salmela
|
r2548 | QStringList strList = line.split(" ", QString::SkipEmptyParts); | ||
Mika Salmela
|
r2556 | //! [2] | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | //! [3] | ||
sortedList.clear(); | ||||
Mika Salmela
|
r2548 | for (int i = 1; i < strList.count(); i++) | ||
sortedList.append(strList.at(i).toDouble()); | ||||
qSort(sortedList.begin(), sortedList.end()); | ||||
Mika Salmela
|
r2556 | //! [3] | ||
Mika Salmela
|
r2548 | |||
int count = sortedList.count(); | ||||
Mika Salmela
|
r2556 | //! [4] | ||
Mika Salmela
|
r2548 | QBoxSet *box = new QBoxSet(strList.first()); | ||
box->setValue(QBoxSet::LowerExtreme, sortedList.first()); | ||||
box->setValue(QBoxSet::UpperExtreme, sortedList.last()); | ||||
box->setValue(QBoxSet::Median, findMedian(0, count)); | ||||
box->setValue(QBoxSet::LowerQuartile, findMedian(0, count / 2)); | ||||
Mika Salmela
|
r2556 | box->setValue(QBoxSet::UpperQuartile, findMedian(count / 2 + (count % 2), count)); | ||
//! [4] | ||||
Mika Salmela
|
r2548 | |||
return box; | ||||
} | ||||
qreal BoxDataReader::findMedian(int begin, int end) | ||||
{ | ||||
Mika Salmela
|
r2556 | //! [5] | ||
Mika Salmela
|
r2548 | int count = end - begin; | ||
Mika Salmela
|
r2556 | if (count % 2) { | ||
return sortedList.at(count / 2 + begin); | ||||
Mika Salmela
|
r2548 | } else { | ||
qreal right = sortedList.at(count / 2 + begin); | ||||
qreal left = sortedList.at(count / 2 - 1 + begin); | ||||
return (right + left) / 2.0; | ||||
} | ||||
Mika Salmela
|
r2556 | //! [5] | ||
Mika Salmela
|
r2548 | } | ||