##// END OF EJS Templates
Minor updates to barModelMapper example and its docs
Marek Rosa -
r1406:0f4e8d578ad7
parent child
Show More
@@ -1,57 +1,57
1 /*!
1 /*!
2 \example examples/barmodelmapper
2 \example examples/barmodelmapper
3 \title BarModelMapper example
3 \title BarModelMapper example
4 \subtitle
4 \subtitle
5
5
6 This example shows how to use QAbstractItemModel derived model as the data for the bar series.
6 This example shows how to use QAbstractItemModel derived model as the data for the bar series.
7
7
8 \image examples_barmodelmapper.png
8 \image examples_barmodelmapper.png
9
9
10 Let's start by creating an instance of CustomTableModel class.
10 Let's start by creating an instance of CustomTableModel class.
11 CustomTableModel class is derived from QAbstractTableModel and it was created for the purpose of this example.
11 CustomTableModel class is derived from QAbstractTableModel and it was created for the purpose of this example.
12 The constructor of this class populates the internal data store of the model with the data that is good for our chart example.
12 The constructor of this class populates the internal data store of the model with the data that is good for our chart example.
13
13
14 \snippet ../examples/barmodelmapper/tablewidget.cpp 1
14 \snippet ../examples/barmodelmapper/tablewidget.cpp 1
15
15
16 We now have a model with data that we would like to display both on the chart and in a QTableView.
16 We now have a model with data that we would like to display both on the chart and in a QTableView.
17 First, we create QTableView and tell it 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.
17 First, we create QTableView and tell it 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.
18
18
19 \snippet ../examples/barmodelmapper/tablewidget.cpp 2
19 \snippet ../examples/barmodelmapper/tablewidget.cpp 2
20
20
21 Now we need QChart instance to display the same data on the chart.
21 Now we need QChart instance to display the same data on the chart.
22 We also enable animations. It makes it easier to see how modifying the model's data affect the chart.
22 We also enable animations. It makes it easier to see how modifying the model's data affect the chart.
23
23
24 \snippet ../examples/barmodelmapper/tablewidget.cpp 3
24 \snippet ../examples/barmodelmapper/tablewidget.cpp 3
25
25
26 First line of the code below creates new grouped bar series. Variables firstRow and rowCount are used to define a custom model mapping.
26 First line of the code below creates new grouped bar series. Variables firstRow and rowCount are used to define a custom model mapping.
27 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.
27 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.
28 Following three lines create an instance of QVBarModelMapper class and specifie that data for the bar sets should be taken from the model's columns(Qt::Vertical) with indexes from 1 to 4(inclusive).
28 Following three lines create an instance of QVBarModelMapper class and specifie that data for the bar sets should be taken from the model's columns with indexes from 1 to 4(inclusive).
29 To create a connection between the series and the model we set both of those objects to QVBarModelMapper.
29 To create a connection between the series and the model we set both of those objects to QVBarModelMapper.
30
30
31 Finally the series is added to the chart.
31 Finally the series is added to the chart.
32
32
33 \snippet ../examples/barmodelmapper/tablewidget.cpp 4
33 \snippet ../examples/barmodelmapper/tablewidget.cpp 4
34
34
35 To show in QTableView which data coresponds with which series this example uses table coloring.
35 To show in QTableView which data coresponds with which bar set this example uses table coloring.
36 When series is added to the chart it is assigned a color beased on the currently selected theme.
36 When series is added to the chart it is assigned a color beased on the currently selected theme.
37 Code below extracts that color from the series and uses it to create colored QTableView.
37 Code below extracts that color from the series and uses it to create colored QTableView.
38 Coloring of the view is not a part of the QChart functionality.
38 Coloring of the view is not a part of the QChart functionality.
39
39
40 \snippet ../examples/barmodelmapper/tablewidget.cpp 5
40 \snippet ../examples/barmodelmapper/tablewidget.cpp 5
41
41
42 We would like to have categories placed on the chart's axis that describe what the data means.
42 We would like to have categories placed on the chart's axis that describe what the data means.
43 Next snippet shows how to do that.
43 Next snippet shows how to do that.
44
44
45 \snippet ../examples/barmodelmapper/tablewidget.cpp 6
45 \snippet ../examples/barmodelmapper/tablewidget.cpp 6
46
46
47 To avoid setting up the QGraphicsScene we use QChartView class that does it for us. QChart object pointer is used as a parameter of the QChartView constructor.
47 To avoid setting up the QGraphicsScene we use QChartView class that does it for us. QChart object pointer is used as a parameter of the QChartView constructor.
48 To make the render look nicer Antialiasing is turned on and the minimum size of the chartView widget is set.
48 To make the render look nicer Antialiasing is turned on and the minimum size of the chartView widget is set.
49
49
50 \snippet ../examples/barmodelmapper/tablewidget.cpp 7
50 \snippet ../examples/barmodelmapper/tablewidget.cpp 7
51
51
52 Finally we place both widgets in a layout and use the layout as the application layout.
52 Finally we place both widgets in a layout and use the layout as the application layout.
53
53
54 \snippet ../examples/barmodelmapper/tablewidget.cpp 8
54 \snippet ../examples/barmodelmapper/tablewidget.cpp 8
55
55
56 Application is ready. Try modifying the data in the table view and see how it affects the chart.
56 Application is ready. Try modifying the data in the table view and see how it affects the chart.
57 */
57 */
@@ -1,109 +1,109
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "tablewidget.h"
21 #include "tablewidget.h"
22 #include "customtablemodel.h"
22 #include "customtablemodel.h"
23 #include <QGridLayout>
23 #include <QGridLayout>
24 #include <QTableView>
24 #include <QTableView>
25 #include <QChart>
25 #include <QChart>
26 #include <QChartView>
26 #include <QChartView>
27 #include <QLineSeries>
27 #include <QLineSeries>
28 #include <QVXYModelMapper>
28 #include <QVXYModelMapper>
29 #include <QGroupedBarSeries>
29 #include <QGroupedBarSeries>
30 #include <QBarSet>
30 #include <QBarSet>
31 #include <QVBarModelMapper>
31 #include <QVBarModelMapper>
32 #include <QHeaderView>
32 #include <QHeaderView>
33
33
34 QTCOMMERCIALCHART_USE_NAMESPACE
34 QTCOMMERCIALCHART_USE_NAMESPACE
35
35
36 TableWidget::TableWidget(QWidget *parent)
36 TableWidget::TableWidget(QWidget *parent)
37 : QWidget(parent)
37 : QWidget(parent)
38 {
38 {
39 // create simple model for storing data
39 // create simple model for storing data
40 // user's table data model
40 // user's table data model
41 //! [1]
41 //! [1]
42 CustomTableModel *model = new CustomTableModel;
42 CustomTableModel *model = new CustomTableModel;
43 //! [1]
43 //! [1]
44
44
45 //! [2]
45 //! [2]
46 // create table view and add model to it
46 // create table view and add model to it
47 QTableView *tableView = new QTableView;
47 QTableView *tableView = new QTableView;
48 tableView->setModel(model);
48 tableView->setModel(model);
49 tableView->setMinimumWidth(300);
49 tableView->setMinimumWidth(300);
50 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
50 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
51 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
51 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
52 //! [2]
52 //! [2]
53
53
54 //! [3]
54 //! [3]
55 QChart *chart = new QChart;
55 QChart *chart = new QChart;
56 chart->setAnimationOptions(QChart::AllAnimations);
56 chart->setAnimationOptions(QChart::AllAnimations);
57 //! [3]
57 //! [3]
58
58
59 // series 1
59 // series 1
60 //! [4]
60 //! [4]
61 QGroupedBarSeries *series = new QGroupedBarSeries;
61 QGroupedBarSeries *series = new QGroupedBarSeries;
62
62
63 int first = 3;
63 int first = 3;
64 int count = 5;
64 int count = 5;
65 QVBarModelMapper *mapper = new QVBarModelMapper(this);
65 QVBarModelMapper *mapper = new QVBarModelMapper(this);
66 mapper->setFirstBarSetColumn(1);
66 mapper->setFirstBarSetColumn(1);
67 mapper->setLastBarSetColumn(4);
67 mapper->setLastBarSetColumn(4);
68 mapper->setFirst(3);
68 mapper->setFirst(first);
69 mapper->setCount(count);
69 mapper->setCount(count);
70 mapper->setSeries(series);
70 mapper->setSeries(series);
71 mapper->setModel(model);
71 mapper->setModel(model);
72 chart->addSeries(series);
72 chart->addSeries(series);
73 //! [4]
73 //! [4]
74
74
75 //! [5]
75 //! [5]
76 // for storing color hex from the series
76 // for storing color hex from the series
77 QString seriesColorHex = "#000000";
77 QString seriesColorHex = "#000000";
78
78
79 // get the color of the series and use it for showing the mapped area
79 // get the color of the series and use it for showing the mapped area
80 QList<QBarSet*> barsets = series->barSets();
80 QList<QBarSet*> barsets = series->barSets();
81 for (int i = 0; i < barsets.count(); i++) {
81 for (int i = 0; i < barsets.count(); i++) {
82 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
82 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
83 model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count()));
83 model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count()));
84 }
84 }
85 //! [5]
85 //! [5]
86
86
87 //! [6]
87 //! [6]
88 QStringList categories;
88 QStringList categories;
89 categories << "April" << "May" << "June" << "July" << "August";
89 categories << "April" << "May" << "June" << "July" << "August";
90
90
91 chart->axisX()->categories()->insert(categories);
91 chart->axisX()->categories()->insert(categories);
92 //! [6]
92 //! [6]
93
93
94 //! [7]
94 //! [7]
95 QChartView *chartView = new QChartView(chart);
95 QChartView *chartView = new QChartView(chart);
96 chartView->setRenderHint(QPainter::Antialiasing);
96 chartView->setRenderHint(QPainter::Antialiasing);
97 chartView->setMinimumSize(640, 480);
97 chartView->setMinimumSize(640, 480);
98 //! [7]
98 //! [7]
99
99
100 //! [8]
100 //! [8]
101 // create main layout
101 // create main layout
102 QGridLayout* mainLayout = new QGridLayout;
102 QGridLayout* mainLayout = new QGridLayout;
103 mainLayout->addWidget(tableView, 1, 0);
103 mainLayout->addWidget(tableView, 1, 0);
104 mainLayout->addWidget(chartView, 1, 1);
104 mainLayout->addWidget(chartView, 1, 1);
105 mainLayout->setColumnStretch(1, 1);
105 mainLayout->setColumnStretch(1, 1);
106 mainLayout->setColumnStretch(0, 0);
106 mainLayout->setColumnStretch(0, 0);
107 setLayout(mainLayout);
107 setLayout(mainLayout);
108 //! [8]
108 //! [8]
109 }
109 }
General Comments 0
You need to be logged in to leave comments. Login now