FunnelChart2D Example

#include "mainwindow.h"

#include <QGroupBox>
#include <QComboBox>
#include <QFormLayout>
#include <QDoubleSpinBox>

/* MainWindow */
MainWindow::MainWindow()
    : DemoChartWindow(tr("Funnel Chart 2D"))
{
    setChart(Q_NULL);
    createSeriesParametrs();
    createFunnelSeries();
    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);

    m_neckHeightInPercents = new QSlider(Qt::Horizontal, seriesTypeGroup);
    m_neckHeightInPercents->setRange(0, 100);
    m_neckHeightInPercents->setSliderPosition(25);
    m_neckHeightInPercents->setSingleStep(1);
    connect(m_neckHeightInPercents, SIGNAL(valueChanged(int)), this, SLOT(neckHeightInPercentsChanged(int)));
    localLayout->addRow(new QLabel(QObject::tr("Neck Height %:"), seriesTypeGroup));
    localLayout->addRow(m_neckHeightInPercents);

    // 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::createFunnelSeries()
{
    createTitle(tr("Web Site Visitor Trend"));
    m_chart->legend()->setVisible(true);

    ChartFunnelSeries2D* series = new ChartFunnelSeries2D();

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

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

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

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

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

    series->setNeckHeightInPercents(25.0);

    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 (ChartFunnelSeries2D* series = qobject_cast<ChartFunnelSeries2D*>(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 (ChartFunnelSeries2D* series = qobject_cast<ChartFunnelSeries2D*>(listSeries.at(i)))
            series->setGapSize(value);
    }
}

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

    for (int i = 0; i < listSeries.count(); i++)
    {
        if (ChartFunnelSeries2D* series = qobject_cast<ChartFunnelSeries2D*>(listSeries.at(i)))
            series->setNeckHeightInPercents(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++)
    {
        ChartFunnelSeries2D* series = (ChartFunnelSeries2D*)listSeries.at(i);
        if (ChartPyramidSeriesLabel* label = (ChartPyramidSeriesLabel*)series->label())
            label->setPosition((ChartPyramidSeriesLabel::Position)var.toInt());
    }
}