summaryrefslogtreecommitdiffstats
path: root/src/charts/doc/src/examples-datetimeaxis.qdoc
blob: d9a75591384f15941fb43dd3cf311e0fb5d717b8 (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
/******************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Charts module.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
******************************************************************************/

/*!
    \example datetimeaxis
    \title DateTimeAxis Example
    \ingroup qtcharts_examples

    \brief The example shows how to use QLineChart with QDateTimeAxis.

    \image examples_datetimeaxis.png

    To create line chart, QLineSeries instance is needed. Let's create one.

    \snippet datetimeaxis/main.cpp 1

    On the charts we will present how the number of sun spots changes in time. The data (from the Space Weather Prediction Center) is read from a text file.
    In the snippet below, notice how the QDateTime::toMSecsSinceEpoch method is used to convert the QDateTime object into a number that can be passed to the QLineSeries append method.

    \snippet datetimeaxis/main.cpp 2

    To present the data on the chart we need the QChart instance. We add the series to it, hide the legend, create the default axes and set the title of the chart.

    \snippet datetimeaxis/main.cpp 3

    Since we use QLineSeries, calling createDefaultAxes will create QValueAxis both as X- and Y-axis. To use QDateTimeAxis we need to set it manually to the chart.
    First, the instance of QDateTimeAxis is created, then the number of ticks to be shown is set. The number of sun spots is provided as an average for the month.
    Therefore we don't need the axis labels to contain the information about the time and the day. This is achieved by setting a custom label format.
    Please refer to the QDateTime::toString() method documentation to learn about the available format options.

    \snippet datetimeaxis/main.cpp 4

    Then we create a QChartView object with QChart as a parameter. This way we don't need to create the QGraphicsView scene ourselves. We also set the Antialiasing on to have the rendered lines look nicer.

    \snippet datetimeaxis/main.cpp 5

    The chart is ready to be shown.

    \snippet datetimeaxis/main.cpp 6

*/