##// END OF EJS Templates
Datareader fixes...
Mika Salmela -
r2512:985019be9a8f
parent child
Show More
@@ -1,59 +1,72
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "boxdatareader.h"
22 22
23 23 #include <QDebug>
24 24
25 BoxDataReader::BoxDataReader(QIODevice *fileHandle) :
26 QTextStream(fileHandle)
25 BoxDataReader::BoxDataReader(QIODevice *device) :
26 QTextStream(device)
27 27 {
28 28 }
29 29
30 30 QBoxSet* BoxDataReader::readBox()
31 31 {
32 32 QString line = readLine();
33 33 if (line.startsWith("#"))
34 34 return 0;
35 35
36 36 QStringList strList = line.split(" ", QString::SkipEmptyParts);
37 QList<qreal> sortedList;
37 sortedList.clear();
38 38 foreach (QString str, strList) {
39 39 sortedList.append(str.toDouble());
40 40 }
41 41
42 42 qSort(sortedList.begin(), sortedList.end());
43 qDebug() << "sortedList = " << sortedList;
44 43
45 44 int count = sortedList.count();
46 int median = (int)(count / 2);
47 int lowerQ = (int)(median / 2);
48 int upperQ = median + lowerQ + 1;
49 45
50 46 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));
47 box->setLowerExtreme(sortedList.first());
55 48 box->setUpperExtreme(sortedList.last());
49 box->setMedian(findMedian(0, count));
50 box->setLowerQuartile(findMedian(0, count / 2));
51 if (count % 2)
52 box->setUpperQuartile(findMedian(count / 2 + 1, count));
53 else // even amount of numbers
54 box->setUpperQuartile(findMedian(count / 2, count));
55
56 qDebug() << "Box = " << box->lowerExtreme() << ", " << box->lowerQuartile() << ", " <<
57 box->median() << ", " << box->upperQuartile() << ", " << box->upperExtreme();
56 58
57 59 return box;
58 60 }
59 61
62 qreal BoxDataReader::findMedian(int begin, int end)
63 {
64 int count = end - begin;
65 if (count % 2 ) {
66 return sortedList.at((int) (count/2) + begin);
67 } else {
68 qreal right = sortedList.at(count / 2 + begin);
69 qreal left = sortedList.at(count / 2 - 1 + begin);
70 return (right + left) / 2.0;
71 }
72 }
@@ -1,41 +1,42
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef BOXDATAREADER_H
22 22 #define BOXDATAREADER_H
23 23
24 24 #include <QTextStream>
25 25 #include <QBoxSet>
26 26
27 27 QTCOMMERCIALCHART_USE_NAMESPACE
28 28
29 29 class BoxDataReader : public QTextStream
30 30 {
31 31 public:
32 explicit BoxDataReader(QIODevice *fileHandle);
32 explicit BoxDataReader(QIODevice *device);
33 33 QBoxSet* readBox();
34 34
35 35 protected:
36 36 qreal findMedian(int begin, int end);
37 37
38 38 private:
39 QList<qreal> sortedList;
39 40 };
40 41
41 42 #endif // BOXDATAREADER_H
@@ -1,126 +1,92
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QBoxPlotSeries>
25 25 #include <QBoxSet>
26 26 #include <QLegend>
27 27 #include <QBarCategoryAxis>
28 #include <QLineSeries>
29 #include <QScatterSeries>
30 #include <QTextStream>
31
32 #include <QBrush>
33 #include <QColor>
34 28
35 29 #include "boxdatareader.h"
36 30
37 31 #include <QDebug>
38 32
39 33 QTCOMMERCIALCHART_USE_NAMESPACE
40 34
41 35 int main(int argc, char *argv[])
42 36 {
43 37 QApplication a(argc, argv);
44 38 //![1]
45 39
46 40 //![2]
47 41 QBoxPlotSeries *series = new QBoxPlotSeries();
48 42 series->setName("Box & Whiskers");
49 43 //![2]
50 44
51 45 QFile stockData(":stock");
52 46 if (!stockData.open(QIODevice::ReadOnly | QIODevice::Text)) {
53 47 return 1;
54 48 }
55 49
56 50 BoxDataReader dataReader(&stockData);
57 51 while (!dataReader.atEnd()) {
58 52 QBoxSet *set = dataReader.readBox();
59 53 if (set)
60 54 series->append(set);
61 55 }
62 56
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 // }
90
91 57 //![3]
92 58 QChart *chart = new QChart();
93 59 chart->addSeries(series);
94 60 chart->setTitle("Simple boxplotchart example");
95 61 chart->setAnimationOptions(QChart::SeriesAnimations);
96 62 //![3]
97 63
98 64 //![4]
99 65 //QStringList categories;
100 66 //categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
101 67 QBarCategoryAxis *axis = new QBarCategoryAxis();
102 68 //axis->append(categories);
103 69 chart->createDefaultAxes();
104 70 chart->setAxisX(axis, series);
105 71 //![4]
106 72
107 73 //![5]
108 74 chart->legend()->setVisible(true);
109 75 chart->legend()->setAlignment(Qt::AlignBottom);
110 76 //![5]
111 77
112 78 //![6]
113 79 QChartView *chartView = new QChartView(chart);
114 80 chartView->setRenderHint(QPainter::Antialiasing);
115 81 //![6]
116 82
117 83 //![7]
118 84 QMainWindow window;
119 85 window.setCentralWidget(chartView);
120 86 window.resize(600, 400);
121 87 window.show();
122 88 //![7]
123 89
124 90 return a.exec();
125 91 }
126 92
@@ -1,5 +1,8
1 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
2 #0 1 2 3 4 5 6 7 8 9
3 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 4 1.0 1.2 2.0 2.4 2.5 3.0 3.5 3.7 4.0 4.7 5.0
2 5 4.6 4.7 5.2 7.3 8.4 8.8 9.1 8.3 7.4 6.4 5.3
3 6 3.5 7.2 5.4 6.3 7.4 8.3 8.8 3.3 5.4 5.7 3.9
4 7 #0 1 2 3 4 5 6 7 8 9 10
5 8 # * * *
General Comments 0
You need to be logged in to leave comments. Login now