##// END OF EJS Templates
QBoxSet changed from list to table mode. New example for file reading....
Mika Salmela -
r2508:25def4b65f99
parent child
Show More
@@ -0,0 +1,59
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "boxdatareader.h"
22
23 #include <QDebug>
24
25 BoxDataReader::BoxDataReader(QIODevice *fileHandle) :
26 QTextStream(fileHandle)
27 {
28 }
29
30 QBoxSet* BoxDataReader::readBox()
31 {
32 QString line = readLine();
33 if (line.startsWith("#"))
34 return 0;
35
36 QStringList strList = line.split(" ", QString::SkipEmptyParts);
37 QList<qreal> sortedList;
38 foreach (QString str, strList) {
39 sortedList.append(str.toDouble());
40 }
41
42 qSort(sortedList.begin(), sortedList.end());
43 qDebug() << "sortedList = " << sortedList;
44
45 int count = sortedList.count();
46 int median = (int)(count / 2);
47 int lowerQ = (int)(median / 2);
48 int upperQ = median + lowerQ + 1;
49
50 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));
55 box->setUpperExtreme(sortedList.last());
56
57 return box;
58 }
59
@@ -0,0 +1,41
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef BOXDATAREADER_H
22 #define BOXDATAREADER_H
23
24 #include <QTextStream>
25 #include <QBoxSet>
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
29 class BoxDataReader : public QTextStream
30 {
31 public:
32 explicit BoxDataReader(QIODevice *fileHandle);
33 QBoxSet* readBox();
34
35 protected:
36 qreal findMedian(int begin, int end);
37
38 private:
39 };
40
41 #endif // BOXDATAREADER_H
@@ -0,0 +1,5
1 <RCC>
2 <qresource prefix="/">
3 <file alias="stock">stock_data.txt</file>
4 </qresource>
5 </RCC>
@@ -0,0 +1,5
1 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
3 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
5 # * * *
@@ -1,6 +1,13
1 !include( ../examples.pri ) {
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
2 error( "Couldn't find the examples.pri file!" )
3 }
3 }
4
4
5 TARGET = boxplotchart
5 TARGET = boxplotchart
6 SOURCES += main.cpp
6 SOURCES += main.cpp \
7 boxdatareader.cpp
8
9 RESOURCES += \
10 boxplotdata.qrc
11
12 HEADERS += \
13 boxdatareader.h
@@ -1,151 +1,126
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>
28 #include <QLineSeries>
29 #include <QScatterSeries>
29 #include <QScatterSeries>
30 #include <QTextStream>
30
31
31 #include <QBrush>
32 #include <QBrush>
32 #include <QColor>
33 #include <QColor>
33
34
35 #include "boxdatareader.h"
36
37 #include <QDebug>
38
34 QTCOMMERCIALCHART_USE_NAMESPACE
39 QTCOMMERCIALCHART_USE_NAMESPACE
35
40
36 int main(int argc, char *argv[])
41 int main(int argc, char *argv[])
37 {
42 {
38 QApplication a(argc, argv);
43 QApplication a(argc, argv);
39
40 //![1]
41 QBoxSet *set0 = new QBoxSet();
42 QBoxSet *set1 = new QBoxSet();
43 QBoxSet *set2 = new QBoxSet();
44 QBoxSet *set3 = new QBoxSet();
45 QBoxSet *set4 = new QBoxSet();
46 QBoxSet *set5 = new QBoxSet();
47 QBoxSet *set6 = new QBoxSet();
48 QBoxSet *set7 = new QBoxSet();
49 QBoxSet *set8 = new QBoxSet();
50 QBoxSet *set9 = new QBoxSet();
51 QBoxSet *set10 = new QBoxSet();
52 QBoxSet *set11 = new QBoxSet();
53
54 // low bot med top upp
55 *set0 << 3 << 4 << 4.4 << 6 << 7;
56 *set1 << 5 << 6 << 7.5 << 8 << 12;
57 *set2 << 3 << 5 << 5.7 << 8 << 9;
58 *set3 << 5 << 6 << 6.8 << 7 << 8;
59 *set4 << 4 << 5 << 5.2 << 6 << 7;
60 *set5 << 4 << 7 << 8.2 << 9 << 10;
61 *set6 << 2.5 << 5 << 5.4 << 6 << 7;
62 *set7 << 5 << 6.3 << 7.5 << 8 << 12;
63 *set8 << 2.6 << 5.1 << 5.7 << 8 << 9;
64 *set9 << 3.1 << 5.8 << 6.8 << 7 << 8;
65 *set10 << 4.2 << 5 << 5.8 << 6 << 7;
66 *set11 << 4.7 << 7 << 8.2 << 9 << 10;
67
68 //![1]
44 //![1]
69
45
70 //![2]
46 //![2]
71 QBoxPlotSeries *series = new QBoxPlotSeries();
47 QBoxPlotSeries *series = new QBoxPlotSeries();
72 series->append(set0);
73 series->append(set1);
74 series->append(set2);
75 series->append(set3);
76 series->append(set4);
77 series->append(set5);
78 series->append(set6);
79 series->append(set7);
80 series->append(set8);
81 series->append(set9);
82 series->append(set10);
83 series->append(set11);
84 series->setName("Box & Whiskers");
48 series->setName("Box & Whiskers");
85 //![2]
49 //![2]
86
50
87 QLineSeries *lineSeries = new QLineSeries();
51 QFile stockData(":stock");
88 lineSeries->append(0, 4.4);
52 if (!stockData.open(QIODevice::ReadOnly | QIODevice::Text)) {
89 lineSeries->append(1, 7.5);
53 return 1;
90 lineSeries->append(2, 5.7);
54 }
91 lineSeries->append(3, 6.8);
55
92 lineSeries->append(4, 5.2);
56 BoxDataReader dataReader(&stockData);
93 lineSeries->append(5, 8.2);
57 while (!dataReader.atEnd()) {
94 lineSeries->append(6, 5.4);
58 QBoxSet *set = dataReader.readBox();
95 lineSeries->append(7, 7.5);
59 if (set)
96 lineSeries->append(8, 5.7);
60 series->append(set);
97 lineSeries->append(9, 6.8);
61 }
98 lineSeries->append(10, 5.2);
62
99 lineSeries->append(11, 8.2);
63 // QTextStream stream(&stockData);
100 lineSeries->setName("Medians");
64 // while (!stream.atEnd()) {
101
65 // QString line = stream.readLine();
102 QScatterSeries *scatterSeries = new QScatterSeries();
66 // if (line.startsWith("#"))
103 scatterSeries->setName("Outliers");
67 // continue;
104 scatterSeries->setMarkerShape(QScatterSeries::MarkerShapeCircle);
68 // QStringList strList = line.split(" ", QString::SkipEmptyParts);
105 scatterSeries->setMarkerSize(7.0);
69 // QList<qreal> realList;
106 scatterSeries->setBrush(QBrush(Qt::white));
70 // foreach (QString str, strList) {
107 scatterSeries->setPen(QPen(Qt::black, 1.0));
71 // realList.append(str.toDouble());
108 scatterSeries->append(1, 4);
72 // }
109 scatterSeries->append(1, 4.1);
73
110 scatterSeries->append(1, 4.2);
74 // qSort(realList.begin(), realList.end());
111 scatterSeries->append(1, 4.3);
75
112 *scatterSeries << QPointF(3, 8.5) << QPointF(3, 8.6);
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 // }
113
90
114 //![3]
91 //![3]
115 QChart *chart = new QChart();
92 QChart *chart = new QChart();
116 chart->addSeries(series);
93 chart->addSeries(series);
117 chart->addSeries(lineSeries);
118 chart->addSeries(scatterSeries);
119 chart->setTitle("Simple boxplotchart example");
94 chart->setTitle("Simple boxplotchart example");
120 chart->setAnimationOptions(QChart::SeriesAnimations);
95 chart->setAnimationOptions(QChart::SeriesAnimations);
121 //![3]
96 //![3]
122
97
123 //![4]
98 //![4]
124 QStringList categories;
99 //QStringList categories;
125 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
100 //categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
126 QBarCategoryAxis *axis = new QBarCategoryAxis();
101 QBarCategoryAxis *axis = new QBarCategoryAxis();
127 axis->append(categories);
102 //axis->append(categories);
128 chart->createDefaultAxes();
103 chart->createDefaultAxes();
129 chart->setAxisX(axis, series);
104 chart->setAxisX(axis, series);
130 //![4]
105 //![4]
131
106
132 //![5]
107 //![5]
133 chart->legend()->setVisible(true);
108 chart->legend()->setVisible(true);
134 chart->legend()->setAlignment(Qt::AlignBottom);
109 chart->legend()->setAlignment(Qt::AlignBottom);
135 //![5]
110 //![5]
136
111
137 //![6]
112 //![6]
138 QChartView *chartView = new QChartView(chart);
113 QChartView *chartView = new QChartView(chart);
139 chartView->setRenderHint(QPainter::Antialiasing);
114 chartView->setRenderHint(QPainter::Antialiasing);
140 //![6]
115 //![6]
141
116
142 //![7]
117 //![7]
143 QMainWindow window;
118 QMainWindow window;
144 window.setCentralWidget(chartView);
119 window.setCentralWidget(chartView);
145 window.resize(600, 400);
120 window.resize(600, 400);
146 window.show();
121 window.show();
147 //![7]
122 //![7]
148
123
149 return a.exec();
124 return a.exec();
150 }
125 }
151
126
@@ -1,397 +1,378
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 #include "qboxset.h"
21 #include "qboxset.h"
22 #include "qboxset_p.h"
22 #include "qboxset_p.h"
23 #include "charthelpers_p.h"
23 #include "charthelpers_p.h"
24
24
25 #include <QDebug> //TODO: remove on release
26
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
28
27 /*!
29 /*!
28 Constructs QBoxSet with parent of \a parent
30 Constructs QBoxSet with parent of \a parent
29 */
31 */
30 QBoxSet::QBoxSet(QObject *parent)
32 QBoxSet::QBoxSet(QObject *parent)
31 : QObject(parent),
33 : QObject(parent),
32 d_ptr(new QBoxSetPrivate(this))
34 d_ptr(new QBoxSetPrivate(this))
33 {
35 {
34 }
36 }
35
37
36 QBoxSet::QBoxSet(qreal value1, qreal value2, qreal value3, qreal value4, qreal value5, QObject *parent)
38 QBoxSet::QBoxSet(qreal value1, qreal value2, qreal value3, qreal value4, qreal value5, QObject *parent)
37 : QObject(parent),
39 : QObject(parent),
38 d_ptr(new QBoxSetPrivate(this))
40 d_ptr(new QBoxSetPrivate(this))
39 {
41 {
40 d_ptr->append(value1);
42 d_ptr->append(value1);
41 d_ptr->append(value2);
43 d_ptr->append(value2);
42 d_ptr->append(value3);
44 d_ptr->append(value3);
43 d_ptr->append(value4);
45 d_ptr->append(value4);
44 d_ptr->append(value5);
46 d_ptr->append(value5);
45 }
47 }
46
48
47 /*!
49 /*!
48 Destroys the boxset
50 Destroys the boxset
49 */
51 */
50 QBoxSet::~QBoxSet()
52 QBoxSet::~QBoxSet()
51 {
53 {
52 // NOTE: d_ptr destroyed by QObject
54 // NOTE: d_ptr destroyed by QObject
53 }
55 }
54
56
55 /*!
57 /*!
56 Appends new value \a value to the end of set.
58 Appends new value \a value to the end of set.
57 */
59 */
58 void QBoxSet::append(const qreal value)
60 void QBoxSet::append(const qreal value)
59 {
61 {
60 int index = d_ptr->m_values.count();
62 //int index = d_ptr->m_values.count();
61 d_ptr->append(value);
63 d_ptr->append(value);
62
64
63 emit valuesAdded(index, 1);
65 emit valuesAdded(d_ptr->m_valuesCount, 1);
64 }
66 }
65
67
66 /*!
68 /*!
67 Appends a list of reals to set. Works like append with single real value. The \a values in list
69 Appends a list of reals to set. Works like append with single real value. The \a values in list
68 are appended to end of boxset
70 are appended to end of boxset
69 \sa append()
71 \sa append()
70 */
72 */
71 void QBoxSet::append(const QList<qreal> &values)
73 void QBoxSet::append(const QList<qreal> &values)
72 {
74 {
73 int index = d_ptr->m_values.count();
75 //int index = d_ptr->m_values.count();
74 d_ptr->append(values);
76 d_ptr->append(values);
75 emit valuesAdded(index, values.count());
77 emit valuesAdded(d_ptr->m_valuesCount, values.count());
76 }
78 }
77
79
78 /*!
80 /*!
79 Sets new value \a value as the lower extreme for the set.
81 Sets new value \a value as the lower extreme for the set.
80 */
82 */
81 void QBoxSet::setLowerExtreme(const qreal value)
83 void QBoxSet::setLowerExtreme(const qreal value)
82 {
84 {
83 d_ptr->replace(QBoxSetPrivate::PosLowerExtreme, value);
85 d_ptr->replace(QBoxSetPrivate::PosLowerExtreme, value);
86 emit d_ptr->restructuredBox();
84 emit valueChanged(QBoxSetPrivate::PosLowerExtreme);
87 emit valueChanged(QBoxSetPrivate::PosLowerExtreme);
85 }
88 }
86
89
87 /*!
90 /*!
88 Returns the lower extreme value of the set.
91 Returns the lower extreme value of the set.
89 */
92 */
90 qreal QBoxSet::lowerExtreme()
93 qreal QBoxSet::lowerExtreme()
91 {
94 {
92 return d_ptr->m_values.at(QBoxSetPrivate::PosLowerExtreme);
95 return d_ptr->m_values[QBoxSetPrivate::PosLowerExtreme];
93 }
96 }
94
97
95 /*!
98 /*!
96 Sets new value \a value as the lower quartile for the set.
99 Sets new value \a value as the lower quartile for the set.
97 */
100 */
98 void QBoxSet::setLowerQuartile(const qreal value)
101 void QBoxSet::setLowerQuartile(const qreal value)
99 {
102 {
100 d_ptr->replace(QBoxSetPrivate::PosLowerQuartile, value);
103 d_ptr->replace(QBoxSetPrivate::PosLowerQuartile, value);
104 emit d_ptr->restructuredBox();
101 emit valueChanged(QBoxSetPrivate::PosLowerQuartile);
105 emit valueChanged(QBoxSetPrivate::PosLowerQuartile);
102 }
106 }
103
107
104 /*!
108 /*!
105 Returns the lower quartile value of the set.
109 Returns the lower quartile value of the set.
106 */
110 */
107 qreal QBoxSet::lowerQuartile()
111 qreal QBoxSet::lowerQuartile()
108 {
112 {
109 return d_ptr->m_values.at(QBoxSetPrivate::PosLowerQuartile);
113 return d_ptr->m_values[QBoxSetPrivate::PosLowerQuartile];
110 }
114 }
111
115
112 /*!
116 /*!
113 Sets new value \a value as the median for the set.
117 Sets new value \a value as the median for the set.
114 */
118 */
115 void QBoxSet::setMedian(const qreal value)
119 void QBoxSet::setMedian(const qreal value)
116 {
120 {
117 d_ptr->replace(QBoxSetPrivate::PosMedian, value);
121 d_ptr->replace(QBoxSetPrivate::PosMedian, value);
122 emit d_ptr->restructuredBox();
118 emit valueChanged(QBoxSetPrivate::PosMedian);
123 emit valueChanged(QBoxSetPrivate::PosMedian);
119 }
124 }
120
125
121 /*!
126 /*!
122 Returns the median value of the set.
127 Returns the median value of the set.
123 */
128 */
124 qreal QBoxSet::median()
129 qreal QBoxSet::median()
125 {
130 {
126 return d_ptr->m_values.at(QBoxSetPrivate::PosMedian);
131 return d_ptr->m_values[QBoxSetPrivate::PosMedian];
127 }
132 }
128
133
129 /*!
134 /*!
130 Sets new value \a value as the upper quartile for the set.
135 Sets new value \a value as the upper quartile for the set.
131 */
136 */
132 void QBoxSet::setUpperQuartile(const qreal value)
137 void QBoxSet::setUpperQuartile(const qreal value)
133 {
138 {
134 d_ptr->replace(QBoxSetPrivate::PosUpperQuartile, value);
139 d_ptr->replace(QBoxSetPrivate::PosUpperQuartile, value);
140 emit d_ptr->restructuredBox();
135 emit valueChanged(QBoxSetPrivate::PosUpperQuartile);
141 emit valueChanged(QBoxSetPrivate::PosUpperQuartile);
136 }
142 }
137
143
138 /*!
144 /*!
139 Returns the upper quartile value of the set.
145 Returns the upper quartile value of the set.
140 */
146 */
141 qreal QBoxSet::upperQuartile()
147 qreal QBoxSet::upperQuartile()
142 {
148 {
143 return d_ptr->m_values.at(QBoxSetPrivate::PosUpperQuartile);
149 return d_ptr->m_values[QBoxSetPrivate::PosUpperQuartile];
144 }
150 }
145
151
146 /*!
152 /*!
147 Sets new value \a value as the upper extreme for the set.
153 Sets new value \a value as the upper extreme for the set.
148 */
154 */
149 void QBoxSet::setUpperExtreme(const qreal value)
155 void QBoxSet::setUpperExtreme(const qreal value)
150 {
156 {
151 d_ptr->replace(QBoxSetPrivate::PosUpperQuartile, value);
157 d_ptr->replace(QBoxSetPrivate::PosUpperExtreme, value);
152 emit valueChanged(QBoxSetPrivate::PosUpperQuartile);
158 emit d_ptr->restructuredBox();
159 emit valueChanged(QBoxSetPrivate::PosUpperExtreme);
153 }
160 }
154
161
155 /*!
162 /*!
156 Returns the upper extreme value of the set.
163 Returns the upper extreme value of the set.
157 */
164 */
158 qreal QBoxSet::upperExtreme()
165 qreal QBoxSet::upperExtreme()
159 {
166 {
160 return d_ptr->m_values.at(QBoxSetPrivate::PosUpperExtreme);
167 return d_ptr->m_values[QBoxSetPrivate::PosUpperExtreme];
161 }
168 }
162
169
163 /*!
170 /*!
164 Convenience operator. Same as append, with real \a value.
171 Convenience operator. Same as append, with real \a value.
165 \sa append()
172 \sa append()
166 */
173 */
167 QBoxSet &QBoxSet::operator << (const qreal &value)
174 QBoxSet &QBoxSet::operator << (const qreal &value)
168 {
175 {
169 append(value);
176 append(value);
170 return *this;
177 return *this;
171 }
178 }
172
179
173 /*!
180 /*!
174 Inserts new \a value on the \a index position.
181 Inserts new \a value on the \a index position.
175 The value that is currently at this postion is moved to postion index + 1
182 The value that is currently at this postion is moved to postion index + 1
176 \sa remove()
177 */
183 */
178 void QBoxSet::insert(const int index, const qreal value)
184 void QBoxSet::insert(const int index, const qreal value)
179 {
185 {
180 d_ptr->insert(index, value);
186 d_ptr->insert(index, value);
181 emit valuesAdded(index, 1);
187 emit valuesAdded(index, 1);
182 }
188 }
183
189
184 /*!
190 /*!
185 Removes \a count number of values from the set starting at \a index.
186 \sa insert()
187 */
188 void QBoxSet::remove(const int index, const int count)
189 {
190 int removedCount = d_ptr->remove(index, count);
191 if (removedCount > 0)
192 emit valuesRemoved(index, removedCount);
193 return;
194 }
195
196 /*!
197 Sets a new value \a value to set, indexed by \a index
191 Sets a new value \a value to set, indexed by \a index
198 */
192 */
199 void QBoxSet::replace(const int index, const qreal value)
193 void QBoxSet::replace(const int index, const qreal value)
200 {
194 {
201 if (index >= 0 && index < d_ptr->m_values.count()) {
195 if (index >= 0 && index < 5) {
202 d_ptr->replace(index, value);
196 d_ptr->replace(index, value);
203 emit valueChanged(index);
197 emit valueChanged(index);
204 }
198 }
205 }
199 }
206
200
207
201
208 /*!
202 /*!
209 Returns value of set indexed by \a index.
203 Returns value of set indexed by \a index.
210 If the index is out of bounds 0.0 is returned.
204 If the index is out of bounds 0.0 is returned.
211 */
205 */
212 qreal QBoxSet::at(const int index) const
206 qreal QBoxSet::at(const int index) const
213 {
207 {
214 if (index < 0 || index >= d_ptr->m_values.count())
208 if (index < 0 || index >= 5)
215 return 0;
209 return 0;
216 return d_ptr->m_values.at(index);
210 return d_ptr->m_values[index];
217 }
211 }
218
212
219 /*!
213 /*!
220 Returns value of set indexed by \a index.
214 Returns value of set indexed by \a index.
221 If the index is out of bounds 0.0 is returned.
215 If the index is out of bounds 0.0 is returned.
222 */
216 */
223 qreal QBoxSet::operator [](const int index) const
217 qreal QBoxSet::operator [](const int index) const
224 {
218 {
225 return at(index);
219 return at(index);
226 }
220 }
227
221
228 /*!
222 /*!
229 Returns count of values in set.
223 Returns count of values in set.
230 */
224 */
231 int QBoxSet::count() const
225 int QBoxSet::count() const
232 {
226 {
233 return d_ptr->m_values.count();
227 return d_ptr->m_valuesCount;
234 }
228 }
235
229
236 /*!
230 /*!
237 Sets pen for set. Boxes of this set are drawn using \a pen
231 Sets pen for set. Boxes of this set are drawn using \a pen
238 */
232 */
239 void QBoxSet::setPen(const QPen &pen)
233 void QBoxSet::setPen(const QPen &pen)
240 {
234 {
241 if (d_ptr->m_pen != pen) {
235 if (d_ptr->m_pen != pen) {
242 d_ptr->m_pen = pen;
236 d_ptr->m_pen = pen;
243 emit d_ptr->updatedBox();
237 emit d_ptr->updatedBox();
244 emit penChanged();
238 emit penChanged();
245 }
239 }
246 }
240 }
247
241
248 /*!
242 /*!
249 Returns pen of the set.
243 Returns pen of the set.
250 */
244 */
251 QPen QBoxSet::pen() const
245 QPen QBoxSet::pen() const
252 {
246 {
253 return d_ptr->m_pen;
247 return d_ptr->m_pen;
254 }
248 }
255
249
256 /*!
250 /*!
257 Sets brush for the set. Boxes of this set are drawn using \a brush
251 Sets brush for the set. Boxes of this set are drawn using \a brush
258 */
252 */
259 void QBoxSet::setBrush(const QBrush &brush)
253 void QBoxSet::setBrush(const QBrush &brush)
260 {
254 {
261 if (d_ptr->m_brush != brush) {
255 if (d_ptr->m_brush != brush) {
262 d_ptr->m_brush = brush;
256 d_ptr->m_brush = brush;
263 emit d_ptr->updatedBox();
257 emit d_ptr->updatedBox();
264 emit brushChanged();
258 emit brushChanged();
265 }
259 }
266 }
260 }
267
261
268 /*!
262 /*!
269 Returns brush of the set.
263 Returns brush of the set.
270 */
264 */
271 QBrush QBoxSet::brush() const
265 QBrush QBoxSet::brush() const
272 {
266 {
273 return d_ptr->m_brush;
267 return d_ptr->m_brush;
274 }
268 }
275
269
276 /*!
270 /*!
277 Returns the color of the brush of boxset.
271 Returns the color of the brush of boxset.
278 */
272 */
279 QColor QBoxSet::color()
273 QColor QBoxSet::color()
280 {
274 {
281 return brush().color();
275 return brush().color();
282 }
276 }
283
277
284 /*!
278 /*!
285 Sets the \a color of brush for this boxset
279 Sets the \a color of brush for this boxset
286 */
280 */
287 void QBoxSet::setColor(QColor color)
281 void QBoxSet::setColor(QColor color)
288 {
282 {
289 QBrush b = brush();
283 QBrush b = brush();
290 if ((b.color() != color) || (b.style() == Qt::NoBrush)) {
284 if ((b.color() != color) || (b.style() == Qt::NoBrush)) {
291 b.setColor(color);
285 b.setColor(color);
292 if (b.style() == Qt::NoBrush) {
286 if (b.style() == Qt::NoBrush) {
293 // Set tyle to Qt::SolidPattern. (Default is Qt::NoBrush)
287 // Set tyle to Qt::SolidPattern. (Default is Qt::NoBrush)
294 // This prevents theme to override color defined in QML side:
288 // This prevents theme to override color defined in QML side:
295 // BoxSet { label: "Bob"; color:"red"; values: [1,2,3] }
289 // BoxSet { label: "Bob"; color:"red"; values: [1,2,3] }
296 // The color must be obeyed, since user wanted it.
290 // The color must be obeyed, since user wanted it.
297 b.setStyle(Qt::SolidPattern);
291 b.setStyle(Qt::SolidPattern);
298 }
292 }
299 setBrush(b);
293 setBrush(b);
300 emit colorChanged(color);
294 emit colorChanged(color);
301 }
295 }
302 }
296 }
303
297
304 /*!
298 /*!
305 Returns the color of pen of this boxset
299 Returns the color of pen of this boxset
306 */
300 */
307 QColor QBoxSet::borderColor()
301 QColor QBoxSet::borderColor()
308 {
302 {
309 return pen().color();
303 return pen().color();
310 }
304 }
311
305
312 /*!
306 /*!
313 Sets the color of pen for this boxset
307 Sets the color of pen for this boxset
314 */
308 */
315 void QBoxSet::setBorderColor(QColor color)
309 void QBoxSet::setBorderColor(QColor color)
316 {
310 {
317 QPen p = pen();
311 QPen p = pen();
318 if (p.color() != color) {
312 if (p.color() != color) {
319 p.setColor(color);
313 p.setColor(color);
320 setPen(p);
314 setPen(p);
321 emit borderColorChanged(color);
315 emit borderColorChanged(color);
322 }
316 }
323 }
317 }
324
318
325 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
319 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
326
320
327 QBoxSetPrivate::QBoxSetPrivate(QBoxSet *parent) : QObject(parent),
321 QBoxSetPrivate::QBoxSetPrivate(QBoxSet *parent) : QObject(parent),
328 q_ptr(parent),
322 q_ptr(parent),
323 m_valuesCount(5),
324 m_appendCount(0),
329 m_pen(QPen(Qt::NoPen)),
325 m_pen(QPen(Qt::NoPen)),
330 m_brush(QBrush(Qt::NoBrush))
326 m_brush(QBrush(Qt::NoBrush))
331 {
327 {
328 m_values = new qreal[m_valuesCount];
332 }
329 }
333
330
334 QBoxSetPrivate::~QBoxSetPrivate()
331 QBoxSetPrivate::~QBoxSetPrivate()
335 {
332 {
336 }
333 }
337
334
338 void QBoxSetPrivate::append(qreal value)
335 void QBoxSetPrivate::append(qreal value)
339 {
336 {
340 if (isValidValue(value)) {
337 if (isValidValue(value) && m_appendCount < m_valuesCount) {
341 m_values.append(value);
338 m_values[m_appendCount++] = value;
342 emit restructuredBox();
339 emit restructuredBox();
343 }
340 }
344 }
341 }
345
342
346 void QBoxSetPrivate::append(QList<qreal> values)
343 void QBoxSetPrivate::append(QList<qreal> values)
347 {
344 {
348 for (int i = 0; i < values.count(); i++) {
345 for (int i = 0; i < values.count(); i++) {
349 if (isValidValue(values.at(i)))
346 if (isValidValue(values.at(i)) && m_appendCount < m_valuesCount)
350 m_values.append(values.at(i));
347 m_values[m_appendCount++] = values.at(i);
351 }
348 }
352 emit restructuredBox();
349 emit restructuredBox();
353 }
350 }
354
351
355 void QBoxSetPrivate::insert(const int index, const qreal value)
352 void QBoxSetPrivate::insert(const int index, const qreal value)
356 {
353 {
357 if (isValidValue(value)) {
354 if (isValidValue(value)) {
358 m_values.insert(index, value);
355 for (int i = 4; i > index; i--)
356 m_values[i] = m_values[i - 1];
357 m_values[index] = value;
359 emit restructuredBox();
358 emit restructuredBox();
360 }
359 }
361 }
360 }
362
361
363 int QBoxSetPrivate::remove(const int index, const int count)
364 {
365 int removeCount = count;
366
367 if ((index < 0) || (m_values.count() == 0))
368 return 0; // Invalid index or not values in list, remove nothing.
369 else if ((index + count) > m_values.count())
370 removeCount = m_values.count() - index; // Trying to remove more items than list has. Limit amount to be removed.
371
372 int c = 0;
373 while (c < removeCount) {
374 m_values.removeAt(index);
375 c++;
376 }
377 emit restructuredBox();
378 return removeCount;
379 }
380
381 void QBoxSetPrivate::replace(const int index, const qreal value)
362 void QBoxSetPrivate::replace(const int index, const qreal value)
382 {
363 {
383 m_values.replace(index, value);
364 m_values[index] = value;
384 emit updatedLayout();
365 emit updatedLayout();
385 }
366 }
386
367
387 qreal QBoxSetPrivate::value(const int index)
368 qreal QBoxSetPrivate::value(const int index)
388 {
369 {
389 if (index < 0 || index >= m_values.count())
370 if (index < 0 || index >= m_valuesCount)
390 return 0;
371 return 0;
391 return m_values.at(index);
372 return m_values[index];
392 }
373 }
393
374
394 #include "moc_qboxset.cpp"
375 #include "moc_qboxset.cpp"
395 #include "moc_qboxset_p.cpp"
376 #include "moc_qboxset_p.cpp"
396
377
397 QTCOMMERCIALCHART_END_NAMESPACE
378 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,87 +1,90
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QBOXSET_P_H
30 #ifndef QBOXSET_P_H
31 #define QBOXSET_P_H
31 #define QBOXSET_P_H
32
32
33 #include "qboxset.h"
33 #include "qboxset.h"
34 #include <QMap>
34 #include <QMap>
35 #include <QPen>
35 #include <QPen>
36 #include <QBrush>
36 #include <QBrush>
37 #include <QFont>
37 #include <QFont>
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 class QBoxSetPrivate : public QObject
41 class QBoxSetPrivate : public QObject
42 {
42 {
43 Q_OBJECT
43 Q_OBJECT
44
44
45 public:
45 public:
46 enum DataPositions {
46 enum DataPositions {
47 PosLowerExtreme,
47 PosLowerExtreme,
48 PosLowerQuartile,
48 PosLowerQuartile,
49 PosMedian,
49 PosMedian,
50 PosUpperQuartile,
50 PosUpperQuartile,
51 PosUpperExtreme
51 PosUpperExtreme
52 };
52 };
53
53
54 public:
54 public:
55 QBoxSetPrivate(QBoxSet *parent);
55 QBoxSetPrivate(QBoxSet *parent);
56 ~QBoxSetPrivate();
56 ~QBoxSetPrivate();
57
57
58 void append(qreal value);
58 void append(qreal value);
59 void append(QList<qreal> values);
59 void append(QList<qreal> values);
60
60
61 void insert(const int index, const qreal value);
61 void insert(const int index, const qreal value);
62 int remove(const int index, const int count);
62 int remove(const int index, const int count);
63
63
64 void replace(const int index, const qreal value);
64 void replace(const int index, const qreal value);
65
65
66 qreal value(const int index);
66 qreal value(const int index);
67
67
68 Q_SIGNALS:
68 Q_SIGNALS:
69 void restructuredBox();
69 void restructuredBox();
70 void updatedBox();
70 void updatedBox();
71 void updatedLayout();
71 void updatedLayout();
72
72
73 public:
73 public:
74 QBoxSet * const q_ptr;
74 QBoxSet * const q_ptr;
75 //QString m_label;
75 //QString m_label;
76 QList<qreal> m_values;
76 //QList<qreal> m_values;
77 const int m_valuesCount;
78 qreal *m_values;
79 int m_appendCount;
77 QPen m_pen;
80 QPen m_pen;
78 QBrush m_brush;
81 QBrush m_brush;
79 QBrush m_labelBrush;
82 QBrush m_labelBrush;
80 QFont m_labelFont;
83 QFont m_labelFont;
81
84
82 friend class QBoxSet;
85 friend class QBoxSet;
83 };
86 };
84
87
85 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
86
89
87 #endif // QBOXSET_P_H
90 #endif // QBOXSET_P_H
General Comments 0
You need to be logged in to leave comments. Login now