diff options
Diffstat (limited to 'examples/charts/chartsgallery/temperaturerecordswidget.cpp')
-rw-r--r-- | examples/charts/chartsgallery/temperaturerecordswidget.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/examples/charts/chartsgallery/temperaturerecordswidget.cpp b/examples/charts/chartsgallery/temperaturerecordswidget.cpp new file mode 100644 index 00000000..36b748be --- /dev/null +++ b/examples/charts/chartsgallery/temperaturerecordswidget.cpp @@ -0,0 +1,65 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include "temperaturerecordswidget.h" + +#include <QBarCategoryAxis> +#include <QBarSeries> +#include <QBarSet> +#include <QChart> +#include <QLegend> +#include <QStackedBarSeries> +#include <QValueAxis> + +TemperatureRecordsWidget::TemperatureRecordsWidget(QWidget *parent) + : ContentWidget(parent) +{ + //![1] + auto low = new QBarSet("Min"); + auto high = new QBarSet("Max"); + + *low << -52 << -50 << -45.3 << -37.0 << -25.6 << -8.0 + << -6.0 << -11.8 << -19.7 << -32.8 << -43.0 << -48.0; + *high << 11.9 << 12.8 << 18.5 << 26.5 << 32.0 << 34.8 + << 38.2 << 34.8 << 29.8 << 20.4 << 15.1 << 11.8; + //![1] + + //![2] + auto series = new QStackedBarSeries; + series->append(low); + series->append(high); + //![2] + + //![3] + auto chart = new QChart; + chart->addSeries(series); + chart->setTitle("Temperature records in Celcius"); + chart->setAnimationOptions(QChart::SeriesAnimations); + //![3] + + //![4] + QStringList categories = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + }; + + auto axisX = new QBarCategoryAxis; + axisX->append(categories); + axisX->setTitleText("Month"); + chart->addAxis(axisX, Qt::AlignBottom); + auto axisY = new QValueAxis; + axisY->setRange(-52, 52); + axisY->setTitleText("Temperature [°C]"); + chart->addAxis(axisY, Qt::AlignLeft); + series->attachAxis(axisX); + series->attachAxis(axisY); + //![4] + + //![5] + chart->legend()->setVisible(true); + chart->legend()->setAlignment(Qt::AlignBottom); + //![5] + + //![6] + createDefaultChartView(chart); + //![6] +} |