@@ -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,11 +109,21 void Widget::refreshChart() | |||
|
102 | 109 | selectedCountriesStrings.append(selectedCountriesItems[i]->text()); |
|
103 | 110 | selectedCountriesStrings.sort(); |
|
104 | 111 | |
|
112 | QSqlQuery query; | |
|
113 | // selected years items list is not sorted. copy the values to QList<Integer> and sort them. | |
|
114 | QList<int> selectedYearsInts; | |
|
115 | QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems(); | |
|
116 | for (int i = 0; i < selectedYearsItems.size(); i++) | |
|
117 | selectedYearsInts.append(selectedYearsItems[i]->text().toInt()); | |
|
118 | qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>()); | |
|
119 | ||
|
120 | if (barChartRadioButton->isChecked()) | |
|
121 | { | |
|
105 | 122 | // use the sorted selected coutries list to initialize BarCategory |
|
106 | 123 | QBarCategory* category = new QBarCategory; |
|
107 | 124 | for (int i = 0; i < selectedCountriesStrings.size(); i++) |
|
108 | 125 | *category << selectedCountriesStrings[i]; |
|
109 |
|
|
|
126 | series0 = new QBarChartSeries(category); | |
|
110 | 127 | |
|
111 | 128 | // prepare the selected counries SQL query |
|
112 | 129 | QString countriesQuery = "country IN ("; |
@@ -119,14 +136,6 void Widget::refreshChart() | |||
|
119 | 136 | countriesQuery.append(")"); |
|
120 | 137 | } |
|
121 | 138 | |
|
122 | QSqlQuery query; | |
|
123 | // selected years items list is not sorted. copy the values to QList<Integer> and sort them. | |
|
124 | QList<int> selectedYearsInts; | |
|
125 | QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems(); | |
|
126 | for (int i = 0; i < selectedYearsItems.size(); i++) | |
|
127 | selectedYearsInts.append(selectedYearsItems[i]->text().toInt()); | |
|
128 | qSort(selectedYearsInts); | |
|
129 | ||
|
130 | 139 | // perform a query for each selected year |
|
131 | 140 | for (int i = 0; i < selectedYearsInts.size(); i++) |
|
132 | 141 | { |
@@ -156,9 +165,49 void Widget::refreshChart() | |||
|
156 | 165 | } |
|
157 | 166 | series0->addBarSet(barSet); |
|
158 | 167 | } |
|
159 | ||
|
160 | 168 | // add the serie to the chart |
|
161 | 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(","); | |
|
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++) | |
|
193 | { | |
|
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 | } | |
|
206 | } | |
|
207 | chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1); | |
|
208 | chartArea->addSeries(series); | |
|
209 | } | |
|
210 | } | |
|
162 | 211 | } |
|
163 | 212 | |
|
164 | 213 | void Widget::printChart() |
@@ -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