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