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