From f6ca68eccfdafc95bede063c16020fc1c3a6a5b7 2013-04-17 07:46:47 From: Mika Salmela Date: 2013-04-17 07:46:47 Subject: [PATCH] Improved handling of different serieses. Change-Id: If5e59868a1a01818fe70c1a9882a315a57543c95 Reviewed-by: Mika Salmela --- diff --git a/examples/boxplotchart/main.cpp b/examples/boxplotchart/main.cpp index f21ae6f..2028257 100644 --- a/examples/boxplotchart/main.cpp +++ b/examples/boxplotchart/main.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -42,6 +43,12 @@ int main(int argc, char *argv[]) QBarSet *set3 = new QBarSet("Apr"); QBarSet *set4 = new QBarSet("May"); QBarSet *set5 = new QBarSet("Jun"); + QBarSet *set6 = new QBarSet("Jul"); + QBarSet *set7 = new QBarSet("Aug"); + QBarSet *set8 = new QBarSet("Sep"); + QBarSet *set9 = new QBarSet("Oct"); + QBarSet *set10 = new QBarSet("Nov"); + QBarSet *set11 = new QBarSet("Dec"); // low bot med top upp *set0 << 3 << 4 << 4.4 << 6 << 7; @@ -50,10 +57,13 @@ int main(int argc, char *argv[]) *set3 << 5 << 6 << 6.8 << 7 << 8; *set4 << 4 << 5 << 5.2 << 6 << 7; *set5 << 4 << 7 << 8.2 << 9 << 10; + *set6 << 2.5 << 5 << 5.4 << 6 << 7; + *set7 << 5 << 6.3 << 7.5 << 8 << 12; + *set8 << 2.6 << 5.1 << 5.7 << 8 << 9; + *set9 << 3.1 << 5.8 << 6.8 << 7 << 8; + *set10 << 4.2 << 5 << 5.8 << 6 << 7; + *set11 << 4.7 << 7 << 8.2 << 9 << 10; - set0->setBrush(QBrush(QColor(Qt::yellow))); - - //set0->setColor(QColor(Qt::darkRed)); //![1] //![2] @@ -64,21 +74,41 @@ int main(int argc, char *argv[]) series->append(set3); series->append(set4); series->append(set5); - series->type(); + series->append(set6); + series->append(set7); + series->append(set8); + series->append(set9); + series->append(set10); + series->append(set11); series->setName("Box & Whiskers"); - //series->setBrush(QBrush(QColor(Qt::yellow))); //![2] + QLineSeries *lineSeries = new QLineSeries(); + lineSeries->append(0, 4.4); + lineSeries->append(1, 7.5); + lineSeries->append(2, 5.7); + lineSeries->append(3, 6.8); + lineSeries->append(4, 5.2); + lineSeries->append(5, 8.2); + lineSeries->append(6, 5.4); + lineSeries->append(7, 7.5); + lineSeries->append(8, 5.7); + lineSeries->append(9, 6.8); + lineSeries->append(10, 5.2); + lineSeries->append(11, 8.2); + lineSeries->setName("Medians"); + //![3] QChart *chart = new QChart(); chart->addSeries(series); + chart->addSeries(lineSeries); chart->setTitle("Simple boxplotchart example"); chart->setAnimationOptions(QChart::SeriesAnimations); //![3] //![4] QStringList categories; - categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; + categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; QBarCategoryAxis *axis = new QBarCategoryAxis(); axis->append(categories); chart->createDefaultAxes(); diff --git a/src/boxplotchart/boxplotchartitem.cpp b/src/boxplotchart/boxplotchartitem.cpp index 48cd2b8..0bd744c 100644 --- a/src/boxplotchart/boxplotchartitem.cpp +++ b/src/boxplotchart/boxplotchartitem.cpp @@ -113,6 +113,13 @@ void BoxPlotChartItem::handleUpdatedBars() item->setBrush(m_series->brush()); item->setPen(m_series->pen()); } + // Override with QBarSet specific settings + foreach (QBarSet *set, m_boxTable.keys()) { + if (set->brush().style() != Qt::NoBrush) + m_boxTable.value(set)->setBrush(set->brush()); + if (set->pen().style() != Qt::NoPen) + m_boxTable.value(set)->setPen(set->pen()); + } } void BoxPlotChartItem::handleBarsetRemove(QList barSets) @@ -177,6 +184,8 @@ void BoxPlotChartItem::initializeLayout() QVector BoxPlotChartItem::calculateLayout() { + qDebug() << "ALERT EMPTY: BoxPlotChartItem::calculateLayout()"; + return QVector(); } diff --git a/src/boxplotchart/boxwhiskers.cpp b/src/boxplotchart/boxwhiskers.cpp index 0ed71ff..4f34abe 100644 --- a/src/boxplotchart/boxwhiskers.cpp +++ b/src/boxplotchart/boxwhiskers.cpp @@ -61,11 +61,13 @@ void BoxWhiskers::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) void BoxWhiskers::setBrush(const QBrush &brush) { m_brush = brush; + update(); } void BoxWhiskers::setPen(const QPen &pen) { m_pen = pen; + update(); } void BoxWhiskers::setLayout(const BoxWhiskersData &data) diff --git a/src/boxplotchart/qboxplotseries.cpp b/src/boxplotchart/qboxplotseries.cpp index 8c338c9..96fff7b 100644 --- a/src/boxplotchart/qboxplotseries.cpp +++ b/src/boxplotchart/qboxplotseries.cpp @@ -168,17 +168,19 @@ void QBoxPlotSeriesPrivate::initializeGraphics(QGraphicsItem* parent) connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), this, SLOT(handleSeriesRemove(QAbstractSeries*)) ); QList serieses = m_chart->series(); - boxPlot->m_seriesCount = serieses.count(); // Tries to find this series from the Chart's list of serieses and deduce the index int index = 0; foreach (QAbstractSeries *s, serieses) { - if (q == static_cast(s)) { - boxPlot->m_seriesIndex = index; - m_index = index; + if (s->type() == QAbstractSeries::SeriesTypeBoxPlot) { + if (q == static_cast(s)) { + boxPlot->m_seriesIndex = index; + m_index = index; + } + index++; } - index++; } + boxPlot->m_seriesCount = index; } // Make BoxPlotChartItem to instantiate box & whisker items @@ -200,12 +202,6 @@ void QBoxPlotSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool f if (forced || m_pen == QPen(Qt::NoPen)) { QPen pen = theme->outlinePen(); pen.setCosmetic(true); - -// QPen pen; -// pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1.0)); -// pen.setWidthF(2.0); -// pen.setCosmetic(true); - q->setPen(pen); } } @@ -262,17 +258,19 @@ void QBoxPlotSeriesPrivate::handleSeriesChange(QAbstractSeries *series) if (m_chart) { QList serieses = m_chart->series(); - boxPlot->m_seriesCount = serieses.count(); // Tries to find this series from the Chart's list of serieses and deduce the index int index = 0; foreach (QAbstractSeries *s, serieses) { - if (q == static_cast(s)) { - boxPlot->m_seriesIndex = index; - m_index = index; + if (s->type() == QAbstractSeries::SeriesTypeBoxPlot) { + if (q == static_cast(s)) { + boxPlot->m_seriesIndex = index; + m_index = index; + } + index++; } - index++; } + boxPlot->m_seriesCount = index; } boxPlot->handleDataStructureChanged(); diff --git a/tests/boxplottester/mainwidget.cpp b/tests/boxplottester/mainwidget.cpp index 0252272..0f7d5fc 100644 --- a/tests/boxplottester/mainwidget.cpp +++ b/tests/boxplottester/mainwidget.cpp @@ -76,11 +76,26 @@ MainWidget::MainWidget(QWidget *parent) : connect(addBoxButton, SIGNAL(clicked()), this, SLOT(addBox())); grid->addWidget(addBoxButton, rowPos++, 1); + // Create insert a box button + QPushButton *insertBoxButton = new QPushButton("Insert a box"); + connect(insertBoxButton, SIGNAL(clicked()), this, SLOT(insertBox())); + grid->addWidget(insertBoxButton, rowPos++, 1); + // Create add a single box button QPushButton *removeBoxButton = new QPushButton("Remove a box"); connect(removeBoxButton, SIGNAL(clicked()), this, SLOT(removeBox())); grid->addWidget(removeBoxButton, rowPos++, 1); + // Create clear button + QPushButton *clearButton = new QPushButton("Clear"); + connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); + grid->addWidget(clearButton, rowPos++, 1); + + // Create set brush button + QPushButton *setBrushButton = new QPushButton("Set brush"); + connect(setBrushButton, SIGNAL(clicked()), this, SLOT(setBrush())); + grid->addWidget(setBrushButton, rowPos++, 1); + initThemeCombo(grid); initCheckboxes(grid); @@ -208,6 +223,8 @@ void MainWidget::addSeries() void MainWidget::removeSeries() { + qDebug() << "BoxPlotTester::MainWidget::removeSeries()"; + if (nSeries > 0) { nSeries--; m_chart->removeSeries(m_series[nSeries]); @@ -233,9 +250,25 @@ void MainWidget::addBox() } } +void MainWidget::insertBox() +{ + qDebug() << "BoxPlotTester::MainWidget::insertBox()"; + + if (nSeries > 0) { + QBarSet *newSet = new QBarSet("New"); + *newSet << 2 << 6 << 6.8 << 7 << 10; + + m_series[0]->insert(1, newSet); + + m_axis->append(addCategories[nNewBoxes]); + + nNewBoxes++; + } +} + void MainWidget::removeBox() { - qDebug() << "MainWidget::removeBox"; + qDebug() << "BoxPlotTester::MainWidget::removeBox"; if (nSeries > 0) { QList sets = m_series[0]->barSets(); @@ -245,6 +278,29 @@ void MainWidget::removeBox() } } +void MainWidget::clear() +{ + qDebug() << "BoxPlotTester::MainWidget::clear"; + + if (nSeries > 0) { + m_series[0]->clear(); + } else { + qDebug() << "Create a series first"; + } +} + +void MainWidget::setBrush() +{ + qDebug() << "BoxPlotTester::MainWidget::setBrush"; + + if (nSeries > 0) { + QList sets = m_series[0]->barSets(); + sets.at(1)->setBrush(QBrush(QColor(Qt::yellow))); + } else { + qDebug() << "Create a series first"; + } +} + void MainWidget::animationToggled(bool enabled) { qDebug() << "BoxPlotTester::Animation toggled to " << enabled; diff --git a/tests/boxplottester/mainwidget.h b/tests/boxplottester/mainwidget.h index b7071c1..961ad3b 100644 --- a/tests/boxplottester/mainwidget.h +++ b/tests/boxplottester/mainwidget.h @@ -49,7 +49,10 @@ private slots: void addSeries(); void removeSeries(); void addBox(); + void insertBox(); void removeBox(); + void clear(); + void setBrush(); void animationToggled(bool enabled); void legendToggled(bool enabled); void titleToggled(bool enabled);