AreaChart2D Example

#include "mainwindow.h"

#include <QGroupBox>
#include <QComboBox>
#include <QSlider>
#include <QFormLayout>

#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
#include <QLocale>
#else
#include <QDateTime>
#endif

/* MainWindow */
MainWindow::MainWindow()
    : DemoChartWindow(tr("Area Chart"))
{
    setChart(Q_NULL);
    createSeriesParametrs();
    createMarkersGroup();
    createLabelsGroup();
    seriesChanged(m_seriesSwitcher->currentIndex());
}

static ChartAreaSeries2D* createChartSeries(const QString& format, MainWindow::SeriesType type)
{
    ChartAreaSeries2D* impl = Q_NULL;
    if (type == MainWindow::Stacked100AreaSplineSeries)
        impl = new ChartStackedSplineAreaSeries2D();
    else if (type == MainWindow::StackedAreaSplineSeries)
        impl = new ChartStackedSplineAreaSeries2D();
    else if (type == MainWindow::AreaSplineSeries)
        impl = new ChartSplineAreaSeries2D();
    else if (type == MainWindow::Stacked100AreaLineSeries)
        impl = new ChartStackedAreaSeries2D();
    else if (type == MainWindow::StackedAreaLineSeries)
        impl = new ChartStackedAreaSeries2D();
    else if (type == MainWindow::AreaLineSeries)
        impl = new ChartAreaSeries2D();
    else
        impl = new ChartAreaSeries2D();

    if (impl)
        impl->label()->setFormat(format);

    impl->setThickness(3);

    return impl;
}

void MainWindow::createSeriesParametrs()
{
    QGroupBox* seriesTypeGroup = createGroupParameters(tr("Series"));
    QFormLayout* localLayout = static_cast<QFormLayout*>(seriesTypeGroup->layout());

    // Option Series
    m_seriesSwitcher = new QComboBox(seriesTypeGroup);
    m_seriesSwitcher->addItem(tr("Area"), QVariant(AreaLineSeries));
    m_seriesSwitcher->addItem(tr("Stacked Area"), QVariant(StackedAreaLineSeries));
    m_seriesSwitcher->addItem(tr("100% Stacked Area"), QVariant(Stacked100AreaLineSeries));
    m_seriesSwitcher->addItem(tr("Area (Spline)"), QVariant(AreaSplineSeries));
    m_seriesSwitcher->addItem(tr("Stacked Area (Spline)"), QVariant(StackedAreaSplineSeries));
    m_seriesSwitcher->addItem(tr("100% Stacked Area (Spline)"), QVariant(Stacked100AreaSplineSeries));
    connect(m_seriesSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(seriesChanged(int)));

    m_transparency = new QSlider(Qt::Horizontal, seriesTypeGroup);
    m_transparency->setRange(0, 255);
    m_transparency->setSliderPosition(190);
    m_transparency->setSingleStep(5);
    connect(m_transparency, SIGNAL(valueChanged(int)), this, SLOT(transparencyChanged(int)));

    localLayout->addRow(m_seriesSwitcher);
    localLayout->addRow(new QLabel(QObject::tr("Transparency:"), seriesTypeGroup));
    localLayout->addRow(m_transparency);
}

void MainWindow::createAreaSeries(SeriesType type)
{
    createTitle(tr("Average Temperatures by Month/Year:"));

    m_chart->legend()->setVisible(true);

    ChartAreaSeries2D* series1 = createChartSeries(QStringLiteral("%.00f"), type);
    m_chart->appendSeries(series1);
    series1->setName(QObject::tr("2010"));

    ChartAreaSeries2D* series2 = createChartSeries(QStringLiteral("%.00f"), type);
    m_chart->appendSeries(series2);
    series2->setName(QObject::tr("2011"));

    ChartAreaSeries2D* series3 = createChartSeries(QStringLiteral("%.00f"), type);
    m_chart->appendSeries(series3);
    series3->setName(QObject::tr("2012"));

    qreal arTemp[] = { 5., 12., 19, 22, 19, 24, 22, 24, 18, 14, 10, 7 };
    for (int month = 1; month <= 12; month++)
    {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
        QString strMonth = QLocale::system().monthName(month, QLocale::ShortFormat);
#else
        QString strMonth = QDate::shortMonthName(month);
#endif
        series1->addAxisPointY(arTemp[month - 1], strMonth);
        series2->addAxisPointY(arTemp[month - 1] + qtn_rand(-4, 4), strMonth);
        series3->addAxisPointY(arTemp[month - 1] + qtn_rand(-4, 4), strMonth);
    }
    ChartView2D* view = static_cast<ChartView2D*>(m_chart->views().at(0));
    Q_ASSERT(view);
    view->axisX()->setSideMargins(false);
}

void MainWindow::updateValueParameters()
{
    DemoChartWindow::updateValueParameters();
    transparencyChanged(m_transparency->value());
}

void MainWindow::seriesChanged(int index)
{
    m_chart->clearSeries();
    m_chart->clearTitles();
    m_chart->setBackgroundBrush(QBrush());

    QVariant var = m_seriesSwitcher->itemData(index);
    createAreaSeries(static_cast<SeriesType>(var.toUInt()));
    updateValueParameters();
}

void MainWindow::transparencyChanged(int value)
{
    const SeriesList& listSeries = m_chart->series();

    for (int i = 0; i < listSeries.count(); i++)
    {
        ChartAreaSeries2D* series = static_cast<ChartAreaSeries2D*>(listSeries.at(i));
        series->setTransparency(255 - value);
    }
}