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