@@ -12,12 +12,12 | |||
|
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 | |
@@ -27,6 +27,10 Widget::Widget(QWidget *parent) | |||
|
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 | |
@@ -42,6 +46,8 Widget::Widget(QWidget *parent) | |||
|
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); |
@@ -78,7 +84,7 Widget::Widget(QWidget *parent) | |||
|
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 | } |
@@ -94,7 +100,8 Widget::~Widget() | |||
|
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(); |
@@ -102,69 +109,111 void Widget::refreshChart() | |||
|
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 |
QBar |
|
|
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 |
|
|
|
147 | qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]); | |
|
148 |
query. |
|
|
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 |
|
|
|
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"); |
@@ -3,11 +3,13 | |||
|
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 | { |
@@ -26,6 +28,9 private: | |||
|
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