diff --git a/example/gdpbarchart/gdpData b/example/gdpbarchart/gdpData new file mode 100644 index 0000000000000000000000000000000000000000..41957f53079631e69a7c3871ce87e39551cb4c88 GIT binary patch literal 31744 zc%1FlJ5Iwu6o%1j2!uBwyx-mf8(;x6h$3~0AW9?bEI$V#xw5j z+}++h%xCTW)AQqW-d@$9nx?MXwyGAZ*F7)NLTc*6=l5@IcFQ0ez5MhG;H$}R{{ZkG zo!8rhuBEh`hG`|OrnR)5HmV=xTWLFu(oPzu-L#kX(?L2+N9j17q| +#include "widget.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + + return a.exec(); +} diff --git a/example/gdpbarchart/widget.cpp b/example/gdpbarchart/widget.cpp new file mode 100644 index 0000000..ae1f98d --- /dev/null +++ b/example/gdpbarchart/widget.cpp @@ -0,0 +1,147 @@ +#include "widget.h" +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ + setGeometry(100, 100, 800, 600); + + // right panel layout + countrieslist = new QListWidget; + countrieslist->setSelectionMode(QAbstractItemView::MultiSelection); + + yearslist = new QListWidget; + yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection); + for (int i = 1990; i < 2011; i++) + yearslist->addItem(QString("%1").arg(i)); + + QPushButton* refreshButton = new QPushButton(tr("Refresh")); + connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart())); + + QVBoxLayout* rightPanelLayout = new QVBoxLayout; + rightPanelLayout->addWidget(countrieslist); + rightPanelLayout->addWidget(yearslist); + rightPanelLayout->addWidget(refreshButton); + rightPanelLayout->setStretch(0, 1); + rightPanelLayout->setStretch(1, 0); + + // main layout + chartArea = new QChartView(this); + chartArea->setChartTitle("GDP by country"); + QGridLayout* mainLayout = new QGridLayout; + mainLayout->addWidget(chartArea, 0, 0); + mainLayout->addLayout(rightPanelLayout, 0, 1); + mainLayout->setColumnStretch(0,1); + setLayout(mainLayout); + + // connect to the database + db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName("gdpData"); + if(!db.open()) + { + qDebug() << "could not open database. SQLite db file missing (?)"; + return; + } + + // get the list of all countires and regions. + QSqlQuery query; + query.exec("SELECT DISTINCT country FROM gdp2"); + + // add the countries to the country filter + while (query.next()) { + countrieslist->addItem(query.value(0).toString()); + } + + // hide axis X labels + QChartAxis newAxis = chartArea->defaultAxisX(); + newAxis.setLabelsVisible(false); +// newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide); + chartArea->setDefaultAxisX(newAxis); +} + +Widget::~Widget() +{ + // + db.close(); +} + +void Widget::refreshChart() +{ + // selected countries items list is not sorted. copy the values to QStringlist and sort them. + QStringList selectedCountriesStrings; + QList selectedCountriesItems = countrieslist->selectedItems(); + for (int i = 0; i < selectedCountriesItems.size(); i++) + selectedCountriesStrings.append(selectedCountriesItems[i]->text()); + selectedCountriesStrings.sort(); + + // use the sorted selected coutries list to initialize BarCategory + QBarCategory* category = new QBarCategory; + for (int i = 0; i < selectedCountriesStrings.size(); i++) + *category << selectedCountriesStrings[i]; + BarChartSeries* series0 = new BarChartSeries(category); + + // prepare the selected counries SQL query + QString countriesQuery = "country IN ("; + for (int i = 0; i < selectedCountriesStrings.size(); i++) + { + countriesQuery.append("'" + selectedCountriesStrings[i] + "'"); + if ( i < selectedCountriesStrings.size() - 1) + countriesQuery.append(","); + else + countriesQuery.append(")"); + } + + QSqlQuery query; + // selected years items list is not sorted. copy the values to QList and sort them. + QList selectedYearsInts; + QList selectedYearsItems = yearslist->selectedItems(); + for (int i = 0; i < selectedYearsItems.size(); i++) + selectedYearsInts.append(selectedYearsItems[i]->text().toInt()); + qSort(selectedYearsInts); + + // perform a query for each selected year + for (int i = 0; i < selectedYearsInts.size(); i++) + { + query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery); + QBarSet* barSet = new QBarSet; +// while (query.next()) { +// qDebug() << query.value(0).toString() << " : " << query.value(1).toString(); +// } + query.first(); + + // the data for some of the coutries for some years might be missing. + // QBarChart needs bars to have same size + for (int k = 0; k < selectedCountriesStrings.size(); k++) + { + if (selectedCountriesStrings[k] == query.value(0).toString()) + { + *barSet << query.value(1).toReal(); + qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]); + query.next(); + } + else + { + // data missing, put 0 + *barSet << 0.0f; + qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]); + } + } + series0->addBarSet(barSet); + } + + // add the serie to the chart + chartArea->addSeries(series0); +} diff --git a/example/gdpbarchart/widget.h b/example/gdpbarchart/widget.h new file mode 100644 index 0000000..fe2b1f1 --- /dev/null +++ b/example/gdpbarchart/widget.h @@ -0,0 +1,30 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class QListWidget; + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = 0); + ~Widget(); + +public slots: + void refreshChart(); + +private: + QChartView* chartArea; + QListWidget* countrieslist; + QListWidget* yearslist; + QSqlDatabase db; +}; + +#endif // WIDGET_H