BarChart2D Example

#include "mainwindow.h"

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

/* MainWindow */
MainWindow::MainWindow()
    : DemoChartWindow(tr("Bar Chart"))
{
    setChart(Q_NULL);
    m_displayShadow = true;
    createSeriesParametrs();
    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("Clustered"),            QVariant(ClusteredSeries));
    m_seriesSwitcher->addItem(tr("Stacked"),              QVariant(StackedSeries));
    m_seriesSwitcher->addItem(tr("100% Stacked"),         QVariant(Stacked100Series));
    m_seriesSwitcher->addItem(tr("Side by Side Stacked"), QVariant(SideBySideSeries));
    connect(m_seriesSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(seriesChanged(int)));
    localLayout->addRow(m_seriesSwitcher);

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

    // Option Labels
    m_dataLabelsGroup = createGroupParameters(tr("Show Data Labels"), true);
    localLayout = (QFormLayout*)m_dataLabelsGroup->layout();

    connect(m_dataLabelsGroup, SIGNAL(toggled(bool)), this, SLOT(showDataLabels(bool)));
    m_posDataLabelsSwitcher = new QComboBox(m_dataLabelsGroup);
    m_posDataLabelsSwitcher->addItem(tr("Top"),    QVariant(ChartBarSeriesLabel::ChartBarLabelTop));
    m_posDataLabelsSwitcher->addItem(tr("Center"), QVariant(ChartBarSeriesLabel::ChartBarLabelCenter));
    m_posDataLabelsSwitcher->setCurrentIndex(1);
    connect(m_posDataLabelsSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(labelsPositionChanged(int)));
    localLayout->addRow(m_posDataLabelsSwitcher);
}

ChartBarSeries2D* MainWindow::createSeries(SeriesType type)
{
    ChartBarSeries2D* series = (type == StackedSeries ||
        type == Stacked100Series) ? new ChartStackedBarSeries2D : new ChartBarSeries2D;

    if (type == Stacked100Series)
        ((ChartStackedBarSeries2D*)series)->setStackHeight(100.0);

    return series;
}

void MainWindow::createBardSeries(SeriesType type)
{
    createTitle(tr("Results"));

    m_chart->legend()->setVisible(true);
    m_chart->legend()->setVerticalAlignment(ChartLegend::LegendCenter);

    ChartBarSeries2D* series1 = createSeries(type);
    m_chart->appendSeries(series1);

    series1->setName(tr("Atom 330"));
    series1->addAxisPointY(490, QStringLiteral("Double-Precision 2"));
    series1->addAxisPointY(50,  QStringLiteral("Double-Precision 3"));
    series1->addAxisPointY(100, QStringLiteral("Double-Precision 4"));
    series1->addAxisPointY(400, QStringLiteral("Double-Precision 5"));
    series1->addAxisPointY(60,  QStringLiteral("Double-Precision 6"));
    series1->addAxisPointY(530, QStringLiteral("Double-Precision 7"));
    series1->addAxisPointY(230, QStringLiteral("Double-Precision 8"));
    series1->addAxisPointY(510, QStringLiteral("Double-Precision 9"));
    series1->addAxisPointY(505, QStringLiteral("Double-Precision 10"));
    series1->addAxisPointY(490, QStringLiteral("Double-Precision 11"));

    const ViewList& listViews = m_chart->views();
    ChartView2D* view2D = qobject_cast<ChartView2D*>(listViews.at(0));
    ChartAxis* axisX = view2D->axisX();
    axisX->label()->setAngle(45);

    ChartBarSeries2D* series2 = createSeries(type);
    m_chart->appendSeries(series2);

    series2->setName(tr("Athlon 64 x2"));
    series2->addAxisPointY(1000, QStringLiteral("Double-Precision 2"));
    series2->addAxisPointY(500,  QStringLiteral("Double-Precision 3"));
    series2->addAxisPointY(100,  QStringLiteral("Double-Precision 4"));
    series2->addAxisPointY(900,  QStringLiteral("Double-Precision 5"));
    series2->addAxisPointY(100,  QStringLiteral("Double-Precision 6"));
    series2->addAxisPointY(730,  QStringLiteral("Double-Precision 7"));
    series2->addAxisPointY(30,   QStringLiteral("Double-Precision 8"));
    series2->addAxisPointY(100,  QStringLiteral("Double-Precision 9"));
    series2->addAxisPointY(1600, QStringLiteral("Double-Precision 10"));
    series2->addAxisPointY(890 , QStringLiteral("Double-Precision 11"));

    ChartBarSeries2D* series3 = createSeries(type);
    m_chart->appendSeries(series3);

    series3->setName(tr("2x Xeon 5500"));
    series3->addAxisPointY(1900, QStringLiteral("Double-Precision 2"));
    series3->addAxisPointY(600,  QStringLiteral("Double-Precision 3"));
    series3->addAxisPointY(500,  QStringLiteral("Double-Precision 4"));
    series3->addAxisPointY(2500, QStringLiteral("Double-Precision 5"));
    series3->addAxisPointY(1500, QStringLiteral("Double-Precision 6"));
    series3->addAxisPointY(1500, QStringLiteral("Double-Precision 7"));
    series3->addAxisPointY(3000, QStringLiteral("Double-Precision 8"));
    series3->addAxisPointY(2400, QStringLiteral("Double-Precision 9"));
    series3->addAxisPointY(1600, QStringLiteral("Double-Precision 10"));
    series3->addAxisPointY(890,  QStringLiteral("Double-Precision 11"));
}

