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