1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include "tablewidget.h"
31#include "customtablemodel.h"
32#include <QtWidgets/QGridLayout>
33#include <QtWidgets/QTableView>
34#include <QtCharts/QChart>
35#include <QtCharts/QChartView>
36#include <QtCharts/QLineSeries>
37#include <QtCharts/QVXYModelMapper>
38#include <QtWidgets/QHeaderView>
39
40QT_CHARTS_USE_NAMESPACE
41
42TableWidget::TableWidget(QWidget *parent)
43 : QWidget(parent)
44{
45 // create simple model for storing data
46 // user's table data model
47 //! [1]
48 CustomTableModel *model = new CustomTableModel;
49 //! [1]
50
51 //! [2]
52 // create table view and add model to it
53 QTableView *tableView = new QTableView;
54 tableView->setModel(model);
55 tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
56 tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
57 //! [2]
58
59 //! [3]
60 QChart *chart = new QChart;
61 chart->setAnimationOptions(QChart::AllAnimations);
62 //! [3]
63
64 // series 1
65 //! [4]
66 QLineSeries *series = new QLineSeries;
67 series->setName("Line 1");
68 QVXYModelMapper *mapper = new QVXYModelMapper(this);
69 mapper->setXColumn(0);
70 mapper->setYColumn(1);
71 mapper->setSeries(series);
72 mapper->setModel(model);
73 chart->addSeries(series);
74 //! [4]
75
76 //! [5]
77 // for storing color hex from the series
78 QString seriesColorHex = "#000000";
79
80 // get the color of the series and use it for showing the mapped area
81 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), base: 16).right(n: 6).toUpper();
82 model->addMapping(color: seriesColorHex, area: QRect(0, 0, 2, model->rowCount()));
83 //! [5]
84
85
86 // series 2
87 //! [6]
88 series = new QLineSeries;
89 series->setName("Line 2");
90
91 mapper = new QVXYModelMapper(this);
92 mapper->setXColumn(2);
93 mapper->setYColumn(3);
94 mapper->setSeries(series);
95 mapper->setModel(model);
96 chart->addSeries(series);
97 //! [6]
98
99 //! [7]
100 // get the color of the series and use it for showing the mapped area
101 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), base: 16).right(n: 6).toUpper();
102 model->addMapping(color: seriesColorHex, area: QRect(2, 0, 2, model->rowCount()));
103 //! [7]
104
105 //! [8]
106 chart->createDefaultAxes();
107 QChartView *chartView = new QChartView(chart);
108 chartView->setRenderHint(hint: QPainter::Antialiasing);
109 chartView->setMinimumSize(minw: 640, minh: 480);
110 //! [8]
111
112 //! [9]
113 // create main layout
114 QGridLayout *mainLayout = new QGridLayout;
115 mainLayout->addWidget(tableView, row: 1, column: 0);
116 mainLayout->addWidget(chartView, row: 1, column: 1);
117 mainLayout->setColumnStretch(column: 1, stretch: 1);
118 mainLayout->setColumnStretch(column: 0, stretch: 0);
119 setLayout(mainLayout);
120 //! [9]
121}
122

source code of qtcharts/examples/charts/modeldata/tablewidget.cpp