LineChart2D Example

#include "mainwindow.h"

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

/* MainWindow */
MainWindow::MainWindow()
    : DemoChartWindow(tr("Line Charts"))
{
    m_displayShadow = true;
    m_lineThicknessIndex = 2;

    m_arYears[0] = QStringLiteral("2008");
    m_arYears[1] = QStringLiteral("2009");
    m_arYears[2] = QStringLiteral("2010");
    m_arYears[3] = QStringLiteral("2011");
    m_arYears[4] = QStringLiteral("2012");

    setChart(Q_NULL);

    createSeriesParametrs();
    createMarkersGroup();
    createLabelsGroup();

    seriesChanged(m_seriesSwitcher->currentIndex());
}

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

    m_seriesSwitcher = new QComboBox(seriesTypeGroup);
    m_seriesSwitcher->addItem(tr("Line"), QVariant(LineSeries));
    m_seriesSwitcher->addItem(tr("Spline"), QVariant(SplineSeries));
    m_seriesSwitcher->addItem(tr("StepLine"), QVariant(StepLineSeries));
    connect(m_seriesSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(seriesChanged(int)));

    m_lineThicknessSwitcher = new QComboBox(seriesTypeGroup);
    m_lineThicknessSwitcher->addItem(tr("1 Pixel"),  QVariant(1));
    m_lineThicknessSwitcher->addItem(tr("2 Pixels"), QVariant(2));
    m_lineThicknessSwitcher->addItem(tr("3 Pixels"), QVariant(3));
    m_lineThicknessSwitcher->addItem(tr("4 Pixels"), QVariant(4));
    m_lineThicknessSwitcher->addItem(tr("5 Pixels"), QVariant(5));
    connect(m_lineThicknessSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(lineThicknessChanged(int)));

    m_displayShadowBox = new QCheckBox(tr("Display shadow"), this);
    connect(m_displayShadowBox, SIGNAL(toggled(bool)), this, SLOT(displayShadowChanged(bool)));

    // Layouts Series
    localLayout->addRow(m_seriesSwitcher);
    localLayout->addRow(new QLabel(QObject::tr("Line thickness:")), m_lineThicknessSwitcher);
    localLayout->addRow(m_displayShadowBox);
}

void MainWindow::createLineSeries()
{
    createTitle(tr("PC Sales"));

    m_chart->legend()->setVisible(true);
    m_chart->legend()->setTextColor(Qt::black);

    ChartLineSeries2D* series1 = new ChartLineSeries2D();
    m_chart->appendSeries(series1);
    series1->setDashStyle(Qt::DashLine);
    series1->setName(QObject::tr("HP"));

    series1->addAxisPointY(1500, m_arYears[0]);
    series1->addAxisPointY(2300, m_arYears[1]);
    series1->addAxisPointY(2500, m_arYears[2]);
    series1->addAxisPointY(2800, m_arYears[3]);
    series1->addAxisPointY(3000, m_arYears[4]);

    ChartLineSeries2D* series2 = new ChartLineSeries2D();
    m_chart->appendSeries(series2);
    series2->setDashStyle(Qt::DashDotLine);
    series2->setName(QObject::tr("Samsung"));

    series2->addAxisPointY(1200, m_arYears[0]);
    series2->addAxisPointY(1400, m_arYears[1]);
    series2->addAxisPointY(1500, m_arYears[2]);
    series2->addAxisPointY(1700, m_arYears[3]);
    series2->addAxisPointY(1800, m_arYears[4]);

    ChartLineSeries2D* series3 = new ChartLineSeries2D();
    m_chart->appendSeries(series3);
    series3->setName(QObject::tr("Acer"));

    series3->addAxisPointY(1300, m_arYears[0]);
    series3->addAxisPointY(1500, m_arYears[1]);
    series3->addAxisPointY(2000, m_arYears[2]);
    series3->addAxisPointY(2600, m_arYears[3]);
    series3->addAxisPointY(3200, m_arYears[4]);
}

