##// END OF EJS Templates
gdpbarchart - changed two includes to .h version, because it did not compile
Marek Rosa -
r834:8388bfb5739d
parent child
Show More
@@ -1,247 +1,248
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 "widget.h"
21 #include "widget.h"
22
22
23 #include <QChart>
23 #include <QChart>
24 #include <QScatterSeries>
24 #include <QScatterSeries>
25 #include <QChartAxis>
25 #include <QChartAxis>
26 #include <QBarset>
26 //#include <QBarset>
27 #include <qbarset.h>
27 #include <QBarSeries>
28 #include <QBarSeries>
28
29
29 #include <QGridLayout>
30 #include <QGridLayout>
30 #include <QPushButton>
31 #include <QPushButton>
31 #include <QLabel>
32 #include <QLabel>
32 #include <QListWidget>
33 #include <QListWidget>
33 #include <QPrinter>
34 #include <QPrinter>
34 #include <QPrintDialog>
35 #include <QPrintDialog>
35 #include <QRadioButton>
36 #include <QRadioButton>
36 #include <QStringList>
37 #include <QStringList>
37 #include <QSqlQuery>
38 #include <QSqlQuery>
38
39
39 QTCOMMERCIALCHART_USE_NAMESPACE
40 QTCOMMERCIALCHART_USE_NAMESPACE
40
41
41 Widget::Widget(QWidget *parent)
42 Widget::Widget(QWidget *parent)
42 : QWidget(parent)
43 : QWidget(parent)
43 {
44 {
44 setGeometry(100, 100, 1000, 600);
45 setGeometry(100, 100, 1000, 600);
45
46
46 // right panel layout
47 // right panel layout
47 barChartRadioButton = new QRadioButton(tr("Bar chart"));
48 barChartRadioButton = new QRadioButton(tr("Bar chart"));
48 barChartRadioButton->setChecked(true);
49 barChartRadioButton->setChecked(true);
49 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
50 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
50 scatterChartRadioButton->setChecked(false);
51 scatterChartRadioButton->setChecked(false);
51 countrieslist = new QListWidget;
52 countrieslist = new QListWidget;
52 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
53 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
53
54
54 //list of years widget
55 //list of years widget
55 yearslist = new QListWidget;
56 yearslist = new QListWidget;
56 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
57 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
57 for (int i = 1990; i < 2011; i++)
58 for (int i = 1990; i < 2011; i++)
58 yearslist->addItem(QString("%1").arg(i));
59 yearslist->addItem(QString("%1").arg(i));
59
60
60 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
61 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
61 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
62 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
62
63
63 QPushButton* printButton = new QPushButton(tr("Print chart"));
64 QPushButton* printButton = new QPushButton(tr("Print chart"));
64 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
65 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
65
66
66 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
67 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
67 rightPanelLayout->addWidget(barChartRadioButton);
68 rightPanelLayout->addWidget(barChartRadioButton);
68 rightPanelLayout->addWidget(scatterChartRadioButton);
69 rightPanelLayout->addWidget(scatterChartRadioButton);
69 rightPanelLayout->addWidget(countrieslist);
70 rightPanelLayout->addWidget(countrieslist);
70 rightPanelLayout->addWidget(yearslist);
71 rightPanelLayout->addWidget(yearslist);
71 rightPanelLayout->addWidget(refreshButton);
72 rightPanelLayout->addWidget(refreshButton);
72 rightPanelLayout->addWidget(printButton);
73 rightPanelLayout->addWidget(printButton);
73 rightPanelLayout->setStretch(0, 1);
74 rightPanelLayout->setStretch(0, 1);
74 rightPanelLayout->setStretch(1, 0);
75 rightPanelLayout->setStretch(1, 0);
75
76
76 QChart *chart = new QChart();
77 QChart *chart = new QChart();
77 chart->setTitle("GDP by country");
78 chart->setTitle("GDP by country");
78
79
79 // main layout
80 // main layout
80 chartView = new QChartView(chart);
81 chartView = new QChartView(chart);
81 QGridLayout* mainLayout = new QGridLayout;
82 QGridLayout* mainLayout = new QGridLayout;
82 mainLayout->addWidget(chartView, 0, 0);
83 mainLayout->addWidget(chartView, 0, 0);
83 mainLayout->addLayout(rightPanelLayout, 0, 1);
84 mainLayout->addLayout(rightPanelLayout, 0, 1);
84 mainLayout->setColumnStretch(0,1);
85 mainLayout->setColumnStretch(0,1);
85 setLayout(mainLayout);
86 setLayout(mainLayout);
86
87
87 // connect to the database
88 // connect to the database
88 db = QSqlDatabase::addDatabase("QSQLITE");
89 db = QSqlDatabase::addDatabase("QSQLITE");
89 db.setDatabaseName("gdpData");
90 db.setDatabaseName("gdpData");
90 if(!db.open())
91 if(!db.open())
91 {
92 {
92 qDebug() << "could not open database. SQLite db file missing (?)";
93 qDebug() << "could not open database. SQLite db file missing (?)";
93 return;
94 return;
94 }
95 }
95
96
96 // get the list of all countires and regions.
97 // get the list of all countires and regions.
97 QSqlQuery query;
98 QSqlQuery query;
98 query.exec("SELECT DISTINCT country FROM gdp2");
99 query.exec("SELECT DISTINCT country FROM gdp2");
99
100
100 // add the countries to the country filter
101 // add the countries to the country filter
101 while (query.next()) {
102 while (query.next()) {
102 countrieslist->addItem(query.value(0).toString());
103 countrieslist->addItem(query.value(0).toString());
103 }
104 }
104
105
105 // hide axis X labels
106 // hide axis X labels
106 //QChartAxis* axis = chartArea->axisX();
107 //QChartAxis* axis = chartArea->axisX();
107 // axis->
108 // axis->
108 // axis->setLabelsVisible(false);
109 // axis->setLabelsVisible(false);
109 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
110 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
110
111
111 }
112 }
112
113
113 Widget::~Widget()
114 Widget::~Widget()
114 {
115 {
115 //
116 //
116 db.close();
117 db.close();
117 }
118 }
118
119
119 /*!
120 /*!
120 refreshes the chart
121 refreshes the chart
121 */
122 */
122 void Widget::refreshChart()
123 void Widget::refreshChart()
123 {
124 {
124 chartView->chart()->removeAllSeries();
125 chartView->chart()->removeAllSeries();
125
126
126 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
127 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
127 QStringList selectedCountriesStrings;
128 QStringList selectedCountriesStrings;
128 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
129 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
129 for (int i = 0; i < selectedCountriesItems.size(); i++)
130 for (int i = 0; i < selectedCountriesItems.size(); i++)
130 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
131 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
131 selectedCountriesStrings.sort();
132 selectedCountriesStrings.sort();
132
133
133 QSqlQuery query;
134 QSqlQuery query;
134 // selected years items list is not sorted. copy the values to QList<int> and sort them.
135 // selected years items list is not sorted. copy the values to QList<int> and sort them.
135 QList<int> selectedYearsInts;
136 QList<int> selectedYearsInts;
136 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
137 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
137 for (int i = 0; i < selectedYearsItems.size(); i++)
138 for (int i = 0; i < selectedYearsItems.size(); i++)
138 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
139 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
139 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
140 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
140
141
141 if (barChartRadioButton->isChecked())
142 if (barChartRadioButton->isChecked())
142 {
143 {
143 // use the sorted selected coutries list to initialize BarCategory
144 // use the sorted selected coutries list to initialize BarCategory
144 QStringList category;
145 QStringList category;
145 for (int i = 0; i < selectedCountriesStrings.size(); i++)
146 for (int i = 0; i < selectedCountriesStrings.size(); i++)
146 category << selectedCountriesStrings[i];
147 category << selectedCountriesStrings[i];
147 QBarSeries* series0 = new QBarSeries(category);
148 QBarSeries* series0 = new QBarSeries(category);
148 series0 = new QBarSeries(category);
149 series0 = new QBarSeries(category);
149
150
150 // prepare the selected counries SQL query
151 // prepare the selected counries SQL query
151 QString countriesQuery = "country IN (";
152 QString countriesQuery = "country IN (";
152 for (int i = 0; i < selectedCountriesStrings.size(); i++)
153 for (int i = 0; i < selectedCountriesStrings.size(); i++)
153 {
154 {
154 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
155 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
155 if ( i < selectedCountriesStrings.size() - 1)
156 if ( i < selectedCountriesStrings.size() - 1)
156 countriesQuery.append(",");
157 countriesQuery.append(",");
157 else
158 else
158 countriesQuery.append(")");
159 countriesQuery.append(")");
159 }
160 }
160
161
161 // perform a query for each selected year
162 // perform a query for each selected year
162 for (int i = 0; i < selectedYearsInts.size(); i++)
163 for (int i = 0; i < selectedYearsInts.size(); i++)
163 {
164 {
164 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
165 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
165 QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
166 QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
166
167
167 // while (query.next()) {
168 // while (query.next()) {
168 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
169 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
169 // }
170 // }
170 query.first();
171 query.first();
171
172
172 // the data for some of the coutries for some years might be missing.
173 // the data for some of the coutries for some years might be missing.
173 // QBarChart needs bars to have same size
174 // QBarChart needs bars to have same size
174 for (int k = 0; k < selectedCountriesStrings.size(); k++)
175 for (int k = 0; k < selectedCountriesStrings.size(); k++)
175 {
176 {
176 if (selectedCountriesStrings[k] == query.value(0).toString())
177 if (selectedCountriesStrings[k] == query.value(0).toString())
177 {
178 {
178 *barSet << query.value(1).toReal();
179 *barSet << query.value(1).toReal();
179 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
180 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
180 query.next();
181 query.next();
181 }
182 }
182 else
183 else
183 {
184 {
184 // data missing, put 0
185 // data missing, put 0
185 *barSet << 0.0f;
186 *barSet << 0.0f;
186 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
187 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
187 }
188 }
188 }
189 }
189 series0->appendBarSet(barSet);
190 series0->appendBarSet(barSet);
190 }
191 }
191 // add the serie to the chart
192 // add the serie to the chart
192 chartView->chart()->addSeries(series0);
193 chartView->chart()->addSeries(series0);
193 }
194 }
194 else if (scatterChartRadioButton->isChecked())
195 else if (scatterChartRadioButton->isChecked())
195 {
196 {
196 QString yearsQuery = "year IN (";
197 QString yearsQuery = "year IN (";
197 for (int i = 0; i < selectedYearsInts.size(); i++)
198 for (int i = 0; i < selectedYearsInts.size(); i++)
198 {
199 {
199 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
200 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
200 if ( i < selectedYearsInts.size() - 1)
201 if ( i < selectedYearsInts.size() - 1)
201 yearsQuery.append(",");
202 yearsQuery.append(",");
202 else
203 else
203 yearsQuery.append(")");
204 yearsQuery.append(")");
204 }
205 }
205
206
206 // perform a query for each selected country
207 // perform a query for each selected country
207 for (int i = 0; i < selectedCountriesStrings.size(); i++)
208 for (int i = 0; i < selectedCountriesStrings.size(); i++)
208 {
209 {
209 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
210 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
210 query.first();
211 query.first();
211
212
212 QScatterSeries* series = new QScatterSeries;
213 QScatterSeries* series = new QScatterSeries;
213 // the data for some of the coutries for some years might be missing.
214 // the data for some of the coutries for some years might be missing.
214 for (int k = 0; k < selectedYearsInts.size(); k++)
215 for (int k = 0; k < selectedYearsInts.size(); k++)
215 {
216 {
216 if (selectedYearsInts[k] == query.value(0).toInt())
217 if (selectedYearsInts[k] == query.value(0).toInt())
217 {
218 {
218 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
219 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
219 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
220 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
220 query.next();
221 query.next();
221 }
222 }
222 else
223 else
223 {
224 {
224 // data missing, put 0
225 // data missing, put 0
225 *series << QPointF(selectedYearsInts[k] , 0.0f);
226 *series << QPointF(selectedYearsInts[k] , 0.0f);
226 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
227 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
227 }
228 }
228 }
229 }
229 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
230 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
230 chartView->chart()->addSeries(series);
231 chartView->chart()->addSeries(series);
231 }
232 }
232 chartView->chart()->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
233 chartView->chart()->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
233 }
234 }
234 }
235 }
235
236
236 void Widget::printChart()
237 void Widget::printChart()
237 {
238 {
238 QPrinter printer;
239 QPrinter printer;
239 // QPrinter printer(QPrinter::HighResolution);
240 // QPrinter printer(QPrinter::HighResolution);
240 printer.setOutputFormat(QPrinter::PdfFormat);
241 printer.setOutputFormat(QPrinter::PdfFormat);
241 printer.setOrientation(QPrinter::Landscape);
242 printer.setOrientation(QPrinter::Landscape);
242 printer.setOutputFileName("print.pdf");
243 printer.setOutputFileName("print.pdf");
243
244
244 QPainter painter;
245 QPainter painter;
245 painter.begin(&printer);
246 painter.begin(&printer);
246 chartView->render(&painter);
247 chartView->render(&painter);
247 }
248 }
@@ -1,54 +1,55
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 #ifndef WIDGET_H
21 #ifndef WIDGET_H
22 #define WIDGET_H
22 #define WIDGET_H
23
23
24 #include <QtGui/QWidget>
24 #include <QtGui/QWidget>
25 #include <QChartview>
25 //#include <QChartview>
26 #include <qchartview.h>
26 #include <QSqlDatabase>
27 #include <QSqlDatabase>
27
28
28 QTCOMMERCIALCHART_USE_NAMESPACE
29 QTCOMMERCIALCHART_USE_NAMESPACE
29
30
30 class QListWidget;
31 class QListWidget;
31 class QRadioButton;
32 class QRadioButton;
32
33
33 class Widget : public QWidget
34 class Widget : public QWidget
34 {
35 {
35 Q_OBJECT
36 Q_OBJECT
36
37
37 public:
38 public:
38 Widget(QWidget *parent = 0);
39 Widget(QWidget *parent = 0);
39 ~Widget();
40 ~Widget();
40
41
41 public slots:
42 public slots:
42 void refreshChart();
43 void refreshChart();
43 void printChart();
44 void printChart();
44
45
45 private:
46 private:
46 QChartView* chartView;
47 QChartView* chartView;
47 QListWidget* countrieslist;
48 QListWidget* countrieslist;
48 QListWidget* yearslist;
49 QListWidget* yearslist;
49 QSqlDatabase db;
50 QSqlDatabase db;
50 QRadioButton* barChartRadioButton;
51 QRadioButton* barChartRadioButton;
51 QRadioButton* scatterChartRadioButton;
52 QRadioButton* scatterChartRadioButton;
52 };
53 };
53
54
54 #endif // WIDGET_H
55 #endif // WIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now