diff --git a/examples/boxplotchart/boxdatareader.cpp b/examples/boxplotchart/boxdatareader.cpp index b76b2a6..ec683f7 100644 --- a/examples/boxplotchart/boxdatareader.cpp +++ b/examples/boxplotchart/boxdatareader.cpp @@ -22,8 +22,8 @@ #include -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 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 5860ea1..bdaae1b 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 sortedList; }; #endif // BOXDATAREADER_H diff --git a/examples/boxplotchart/main.cpp b/examples/boxplotchart/main.cpp index be922f4..115e10e 100644 --- a/examples/boxplotchart/main.cpp +++ b/examples/boxplotchart/main.cpp @@ -25,12 +25,6 @@ #include #include #include -#include -#include -#include - -#include -#include #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 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 d44abc5..a8e77e6 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