From 3b2495c81f70d90edd1e37aa477c23c32b3422a9 2012-03-12 10:45:36 From: Tero Ahola Date: 2012-03-12 10:45:36 Subject: [PATCH] Fixed bug with changing theme when several series exist --- diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 9f512b8..8271f4b 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -258,6 +258,19 @@ int ChartDataSet::seriesCount(QSeries::QSeriesType type) return count; } +int ChartDataSet::seriesIndex(QSeries *series) +{ + int count(-1); + QMapIterator i(m_seriesAxisMap); + while (i.hasNext()) { + i.next(); + count++; + if (i.key() == series) + return count; + } + return count; +} + QChartAxis* ChartDataSet::axisY(QSeries* series) const { if(series == 0) return m_axisY; diff --git a/src/chartdataset_p.h b/src/chartdataset_p.h index 3cf6cd9..e4d8db3 100644 --- a/src/chartdataset_p.h +++ b/src/chartdataset_p.h @@ -25,6 +25,7 @@ public: void scrollDomain(int dx,int dy,const QSizeF& size); int seriesCount(QSeries::QSeriesType type); + int seriesIndex(QSeries *series); Domain* domain(QSeries* series) const; Domain* domain(QChartAxis* axis) const; diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 6d675a0..dfd5c26 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -145,7 +145,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) if(m_options.testFlag(QChart::SeriesAnimations)) { m_animator->addAnimation(line); } - m_chartTheme->decorate(line,lineSeries,m_dataset->seriesCount(series->type())); + m_chartTheme->decorate(line, lineSeries, m_dataset->seriesIndex(series)); QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&))); QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); item = line; @@ -159,7 +159,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) if(m_options.testFlag(QChart::SeriesAnimations)) { // m_animator->addAnimation(area); } - m_chartTheme->decorate(area,areaSeries,m_dataset->seriesCount(series->type())); + m_chartTheme->decorate(area, areaSeries, m_dataset->seriesIndex(series)); QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&))); QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); item=area; @@ -172,7 +172,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) if(m_options.testFlag(QChart::SeriesAnimations)) { // m_animator->addAnimation(bar); } - m_chartTheme->decorate(bar,barSeries,m_dataset->seriesCount(series->type())); + m_chartTheme->decorate(bar, barSeries, m_dataset->seriesIndex(barSeries)); QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); item=bar; @@ -185,7 +185,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) if(m_options.testFlag(QChart::SeriesAnimations)) { // m_animator->addAnimation(bar); } - m_chartTheme->decorate(bar,stackedBarSeries,m_dataset->seriesCount(series->type())); + m_chartTheme->decorate(bar, stackedBarSeries, m_dataset->seriesIndex(stackedBarSeries)); QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); item=bar; @@ -198,7 +198,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) if(m_options.testFlag(QChart::SeriesAnimations)) { // m_animator->addAnimation(bar); } - m_chartTheme->decorate(bar,percentBarSeries ,m_dataset->seriesCount(series->type())); + m_chartTheme->decorate(bar, percentBarSeries, m_dataset->seriesIndex(percentBarSeries)); QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); item=bar; @@ -207,15 +207,15 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypeScatter: { QScatterSeries *scatterSeries = static_cast(series); - ScatterChartItem *scatter = new ScatterChartItem(scatterSeries, m_chart); - if(m_options.testFlag(QChart::SeriesAnimations)) { - m_animator->addAnimation(scatter); - } - m_chartTheme->decorate(scatter, scatterSeries, m_dataset->seriesCount(series->type())); - QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&))); - QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); - item=scatter; - break; + ScatterChartItem *scatter = new ScatterChartItem(scatterSeries, m_chart); + if(m_options.testFlag(QChart::SeriesAnimations)) { + m_animator->addAnimation(scatter); + } + m_chartTheme->decorate(scatter, scatterSeries, m_dataset->seriesIndex(series)); + QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); + item = scatter; + break; } case QSeries::SeriesTypePie: { @@ -224,7 +224,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) if(m_options.testFlag(QChart::SeriesAnimations)) { // m_animator->addAnimation(pie); } - m_chartTheme->decorate(pie, pieSeries, m_dataset->seriesCount(series->type())); + m_chartTheme->decorate(pie, pieSeries, m_dataset->seriesIndex(series)); QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&))); QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); // Hide all from background when there is only piechart @@ -239,18 +239,17 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) } case QSeries::SeriesTypeSpline: { - - QSplineSeries* splineSeries = static_cast(series); - SplineChartItem* spline = new SplineChartItem(splineSeries, m_chart); - if(m_options.testFlag(QChart::SeriesAnimations)) { - m_animator->addAnimation(spline); - } - m_chartTheme->decorate(spline, splineSeries, m_dataset->seriesCount(series->type())); - QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&))); - QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); - item=spline; - break; - } + QSplineSeries* splineSeries = static_cast(series); + SplineChartItem* spline = new SplineChartItem(splineSeries, m_chart); + if(m_options.testFlag(QChart::SeriesAnimations)) { + m_animator->addAnimation(spline); + } + m_chartTheme->decorate(spline, splineSeries, m_dataset->seriesIndex(series)); + QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); + item=spline; + break; + } default: { qDebug()<< "Series type" << series->type() << "not implemented."; break; diff --git a/src/charttheme.cpp b/src/charttheme.cpp index 36acd20..e8bd1b2 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -64,7 +64,7 @@ void ChartTheme::decorate(QChart* chart) chart->setChartBackgroundBrush(m_backgroundGradient); } -void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series,int count) +void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index) { QPen pen; QBrush brush; @@ -72,7 +72,7 @@ void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series,int count) if (pen != series->pen()){ item->setPen(series->pen()); } else { - pen.setColor(colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 1.0)); + pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0)); pen.setWidthF(2); item->setPen(pen); } @@ -80,106 +80,106 @@ void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series,int count) if (brush != series->brush()) { item->setBrush(series->brush()); } else { - QBrush brush(m_seriesColors.at(count % m_seriesColors.size())); + QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); item->setBrush(brush); } } -void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count) +void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index) { QPen pen; if(pen != series->pen()){ item->setLinePen(series->pen()); return; } - pen.setColor(m_seriesColors.at(count%m_seriesColors.size())); + pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); pen.setWidthF(2); item->setLinePen(pen); } -void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count) +void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index) { QList sets = series->barSets(); for (int i=0; i 1) pos = (qreal) i / (qreal) (sets.count() - 1); - QColor c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), pos); + QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); sets.at(i)->setBrush(QBrush(c)); // Pick label color as far as possible from bar color (within gradient). // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :) // TODO: better picking of label color? if (pos < 0.3) { - c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 1); + c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); } else { - c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 0); + c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); } sets.at(i)->setFloatingValuePen(QPen(c)); } } -void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count) +void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index) { QList sets = series->barSets(); for (int i=0; i 1) pos = (qreal) i / (qreal) (sets.count() - 1); - QColor c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), pos); + QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); sets.at(i)->setBrush(QBrush(c)); if (pos < 0.3) { - c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 1); + c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); } else { - c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 0); + c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); } sets.at(i)->setFloatingValuePen(QPen(c)); } } -void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count) +void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index) { QList sets = series->barSets(); for (int i=0; i 1) pos = (qreal) i / (qreal) (sets.count() - 1); - QColor c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), pos); + QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); sets.at(i)->setBrush(QBrush(c)); if (pos < 0.3) { - c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 1); + c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); } else { - c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 0); + c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); } sets.at(i)->setFloatingValuePen(QPen(c)); } } -void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int count) +void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int index) { Q_ASSERT(item); Q_ASSERT(series); // Use a base color for brush - item->setBrush(m_seriesColors.at(count % m_seriesColors.size())); + item->setBrush(m_seriesColors.at(index % m_seriesColors.size())); // Take pen near from gradient start, effectively using a lighter color for outline QPen pen(QBrush(Qt::SolidPattern), 3); - pen.setColor(colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 1.0)); + pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0)); item->setPen(pen); } -void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int count) +void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index) { // Get color for a slice from a gradient linearly, beginning from the start of the gradient for (int i(0); i < series->slices().count(); i++) { qreal pos = (qreal) i / (qreal) series->count(); - QColor penColor = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 0.1); + QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1); series->slices().at(i)->setSlicePen(penColor); - QColor brushColor = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), pos); + QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); series->slices().at(i)->setSliceBrush(brushColor); } } @@ -194,7 +194,7 @@ void ChartTheme::decorate(QChartAxis* axis, AxisItem* item) axis->setShadesOpacity(0.5); } -void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int count) +void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int index) { Q_ASSERT(item); Q_ASSERT(series); @@ -204,12 +204,12 @@ void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int coun if(pen != series->pen()){ item->setLinePen(series->pen()); }else{ - pen.setColor(m_seriesColors.at(count%m_seriesColors.size())); + pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); pen.setWidthF(series->pen().widthF()); item->setLinePen(series->pen()); } -// QColor color = m_seriesColors.at(count % m_seriesColors.size()); +// QColor color = m_seriesColors.at(index % m_seriesColors.size()); // TODO: define alpha in the theme? or in the series? //color.setAlpha(120); diff --git a/src/charttheme_p.h b/src/charttheme_p.h index a1ae808..91235a8 100644 --- a/src/charttheme_p.h +++ b/src/charttheme_p.h @@ -35,16 +35,16 @@ public: static ChartTheme* createTheme(QChart::ChartTheme theme); QChart::ChartTheme id() const {return m_id;} void decorate(QChart* chart); - //void decorate(ChartItem* item, QSeries* series,int count); - void decorate(BarPresenter* item, QBarSeries* series,int count); - void decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count); - void decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count); - void decorate(LineChartItem* item, QLineSeries* series,int count); - void decorate(AreaChartItem* item, QAreaSeries* series,int count); - void decorate(ScatterChartItem* presenter, QScatterSeries* series, int count); - void decorate(PiePresenter* item, QPieSeries* series, int count); + //void decorate(ChartItem* item, QSeries* series,int index); + void decorate(BarPresenter* item, QBarSeries* series, int index); + void decorate(StackedBarPresenter* item, QStackedBarSeries* series, int index); + void decorate(PercentBarPresenter* item, QPercentBarSeries* series, int index); + void decorate(LineChartItem* item, QLineSeries* series, int index); + void decorate(AreaChartItem* item, QAreaSeries* series, int index); + void decorate(ScatterChartItem* presenter, QScatterSeries* series, int index); + void decorate(PiePresenter* item, QPieSeries* series, int index); void decorate(QChartAxis* axis,AxisItem* item); - void decorate(SplineChartItem* presenter, QSplineSeries* series, int count); + void decorate(SplineChartItem* presenter, QSplineSeries* series, int index); public: // utils void generateSeriesGradients();