FinancialChart2D Example

#include "mainwindow.h"

#include <QGroupBox>
#include <QComboBox>
#include <QFormLayout>
#include <QSpinBox>

/* MainWindow */
MainWindow::MainWindow()
    : DemoChartWindow(tr("Financial Charts"))
{
    setChart(Q_NULL);
    createSeriesParametrs();
    createMarkersGroup();
    createLabelsGroup();

    seriesChanged(m_seriesSwitcher->currentIndex());
}

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

    m_seriesSwitcher = new QComboBox(seriesTypeGroup);
    m_seriesSwitcher->addItem(QStringLiteral("OpenHighLowClose"), QVariant(OpenHighLowCloseSeries));
    m_seriesSwitcher->addItem(QStringLiteral("CandleStick"), QVariant(CandleStickSeries));
    connect(m_seriesSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(seriesChanged(int)));

    m_thicknessLabelBox = new QLabel(tr("Thickness"), seriesTypeGroup);
    m_thicknessBox = new QSpinBox(seriesTypeGroup);
    m_thicknessBox->setValue(5);
    m_thicknessBox->setSingleStep(1);
    m_thicknessBox->setRange(1, 5);
    connect(m_thicknessBox, SIGNAL(valueChanged(int)), this, SLOT(valueThicknessChanged(int)));

    localLayout->addRow(m_seriesSwitcher);
    localLayout->addRow(m_thicknessLabelBox, m_thicknessBox);
}

static void appendSumbolsPoint(ChartOpenHighLowCloseSeries* series, const QString& arg, qreal low, qreal high, qreal open, qreal close)
{
    series->addXYY1Y2Y3(arg, low, high, open, close);
}

void MainWindow::createOpenHighLowCloseSeries()
{
    createTitle(tr("Stock Prices (OHLC)"));
    m_chart->legend()->setVisible(true);

    ChartOpenHighLowCloseSeries* series = new ChartOpenHighLowCloseSeries();
    series->setName(tr("Microsoft"));
    //                             Time         low,   high,   open,   close
    appendSumbolsPoint(series, tr("2018-Jan-24"), 305.19, 315.80, 311.53, 306.40);
    appendSumbolsPoint(series, tr("2018-Jan-25"), 307.23, 313.5,  307.83, 312.88);
    appendSumbolsPoint(series, tr("2018-Jan-26"), 310.8,  315.60, 312.36, 311.40);
    appendSumbolsPoint(series, tr("2018-Jan-27"), 311.48, 316.50, 313.10, 315.25);
    appendSumbolsPoint(series, tr("2018-Jun-04"), 314.59, 328.00, 315.02, 324.15);
    m_chart->appendSeries(series);

    const SeriesList& seriesList = m_chart->series();
    ChartView2D* view2D = static_cast<ChartView2D *>(seriesList.at(0)->view());
    view2D->axisY()->title()->setText(tr("Price"));
    view2D->axisY()->title()->setVisible(true);
    view2D->axisY()->setAutoRange(false);
    view2D->axisY()->setFixedRange(300, 335);
    view2D->axisY()->setShowZeroLevel(false);
    view2D->axisY()->setMinorGridLinesVisible(false);
    view2D->axisX()->title()->setText(tr("Day"));
    view2D->axisX()->title()->setVisible(true);
    view2D->axisX()->label()->setVisible(true);
}

