##// END OF EJS Templates
Initial commit for gdpBarChart example.
Marek Rosa -
r205:a2592bf5395a
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
@@ -0,0 +1,23
1 #-------------------------------------------------
2 #
3 # Project created by QtCreator 2012-02-14T15:27:32
4 #
5 #-------------------------------------------------
6
7 !include( ../../common.pri ) {
8 error( "Couldn't find the common.pri file!" )
9 }
10 !include( ../../integrated.pri ) {
11 error( "Couldn't find the integrated.pri file !")
12 }
13
14 QT += core gui sql
15
16 TARGET = gdpbarchart
17 TEMPLATE = app
18
19
20 SOURCES += main.cpp\
21 widget.cpp
22
23 HEADERS += widget.h
@@ -0,0 +1,11
1 #include <QtGui/QApplication>
2 #include "widget.h"
3
4 int main(int argc, char *argv[])
5 {
6 QApplication a(argc, argv);
7 Widget w;
8 w.show();
9
10 return a.exec();
11 }
@@ -0,0 +1,147
1 #include "widget.h"
2 #include <QGridLayout>
3 #include <QPushButton>
4 #include <QLabel>
5
6 #include <QSqlQuery>
7 #include <qscatterseries.h>
8 #include <qchartview.h>
9 #include <qchartaxis.h>
10 #include <barchartseries.h>
11 #include <qbarcategory.h>
12 #include <qbarset.h>
13 #include <QListWidget>
14
15 QTCOMMERCIALCHART_USE_NAMESPACE
16
17 Widget::Widget(QWidget *parent)
18 : QWidget(parent)
19 {
20 setGeometry(100, 100, 800, 600);
21
22 // right panel layout
23 countrieslist = new QListWidget;
24 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
25
26 yearslist = new QListWidget;
27 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
28 for (int i = 1990; i < 2011; i++)
29 yearslist->addItem(QString("%1").arg(i));
30
31 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
32 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
33
34 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
35 rightPanelLayout->addWidget(countrieslist);
36 rightPanelLayout->addWidget(yearslist);
37 rightPanelLayout->addWidget(refreshButton);
38 rightPanelLayout->setStretch(0, 1);
39 rightPanelLayout->setStretch(1, 0);
40
41 // main layout
42 chartArea = new QChartView(this);
43 chartArea->setChartTitle("GDP by country");
44 QGridLayout* mainLayout = new QGridLayout;
45 mainLayout->addWidget(chartArea, 0, 0);
46 mainLayout->addLayout(rightPanelLayout, 0, 1);
47 mainLayout->setColumnStretch(0,1);
48 setLayout(mainLayout);
49
50 // connect to the database
51 db = QSqlDatabase::addDatabase("QSQLITE");
52 db.setDatabaseName("gdpData");
53 if(!db.open())
54 {
55 qDebug() << "could not open database. SQLite db file missing (?)";
56 return;
57 }
58
59 // get the list of all countires and regions.
60 QSqlQuery query;
61 query.exec("SELECT DISTINCT country FROM gdp2");
62
63 // add the countries to the country filter
64 while (query.next()) {
65 countrieslist->addItem(query.value(0).toString());
66 }
67
68 // hide axis X labels
69 QChartAxis newAxis = chartArea->defaultAxisX();
70 newAxis.setLabelsVisible(false);
71 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
72 chartArea->setDefaultAxisX(newAxis);
73 }
74
75 Widget::~Widget()
76 {
77 //
78 db.close();
79 }
80
81 void Widget::refreshChart()
82 {
83 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
84 QStringList selectedCountriesStrings;
85 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
86 for (int i = 0; i < selectedCountriesItems.size(); i++)
87 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
88 selectedCountriesStrings.sort();
89
90 // use the sorted selected coutries list to initialize BarCategory
91 QBarCategory* category = new QBarCategory;
92 for (int i = 0; i < selectedCountriesStrings.size(); i++)
93 *category << selectedCountriesStrings[i];
94 BarChartSeries* series0 = new BarChartSeries(category);
95
96 // prepare the selected counries SQL query
97 QString countriesQuery = "country IN (";
98 for (int i = 0; i < selectedCountriesStrings.size(); i++)
99 {
100 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
101 if ( i < selectedCountriesStrings.size() - 1)
102 countriesQuery.append(",");
103 else
104 countriesQuery.append(")");
105 }
106
107 QSqlQuery query;
108 // selected years items list is not sorted. copy the values to QList<Integer> and sort them.
109 QList<int> selectedYearsInts;
110 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
111 for (int i = 0; i < selectedYearsItems.size(); i++)
112 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
113 qSort(selectedYearsInts);
114
115 // perform a query for each selected year
116 for (int i = 0; i < selectedYearsInts.size(); i++)
117 {
118 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
119 QBarSet* barSet = new QBarSet;
120 // while (query.next()) {
121 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
122 // }
123 query.first();
124
125 // the data for some of the coutries for some years might be missing.
126 // QBarChart needs bars to have same size
127 for (int k = 0; k < selectedCountriesStrings.size(); k++)
128 {
129 if (selectedCountriesStrings[k] == query.value(0).toString())
130 {
131 *barSet << query.value(1).toReal();
132 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
133 query.next();
134 }
135 else
136 {
137 // data missing, put 0
138 *barSet << 0.0f;
139 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]);
140 }
141 }
142 series0->addBarSet(barSet);
143 }
144
145 // add the serie to the chart
146 chartArea->addSeries(series0);
147 }
@@ -0,0 +1,30
1 #ifndef WIDGET_H
2 #define WIDGET_H
3
4 #include <QtGui/QWidget>
5 #include <qchartview.h>
6 #include <QSqlDatabase>
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
10 class QListWidget;
11
12 class Widget : public QWidget
13 {
14 Q_OBJECT
15
16 public:
17 Widget(QWidget *parent = 0);
18 ~Widget();
19
20 public slots:
21 void refreshChart();
22
23 private:
24 QChartView* chartArea;
25 QListWidget* countrieslist;
26 QListWidget* yearslist;
27 QSqlDatabase db;
28 };
29
30 #endif // WIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now