| 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 <QtWidgets/QGridLayout> |
| 32 | #include <QtWidgets/QTableView> |
| 33 | #include <QtCharts/QChart> |
| 34 | #include <QtCharts/QChartView> |
| 35 | #include <QtCharts/QLineSeries> |
| 36 | #include <QtCharts/QVXYModelMapper> |
| 37 | #include <QtCharts/QBarSeries> |
| 38 | #include <QtCharts/QBarSet> |
| 39 | #include <QtCharts/QVBarModelMapper> |
| 40 | #include <QtWidgets/QHeaderView> |
| 41 | #include <QtCharts/QBarCategoryAxis> |
| 42 | #include <QtCharts/QValueAxis> |
| 43 | |
| 44 | QT_CHARTS_USE_NAMESPACE |
| 45 | |
| 46 | TableWidget::TableWidget(QWidget *parent) |
| 47 | : QWidget(parent) |
| 48 | { |
| 49 | // create simple model for storing data |
| 50 | // user's table data model |
| 51 | //! [1] |
| 52 | m_model = new CustomTableModel; |
| 53 | //! [1] |
| 54 | |
| 55 | //! [2] |
| 56 | // create table view and add model to it |
| 57 | QTableView *tableView = new QTableView; |
| 58 | tableView->setModel(m_model); |
| 59 | tableView->setMinimumWidth(300); |
| 60 | tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
| 61 | tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
| 62 | m_model->setParent(tableView); |
| 63 | //! [2] |
| 64 | |
| 65 | //! [3] |
| 66 | QChart *chart = new QChart; |
| 67 | chart->setAnimationOptions(QChart::AllAnimations); |
| 68 | //! [3] |
| 69 | |
| 70 | // series 1 |
| 71 | //! [4] |
| 72 | QBarSeries *series = new QBarSeries; |
| 73 | |
| 74 | int first = 3; |
| 75 | int count = 5; |
| 76 | QVBarModelMapper *mapper = new QVBarModelMapper(this); |
| 77 | mapper->setFirstBarSetColumn(1); |
| 78 | mapper->setLastBarSetColumn(4); |
| 79 | mapper->setFirstRow(first); |
| 80 | mapper->setRowCount(count); |
| 81 | mapper->setSeries(series); |
| 82 | mapper->setModel(m_model); |
| 83 | chart->addSeries(series); |
| 84 | //! [4] |
| 85 | |
| 86 | //! [5] |
| 87 | // for storing color hex from the series |
| 88 | QString seriesColorHex = "#000000" ; |
| 89 | |
| 90 | // get the color of the series and use it for showing the mapped area |
| 91 | QList<QBarSet *> barsets = series->barSets(); |
| 92 | for (int i = 0; i < barsets.count(); i++) { |
| 93 | seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), base: 16).right(n: 6).toUpper(); |
| 94 | m_model->addMapping(color: seriesColorHex, area: QRect(1 + i, first, 1, barsets.at(i)->count())); |
| 95 | } |
| 96 | //! [5] |
| 97 | |
| 98 | //! [6] |
| 99 | QStringList categories; |
| 100 | categories << "April" << "May" << "June" << "July" << "August" ; |
| 101 | QBarCategoryAxis *axisX = new QBarCategoryAxis(); |
| 102 | axisX->append(categories); |
| 103 | chart->addAxis(axis: axisX, alignment: Qt::AlignBottom); |
| 104 | series->attachAxis(axis: axisX); |
| 105 | QValueAxis *axisY = new QValueAxis(); |
| 106 | chart->addAxis(axis: axisY, alignment: Qt::AlignLeft); |
| 107 | series->attachAxis(axis: axisY); |
| 108 | //! [6] |
| 109 | |
| 110 | //! [7] |
| 111 | QChartView *chartView = new QChartView(chart); |
| 112 | chartView->setRenderHint(hint: QPainter::Antialiasing); |
| 113 | chartView->setMinimumSize(minw: 640, minh: 480); |
| 114 | //! [7] |
| 115 | |
| 116 | //! [8] |
| 117 | // create main layout |
| 118 | QGridLayout *mainLayout = new QGridLayout; |
| 119 | mainLayout->addWidget(tableView, row: 1, column: 0); |
| 120 | mainLayout->addWidget(chartView, row: 1, column: 1); |
| 121 | mainLayout->setColumnStretch(column: 1, stretch: 1); |
| 122 | mainLayout->setColumnStretch(column: 0, stretch: 0); |
| 123 | setLayout(mainLayout); |
| 124 | //! [8] |
| 125 | } |
| 126 | |