summaryrefslogtreecommitdiffstats
path: root/src/charts/doc/src/examples-barmodelmapper.qdoc
blob: fe2e8dd0eec9b228b08f55fca9dd3b71facf3580 (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
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, 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 Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.io
**
****************************************************************************/

/*!
     \example barmodelmapper
     \title BarModelMapper Example
     \ingroup qtcharts_examples

     \brief This example shows how to use QAbstractItemModel derived model as the data for the bar
     series.

     \image examples_barmodelmapper.png

     Let's start by creating an instance of CustomTableModel class.
     The CustomTableModel class is derived from QAbstractTableModel, and it was created for the purpose of this example.
     The constructor of this class populates the model's internal data store with the data required for our chart example.

     \snippet barmodelmapper/tablewidget.cpp 1

     We now have a model with data that we would like to display both on the chart and in a QTableView.
     First, we create QTableView and tell it to use the model as a data source. To have the data presented nicely, the minimum width of the table view is set and its headers resize mode changed to stretch.

     \snippet barmodelmapper/tablewidget.cpp 2

     Now we need a QChart instance to display the same data on the chart.
     We also enable animations. It makes it easier to see how a modification to the model's data affects the chart.

     \snippet barmodelmapper/tablewidget.cpp 3

     The first line of the code below creates new bar series. Variables firstRow and rowCount are used to define a custom model mapping.
     Custom mapping allows to take only part of the data from the model. In this case data from 5 rows starting with the row with the index 3.
     The following three lines create an instance of the QVBarModelMapper class and specify that the data for the bar sets should be taken from the model's columns with indexes from 1 to 4 (inclusive).
     To create a connection between the series and the model we set both of those objects to QVBarModelMapper.

     Finally the series is added to the chart.

     \snippet barmodelmapper/tablewidget.cpp 4

     To show in QTableView which data corresponds with which bar set, this example uses table coloring.
     When series is added to the chart, it is assigned a color based on the currently selected theme.
     Code below extracts that color from the series and uses it to create colored QTableView.
     Coloring of the view is not a part of the QChart functionality.

     \snippet barmodelmapper/tablewidget.cpp 5

     We would like to have categories placed on the chart's axis that describe what the data means.
     Next snippet shows how to do that.

     \snippet barmodelmapper/tablewidget.cpp 6

     To avoid setting up the QGraphicsScene we use the QChartView class that does it for us. The QChart object pointer is used as a parameter of the QChartView constructor.
     To make the render look nicer Antialiasing is turned on and the minimum size of the chartView widget is set.

     \snippet barmodelmapper/tablewidget.cpp 7

     Finally, we place both widgets in a layout and use the layout as the application layout.

     \snippet barmodelmapper/tablewidget.cpp 8

     The application is ready. Try modifying the data in the table view and see how it affects the chart.
*/