void MainWindow::createSplineSeries()
{
    createTitle(tr("Population"));
    m_chart->legend()->setVisible(true);
    m_chart->legend()->setTextColor(Qt::black);

    ChartSplineSeries2D* series1 = new ChartSplineSeries2D();
    m_chart->appendSeries(series1);
    series1->setName(tr("Moscow"));

    series1->addXY(1900, 8268);
    series1->addXY(1950, 24830);
    series1->addXY(1990, 27990);
    series1->addXY(2000, 28976);
    series1->addXY(2008, 29490);

    ChartSplineSeries2D* series2 = new ChartSplineSeries2D();
    m_chart->appendSeries(series2);
    series2->setName(tr("Voronezh"));

    series2->addXY(1900, 7485);
    series2->addXY(1950, 20586);
    series2->addXY(1990, 23760);
    series2->addXY(2000, 23871);
    series2->addXY(2008, 26756);

    ChartSplineSeries2D* series3 = new ChartSplineSeries2D();
    m_chart->appendSeries(series3);
    series3->setName(tr("Ryazan"));

    series3->addXY(1900, 1028);
    series3->addXY(1950, 1871);
    series3->addXY(1990, 12100);
    series3->addXY(2000, 14982);
    series3->addXY(2008, 15328);

    ChartSplineSeries2D* series4 = new ChartSplineSeries2D();
    m_chart->appendSeries(series4);
    series4->setName(tr("Cherepovets"));

    series4->addXY(1900, 2048);
    series4->addXY(1950, 5711);
    series4->addXY(1990, 12986);
    series4->addXY(2000, 10851);
    series4->addXY(2008, 20326);

    const SeriesList& series2D = m_chart->series();
    // Set the X and Y Axis title for the series.
    ChartView2D* view2D = static_cast<ChartView2D*>(series2D.at(0)->view());
    if (ChartAxis* axisX = view2D->axisX())
    {
        if (ChartAxisTitle* title = axisX->title())
        {
            title->setText(tr("Decade / Year"));
            title->setVisible(true);
        }
    }

    if (ChartAxis* axisY = view2D->axisY())
    {
        if (ChartAxisTitle* title = axisY->title())
        {
            title->setText(tr("Population Growth (Thousands)"));
            title->setVisible(true);
        }
    }
}

void MainWindow::createStepLineSeries()
{
    createTitle(tr("Transport expenses"));
    ChartTitle* title = m_chart->titles().at(0);
    title->setTextColor(Qt::white);

    m_chart->legend()->setVisible(true);
    m_chart->legend()->setTextColor(Qt::white);
    m_chart->setBackgroundBrush(QBrush(QPixmap(QStringLiteral(":/res/cars.png"))));

    ChartStepLineSeries2D* series = new ChartStepLineSeries2D();
    m_chart->appendSeries(series);

    ChartView2D* view2D = static_cast<ChartView2D *>(series->view());
    view2D->setBackgroundColor(QColor(192, 192, 192, 150));
    view2D->axisX()->label()->setTextColor(Qt::white);
    view2D->axisY()->label()->setTextColor(Qt::white);
    view2D->axisX()->setColor(Qt::white);
    view2D->axisY()->setColor(Qt::white);

    series->setName(QObject::tr("Series 1"));

    QColor colorPoint = QColor(Qt::red).darker();
    series->setColor(colorPoint);
    series->addAxisPointY(50.0,  m_arYears[0])->setColor(colorPoint);
    series->addAxisPointY(80.0,  m_arYears[1])->setColor(colorPoint);
    series->addAxisPointY(40.0,  m_arYears[2])->setColor(colorPoint);
    series->addAxisPointY(100.0, m_arYears[3])->setColor(colorPoint);
    series->addAxisPointY(200.0, m_arYears[4])->setColor(colorPoint);
}

void MainWindow::updateValueParameters()
{
    DemoChartWindow::updateValueParameters();
    m_displayShadowBox->setCheckState(m_displayShadow ? Qt::Checked : Qt::Unchecked);
    m_lineThicknessSwitcher->setCurrentIndex(m_lineThicknessIndex);
}

void MainWindow::displayShadowSeries()
{
    const SeriesList& listSeries = m_chart->series();
    for (int i = 0, count = listSeries.count(); i < count; i++)
    {
        ChartSeries* series = listSeries.at(i);
        series->setShadow(m_displayShadow);
    }
}

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 LineSeries :
                createLineSeries();
            break;
        case SplineSeries :
                createSplineSeries();
            break;
        case StepLineSeries :
                createStepLineSeries();
            break;
        default:
            break;
    }
    displayShadowSeries();
    lineThicknessSeries();
    updateValueParameters();
}

void MainWindow::lineThicknessChanged(int index)
{
    if (m_lineThicknessIndex != index)
    {
        m_lineThicknessIndex = index;
        lineThicknessSeries();
    }
}

void MainWindow::displayShadowChanged(bool checked)
{
    if (m_displayShadow != checked)
    {
        m_displayShadow = checked;
        displayShadowSeries();
    }
}

void MainWindow::lineThicknessSeries()
{
    QVariant var = m_lineThicknessSwitcher->itemData(m_lineThicknessIndex);

    const SeriesList& listSeries = m_chart->series();
    for (int i = 0, count = listSeries.count(); i < count; i++)
    {
        if (ChartLineSeries2D* series = qobject_cast<ChartLineSeries2D*>(listSeries.at(i)))
            series->setThickness(var.toInt());
    }
}