summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorMika Salmela <mika.salmela@digia.com>2013-06-06 14:57:44 +0300
committerMika Salmela <mika.salmela@digia.com>2013-06-06 15:19:33 +0300
commit89b8a97da4fc6bdd4a0e1fd52ee57701c4615145 (patch)
tree815443317fe8fa5fdd3ebd58e7fbc3d9d6381a16 /examples
parent8f0e18a7e02cc4f9018d95379efbfb9cf167dd56 (diff)
Example finalization and documentation
Change-Id: I0ccfa85c689bf6f9519fcce5eb38941de6d8d492 Reviewed-by: Mika Salmela <mika.salmela@digia.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/boxplotchart/acme_data.txt (renamed from examples/boxplotchart/stock_data.txt)0
-rw-r--r--examples/boxplotchart/boxdatareader.cpp28
-rw-r--r--examples/boxplotchart/boxdatareader.h3
-rw-r--r--examples/boxplotchart/boxplotdata.qrc3
-rw-r--r--examples/boxplotchart/boxwhisk_data.txt13
-rw-r--r--examples/boxplotchart/main.cpp63
6 files changed, 78 insertions, 32 deletions
diff --git a/examples/boxplotchart/stock_data.txt b/examples/boxplotchart/acme_data.txt
index 9511418d..9511418d 100644
--- a/examples/boxplotchart/stock_data.txt
+++ b/examples/boxplotchart/acme_data.txt
diff --git a/examples/boxplotchart/boxdatareader.cpp b/examples/boxplotchart/boxdatareader.cpp
index 4bc2d404..33babaf1 100644
--- a/examples/boxplotchart/boxdatareader.cpp
+++ b/examples/boxplotchart/boxdatareader.cpp
@@ -25,43 +25,55 @@ BoxDataReader::BoxDataReader(QIODevice *device) :
{
}
-QBoxSet* BoxDataReader::readBox()
+void BoxDataReader::readFile(QIODevice *device)
{
+ QTextStream::setDevice(device);
+}
+
+QBoxSet *BoxDataReader::readBox()
+{
+ //! [1]
QString line = readLine();
if (line.startsWith("#"))
return 0;
+ //! [1]
+ //! [2]
QStringList strList = line.split(" ", QString::SkipEmptyParts);
- sortedList.clear();
+ //! [2]
+ //! [3]
+ sortedList.clear();
for (int i = 1; i < strList.count(); i++)
sortedList.append(strList.at(i).toDouble());
qSort(sortedList.begin(), sortedList.end());
+ //! [3]
int count = sortedList.count();
+ //! [4]
QBoxSet *box = new QBoxSet(strList.first());
box->setValue(QBoxSet::LowerExtreme, sortedList.first());
box->setValue(QBoxSet::UpperExtreme, sortedList.last());
box->setValue(QBoxSet::Median, findMedian(0, count));
box->setValue(QBoxSet::LowerQuartile, findMedian(0, count / 2));
- if (count % 2)
- box->setValue(QBoxSet::UpperQuartile, findMedian(count / 2 + 1, count));
- else // even amount of numbers
- box->setValue(QBoxSet::UpperQuartile, findMedian(count / 2, count));
+ box->setValue(QBoxSet::UpperQuartile, findMedian(count / 2 + (count % 2), count));
+ //! [4]
return box;
}
qreal BoxDataReader::findMedian(int begin, int end)
{
+ //! [5]
int count = end - begin;
- if (count % 2 ) {
- return sortedList.at(int(count/2) + begin);
+ if (count % 2) {
+ return sortedList.at(count / 2 + begin);
} else {
qreal right = sortedList.at(count / 2 + begin);
qreal left = sortedList.at(count / 2 - 1 + begin);
return (right + left) / 2.0;
}
+ //! [5]
}
diff --git a/examples/boxplotchart/boxdatareader.h b/examples/boxplotchart/boxdatareader.h
index bdaae1bd..bf725c09 100644
--- a/examples/boxplotchart/boxdatareader.h
+++ b/examples/boxplotchart/boxdatareader.h
@@ -30,7 +30,8 @@ class BoxDataReader : public QTextStream
{
public:
explicit BoxDataReader(QIODevice *device);
- QBoxSet* readBox();
+ QBoxSet *readBox();
+ void readFile(QIODevice *device);
protected:
qreal findMedian(int begin, int end);
diff --git a/examples/boxplotchart/boxplotdata.qrc b/examples/boxplotchart/boxplotdata.qrc
index 4cea4ba1..fe6d52b5 100644
--- a/examples/boxplotchart/boxplotdata.qrc
+++ b/examples/boxplotchart/boxplotdata.qrc
@@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
- <file alias="stock">stock_data.txt</file>
+ <file alias="acme">acme_data.txt</file>
+ <file alias="boxwhisk">boxwhisk_data.txt</file>
</qresource>
</RCC>
diff --git a/examples/boxplotchart/boxwhisk_data.txt b/examples/boxplotchart/boxwhisk_data.txt
new file mode 100644
index 00000000..f1d49cf0
--- /dev/null
+++ b/examples/boxplotchart/boxwhisk_data.txt
@@ -0,0 +1,13 @@
+# BoxWhisk Inc share deviation in 2012
+Jan 25.74 25.28 25.86 26.05 26.64 28.47 28.30 29.22 29.72 27.50 25.62 25.50 25.15 26.47 27.41 25.78 24.82 24.89 24.88 24.60 23.85
+Feb 30.79 29.62 29.67 30.37 30.16 30.22 31.02 33.70 32.60 32.24 31.98 31.79 31.10 30.79 31.53 30.92 29.00 29.58 30.37 29.40 28.60
+Mar 29.64 29.34 30.13 30.43 31.75 30.77 30.72 31.52 32.12 33.75 33.51 33.69 32.83 32.47 32.41 32.39 32.78 31.08 29.46 31.58 31.39 31.41
+Apr 24.96 25.62 25.19 28.37 28.78 26.50 25.90 26.40 26.86 26.90 27.91 27.32 27.99 26.86 26.68 27.57 27.50 26.96 26.50
+May 21.85 21.08 22.98 22.61 22.45 21.73 21.71 23.27 22.14 19.65 22.95 21.23 23.17 24.26 24.17 21.97 22.53 25.49 25.51 26.46 26.65
+Jun 15.08 15.19 15.36 15.21 15.31 18.19 18.30 15.53 15.35 15.80 15.17 16.95 18.25 20.52 20.61 20.40 20.45 19.43 19.11 19.74
+Jul 17.75 18.24 17.57 16.53 15.98 16.06 16.64 17.69 17.91 19.00 19.03 19.14 19.10 17.86 19.12 19.53 19.43 19.30 19.03 19.76 18.79 18.33
+Aug 19.69 19.54 19.39 19.49 19.96 19.72 20.25 20.70 21.13 20.74 20.27 19.25 18.72 19.02 19.20 19.24 19.60 19.22 19.60 18.98 18.27 17.70 18.19
+Sep 19.35 19.82 19.96 20.96 20.75 21.55 21.68 22.19 22.14 22.48 22.45 21.74 21.97 21.18 20.66 20.54 19.89 19.39 19.26 19.86
+Oct 17.95 17.80 17.45 17.89 18.38 18.12 17.85 18.59 18.65 18.46 18.43 18.30 18.87 19.61 19.55 19.59 20.27 20.54 21.02 20.23 19.05 19.52 19.71
+Now 20.36 20.29 19.22 19.74 20.05 20.13 19.67 19.19 18.94 19.04 18.49 18.53 18.64 19.00 19.21 19.19 19.30 19.11 19.17 18.76 18.80 18.52
+Dec 22.95 22.19 22.15 21.42 21.39 21.65 22.39 20.86 20.48 20.70 20.94 20.82 19.25 21.21 19.63 20.55
diff --git a/examples/boxplotchart/main.cpp b/examples/boxplotchart/main.cpp
index 7ebb6093..c4b04355 100644
--- a/examples/boxplotchart/main.cpp
+++ b/examples/boxplotchart/main.cpp
@@ -33,52 +33,71 @@ QTCOMMERCIALCHART_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
-//![1]
-//![2]
- QBoxPlotSeries *series = new QBoxPlotSeries();
- series->setName("Acme Ltd");
-//![2]
+ //! [1]
+ QBoxPlotSeries *acmeSeries = new QBoxPlotSeries();
+ acmeSeries->setName("Acme Ltd");
- QFile stockData(":stock");
- if (!stockData.open(QIODevice::ReadOnly | QIODevice::Text))
+ QBoxPlotSeries *boxWhiskSeries = new QBoxPlotSeries();
+ boxWhiskSeries->setName("BoxWhisk Inc");
+ //! [1]
+
+ //! [2]
+ QFile acmeData(":acme");
+ if (!acmeData.open(QIODevice::ReadOnly | QIODevice::Text))
+ return 1;
+
+ BoxDataReader dataReader(&acmeData);
+ while (!dataReader.atEnd()) {
+ QBoxSet *set = dataReader.readBox();
+ if (set)
+ acmeSeries->append(set);
+ }
+ //! [2]
+
+ //! [3]
+ QFile boxwhiskData(":boxwhisk");
+ if (!boxwhiskData.open(QIODevice::ReadOnly | QIODevice::Text))
return 1;
- BoxDataReader dataReader(&stockData);
+ dataReader.readFile(&boxwhiskData);
while (!dataReader.atEnd()) {
QBoxSet *set = dataReader.readBox();
if (set)
- series->append(set);
+ boxWhiskSeries->append(set);
}
+ //! [3]
-//![3]
+ //! [4]
QChart *chart = new QChart();
- chart->addSeries(series);
- chart->setTitle("Acme's share deviation in 2012");
+ chart->addSeries(acmeSeries);
+ chart->addSeries(boxWhiskSeries);
+ chart->setTitle("Acme Ltd and BoxWhisk Inc share deviation in 2012");
chart->setAnimationOptions(QChart::SeriesAnimations);
-//![3]
+ //! [4]
-//![4]
+ //! [5]
chart->createDefaultAxes();
chart->axisY()->setMin(15.0);
-//![4]
+ chart->axisY()->setMax(34.0);
+ //! [5]
-//![5]
+ //! [6]
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
-//![5]
+ //! [6]
-//![6]
+ //! [7]
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
-//![6]
+ //! [7]
-//![7]
+ //! [8]
QMainWindow window;
window.setCentralWidget(chartView);
- window.resize(600, 400);
+ window.resize(800, 600);
window.show();
-//![7]
+ //! [8]
return a.exec();
}