examples-boxplotchart.qdoc
76 lines
| 3.7 KiB
| text/plain
|
TextLexer
Mika Salmela
|
r2548 | /*! | ||
\example examples/boxplotchart | ||||
\title Box and Whiskers Example | ||||
\subtitle | ||||
Mika Salmela
|
r2556 | The example shows how to create a box-and-whiskers chart. It also shows how to read the non-continuous data from the file, | ||
arrange it and find medians needed for box-and-whiskers plotting. | ||||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | \image examples_boxplotchart.png | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | To show the share deviation of two companies we start by creating two QBoxPlotSeries to handle monthly data. | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | \snippet ../examples/boxplotchart/main.cpp 1 | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | 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. | ||||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | \snippet ../examples/boxplotchart/main.cpp 2 | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | In this section a second file is opened for reading the data for the second company. | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | \snippet ../examples/boxplotchart/main.cpp 3 | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | In this code snippet a new QChart instance is created and previously created series are added to it. Also title is defined and | ||
animation is set to be SeriesAnimation. | ||||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | \snippet ../examples/boxplotchart/main.cpp 4 | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | Here we ask chart to create default axes for our presentation. We also set range for vertical axis by querying the pointer | ||
for the axis from the chart and then setting the min and max for that axis. | ||||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | \snippet ../examples/boxplotchart/main.cpp 5 | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | In this section we set legends visible and place them at the bottom of the chart. | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | \snippet ../examples/boxplotchart/main.cpp 6 | ||
Mika Salmela
|
r2548 | |||
Mika Salmela
|
r2556 | Finally we add the chart onto a view. We also turn on the antialiasing for the chartView. | ||
\snippet ../examples/boxplotchart/main.cpp 7 | ||||
The chart is ready to be shown. We set the chart to be central widget of the window. | ||||
We also set the size for the chart window and show it. | ||||
\snippet ../examples/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 ../examples/boxplotchart/boxdatareader.cpp 1 | ||||
On the file the data is arranged as number, space, number, space and so on. On this snippet the line is split into single number strings which | ||||
are stored on QStringList. | ||||
\snippet ../examples/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 ../examples/boxplotchart/boxdatareader.cpp 3 | ||||
Here is a code sample 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 | ||||
amount of the numbers is even or uneven. The end number for lower half comes naturally from int rounding. | ||||
\snippet ../examples/boxplotchart/boxdatareader.cpp 4 | ||||
Here's 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 ../examples/boxplotchart/boxdatareader.cpp 5 | ||||
Mika Salmela
|
r2548 | */ | ||