Box and Whiskers Example¶
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.
Running the Example¶
To run the example from Qt Creator , open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.
Creating Box-and-Whiskers Charts¶
To show the share deviation of two companies we start by creating two
QBoxPlotSeries to handle monthly data.
acmeSeries = QBoxPlotSeries() acmeSeries.setName("Acme Ltd") boxWhiskSeries =.
acmeData = QFile(":acme") if (not acmeData.open(QIODevice.ReadOnly | QIODevice.Text)) return 1 dataReader = BoxDataReader(acmeData) while (not dataReader.atEnd()) { set = dataReader.readBox() if (set) acmeSeries.append(set)
In this section a second file is opened for reading the data for the second company.
boxwhiskData = QFile(":boxwhisk") if (not boxwhiskData.open(QIODevice.ReadOnly | QIODevice.Text)) return 1 dataReader.readFile(boxwhiskData) while (not dataReader.atEnd()) { set = dataReader.readBox() if (set) boxWhiskSeries.append(set)
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.
chart =.axes(Qt.Vertical).first().setMin(15.0) chart.axes(Qt.Horizontal).first().setMax(34.0)
In this section we set the legends to be visible and place them at the bottom of the chart.(800, 600) window.show()
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.
line = readLine() if (line.startsWith("#")) return 0
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 .
strList = line.split(' '), Qt i in range(1, strList.count()): sortedList.append(strList.at(i).toDouble()) std::sort.
box =.
count = end - begin if (count % 2) { return sortedList.at(count / 2 + begin) else: right = sortedList.at(count / 2 + begin) left = sortedList.at(count / 2 - 1 + begin) return (right + left) / 2.0.