main.cpp
110 lines
| 3.7 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r830 | /**************************************************************************** | ||
** | ||||
Titta Heikkala
|
r2688 | ** Copyright (C) 2014 Digia Plc | ||
Jani Honkonen
|
r830 | ** All rights reserved. | ||
Titta Heikkala
|
r2740 | ** For any questions to Digia, please use contact form at http://qt.io | ||
Jani Honkonen
|
r830 | ** | ||
Titta Heikkala
|
r2740 | ** This file is part of the Qt Charts module. | ||
Jani Honkonen
|
r830 | ** | ||
Titta Heikkala
|
r2740 | ** Licensees holding valid commercial license for Qt may use this file in | ||
** accordance with the Qt License Agreement provided with the Software | ||||
** or, alternatively, in accordance with the terms contained in a written | ||||
** agreement between you and Digia. | ||||
Jani Honkonen
|
r830 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
Titta Heikkala
|
r2740 | ** contact form at http://qt.io | ||
Jani Honkonen
|
r830 | ** | ||
****************************************************************************/ | ||||
Titta Heikkala
|
r2714 | #include <QtWidgets/QApplication> | ||
#include <QtWidgets/QMainWindow> | ||||
#include <QtCharts/QChartView> | ||||
#include <QtCharts/QBarSet> | ||||
#include <QtCharts/QLegend> | ||||
sauimone
|
r903 | #include "drilldownseries.h" | ||
#include "drilldownchart.h" | ||||
sauimone
|
r425 | |||
Titta Heikkala
|
r2712 | QT_CHARTS_USE_NAMESPACE | ||
sauimone
|
r425 | |||
int main(int argc, char *argv[]) | ||||
{ | ||||
QApplication a(argc, argv); | ||||
QMainWindow window; | ||||
sauimone
|
r1415 | //! [1] | ||
Jani Honkonen
|
r2102 | DrilldownChart *drilldownChart = new DrilldownChart(); | ||
Jani Honkonen
|
r829 | drilldownChart->setAnimationOptions(QChart::SeriesAnimations); | ||
sauimone
|
r1415 | //! [1] | ||
sauimone
|
r430 | |||
sauimone
|
r1415 | //! [2] | ||
sauimone
|
r425 | // Define categories | ||
sauimone
|
r430 | QStringList months; | ||
sauimone
|
r462 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; | ||
sauimone
|
r430 | QStringList weeks; | ||
weeks << "week 1" << "week 2" << "week 3" << "week 4"; | ||||
QStringList plants; | ||||
plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; | ||||
sauimone
|
r1415 | //! [2] | ||
sauimone
|
r449 | |||
sauimone
|
r1415 | //! [3] | ||
sauimone
|
r449 | // Create drilldown structure | ||
Jani Honkonen
|
r2102 | DrilldownBarSeries *seasonSeries = new DrilldownBarSeries(months, drilldownChart); | ||
Tero Ahola
|
r733 | seasonSeries->setName("Crop by month - Season"); | ||
sauimone
|
r425 | |||
sauimone
|
r449 | // Each month in season series has drilldown series for weekly data | ||
Jani Honkonen
|
r2098 | for (int month = 0; month < months.count(); month++) { | ||
sauimone
|
r430 | |||
sauimone
|
r449 | // Create drilldown series for every week | ||
Jani Honkonen
|
r2102 | DrilldownBarSeries *weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); | ||
sauimone
|
r462 | seasonSeries->mapDrilldownSeries(month, weeklySeries); | ||
sauimone
|
r438 | |||
sauimone
|
r449 | // Drilling down from weekly data brings us back to season data. | ||
Jani Honkonen
|
r2098 | for (int week = 0; week < weeks.count(); week++) { | ||
sauimone
|
r462 | weeklySeries->mapDrilldownSeries(week, seasonSeries); | ||
sauimone
|
r1321 | weeklySeries->setName(QString("Crop by week - " + months.at(month))); | ||
sauimone
|
r430 | } | ||
sauimone
|
r1563 | // Use clicked signal to implement drilldown | ||
Jani Honkonen
|
r2110 | QObject::connect(weeklySeries, SIGNAL(clicked(int,QBarSet*)), drilldownChart, SLOT(handleClicked(int,QBarSet*))); | ||
sauimone
|
r430 | } | ||
sauimone
|
r462 | |||
sauimone
|
r1563 | // Enable drilldown from season series using clicked signal | ||
Jani Honkonen
|
r2110 | QObject::connect(seasonSeries, SIGNAL(clicked(int,QBarSet*)), drilldownChart, SLOT(handleClicked(int,QBarSet*))); | ||
sauimone
|
r1415 | //! [3] | ||
sauimone
|
r430 | |||
sauimone
|
r1415 | //! [4] | ||
sauimone
|
r449 | // Fill monthly and weekly series with data | ||
sauimone
|
r438 | foreach (QString plant, plants) { | ||
Jani Honkonen
|
r2102 | QBarSet *monthlyCrop = new QBarSet(plant); | ||
Jani Honkonen
|
r2098 | for (int month = 0; month < months.count(); month++) { | ||
Jani Honkonen
|
r2102 | QBarSet *weeklyCrop = new QBarSet(plant); | ||
Jani Honkonen
|
r2098 | for (int week = 0; week < weeks.count(); week++) | ||
sauimone
|
r438 | *weeklyCrop << (qrand() % 20); | ||
sauimone
|
r462 | // Get the drilldown series from season series and add crop to it. | ||
sauimone
|
r1194 | seasonSeries->drilldownSeries(month)->append(weeklyCrop); | ||
sauimone
|
r934 | *monthlyCrop << weeklyCrop->sum(); | ||
sauimone
|
r438 | } | ||
sauimone
|
r1194 | seasonSeries->append(monthlyCrop); | ||
sauimone
|
r438 | } | ||
sauimone
|
r1415 | //! [4] | ||
sauimone
|
r449 | |||
sauimone
|
r1415 | //! [5] | ||
sauimone
|
r449 | // Show season series in initial view | ||
drilldownChart->changeSeries(seasonSeries); | ||||
Jani Honkonen
|
r829 | drilldownChart->setTitle(seasonSeries->name()); | ||
sauimone
|
r1415 | //! [5] | ||
sauimone
|
r430 | |||
sauimone
|
r1415 | //! [6] | ||
Michal Klocek
|
r535 | drilldownChart->axisX()->setGridLineVisible(false); | ||
sauimone
|
r891 | drilldownChart->legend()->setVisible(true); | ||
Tero Ahola
|
r1357 | drilldownChart->legend()->setAlignment(Qt::AlignBottom); | ||
sauimone
|
r1415 | //! [6] | ||
sauimone
|
r430 | |||
Jani Honkonen
|
r829 | QChartView *chartView = new QChartView(drilldownChart); | ||
window.setCentralWidget(chartView); | ||||
Titta Heikkala
|
r2647 | window.resize(480, 300); | ||
sauimone
|
r425 | window.show(); | ||
sauimone
|
r430 | |||
sauimone
|
r425 | return a.exec(); | ||
} | ||||