##// END OF EJS Templates
Added another chart type choice
Marek Rosa -
r268:bd1ae0fe1b01
parent child
Show More
@@ -12,12 +12,12
12 #include <qscatterseries.h>
12 #include <qscatterseries.h>
13 #include <qchartview.h>
13 #include <qchartview.h>
14 #include <qchartaxis.h>
14 #include <qchartaxis.h>
15 #include <qbarchartseries.h>
16 #include <qbarcategory.h>
15 #include <qbarcategory.h>
17 #include <qbarset.h>
16 #include <qbarset.h>
18 #include <QListWidget>
17 #include <QListWidget>
19 #include <QPrinter>
18 #include <QPrinter>
20 #include <QPrintDialog>
19 #include <QPrintDialog>
20 #include <QRadioButton>
21
21
22 QTCOMMERCIALCHART_USE_NAMESPACE
22 QTCOMMERCIALCHART_USE_NAMESPACE
23
23
@@ -27,6 +27,10 Widget::Widget(QWidget *parent)
27 setGeometry(100, 100, 1000, 600);
27 setGeometry(100, 100, 1000, 600);
28
28
29 // right panel layout
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 countrieslist = new QListWidget;
34 countrieslist = new QListWidget;
31 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
35 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
32
36
@@ -42,6 +46,8 Widget::Widget(QWidget *parent)
42 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
46 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
43
47
44 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
48 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
49 rightPanelLayout->addWidget(barChartRadioButton);
50 rightPanelLayout->addWidget(scatterChartRadioButton);
45 rightPanelLayout->addWidget(countrieslist);
51 rightPanelLayout->addWidget(countrieslist);
46 rightPanelLayout->addWidget(yearslist);
52 rightPanelLayout->addWidget(yearslist);
47 rightPanelLayout->addWidget(refreshButton);
53 rightPanelLayout->addWidget(refreshButton);
@@ -78,7 +84,7 Widget::Widget(QWidget *parent)
78
84
79 // hide axis X labels
85 // hide axis X labels
80 QChartAxis* axis = chartArea->axisX();
86 QChartAxis* axis = chartArea->axisX();
81 axis->setLabelsVisible(false);
87 // axis->setLabelsVisible(false);
82 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
88 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
83
89
84 }
90 }
@@ -94,7 +100,8 Widget::~Widget()
94 */
100 */
95 void Widget::refreshChart()
101 void Widget::refreshChart()
96 {
102 {
97 // chartArea->
103 chartArea->removeSeries(series0);
104
98 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
105 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
99 QStringList selectedCountriesStrings;
106 QStringList selectedCountriesStrings;
100 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
107 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
@@ -102,69 +109,111 void Widget::refreshChart()
102 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
109 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
103 selectedCountriesStrings.sort();
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 QSqlQuery query;
112 QSqlQuery query;
123 // selected years items list is not sorted. copy the values to QList<Integer> and sort them.
113 // selected years items list is not sorted. copy the values to QList<Integer> and sort them.
124 QList<int> selectedYearsInts;
114 QList<int> selectedYearsInts;
125 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
115 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
126 for (int i = 0; i < selectedYearsItems.size(); i++)
116 for (int i = 0; i < selectedYearsItems.size(); i++)
127 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
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
120 if (barChartRadioButton->isChecked())
131 for (int i = 0; i < selectedYearsInts.size(); i++)
132 {
121 {
133 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
122 // use the sorted selected coutries list to initialize BarCategory
134 QBarSet* barSet = new QBarSet;
123 QBarCategory* category = new QBarCategory;
135 // while (query.next()) {
124 for (int i = 0; i < selectedCountriesStrings.size(); i++)
136 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
125 *category << selectedCountriesStrings[i];
137 // }
126 series0 = new QBarChartSeries(category);
138 query.first();
127
139
128 // prepare the selected counries SQL query
140 // the data for some of the coutries for some years might be missing.
129 QString countriesQuery = "country IN (";
141 // QBarChart needs bars to have same size
130 for (int i = 0; i < selectedCountriesStrings.size(); i++)
142 for (int k = 0; k < selectedCountriesStrings.size(); k++)
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();
153 if (selectedCountriesStrings[k] == query.value(0).toString())
147 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
154 {
148 query.next();
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 else
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
194 if (selectedYearsInts[k] == query.value(0).toInt())
153 *barSet << 0.0f;
195 {
154 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]);
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 void Widget::printChart()
213 void Widget::printChart()
165 {
214 {
166 QPrinter printer;
215 QPrinter printer;
167 // QPrinter printer(QPrinter::HighResolution);
216 // QPrinter printer(QPrinter::HighResolution);
168 printer.setOutputFormat(QPrinter::PdfFormat);
217 printer.setOutputFormat(QPrinter::PdfFormat);
169 printer.setOrientation(QPrinter::Landscape);
218 printer.setOrientation(QPrinter::Landscape);
170 printer.setOutputFileName("print.pdf");
219 printer.setOutputFileName("print.pdf");
@@ -3,11 +3,13
3
3
4 #include <QtGui/QWidget>
4 #include <QtGui/QWidget>
5 #include <qchartview.h>
5 #include <qchartview.h>
6 #include <qbarchartseries.h>
6 #include <QSqlDatabase>
7 #include <QSqlDatabase>
7
8
8 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
9
10
10 class QListWidget;
11 class QListWidget;
12 class QRadioButton;
11
13
12 class Widget : public QWidget
14 class Widget : public QWidget
13 {
15 {
@@ -26,6 +28,9 private:
26 QListWidget* countrieslist;
28 QListWidget* countrieslist;
27 QListWidget* yearslist;
29 QListWidget* yearslist;
28 QSqlDatabase db;
30 QSqlDatabase db;
31 QBarChartSeries* series0;
32 QRadioButton* barChartRadioButton;
33 QRadioButton* scatterChartRadioButton;
29 };
34 };
30
35
31 #endif // WIDGET_H
36 #endif // WIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now