summaryrefslogtreecommitdiffstats
path: root/src/charts/doc/src/examples-boxplotchart.qdoc
blob: f44d6b35a54fb3c5fe50f6e89f9028feaa0c0b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://qt.io
**
** This file is part of the Qt Charts module.
**
** Licensees holding valid commercial license for Qt may use this file in
** accordance with the Qt License Agreement provided with the Software
** or, alternatively, in accordance with the terms contained in a written
** agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.io
**
****************************************************************************/

/*!
    \example boxplotchart
    \title Box and Whiskers Example
    \ingroup qtcharts_examples

    \brief The example shows how to create a box-and-whiskers chart.

    The example also shows how to read the non-continuous data from a file,
    arrange it and find medians needed for box-and-whiskers plotting.

    \image examples_boxplotchart.png

    To show the share deviation of two companies we start by creating two QBoxPlotSeries to handle monthly data.

    \snippet boxplotchart/main.cpp 1

    QFile class is used to open a text file where the non-continuous data is kept. The BoxDataReader is an auxiliary class for
    reading the text file and finding the extreme and median values from the data. The BoxDataReader is explained in more detail later.
    The method readBox reads the values and sets them to the QBoxSet item which the method returns for the caller. The returned QBoxSet
    item is added to the series.

    \snippet boxplotchart/main.cpp 2

    In this section a second file is opened for reading the data for the second company.

    \snippet boxplotchart/main.cpp 3

    In this code snippet a new QChart instance is created and previously created series are added to it. The title is also defined and
    animation is set to be SeriesAnimation.

    \snippet boxplotchart/main.cpp 4

    Here we ask the chart to create default axes for our presentation. We also set the range for the vertical axis by querying the pointer
    for the axis from the chart, and then setting the min and max for that axis.

    \snippet boxplotchart/main.cpp 5

    In this section we set the legends to be visible and place them at the bottom of the chart.

    \snippet boxplotchart/main.cpp 6

    Finally, we add the chart onto a view. We also turn on the antialiasing for the chartView.

    \snippet boxplotchart/main.cpp 7

    The chart is ready to be shown. We set the chart to be the central widget of the window.
    We also set the size for the chart window and show it.

    \snippet boxplotchart/main.cpp 8

    Here the method readBox is explained in detail. Firstly, a line is read from the file and lines starting with # are rejected
    since they are considered as comment lines.

    \snippet boxplotchart/boxdatareader.cpp 1

    In this file the data is arranged as number, space, number, or space. On this snippet the line is split into single number strings which
    are stored on QStringList.

    \snippet boxplotchart/boxdatareader.cpp 2

    The sortedList will hold the numbers in continuous order and in this code segment we show how to do it. First the sortedList is cleared and numbers
    are read from the strList and stored into sortedList in double format. The qSort method arranges the sortedList into continuous order
    starting from the smallest.

    \snippet boxplotchart/boxdatareader.cpp 3

    Below you will find a code sample showing how to select extremes and medians from the continuous data. Firstly a new QBoxSet is created.
    Lower and upper extremes are simple to select; they are just first and last items on the sortedList. For medians we use a helper
    method findMedian which is explained later. For the median from the upper half we need to adjust the begin number if the
    amount of the numbers is even or uneven. The end number for lower half comes naturally from int rounding.

    \snippet boxplotchart/boxdatareader.cpp 4

    Below you will find the code sample for the method findMedian. If the amount of numbers is uneven we select the number from
    the middle. For even amount numbers we take two numbers from the middle and calculate the mean value.

    \snippet boxplotchart/boxdatareader.cpp 5
*/