diff options
author | Mika Salmela <mika.salmela@digia.com> | 2013-05-15 11:38:34 +0300 |
---|---|---|
committer | Mika Salmela <mika.salmela@digia.com> | 2013-05-15 11:39:14 +0300 |
commit | 985019be9a8fda369e7ca5b4bf163bbd751d92a8 (patch) | |
tree | c6f48c47be1435e88ee47b19918eeda3de8e1fd1 | |
parent | 25def4b65f99e8a2c4099725e054861db5b836bb (diff) |
Datareader fixes
Change-Id: I9d5b82aa93d0e5b1977706cc6a06e4d966abedcb
Reviewed-by: Mika Salmela <mika.salmela@digia.com>
-rw-r--r-- | examples/boxplotchart/boxdatareader.cpp | 35 | ||||
-rw-r--r-- | examples/boxplotchart/boxdatareader.h | 3 | ||||
-rw-r--r-- | examples/boxplotchart/main.cpp | 34 | ||||
-rw-r--r-- | examples/boxplotchart/stock_data.txt | 3 |
4 files changed, 29 insertions, 46 deletions
diff --git a/examples/boxplotchart/boxdatareader.cpp b/examples/boxplotchart/boxdatareader.cpp index b76b2a6e..ec683f7d 100644 --- a/examples/boxplotchart/boxdatareader.cpp +++ b/examples/boxplotchart/boxdatareader.cpp @@ -22,8 +22,8 @@ #include <QDebug> -BoxDataReader::BoxDataReader(QIODevice *fileHandle) : - QTextStream(fileHandle) +BoxDataReader::BoxDataReader(QIODevice *device) : + QTextStream(device) { } @@ -34,26 +34,39 @@ QBoxSet* BoxDataReader::readBox() return 0; QStringList strList = line.split(" ", QString::SkipEmptyParts); - QList<qreal> sortedList; + sortedList.clear(); foreach (QString str, strList) { sortedList.append(str.toDouble()); } qSort(sortedList.begin(), sortedList.end()); -qDebug() << "sortedList = " << sortedList; int count = sortedList.count(); - int median = (int)(count / 2); - int lowerQ = (int)(median / 2); - int upperQ = median + lowerQ + 1; QBoxSet *box = new QBoxSet(); - box->setLowerExtreme(sortedList.at(0)); - box->setLowerQuartile(sortedList.at(lowerQ)); - box->setMedian(sortedList.at(median)); - box->setUpperQuartile(sortedList.at(upperQ)); + box->setLowerExtreme(sortedList.first()); box->setUpperExtreme(sortedList.last()); + box->setMedian(findMedian(0, count)); + box->setLowerQuartile(findMedian(0, count / 2)); + if (count % 2) + box->setUpperQuartile(findMedian(count / 2 + 1, count)); + else // even amount of numbers + box->setUpperQuartile(findMedian(count / 2, count)); + + qDebug() << "Box = " << box->lowerExtreme() << ", " << box->lowerQuartile() << ", " << + box->median() << ", " << box->upperQuartile() << ", " << box->upperExtreme(); return box; } +qreal BoxDataReader::findMedian(int begin, int end) +{ + int count = end - begin; + if (count % 2 ) { + return sortedList.at((int) (count/2) + begin); + } else { + qreal right = sortedList.at(count / 2 + begin); + qreal left = sortedList.at(count / 2 - 1 + begin); + return (right + left) / 2.0; + } +} diff --git a/examples/boxplotchart/boxdatareader.h b/examples/boxplotchart/boxdatareader.h index 5860ea1e..bdaae1bd 100644 --- a/examples/boxplotchart/boxdatareader.h +++ b/examples/boxplotchart/boxdatareader.h @@ -29,13 +29,14 @@ QTCOMMERCIALCHART_USE_NAMESPACE class BoxDataReader : public QTextStream { public: - explicit BoxDataReader(QIODevice *fileHandle); + explicit BoxDataReader(QIODevice *device); QBoxSet* readBox(); protected: qreal findMedian(int begin, int end); private: + QList<qreal> sortedList; }; #endif // BOXDATAREADER_H diff --git a/examples/boxplotchart/main.cpp b/examples/boxplotchart/main.cpp index be922f4e..115e10e7 100644 --- a/examples/boxplotchart/main.cpp +++ b/examples/boxplotchart/main.cpp @@ -25,12 +25,6 @@ #include <QBoxSet> #include <QLegend> #include <QBarCategoryAxis> -#include <QLineSeries> -#include <QScatterSeries> -#include <QTextStream> - -#include <QBrush> -#include <QColor> #include "boxdatareader.h" @@ -60,34 +54,6 @@ int main(int argc, char *argv[]) series->append(set); } -// QTextStream stream(&stockData); -// while (!stream.atEnd()) { -// QString line = stream.readLine(); -// if (line.startsWith("#")) -// continue; -// QStringList strList = line.split(" ", QString::SkipEmptyParts); -// QList<qreal> realList; -// foreach (QString str, strList) { -// realList.append(str.toDouble()); -// } - -// qSort(realList.begin(), realList.end()); - -// int count = realList.count(); -// int median = (int)(count / 2); -// int lowerQ = (int)(median / 2); -// int upperQ = median + lowerQ + 1; -// QBoxSet *set = new QBoxSet(); - -// set->setLowerExtreme(realList.at(0)); -// set->setLowerQuartile(realList.at(lowerQ)); -// set->setMedian(realList.at(median)); -// set->setUpperQuartile(realList.at(upperQ)); -// set->setUpperExtreme(realList.last()); - -// series->append(set); -// } - //![3] QChart *chart = new QChart(); chart->addSeries(series); diff --git a/examples/boxplotchart/stock_data.txt b/examples/boxplotchart/stock_data.txt index d44abc59..a8e77e6d 100644 --- a/examples/boxplotchart/stock_data.txt +++ b/examples/boxplotchart/stock_data.txt @@ -1,3 +1,6 @@ +1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 +#0 1 2 3 4 5 6 7 8 9 +4.3 5.1 3.9 4.5 4.4 4.9 5.0 4.7 4.1 4.6 4.4 4.3 4.8 4.4 4.2 4.5 4.4 1.0 1.2 2.0 2.4 2.5 3.0 3.5 3.7 4.0 4.7 5.0 4.6 4.7 5.2 7.3 8.4 8.8 9.1 8.3 7.4 6.4 5.3 3.5 7.2 5.4 6.3 7.4 8.3 8.8 3.3 5.4 5.7 3.9 |