PolarChart2D Example

#include "mainwindow.h"

#include <QGroupBox>
#include <QComboBox>
#include <QFormLayout>
#include <QCheckBox>
#include <QDateTime>
#include <QLocale>

/* MainWindow */
MainWindow::MainWindow()
    : DemoChartWindow(tr("Polar Charts"))
{
    setChart(Q_NULL);
    createSeriesParametrs();
    createPolarPointSeries();
    createMarkersGroup();
    // Option Labels
    createLabelsGroup();

    seriesChanged(m_seriesSwitcher->currentIndex());
    updateValueParameters();
}

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

    m_seriesSwitcher = new QComboBox(seriesTypeGroup);
    m_seriesSwitcher->addItem(tr("Polar Point"), PolarPointSeries);
    m_seriesSwitcher->addItem(tr("Polar Line"), PolarLineSeries);
    m_seriesSwitcher->addItem(tr("Polar Area"), PolarAreaSeries);
    m_seriesSwitcher->addItem(tr("Polar Spline"), PolarRadarSpline);

    m_seriesSwitcher->setCurrentIndex(0);
    connect(m_seriesSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(seriesChanged(int)));
    localLayout->addRow(m_seriesSwitcher);
}

void MainWindow::createPolarPointSeries()
{
    createTitle(tr("Polar Point"));
    m_chart->legend()->setVisible(true);

    ChartPolarPointSeries2D* series = new ChartPolarPointSeries2D();
    series->setName(tr("Points 1"));

    series->addXY(0.0  , 120.0);
    series->addXY(30.0 , 110.0);
    series->addXY(60.0 , 180.0);
    series->addXY(90.0 , 145.0);
    series->addXY(120.0, 145.0);
    series->addXY(180.0, 100.0);
    series->addXY(225.0, 190.0);
    series->addXY(270.0, 120.0);
    series->addXY(300.0, 130.0);
    m_chart->appendSeries(series);

    series = new ChartPolarPointSeries2D();
    series->setName(tr("Points 2"));

    series->addXY(0.0  , 220.0);
    series->addXY(30.0 , 80.0 );
    series->addXY(60.0 , 100.0);
    series->addXY(90.0 , 70.0 );
    series->addXY(120.0, 20.0 );
    series->addXY(150.0, 200.0);
    series->addXY(180.0, 210.0);
    series->addXY(210.0, 110.0);
    series->addXY(270.0, 150.0);
    series->addXY(300.0, 170.0);
    series->addXY(330.0, 120.0);
    m_chart->appendSeries(series);
}

void MainWindow::createPolarLineSeries()
{
    createTitle(tr("Polar Line"));
    m_chart->legend()->setVisible(true);

    ChartPolarLineSeries2D* series = new ChartPolarLineSeries2D();
    series->setName(tr("Line 1"));

    for (int month = 1; month <= 12; month++)
    {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
        QString strMonth = QLocale::system().monthName(month);
#else
        QString strMonth = QDate::shortMonthName(month);
#endif
        series->addAxisPointY(50 + (rand() % 50), strMonth);
    }
    m_chart->appendSeries(series);

    /*
    const AreaList& list = m_chart->areas();
    if (list.size() > 0)
    {
        ChartArea2D* area = (ChartArea2D*)list.at(0);
        area->axisX()->setInterlaced(false);
        area->axisY()->setInterlaced(true);
    }
    */
}

void MainWindow::createPolarAreaSeries()
{
    createTitle(tr("Polar Area"));

    ChartPolarAreaSeries2D* series = new ChartPolarAreaSeries2D();
    series->setName(tr("Area 1"));

    for (int month = 1; month <= 12; month++)
    {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
        QString strMonth = QLocale::system().monthName(month);
#else
        QString strMonth = QDate::shortMonthName(month);
#endif
        series->addAxisPointY(50 + (rand() % 50), strMonth);
    }

    m_chart->appendSeries(series);
}

void MainWindow::createPolarSplineSeries()
{
    createTitle(tr("EM Field Strength(Polar)"));

    ChartPolarSplineAreaSeries2D* series1 = new ChartPolarSplineAreaSeries2D();
    series1->setName(tr("Above 100MHz"));

    series1->addXY(0,    5.5);
    series1->addXY(30,   1.7);
    series1->addXY(60,   5.0);
    series1->addXY(90,   4.0);
    series1->addXY(115,  9.0);
    series1->addXY(120,  1.8);
    series1->addXY(134,  9.5);
    series1->addXY(150,  10.0);
    series1->addXY(180,  9.7);
    series1->addXY(210,  2.0);
    series1->addXY(240,  3.9);
    series1->addXY(254,  8.0);
    series1->addXY(270,  9.0);
    series1->addXY(300,  5.0);
    series1->addXY(330,  6.0);

    m_chart->appendSeries(series1);

    ChartPolarSplineSeries2D* series2 = new ChartPolarSplineSeries2D();
    series2->setName(tr("Below 100MHz"));

    series2->addXY(0,    8.0);
    series2->addXY(0.5,  6.0);
    series2->addXY(1,    4.0);
    series2->addXY(6,    2.6);
    series2->addXY(30,   5.0);
    series2->addXY(60,   5.4);
    series2->addXY(75,   8.4);
    series2->addXY(90,   9.0);
    series2->addXY(115,  7.7);
    series2->addXY(120,  8.6);
    series2->addXY(150,  5.6);
    series2->addXY(165,  7.6);
    series2->addXY(180,  3.0);
    series2->addXY(195,  9.0);
    series2->addXY(210,  7.1);
    series2->addXY(224,  8.9);
    series2->addXY(234,  8.4);
    series2->addXY(240,  8.1);
    series2->addXY(255,  2.3);
    series2->addXY(270,  5.0);
    series2->addXY(285,  4.0);
    series2->addXY(300,  1.6);
    series2->addXY(315,  8.1);
    series2->addXY(330,  8.4);
    series2->addXY(340,  9.4);
    series2->addXY(345,  9.2);
    series2->addXY(0,    8.0);

    m_chart->appendSeries(series2);
}

void MainWindow::updateValueParameters()
{
    DemoChartWindow::updateValueParameters();
}

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

    if (PolarPointSeries == index)
        createPolarPointSeries();
    else if (PolarLineSeries == index)
        createPolarLineSeries();
    else if (PolarAreaSeries == index)
        createPolarAreaSeries();
    else if (PolarRadarSpline == index)
        createPolarSplineSeries();
    updateValueParameters();
}