##// END OF EJS Templates
Added barmodelmapper example
Marek Rosa -
r1367:5e4ada19cdb6
parent child
Show More
@@ -0,0 +1,18
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 QT += core gui
6
7 TARGET = barmodelmapper
8 TEMPLATE = app
9
10
11 SOURCES += main.cpp\
12 tablewidget.cpp \
13 customtablemodel.cpp
14
15 HEADERS += tablewidget.h \
16 customtablemodel.h
17
18 !system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_EXAMPLES_BIN_DIR"
@@ -0,0 +1,117
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "customtablemodel.h"
22 #include <QVector>
23 #include <QTime>
24 #include <QRect>
25 #include <QColor>
26
27 CustomTableModel::CustomTableModel(QObject *parent) :
28 QAbstractTableModel(parent)
29 {
30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
31
32 m_columnCount = 6;
33 m_rowCount = 10;
34
35 // m_data
36 for (int i = 0; i < m_rowCount; i++)
37 {
38 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
39 for (int k = 0; k < dataVec->size(); k++)
40 {
41 if (k%2 == 0)
42 dataVec->replace(k, i * 50 + qrand()%20);
43 else
44 dataVec->replace(k, qrand()%100);
45 }
46 m_data.append(dataVec);
47 }
48 }
49
50 int CustomTableModel::rowCount(const QModelIndex & parent) const
51 {
52 Q_UNUSED(parent)
53 return m_data.count();
54 }
55
56 int CustomTableModel::columnCount(const QModelIndex & parent) const
57 {
58 Q_UNUSED(parent)
59 return m_columnCount;
60 }
61
62 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
63 {
64 if (role != Qt::DisplayRole)
65 return QVariant();
66
67 if (orientation == Qt::Horizontal)
68 {
69 return QString("201%1").arg(section);
70 }
71 else
72 return QString("%1").arg(section);
73 }
74
75 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
76 {
77 if (role == Qt::DisplayRole)
78 {
79 return m_data[index.row()]->at(index.column());
80 }
81 else if (role == Qt::EditRole)
82 {
83 return m_data[index.row()]->at(index.column());
84 }
85 else if (role == Qt::BackgroundRole)
86 {
87 QRect rect;
88 foreach(rect, m_mapping)
89 if(rect.contains(index.column(), index.row()))
90 return QColor(m_mapping.key(rect));
91
92 // cell not mapped return white color
93 return QColor(Qt::white);
94 }
95 return QVariant();
96 }
97
98 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
99 {
100 if (index.isValid() && role == Qt::EditRole)
101 {
102 m_data[index.row()]->replace(index.column(), value.toDouble());
103 emit dataChanged(index, index);
104 return true;
105 }
106 return false;
107 }
108
109 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
110 {
111 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
112 }
113
114 void CustomTableModel::addMapping(QString color, QRect area)
115 {
116 m_mapping.insertMulti(color, area);
117 }
@@ -0,0 +1,51
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef XYPOINTSMODEL_H
22 #define XYPOINTSMODEL_H
23
24 #include <QAbstractTableModel>
25 #include <QHash>
26 #include <QRect>
27
28 class CustomTableModel : public QAbstractTableModel
29 {
30 Q_OBJECT
31 public:
32 explicit CustomTableModel(QObject *parent = 0);
33
34 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
35 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
36 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
37 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
38 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
39 Qt::ItemFlags flags ( const QModelIndex & index ) const;
40
41 void addMapping(QString color, QRect area);
42 void clearMapping() { m_mapping.clear(); }
43
44 private:
45 QList<QVector<qreal> * > m_data;
46 QHash<QString, QRect> m_mapping;
47 int m_columnCount;
48 int m_rowCount;
49 };
50
51 #endif // XYPOINTSMODEL_H
@@ -0,0 +1,31
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include <QtGui/QApplication>
22 #include "tablewidget.h"
23
24 int main(int argc, char *argv[])
25 {
26 QApplication a(argc, argv);
27 TableWidget w;
28 w.show();
29
30 return a.exec();
31 }
@@ -0,0 +1,106
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "tablewidget.h"
22 #include "customtablemodel.h"
23 #include <QGridLayout>
24 #include <QTableView>
25 #include <QChart>
26 #include <QChartView>
27 #include <QLineSeries>
28 #include <QVXYModelMapper>
29 #include <QGroupedBarSeries>
30 #include <QBarSet>
31 #include <QVBarModelMapper>
32 #include <QHeaderView>
33
34 QTCOMMERCIALCHART_USE_NAMESPACE
35
36 TableWidget::TableWidget(QWidget *parent)
37 : QWidget(parent)
38 {
39 // create simple model for storing data
40 // user's table data model
41 //! [1]
42 CustomTableModel *model = new CustomTableModel;
43 //! [1]
44
45 //! [2]
46 // create table view and add model to it
47 QTableView *tableView = new QTableView;
48 tableView->setModel(model);
49 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
50 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
51 //! [2]
52
53 //! [3]
54 QChart *chart = new QChart;
55 chart->setAnimationOptions(QChart::AllAnimations);
56 //! [3]
57
58 // series 1
59 //! [4]
60 QGroupedBarSeries *series = new QGroupedBarSeries;
61
62 int first = 3;
63 int count = 5;
64 QVBarModelMapper *mapper = new QVBarModelMapper(this);
65 mapper->setFirstBarSetColumn(1);
66 mapper->setLastBarSetColumn(4);
67 mapper->setFirst(3);
68 mapper->setCount(count);
69 mapper->setSeries(series);
70 mapper->setModel(model);
71 chart->addSeries(series);
72 //! [4]
73
74 QStringList categories;
75 categories << "June" << "July" << "August" << "September" << "October" << "November";
76
77 chart->axisX()->categories()->insert(categories);
78
79 //! [5]
80 // for storing color hex from the series
81 QString seriesColorHex = "#000000";
82
83 // get the color of the series and use it for showing the mapped area
84 QList<QBarSet*> barsets = series->barSets();
85 for (int i = 0; i < barsets.count(); i++) {
86 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
87 model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count()));
88 }
89 //! [5]
90
91 //! [8]
92 QChartView *chartView = new QChartView(chart);
93 chartView->setRenderHint(QPainter::Antialiasing);
94 chartView->setMinimumSize(640, 480);
95 //! [8]
96
97 //! [9]
98 // create main layout
99 QGridLayout* mainLayout = new QGridLayout;
100 mainLayout->addWidget(tableView, 1, 0);
101 mainLayout->addWidget(chartView, 1, 1);
102 mainLayout->setColumnStretch(1, 1);
103 mainLayout->setColumnStretch(0, 0);
104 setLayout(mainLayout);
105 //! [9]
106 }
@@ -0,0 +1,34
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef TABLEWIDGET_H
22 #define TABLEWIDGET_H
23
24 #include <QtGui/QWidget>
25
26 class TableWidget : public QWidget
27 {
28 Q_OBJECT
29
30 public:
31 TableWidget(QWidget *parent = 0);
32 };
33
34 #endif // TABLEWIDGET_H
@@ -1,24 +1,25
1 CURRENTLY_BUILDING_COMPONENTS = "examples"
1 CURRENTLY_BUILDING_COMPONENTS = "examples"
2 !include( ../config.pri ) {
2 !include( ../config.pri ) {
3 error( "Couldn't find the config.pri file!" )
3 error( "Couldn't find the config.pri file!" )
4 }
4 }
5
5
6 TEMPLATE = subdirs
6 TEMPLATE = subdirs
7 SUBDIRS += \
7 SUBDIRS += \
8 areachart \
8 areachart \
9 barchart \
9 barchart \
10 customchart \
10 customchart \
11 linechart \
11 linechart \
12 percentbarchart \
12 percentbarchart \
13 piechart \
13 piechart \
14 piechartdrilldown \
14 piechartdrilldown \
15 presenterchart \
15 presenterchart \
16 scatterchart \
16 scatterchart \
17 scatterinteractions \
17 scatterinteractions \
18 splinechart \
18 splinechart \
19 stackedbarchart \
19 stackedbarchart \
20 stackedbarchartdrilldown \
20 stackedbarchartdrilldown \
21 zoomlinechart \
21 zoomlinechart \
22 modeldata \
22 modeldata \
23 groupedbarchart \
23 groupedbarchart \
24 legend
24 legend \
25 barmodelmapper
@@ -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 <QHeaderView>
29 #include <QHeaderView>
30
30
31 QTCOMMERCIALCHART_USE_NAMESPACE
31 QTCOMMERCIALCHART_USE_NAMESPACE
32
32
33 TableWidget::TableWidget(QWidget *parent)
33 TableWidget::TableWidget(QWidget *parent)
34 : QWidget(parent)
34 : QWidget(parent)
35 {
35 {
36 // create simple model for storing data
36 // create simple model for storing data
37 // user's table data model
37 // user's table data model
38 //! [1]
38 //! [1]
39 CustomTableModel *model = new CustomTableModel;
39 CustomTableModel *model = new CustomTableModel;
40 //! [1]
40 //! [1]
41
41
42 //! [2]
42 //! [2]
43 // create table view and add model to it
43 // create table view and add model to it
44 QTableView *tableView = new QTableView;
44 QTableView *tableView = new QTableView;
45 tableView->setModel(model);
45 tableView->setModel(model);
46 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
46 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
47 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
47 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
48 //! [2]
48 //! [2]
49
49
50 //! [3]
50 //! [3]
51 QChart *chart = new QChart;
51 QChart *chart = new QChart;
52 chart->setAnimationOptions(QChart::AllAnimations);
52 chart->setAnimationOptions(QChart::AllAnimations);
53 //! [3]
53 //! [3]
54
54
55 // series 1
55 // series 1
56 //! [4]
56 //! [4]
57 QLineSeries *series = new QLineSeries;
57 QLineSeries *series = new QLineSeries;
58 QVXYModelMapper *mapper = new QVXYModelMapper(this);
58 QVXYModelMapper *mapper = new QVXYModelMapper(this);
59 mapper->setXColumn(0);
59 mapper->setXColumn(0);
60 mapper->setYColumn(1);
60 mapper->setYColumn(1);
61 mapper->setSeries(series);
61 mapper->setSeries(series);
62 mapper->setModel(model);
62 mapper->setModel(model);
63 chart->addSeries(series);
63 chart->addSeries(series);
64 //! [4]
64 //! [4]
65
65
66 //! [5]
66 //! [5]
67 // for storing color hex from the series
67 // for storing color hex from the series
68 QString seriesColorHex = "#000000";
68 QString seriesColorHex = "#000000";
69
69
70 // get the color of the series and use it for showing the mapped area
70 // get the color of the series and use it for showing the mapped area
71 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
71 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
72 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
72 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
73 //! [5]
73 //! [5]
74
74
75
75
76 // series 2
76 // series 2
77 //! [6]
77 //! [6]
78 series = new QLineSeries;
78 series = new QLineSeries;
79
79
80 mapper = new QVXYModelMapper(this);
80 mapper = new QVXYModelMapper(this);
81 mapper->setXColumn(2);
81 mapper->setXColumn(2);
82 mapper->setYColumn(3);
82 mapper->setYColumn(3);
83 mapper->setSeries(series);
83 mapper->setSeries(series);
84 mapper->setModel(model);
84 mapper->setModel(model);
85 chart->addSeries(series);
85 chart->addSeries(series);
86 //! [6]
86 //! [6]
87
87
88 //! [7]
88 //! [7]
89 // get the color of the series and use it for showing the mapped area
89 // get the color of the series and use it for showing the mapped area
90 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
90 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
91 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
91 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
92 //! [7]
92 //! [7]
93
93
94 //! [8]
94 //! [8]
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 //! [8]
98 //! [8]
99
99
100 //! [9]
100 //! [9]
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 //! [9]
108 //! [9]
109 }
109 }
General Comments 0
You need to be logged in to leave comments. Login now