diff options
author | Jani Honkonen <jani.honkonen@digia.com> | 2012-04-02 16:45:23 +0300 |
---|---|---|
committer | Jani Honkonen <jani.honkonen@digia.com> | 2012-04-02 16:46:15 +0300 |
commit | e44172bd08468c0e92faa6fa74168abf7cdfa556 (patch) | |
tree | 228d90e528c1927b68b7cb3f9c6bbc3008c7f352 /demos | |
parent | 851f130d7cc15beb6da6dfedff14f4a07588dbe7 (diff) |
Move gdpbarchart to demos
Diffstat (limited to 'demos')
-rw-r--r-- | demos/demos.pro | 3 | ||||
-rw-r--r-- | demos/gdpbarchart/gdpData | bin | 0 -> 165888 bytes | |||
-rw-r--r-- | demos/gdpbarchart/gdpbarchart.pro | 14 | ||||
-rw-r--r-- | demos/gdpbarchart/main.cpp | 11 | ||||
-rw-r--r-- | demos/gdpbarchart/widget.cpp | 233 | ||||
-rw-r--r-- | demos/gdpbarchart/widget.h | 34 |
6 files changed, 294 insertions, 1 deletions
diff --git a/demos/demos.pro b/demos/demos.pro index d9c428d2..9c4a5e81 100644 --- a/demos/demos.pro +++ b/demos/demos.pro @@ -1,3 +1,4 @@ TEMPLATE = subdirs SUBDIRS += chartthemes \ - piechartcustomization
\ No newline at end of file + piechartcustomization \ + gdpbarchart diff --git a/demos/gdpbarchart/gdpData b/demos/gdpbarchart/gdpData Binary files differnew file mode 100644 index 00000000..6cf75b82 --- /dev/null +++ b/demos/gdpbarchart/gdpData diff --git a/demos/gdpbarchart/gdpbarchart.pro b/demos/gdpbarchart/gdpbarchart.pro new file mode 100644 index 00000000..54fd2620 --- /dev/null +++ b/demos/gdpbarchart/gdpbarchart.pro @@ -0,0 +1,14 @@ +!include( ../demos.pri ):error( "Couldn't find the demos.pri file!" ) + +QT+= sql +TARGET = gdpbarchart +SOURCES += main.cpp\ + widget.cpp +HEADERS += widget.h + +!mac { + FILE = $$PWD/gdpData + win32:{FILE = $$replace(FILE, "/","\\")} + # TODO: QMAKE_POST_LINK does not work on mac; how to copy the data file? + QMAKE_POST_LINK += $$QMAKE_COPY $$FILE $$CHART_BUILD_BIN_DIR +} diff --git a/demos/gdpbarchart/main.cpp b/demos/gdpbarchart/main.cpp new file mode 100644 index 00000000..102963fe --- /dev/null +++ b/demos/gdpbarchart/main.cpp @@ -0,0 +1,11 @@ +#include <QtGui/QApplication> +#include "widget.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + + return a.exec(); +} diff --git a/demos/gdpbarchart/widget.cpp b/demos/gdpbarchart/widget.cpp new file mode 100644 index 00000000..13c7ee0d --- /dev/null +++ b/demos/gdpbarchart/widget.cpp @@ -0,0 +1,233 @@ +/*! + \class Widget + \brief Ui for the application. + \internal +*/ + +#include "widget.h" + +#include <QChart> +#include <QScatterSeries> +#include <QChartAxis> +#include <QBarset> +#include <QBarSeries> + +#include <QGridLayout> +#include <QPushButton> +#include <QLabel> +#include <QListWidget> +#include <QPrinter> +#include <QPrintDialog> +#include <QRadioButton> +#include <QStringList> +#include <QSqlQuery> + +QTCOMMERCIALCHART_USE_NAMESPACE + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ + setGeometry(100, 100, 1000, 600); + + // right panel layout + barChartRadioButton = new QRadioButton(tr("Bar chart")); + barChartRadioButton->setChecked(true); + scatterChartRadioButton = new QRadioButton(tr("Scatter chart")); + scatterChartRadioButton->setChecked(false); + countrieslist = new QListWidget; + countrieslist->setSelectionMode(QAbstractItemView::MultiSelection); + + //list of years widget + 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())); + + QPushButton* printButton = new QPushButton(tr("Print chart")); + connect(printButton, SIGNAL(clicked()), this, SLOT(printChart())); + + QVBoxLayout* rightPanelLayout = new QVBoxLayout; + rightPanelLayout->addWidget(barChartRadioButton); + rightPanelLayout->addWidget(scatterChartRadioButton); + rightPanelLayout->addWidget(countrieslist); + rightPanelLayout->addWidget(yearslist); + rightPanelLayout->addWidget(refreshButton); + rightPanelLayout->addWidget(printButton); + rightPanelLayout->setStretch(0, 1); + rightPanelLayout->setStretch(1, 0); + + QChart *chart = new QChart(); + chart->setTitle("GDP by country"); + + // main layout + chartView = new QChartView(chart); + QGridLayout* mainLayout = new QGridLayout; + mainLayout->addWidget(chartView, 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* axis = chartArea->axisX(); +// axis-> + // axis->setLabelsVisible(false); + // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide); + +} + +Widget::~Widget() +{ + // + db.close(); +} + +/*! + refreshes the chart +*/ +void Widget::refreshChart() +{ + chartView->chart()->removeAllSeries(); + + // selected countries items list is not sorted. copy the values to QStringlist and sort them. + QStringList selectedCountriesStrings; + QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems(); + for (int i = 0; i < selectedCountriesItems.size(); i++) + selectedCountriesStrings.append(selectedCountriesItems[i]->text()); + selectedCountriesStrings.sort(); + + QSqlQuery query; + // selected years items list is not sorted. copy the values to QList<int> and sort them. + QList<int> selectedYearsInts; + QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems(); + for (int i = 0; i < selectedYearsItems.size(); i++) + selectedYearsInts.append(selectedYearsItems[i]->text().toInt()); + qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>()); + + if (barChartRadioButton->isChecked()) + { + // use the sorted selected coutries list to initialize BarCategory + QStringList category; + for (int i = 0; i < selectedCountriesStrings.size(); i++) + category << selectedCountriesStrings[i]; + QBarSeries* series0 = new QBarSeries(category); + series0 = new QBarSeries(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(")"); + } + + // 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("Barset" + QString::number(i)); + + // 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 the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]); + } + } + series0->appendBarSet(barSet); + } + // add the serie to the chart + chartView->chart()->addSeries(series0); + } + else if (scatterChartRadioButton->isChecked()) + { + QString yearsQuery = "year IN ("; + for (int i = 0; i < selectedYearsInts.size(); i++) + { + yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'"); + if ( i < selectedYearsInts.size() - 1) + yearsQuery.append(","); + else + yearsQuery.append(")"); + } + + // perform a query for each selected country + for (int i = 0; i < selectedCountriesStrings.size(); i++) + { + query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery); + query.first(); + + QScatterSeries* series = new QScatterSeries; + // the data for some of the coutries for some years might be missing. + for (int k = 0; k < selectedYearsInts.size(); k++) + { + if (selectedYearsInts[k] == query.value(0).toInt()) + { + *series << QPointF(query.value(0).toInt() , query.value(1).toReal()); + qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]); + query.next(); + } + else + { + // data missing, put 0 + *series << QPointF(selectedYearsInts[k] , 0.0f); + qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt(); + } + } + // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1); + chartView->chart()->addSeries(series); + } + chartView->chart()->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1); + } +} + +void Widget::printChart() +{ + QPrinter printer; + // QPrinter printer(QPrinter::HighResolution); + printer.setOutputFormat(QPrinter::PdfFormat); + printer.setOrientation(QPrinter::Landscape); + printer.setOutputFileName("print.pdf"); + + QPainter painter; + painter.begin(&printer); + chartView->render(&painter); +} diff --git a/demos/gdpbarchart/widget.h b/demos/gdpbarchart/widget.h new file mode 100644 index 00000000..2bc773ab --- /dev/null +++ b/demos/gdpbarchart/widget.h @@ -0,0 +1,34 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include <QtGui/QWidget> +#include <QChartview> +#include <QSqlDatabase> + +QTCOMMERCIALCHART_USE_NAMESPACE + +class QListWidget; +class QRadioButton; + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = 0); + ~Widget(); + +public slots: + void refreshChart(); + void printChart(); + +private: + QChartView* chartView; + QListWidget* countrieslist; + QListWidget* yearslist; + QSqlDatabase db; + QRadioButton* barChartRadioButton; + QRadioButton* scatterChartRadioButton; +}; + +#endif // WIDGET_H |