main.cpp Example File
stackedbarchartdrilldown/main.cpp
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCore/QRandomGenerator>
#include <QtCharts/QChartView>
#include <QtCharts/QBarSet>
#include <QtCharts/QLegend>
#include "drilldownseries.h"
#include "drilldownchart.h"
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow window;
DrilldownChart *drilldownChart = new DrilldownChart();
drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
const QStringList months = {
"May", "Jun", "Jul", "Aug", "Sep"
};
const QStringList weeks = {
"week 1", "week 2", "week 3", "week 4"
};
const QStringList plants = {
"Habanero", "Lemon Drop", "Starfish", "Aji Amarillo"
};
DrilldownBarSeries *seasonSeries = new DrilldownBarSeries(months, drilldownChart);
seasonSeries->setName("Crop by month - Season");
for (int month = 0; month < months.count(); month++) {
DrilldownBarSeries *weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
seasonSeries->mapDrilldownSeries(month, weeklySeries);
for (int week = 0; week < weeks.count(); week++) {
weeklySeries->mapDrilldownSeries(week, seasonSeries);
weeklySeries->setName(QString("Crop by week - " + months.at(month)));
}
QObject::connect(weeklySeries, &DrilldownBarSeries::clicked,
drilldownChart, &DrilldownChart::handleClicked);
}
QObject::connect(seasonSeries, &DrilldownBarSeries::clicked,
drilldownChart, &DrilldownChart::handleClicked);
for (const QString &plant : plants) {
QBarSet *monthlyCrop = new QBarSet(plant);
for (int month = 0; month < months.count(); month++) {
QBarSet *weeklyCrop = new QBarSet(plant);
for (int week = 0; week < weeks.count(); week++)
*weeklyCrop << QRandomGenerator::global()->bounded(20);
seasonSeries->drilldownSeries(month)->append(weeklyCrop);
*monthlyCrop << weeklyCrop->sum();
}
seasonSeries->append(monthlyCrop);
}
drilldownChart->changeSeries(seasonSeries);
drilldownChart->setTitle(seasonSeries->name());
drilldownChart->axisX()->setGridLineVisible(false);
drilldownChart->legend()->setVisible(true);
drilldownChart->legend()->setAlignment(Qt::AlignBottom);
QChartView *chartView = new QChartView(drilldownChart);
window.setCentralWidget(chartView);
window.resize(480, 300);
window.show();
return a.exec();
}