summaryrefslogtreecommitdiffstats
path: root/examples/datavisualization/bars/doc/src/bars.qdoc
blob: 75c7237358a38da456b01f0bc1f8e33a5bb700b3 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Data Visualization module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** 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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example bars
    \title Bars Example
    \ingroup qtdatavisualization_examples
    \brief Using Q3DBars in a widget application.

    The bars example shows how to make a 3D bar graph using Q3DBars and combining the use of
    widgets for adjusting several adjustable qualities. The example shows how to:

    \list
        \li Create an application with Q3DBars and some widgets
        \li Use QBar3DSeries and QBarDataProxy to set data to the graph
        \li Adjust some graph and series properties using widget controls
        \li Select a row or a column by clicking an axis label
    \endlist

    It also demonstrates how having negative bar values affects the graph.

    For instructions about how to interact with the graph, see \l{Qt Data Visualization Interacting with Data}{this page}.

    \image bars-example.png

    \section1 Creating the Application

    First, in main.cpp, we create a QApplication, instantiate Q3DBars and a window container
    for it:

    \snippet bars/main.cpp 0

    The call to QWidget::createWindowContainer is required, as all data visualization graph classes
    (Q3DBars, Q3DScatter, Q3DSurface) inherit QWindow. Any class inheriting QWindow cannot be used
    as a widget any other way.

    Then we'll create horizontal and vertical layouts. We'll add the graph and the vertical
    layout into the horizontal one:

    \snippet bars/main.cpp 1

    We're not using the vertical layout for anything yet, but we'll get back to it in
    \l {Using widgets to control the graph}

    Next, let's create another class to handle the data addition and other interaction with the
    graph. Let's call it \c GraphModifier (See \l {Setting up the graph} and
    \l {Adding data to the graph} for details):

    \snippet bars/main.cpp 2

    The application main is done and we can show the graph and start the event loop:

    \snippet bars/main.cpp 3

    \section1 Setting up the Graph

    Let's set up the graph in the constructor of the \c GraphModifier class we instantiated in the
    application main:

    \snippet bars/graphmodifier.cpp 0

    Let's take a closer look at parts of the code.

    First we're creating the axes and the series into member variables to support changing them
    easily later on, if we want to:

    \snippet bars/graphmodifier.cpp 1

    Then we're setting some of the visual qualities for the graph:

    \snippet bars/graphmodifier.cpp 2

    We're also setting up the axes and setting them to the graph as active axes:

    \snippet bars/graphmodifier.cpp 3

    We give axis labels a small autorotation angle to make them orient somewhat toward the
    camera. This is done to improve axis label readability at extreme camera angles.

    Next we initialize the visual properties of the series.
    Note that the second series is initially not visible:

    \snippet bars/graphmodifier.cpp 8

    The series need to be added to the graph to show them:

    \snippet bars/graphmodifier.cpp 4

    Finally, we set the camera angle by calling the same method the camera angle change button
    in the UI uses to cycle through various camera angles:

    \snippet bars/graphmodifier.cpp 6

    There you can see that the camera is controlled via the scene object of the graph:

    \snippet bars/graphmodifier.cpp 10

    For more information about using scene and cameras, see Q3DScene and Q3DCamera.

    That concludes setting up the graph.

    \section1 Adding Data to the Graph

    At the end of the constructor there's a call:

    \snippet bars/graphmodifier.cpp 9

    This method is used to add data to the proxies of the two series:

    \snippet bars/graphmodifier.cpp 5

    Now the series have data to show.

    \section1 Using Widgets to Control the Graph

    There isn't much interaction yet, so let's continue by adding some widgets back in the
    application main. Let's just focus on two as an example:

    \snippet bars/main.cpp 4

    We can use these slider widgets to rotate the graph instead of just using the mouse or
    touch.

    Let's add them to the vertical layout we created earlier:

    \snippet bars/main.cpp 5

    Then we'll connect them to methods in \c GraphModifier:

    \snippet bars/main.cpp 6

    Here are the methods in \c GraphModifier the signals were connected to. The camera
    is controlled via the scene object. This time we specify the actual camera position
    along the orbit around the center point, instead of specifying a preset camera angle:

    \snippet bars/graphmodifier.cpp 7

    Now these two sliders can be used to rotate the graph.

    And so we have an application in which we can control:

    \list
        \li Graph rotation
        \li Label style
        \li Camera preset
        \li Background visibility
        \li Grid visibility
        \li Bar shading smoothness
        \li Visibility of the second bar series
        \li Value axis direction
        \li Axis title visibility and rotation
        \li Data range to be shown
        \li Bar style
        \li Selection mode
        \li Theme
        \li Shadow quality
        \li Font
        \li Font size
        \li Axis label rotation
    \endlist

    \section1 Selecting a Row/column by Clicking an Axis Label

    Selection by axis label is default functionality for bar graphs. As an example, you can select
    rows by clicking an axis label in the following way:

    \list
        \li Change selection mode to \c SelectionRow
        \li Click a year label
        \li The row with the clicked year is selected
    \endlist

    You can use the same method with \c SelectionSlice and \c SelectionItem flags, as long as
    you have either \c SelectionRow or \c SelectionColumn set as well.

    \section1 Zooming to Selection

    As an example of adjusting camera target we have implemented an animation of zooming to
    selection via a button press. Animation initializations are done in the constructor:

    \snippet bars/graphmodifier.cpp 12

    The function \c{GraphModifier::zoomToSelectedBar()} contains the rest of the functionality:

    \snippet bars/graphmodifier.cpp 11

    The QPropertyAnimation \c m_animationCameraTarget targets Q3DCamera::target property,
    which takes a value normalized to the range (-1, 1). We figure out where the selected bar
    is relative to axes, and use that as the end value for \c{m_animationCameraTarget}:

    \snippet bars/graphmodifier.cpp 13
    \dots
    \snippet bars/graphmodifier.cpp 14

    Likewise, we want to angle the camera so that it always points approximately to the center of
    the graph at the end of the animation:

    \snippet bars/graphmodifier.cpp 15

    \section1 Example Contents

*/