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