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