ZoomAndScrollDemo Example

#include "mainwindow.h"

#include <QApplication>
#include <QGroupBox>
#include <QComboBox>
#include <QFormLayout>

#include <QtnChartTitle.h>

/* MainWindow */
MainWindow::MainWindow()
    : DemoChartWindow(tr("Zoom Scroll Charts"))
{
    setChart(Q_NULL);
    createSeriesParametrs();
    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("Line"),   QVariant(Line));
    m_seriesSwitcher->addItem(tr("Area"),   QVariant(Area));
    m_seriesSwitcher->addItem(tr("Column"), QVariant(Column));
    m_seriesSwitcher->setCurrentIndex(Line);
    connect(m_seriesSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(seriesChanged(int)));
    localLayout->addRow(m_seriesSwitcher);

    QGroupBox* optionsGroup = createGroupParameters(tr("Chart Options:"));
    QFormLayout* localOptionsLayout = (QFormLayout*)optionsGroup->layout();

    m_dataPointsCountSwitcher = new QComboBox(optionsGroup);
    m_dataPointsCountSwitcher->addItem(QStringLiteral("500"),   QVariant(500));
    m_dataPointsCountSwitcher->addItem(QStringLiteral("1000"),  QVariant(1000));
    m_dataPointsCountSwitcher->addItem(QStringLiteral("1500"),  QVariant(1500));
    m_dataPointsCountSwitcher->addItem(QStringLiteral("2000"),  QVariant(2000));
    m_dataPointsCountSwitcher->setCurrentIndex(0);
    connect(m_dataPointsCountSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(pointsCountChanged(int)));
    localOptionsLayout->addRow(tr("Data Points Count:"), m_dataPointsCountSwitcher);

    ChartTitle* subTitle = new ChartTitle();
    m_chart->appendTitle(subTitle);
    subTitle->setText(tr("To zoom chart use mouse wheel. If you want to pan, use the left mouse button."));
    subTitle->setDockPosition(ChartTitle::BottomDock);
    subTitle->setAlignment(Qt::AlignLeft);
    subTitle->setFont(QFont(QLatin1String("Tahoma"), 8));
    subTitle->setTextColor(Qt::gray);
}

ChartSeries2D* MainWindow::createLineSeries()
{
    return new ChartLineSeries2D();
}

ChartSeries2D* MainWindow::createAreaSeries()
{
    return new ChartSplineAreaSeries2D();
}

ChartSeries2D* MainWindow::createColumnSeries()
{
    return new ChartBarSeries2D();
}

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

void MainWindow::seriesChanged(int index)
{
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
//    m_chart->setCursor(QCursor(Qt::WaitCursor));
    m_chart->clearSeries();
    m_chart->clearViews();
    m_chart->setBackgroundBrush(QBrush());

    ChartSeries2D* series = Q_NULL;
    if (Line == index)
        series = createLineSeries();
    else if (Area == index)
        series = createAreaSeries();
    else if (Column == index)
        series = createColumnSeries();

    if (series == Q_NULL)
        return;

    m_chart->appendSeries(series);
    series->label()->setVisible(false);

    ChartView2D* view2D = static_cast<ChartView2D *>(m_chart->views().at(0));
    series->setXScaleType(ChartSeries2D::ScaleNumerical);
    view2D->axisX()->setViewAutoRange(false);
    view2D->axisX()->setFixedViewRange(0, 100);

    if (ChartPointSeries* pointSeries = qobject_cast<ChartPointSeries*>(series))
    {
        pointSeries->setMarkerSize(5);
        pointSeries->setMarkerVisible(false);
    }

    view2D->setZoomEnabled(true);
    view2D->setScrollBarEnabled(true);

    pointsCountChanged(m_dataPointsCountSwitcher->currentIndex());
    updateValueParameters();
    QApplication::restoreOverrideCursor();
}

void MainWindow::pointsCountChanged(int index)
{
    if (m_chart->series().count() == 0)
        return;

    if (ChartSeries2D* series = qobject_cast<ChartSeries2D*>(m_chart->series().at(0)))
    {
        series->clear();

        QVariant var = m_dataPointsCountSwitcher->itemData(index);
        int batchCount = var.toInt();

        // Fill series data
        double maxVal = 0;
        double val = 0;
        for(int pointIndex = 0; pointIndex < batchCount; pointIndex++)
        {
            double delta = val > 250 ? qtn_rand(-250, 250) : qtn_rand(0, 500);
            val += delta;
            series->addXY(pointIndex, val);
            maxVal = qMax(maxVal, val);
        }
    }
}