@@ -0,0 +1,55 | |||
|
1 | /*! | |
|
2 | \example examples/barmodelmapper | |
|
3 | \title BarModelMapper example | |
|
4 | \subtitle | |
|
5 | ||
|
6 | This example shows how to use QAbstractItemModel derived model as the data for the bar series. | |
|
7 | ||
|
8 | Let's start by creating an instance of CustomTableModel class. | |
|
9 | CustomTableModel class is derived from QAbstractTableModel and it was created for the purpose of this example. | |
|
10 | The constructor of this class populates the internal data store of the model with the data that is good for our chart example. | |
|
11 | ||
|
12 | \snippet ../examples/barmodelmapper/tablewidget.cpp 1 | |
|
13 | ||
|
14 | We now have a model with data that we would like to display both on the chart and in a QTableView. | |
|
15 | First, we create QTableView and tell it use the model as a data source. To make the data cells fill the table view we also change headers resize mode. | |
|
16 | ||
|
17 | \snippet ../examples/barmodelmapper/tablewidget.cpp 2 | |
|
18 | ||
|
19 | Now we need QChart instance to display the same data on the chart. | |
|
20 | We also enable animations. It makes it easier to see how modifying the model's data affect the chart. | |
|
21 | ||
|
22 | \snippet ../examples/barmodelmapper/tablewidget.cpp 3 | |
|
23 | ||
|
24 | First line of the code below creates new grouped bar series. Variables firstRow and rowCount are used to define a custom model mapping. | |
|
25 | 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. | |
|
26 | 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). | |
|
27 | To create a connection between the series and the model we set both of those objects to QVBarModelMapper. | |
|
28 | ||
|
29 | Finally the series is added to the chart. | |
|
30 | ||
|
31 | \snippet ../examples/barmodelmapper/tablewidget.cpp 4 | |
|
32 | ||
|
33 | To show in QTableView which data coresponds with which series this example uses table coloring. | |
|
34 | When series is added to the chart it is assigned a color beased on the currently selected theme. | |
|
35 | Code below extracts that color from the series and uses it to create colored QTableView. | |
|
36 | Coloring of the view is not a part of the QChart functionality. | |
|
37 | ||
|
38 | \snippet ../examples/barmodelmapper/tablewidget.cpp 5 | |
|
39 | ||
|
40 | We would like to have categories placed on the chart's axis that describe what the data means. | |
|
41 | Next snippet shows how to do that. | |
|
42 | ||
|
43 | \snippet ../examples/barmodelmapper/tablewidget.cpp 6 | |
|
44 | ||
|
45 | 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. | |
|
46 | To make the render look nicer Antialiasing is turned on and the minimum size of the chartView widget is set. | |
|
47 | ||
|
48 | \snippet ../examples/barmodelmapper/tablewidget.cpp 7 | |
|
49 | ||
|
50 | Finally we place both widgets in a layout and use the layout as the application layout. | |
|
51 | ||
|
52 | \snippet ../examples/barmodelmapper/tablewidget.cpp 8 | |
|
53 | ||
|
54 | Application is ready. Try modifying the data in the table view and see how it affects the chart. | |
|
55 | */ |
@@ -20,6 +20,7 | |||
|
20 | 20 | <li><a href="examples-legend.html">Legend</a></li> |
|
21 | 21 | <li><a href="examples-linechart.html">Line chart</a></li> |
|
22 | 22 | <li><a href="examples-modeldata.html">Model data</a></li> |
|
23 | <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li> | |
|
23 | 24 | <li><a href="examples-percentbarchart.html">Percent bar chart</a></li> |
|
24 | 25 | <li><a href="examples-piechart.html">Pie chart</a></li> |
|
25 | 26 | <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li> |
@@ -30,7 +30,7 CustomTableModel::CustomTableModel(QObject *parent) : | |||
|
30 | 30 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
31 | 31 | |
|
32 | 32 | m_columnCount = 6; |
|
33 |
m_rowCount = 1 |
|
|
33 | m_rowCount = 12; | |
|
34 | 34 | |
|
35 | 35 | // m_data |
|
36 | 36 | for (int i = 0; i < m_rowCount; i++) |
@@ -69,7 +69,7 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, | |||
|
69 | 69 | return QString("201%1").arg(section); |
|
70 | 70 | } |
|
71 | 71 | else |
|
72 | return QString("%1").arg(section); | |
|
72 | return QString("%1").arg(section + 1); | |
|
73 | 73 | } |
|
74 | 74 | |
|
75 | 75 | QVariant CustomTableModel::data(const QModelIndex & index, int role) const |
@@ -46,7 +46,7 TableWidget::TableWidget(QWidget *parent) | |||
|
46 | 46 | // create table view and add model to it |
|
47 | 47 | QTableView *tableView = new QTableView; |
|
48 | 48 | tableView->setModel(model); |
|
49 | tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); | |
|
49 | tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); | |
|
50 | 50 |
|
|
51 | 51 | //! [2] |
|
52 | 52 | |
@@ -71,11 +71,6 TableWidget::TableWidget(QWidget *parent) | |||
|
71 | 71 | chart->addSeries(series); |
|
72 | 72 | //! [4] |
|
73 | 73 | |
|
74 | QStringList categories; | |
|
75 | categories << "June" << "July" << "August" << "September" << "October" << "November"; | |
|
76 | ||
|
77 | chart->axisX()->categories()->insert(categories); | |
|
78 | ||
|
79 | 74 | //! [5] |
|
80 | 75 | // for storing color hex from the series |
|
81 | 76 | QString seriesColorHex = "#000000"; |
@@ -88,13 +83,20 TableWidget::TableWidget(QWidget *parent) | |||
|
88 | 83 | } |
|
89 | 84 | //! [5] |
|
90 | 85 | |
|
91 |
//! [ |
|
|
86 | //! [6] | |
|
87 | QStringList categories; | |
|
88 | categories << "April" << "May" << "June" << "July" << "August"; | |
|
89 | ||
|
90 | chart->axisX()->categories()->insert(categories); | |
|
91 | //! [6] | |
|
92 | ||
|
93 | //! [7] | |
|
92 | 94 | QChartView *chartView = new QChartView(chart); |
|
93 | 95 | chartView->setRenderHint(QPainter::Antialiasing); |
|
94 | 96 | chartView->setMinimumSize(640, 480); |
|
95 |
//! [ |
|
|
97 | //! [7] | |
|
96 | 98 | |
|
97 |
//! [ |
|
|
99 | //! [8] | |
|
98 | 100 | // create main layout |
|
99 | 101 | QGridLayout* mainLayout = new QGridLayout; |
|
100 | 102 | mainLayout->addWidget(tableView, 1, 0); |
@@ -102,5 +104,5 TableWidget::TableWidget(QWidget *parent) | |||
|
102 | 104 | mainLayout->setColumnStretch(1, 1); |
|
103 | 105 | mainLayout->setColumnStretch(0, 0); |
|
104 | 106 | setLayout(mainLayout); |
|
105 |
//! [ |
|
|
107 | //! [8] | |
|
106 | 108 | } |
@@ -126,6 +126,9 QAbstractSeries::SeriesType QAreaSeries::type() const | |||
|
126 | 126 | return QAbstractSeries::SeriesTypeArea; |
|
127 | 127 | } |
|
128 | 128 | |
|
129 | /*! | |
|
130 | Sets the \a series that is to be used as the area chart upper series. | |
|
131 | */ | |
|
129 | 132 | void QAreaSeries::setUpperSeries(QLineSeries* series) |
|
130 | 133 | { |
|
131 | 134 | Q_D(QAreaSeries); |
@@ -138,6 +141,9 QLineSeries* QAreaSeries::upperSeries() const | |||
|
138 | 141 | return d->m_upperSeries; |
|
139 | 142 | } |
|
140 | 143 | |
|
144 | /*! | |
|
145 | Sets the \a series that is to be used as the area chart lower series. | |
|
146 | */ | |
|
141 | 147 | void QAreaSeries::setLowerSeries(QLineSeries* series) |
|
142 | 148 | { |
|
143 | 149 | Q_D(QAreaSeries); |
General Comments 0
You need to be logged in to leave comments.
Login now