void MainWindow::createCandleStickSeries()
{
    createTitle(tr("Stock Prices (Candlestick)"));
    m_chart->legend()->setVisible(true);

    ChartCandleStickSeries* series = new ChartCandleStickSeries();
    series->setName(tr("ACME Corp."));
    //                             Time      low,   high,  open,   close
    appendSumbolsPoint(series, QStringLiteral("28"), 40.89, 41.18, 41.00, 41.17);
    appendSumbolsPoint(series, QStringLiteral("29"), 41.23, 41.50, 41.35, 41.39);
    appendSumbolsPoint(series, QStringLiteral("30"), 40.8,  41.29, 41.15, 40.96);
    appendSumbolsPoint(series, QStringLiteral("31"), 40.48, 40.99, 40.98, 40.48);
    appendSumbolsPoint(series, QStringLiteral("04"), 40.59, 41.10, 40.62, 40.95);
    appendSumbolsPoint(series, QStringLiteral("05"), 40.64, 41.10, 40.85, 40.96);
    appendSumbolsPoint(series, QStringLiteral("06"), 40.52, 41.08, 40.88, 40.77);
    appendSumbolsPoint(series, QStringLiteral("07"), 40.19, 40.70, 40.63, 40.45);
    appendSumbolsPoint(series, QStringLiteral("08"), 40.24, 40.88, 40.28, 40.66);
    appendSumbolsPoint(series, QStringLiteral("11"), 40.12, 40.76, 40.71, 40.27);
    appendSumbolsPoint(series, QStringLiteral("12"), 39.91, 40.40, 40.15, 40.07);
    appendSumbolsPoint(series, QStringLiteral("13"), 40.01, 40.52, 40.26, 40.35);
    appendSumbolsPoint(series, QStringLiteral("14"), 40.26, 41.10, 40.31, 40.96);
    appendSumbolsPoint(series, QStringLiteral("15"), 40.71, 41.24, 41.08, 40.86);
    appendSumbolsPoint(series, QStringLiteral("19"), 40.68, 41.24, 40.75, 41.10);
    appendSumbolsPoint(series, QStringLiteral("20"), 40.31, 40.94, 40.81, 40.59);
    appendSumbolsPoint(series, QStringLiteral("21"), 40.00, 40.72, 40.61, 40.01);
    appendSumbolsPoint(series, QStringLiteral("22"), 38.84, 40.20, 40.00, 38.96);
    appendSumbolsPoint(series, QStringLiteral("25"), 39.10, 39.66, 39.24, 39.32);
    appendSumbolsPoint(series, QStringLiteral("26"), 39.09, 39.85, 39.20, 39.50);
    appendSumbolsPoint(series, QStringLiteral("27"), 39.02, 39.82, 39.35, 39.67);
    appendSumbolsPoint(series, QStringLiteral("28"), 38.89, 39.87, 39.84, 39.16);
    appendSumbolsPoint(series, QStringLiteral("29"), 37.66, 39.92, 39.90, 38.18);
    appendSumbolsPoint(series, QStringLiteral("01"), 37.92, 38.48, 38.39, 38.41);
    appendSumbolsPoint(series, QStringLiteral("02"), 38.14, 38.50, 38.37, 38.46);
    appendSumbolsPoint(series, QStringLiteral("03"), 38.12, 38.79, 38.26, 38.63);
    m_chart->appendSeries(series);

    const SeriesList& series2D = m_chart->series();
    ChartView2D* view2D = static_cast<ChartView2D *>(series2D.at(0)->view());
    view2D->axisY()->title()->setText(tr("Price"));
    view2D->axisY()->title()->setVisible(true);
    view2D->axisY()->setAutoRange(false);
    view2D->axisY()->setFixedRange(37, 43);
    view2D->axisY()->setShowZeroLevel(false);
    view2D->axisY()->setMinorGridLinesVisible(false);

    view2D->axisX()->title()->setText(tr("Day"));
    view2D->axisX()->title()->setVisible(true);
    view2D->axisX()->label()->setVisible(true);
    view2D->axisX()->setMinorCount(0);
}

void MainWindow::updateValueParameters()
{
    valueThicknessChanged(m_thicknessBox->value());
}

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

    QVariant var = m_seriesSwitcher->itemData(index);
    switch((SeriesType)var.toUInt())
    {
        case OpenHighLowCloseSeries:
            {
                m_dataLabelsGroup->show();
                m_markerShapeSizeTitleSwitcher->show();
                m_dataMarkersGroup->hide();
                m_dataLabelsGroup->hide();
                createOpenHighLowCloseSeries();
            }
            break;
        case CandleStickSeries:
        {
            m_dataLabelsGroup->show();
            m_markerShapeSizeTitleSwitcher->show();
            m_dataMarkersGroup->hide();
            m_dataLabelsGroup->hide();
            createCandleStickSeries();
            m_thicknessBox->setValue(2);
//            valueThicknessChanged(2);
        }
        break;
        default:
            break;
    }
    updateValueParameters();
}

void MainWindow::valueThicknessChanged(int value)
{
    const SeriesList& listSeries = m_chart->series();
    for (int i = 0; i < listSeries.count(); i++)
    {
        if (ChartOpenHighLowCloseSeries* series = qobject_cast<ChartOpenHighLowCloseSeries*>(listSeries.at(i)))
            series->setLineThickness(value);
    }
}