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