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