void MainWindow::createSideBySideSeries()
{
    m_chart->legend()->setVisible(true);
    m_chart->legend()->setVerticalAlignment(ChartLegend::LegendNear);

    for (int i = 0; i < 4; i++)
    {
        ChartStackedBarSeries2D* series = new ChartStackedBarSeries2D();
        m_chart->appendSeries(series);
        series->setName(QStringLiteral("Qtn %1 Project 1").arg(i + 1));
        series->setStackGroup(i);

        series->addAxisPointY(8 + rand() % 20,  tr("USA"));
        series->addAxisPointY(12 + rand() % 20, tr("Europe"));
        series->addAxisPointY(7 + rand() % 20,  tr("Asia"));

        series = new ChartStackedBarSeries2D();
        m_chart->appendSeries(series);
        series->setName(QStringLiteral("Qtn %1 Project 2").arg(i + 1));
        series->setStackGroup(i);

        series->addAxisPointY(10 + rand() % 20, tr("USA"));
        series->addAxisPointY(8 + rand() % 20,  tr("Europe"));
        series->addAxisPointY(12 + rand() % 20, tr("Asia"));
    }
}

void MainWindow::updateValueParameters()
{
    DemoChartWindow::updateValueParameters();
    m_displayShadowBox->setCheckState(m_displayShadow ? Qt::Checked : Qt::Unchecked);
    labelsPositionChanged(m_posDataLabelsSwitcher->currentIndex());
}

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 ClusteredSeries :
        case StackedSeries :
        case Stacked100Series :
                createBardSeries((SeriesType)var.toUInt());
            break;
        case SideBySideSeries :
                createSideBySideSeries();
            break;
        default:
            break;
    }
    displayShadowSeries();
    updateValueParameters();
}

void MainWindow::labelsPositionChanged(int index)
{
    QVariant var = m_posDataLabelsSwitcher->itemData(index);

    const SeriesList& listSeries = m_chart->series();
    for (int i = 0; i < listSeries.count(); i++)
    {
        ChartBarSeries2D* series= (ChartBarSeries2D*)listSeries.at(i);
        ChartBarSeriesLabel* label = (ChartBarSeriesLabel*)series->label();
        label->setPosition((ChartBarSeriesLabel::BarLabelPosition)(var.toUInt()));
    }
}

void MainWindow::typeSeriesChanged(int state)
{
    Q_UNUSED(state)
}

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