@@ -1,73 +1,68 | |||||
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> |
|
|||
24 |
|
||||
25 | BoxDataReader::BoxDataReader(QIODevice *device) : |
|
23 | BoxDataReader::BoxDataReader(QIODevice *device) : | |
26 | QTextStream(device) |
|
24 | QTextStream(device) | |
27 | { |
|
25 | { | |
28 | } |
|
26 | } | |
29 |
|
27 | |||
30 | QBoxSet* BoxDataReader::readBox() |
|
28 | QBoxSet* BoxDataReader::readBox() | |
31 | { |
|
29 | { | |
32 | QString line = readLine(); |
|
30 | QString line = readLine(); | |
33 | if (line.startsWith("#")) |
|
31 | if (line.startsWith("#")) | |
34 | return 0; |
|
32 | return 0; | |
35 |
|
33 | |||
36 | QStringList strList = line.split(" ", QString::SkipEmptyParts); |
|
34 | QStringList strList = line.split(" ", QString::SkipEmptyParts); | |
37 | sortedList.clear(); |
|
35 | sortedList.clear(); | |
38 |
|
36 | |||
39 | for (int i = 1; i < strList.count(); i++) { |
|
37 | for (int i = 1; i < strList.count(); i++) { | |
40 | sortedList.append(strList.at(i).toDouble()); |
|
38 | sortedList.append(strList.at(i).toDouble()); | |
41 | } |
|
39 | } | |
42 |
|
40 | |||
43 | qSort(sortedList.begin(), sortedList.end()); |
|
41 | qSort(sortedList.begin(), sortedList.end()); | |
44 |
|
42 | |||
45 | int count = sortedList.count(); |
|
43 | int count = sortedList.count(); | |
46 |
|
44 | |||
47 | QBoxSet *box = new QBoxSet(strList.first()); |
|
45 | QBoxSet *box = new QBoxSet(strList.first()); | |
48 | box->setLowerExtreme(sortedList.first()); |
|
46 | box->setLowerExtreme(sortedList.first()); | |
49 | box->setUpperExtreme(sortedList.last()); |
|
47 | box->setUpperExtreme(sortedList.last()); | |
50 | box->setMedian(findMedian(0, count)); |
|
48 | box->setMedian(findMedian(0, count)); | |
51 | box->setLowerQuartile(findMedian(0, count / 2)); |
|
49 | box->setLowerQuartile(findMedian(0, count / 2)); | |
52 | if (count % 2) |
|
50 | if (count % 2) | |
53 | box->setUpperQuartile(findMedian(count / 2 + 1, count)); |
|
51 | box->setUpperQuartile(findMedian(count / 2 + 1, count)); | |
54 | else // even amount of numbers |
|
52 | else // even amount of numbers | |
55 | box->setUpperQuartile(findMedian(count / 2, count)); |
|
53 | box->setUpperQuartile(findMedian(count / 2, count)); | |
56 |
|
54 | |||
57 | qDebug() << "Box = " << box->lowerExtreme() << ", " << box->lowerQuartile() << ", " << |
|
|||
58 | box->median() << ", " << box->upperQuartile() << ", " << box->upperExtreme(); |
|
|||
59 |
|
||||
60 | return box; |
|
55 | return box; | |
61 | } |
|
56 | } | |
62 |
|
57 | |||
63 | qreal BoxDataReader::findMedian(int begin, int end) |
|
58 | qreal BoxDataReader::findMedian(int begin, int end) | |
64 | { |
|
59 | { | |
65 | int count = end - begin; |
|
60 | int count = end - begin; | |
66 | if (count % 2 ) { |
|
61 | if (count % 2 ) { | |
67 | return sortedList.at((int) (count/2) + begin); |
|
62 | return sortedList.at((int) (count/2) + begin); | |
68 | } else { |
|
63 | } else { | |
69 | qreal right = sortedList.at(count / 2 + begin); |
|
64 | qreal right = sortedList.at(count / 2 + begin); | |
70 | qreal left = sortedList.at(count / 2 - 1 + begin); |
|
65 | qreal left = sortedList.at(count / 2 - 1 + begin); | |
71 | return (right + left) / 2.0; |
|
66 | return (right + left) / 2.0; | |
72 | } |
|
67 | } | |
73 | } |
|
68 | } |
@@ -1,92 +1,87 | |||||
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 |
|
28 | |||
29 | #include "boxdatareader.h" |
|
29 | #include "boxdatareader.h" | |
30 |
|
30 | |||
31 | #include <QDebug> |
|
|||
32 |
|
||||
33 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
31 | QTCOMMERCIALCHART_USE_NAMESPACE | |
34 |
|
32 | |||
35 | int main(int argc, char *argv[]) |
|
33 | int main(int argc, char *argv[]) | |
36 | { |
|
34 | { | |
37 | QApplication a(argc, argv); |
|
35 | QApplication a(argc, argv); | |
38 | //![1] |
|
36 | //![1] | |
39 |
|
37 | |||
40 | //![2] |
|
38 | //![2] | |
41 | QBoxPlotSeries *series = new QBoxPlotSeries(); |
|
39 | QBoxPlotSeries *series = new QBoxPlotSeries(); | |
42 | series->setName("Box & Whiskers"); |
|
40 | series->setName("Box & Whiskers"); | |
43 | //![2] |
|
41 | //![2] | |
44 |
|
42 | |||
45 | QFile stockData(":stock"); |
|
43 | QFile stockData(":stock"); | |
46 | if (!stockData.open(QIODevice::ReadOnly | QIODevice::Text)) { |
|
44 | if (!stockData.open(QIODevice::ReadOnly | QIODevice::Text)) { | |
47 | return 1; |
|
45 | return 1; | |
48 | } |
|
46 | } | |
49 |
|
47 | |||
50 | BoxDataReader dataReader(&stockData); |
|
48 | BoxDataReader dataReader(&stockData); | |
51 | while (!dataReader.atEnd()) { |
|
49 | while (!dataReader.atEnd()) { | |
52 | QBoxSet *set = dataReader.readBox(); |
|
50 | QBoxSet *set = dataReader.readBox(); | |
53 | if (set) |
|
51 | if (set) | |
54 | series->append(set); |
|
52 | series->append(set); | |
55 | } |
|
53 | } | |
56 |
|
54 | |||
57 | //![3] |
|
55 | //![3] | |
58 | QChart *chart = new QChart(); |
|
56 | QChart *chart = new QChart(); | |
59 | chart->addSeries(series); |
|
57 | chart->addSeries(series); | |
60 | chart->setTitle("Simple boxplotchart example"); |
|
58 | chart->setTitle("Simple boxplotchart example"); | |
61 | chart->setAnimationOptions(QChart::SeriesAnimations); |
|
59 | chart->setAnimationOptions(QChart::SeriesAnimations); | |
62 | //![3] |
|
60 | //![3] | |
63 |
|
61 | |||
64 | //![4] |
|
62 | //![4] | |
65 | //QStringList categories; |
|
|||
66 | //categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
|||
67 | QBarCategoryAxis *axis = new QBarCategoryAxis(); |
|
63 | QBarCategoryAxis *axis = new QBarCategoryAxis(); | |
68 | //axis->append(categories); |
|
|||
69 | chart->createDefaultAxes(); |
|
64 | chart->createDefaultAxes(); | |
70 | chart->setAxisX(axis, series); |
|
65 | chart->setAxisX(axis, series); | |
71 | //![4] |
|
66 | //![4] | |
72 |
|
67 | |||
73 | //![5] |
|
68 | //![5] | |
74 | chart->legend()->setVisible(true); |
|
69 | chart->legend()->setVisible(true); | |
75 | chart->legend()->setAlignment(Qt::AlignBottom); |
|
70 | chart->legend()->setAlignment(Qt::AlignBottom); | |
76 | //![5] |
|
71 | //![5] | |
77 |
|
72 | |||
78 | //![6] |
|
73 | //![6] | |
79 | QChartView *chartView = new QChartView(chart); |
|
74 | QChartView *chartView = new QChartView(chart); | |
80 | chartView->setRenderHint(QPainter::Antialiasing); |
|
75 | chartView->setRenderHint(QPainter::Antialiasing); | |
81 | //![6] |
|
76 | //![6] | |
82 |
|
77 | |||
83 | //![7] |
|
78 | //![7] | |
84 | QMainWindow window; |
|
79 | QMainWindow window; | |
85 | window.setCentralWidget(chartView); |
|
80 | window.setCentralWidget(chartView); | |
86 | window.resize(600, 400); |
|
81 | window.resize(600, 400); | |
87 | window.show(); |
|
82 | window.show(); | |
88 | //![7] |
|
83 | //![7] | |
89 |
|
84 | |||
90 | return a.exec(); |
|
85 | return a.exec(); | |
91 | } |
|
86 | } | |
92 |
|
87 |
General Comments 0
You need to be logged in to leave comments.
Login now