PyramidChart2D Example

#include "mainwindow.h"

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

/* MainWindow */
MainWindow::MainWindow()
    : DemoChartWindow(tr("Pyramid Chart 2D"))
{
    setChart(new Chart);
    createSeriesParametrs();
    createPyramidSeries();
    updateValueParameters();
}

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

    m_transparency = new QSlider(Qt::Horizontal, seriesTypeGroup);
    m_transparency->setRange(0, 255);
    m_transparency->setSliderPosition(255);
    m_transparency->setSingleStep(5);
    connect(m_transparency, SIGNAL(valueChanged(int)), this, SLOT(transparencyChanged(int)));
    localLayout->addRow(new QLabel(QObject::tr("Transparency:"), seriesTypeGroup));
    localLayout->addRow(m_transparency);

    m_gapSize = new QSlider(Qt::Horizontal, seriesTypeGroup);
    m_gapSize->setRange(0, 20);
    m_gapSize->setSliderPosition(4);
    m_gapSize->setSingleStep(1);
    connect(m_gapSize, SIGNAL(valueChanged(int)), this, SLOT(pointGapChanged(int)));
    localLayout->addRow(new QLabel(QObject::tr("Point Gap:"), seriesTypeGroup));
    localLayout->addRow(m_gapSize);

    // 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_angleDataLabelsSwitcher = new QComboBox(m_dataLabelsGroup);
    m_angleDataLabelsSwitcher->addItem(tr("Left"), ChartPyramidSeriesLabel::Left);
    m_angleDataLabelsSwitcher->addItem(tr("Right"), ChartPyramidSeriesLabel::Right);
    m_angleDataLabelsSwitcher->addItem(tr("Center"), ChartPyramidSeriesLabel::Center);
    m_angleDataLabelsSwitcher->setCurrentIndex(1);
    connect(m_angleDataLabelsSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(labelsPositionChanged(int)));
    // Layouts Labels
    localLayout->addRow(tr("Position:"), m_angleDataLabelsSwitcher);

}

void MainWindow::createPyramidSeries()
{
    createTitle(tr("Web Site Visitor Trend"));
    m_chart->legend()->setVisible(true);

    ChartPyramidSeries2D* series = new ChartPyramidSeries2D();

    ChartDataPoint* pnt = series->add(9152, tr("Visited a Web Site: 9152"));
    pnt->setLegendText(tr("Visited a Web Site: 100%"));

    pnt = series->add(6870.0, tr("Downloaded a Trial: 6870"));
    pnt->setLegendText(tr("Downloaded a Trial: 75%"));

    pnt = series->add(5121.0, tr("Contacted to Support: 5121"));
    pnt->setLegendText(tr("Contacted to Support: 56%"));

    pnt = series->add(2224.0,  tr("Subscribed: 2224"));
    pnt->setLegendText(tr("Subscribed: 24%"));

    pnt = series->add(1670.0,  tr("Renewed: 1670"));
    pnt->setLegendText(tr("Renewed: 18%"));

//    series->setEqualHeight(true);
    m_chart->appendSeries(series);
}

void MainWindow::updateValueParameters()
{
    DemoChartWindow::updateValueParameters();
    transparencyChanged(m_transparency->value());
    pointGapChanged(m_gapSize->value());
    labelsPositionChanged(m_angleDataLabelsSwitcher->currentIndex());
}

void MainWindow::transparencyChanged(int value)
{
    const SeriesList& listSeries = m_chart->series();

    for (int i = 0; i < listSeries.count(); i++)
    {
        if (ChartPyramidSeries2D* series = qobject_cast<ChartPyramidSeries2D*>(listSeries.at(i)))
            series->setTransparency(value);
    }
}

void MainWindow::pointGapChanged(int value)
{
    const SeriesList& listSeries = m_chart->series();

    for (int i = 0; i < listSeries.count(); i++)
    {
        if (ChartPyramidSeries2D* series = qobject_cast<ChartPyramidSeries2D*>(listSeries.at(i)))
            series->setGapSize(value);
    }
}

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

    const SeriesList& listSeries = m_chart->series();
    for (int i = 0, count = listSeries.count(); i < count; i++)
    {
        ChartPyramidSeries2D* series = (ChartPyramidSeries2D*)listSeries.at(i);
        if (ChartPyramidSeriesLabel* label = (ChartPyramidSeriesLabel*)series->label())
            label->setPosition((ChartPyramidSeriesLabel::Position)var.toInt());
    }
}