diff --git a/src/charttheme.cpp b/src/charttheme.cpp index 4aa2085..1765b77 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -196,31 +196,12 @@ void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int co void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int count) { - // create a list of slice colors based on current theme - int i = 0; - QList colors; - bool toggle = false; - while (colors.count() < series->count()) { - - qreal pos = (qreal) colors.count() / (qreal) series->count(); - if (toggle) - pos += 0.5; - toggle = !toggle; - if (pos > 1.0) - pos = pos - 1.0; - - //qreal pos = (qreal) (qrand() % series->count()) / (qreal) series->count(); - + // 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 c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), pos); - - colors << c; - } - - // finally update colors - foreach (QPieSlice* s, series->slices()) { - QColor c = colors.takeFirst(); - s->setSlicePen(c); - s->setSliceBrush(c); + series->slices().at(i)->setSlicePen(c); + series->slices().at(i)->setSliceBrush(c); } } @@ -261,6 +242,32 @@ void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int coun // presenter->m_markerPen = pen; } +void ChartTheme::generateSeriesGradients() +{ + // Generate gradients in HSV color space + foreach (QColor color, m_seriesColors) { + QLinearGradient g; + qreal h = color.hsvHueF(); + qreal s = color.hsvSaturationF(); + + // TODO: tune the algorithm to give nice results with most base colors defined in + // most themes. The rest of the gradients we can define manually in theme specific + // implementation. + QColor start = color; + start.setHsvF(h, 0.05, 0.95); + g.setColorAt(0.0, start); + + g.setColorAt(0.5, color); + + QColor end = color; + end.setHsvF(h, s, 0.25); + g.setColorAt(1.0, end); + + m_seriesGradients << g; + } +} + + QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos) { Q_ASSERT(pos >=0.0 && pos <= 1.0); diff --git a/src/charttheme_p.h b/src/charttheme_p.h index 53a3dfa..45599d9 100644 --- a/src/charttheme_p.h +++ b/src/charttheme_p.h @@ -47,6 +47,7 @@ public: void decorate(SplineChartItem* presenter, QSplineSeries* series, int count); public: // utils + void generateSeriesGradients(); static QColor colorAt(const QColor &start, const QColor &end, qreal pos); static QColor colorAt(const QGradient &gradient, qreal pos); @@ -55,6 +56,14 @@ protected: QList m_seriesColors; QList m_seriesGradients; QLinearGradient m_backgroundGradient; + + // TODO: Add something like the following to themes: +// QPen axisLinePen; +// QPen backgroundHorizontalGridPen; +// QPen backgroundVerticalGridPen; +// // FillAll, FillEverySecondRow, FillEverySecondColumn, FillEverySecondRowAndColumn, FillNone +// int backgroundType; +// QFont masterFont; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/themes/chartthemedefault_p.h b/src/themes/chartthemedefault_p.h index 9a8fcb9..84c26e7 100644 --- a/src/themes/chartthemedefault_p.h +++ b/src/themes/chartthemedefault_p.h @@ -7,13 +7,11 @@ class ChartThemeDefault: public ChartTheme public: ChartThemeDefault():ChartTheme(QChart::ChartThemeDefault) { - m_seriesColors << QRgb(0xff000000); + // TODO: replace this dummy theme with an actual theme m_seriesColors << QRgb(0xff707070); + m_seriesColors << QRgb(0xffA0A0A0); - QLinearGradient g1; - g1.setColorAt(0.0, m_seriesColors.at(0)); - g1.setColorAt(1.0, m_seriesColors.at(1)); - m_seriesGradients << g1; + generateSeriesGradients(); QLinearGradient backgroundGradient; backgroundGradient.setColorAt(0.0, QRgb(0xffffffff)); diff --git a/src/themes/chartthemegrayscale_p.h b/src/themes/chartthemegrayscale_p.h index 41610d2..9356c99 100644 --- a/src/themes/chartthemegrayscale_p.h +++ b/src/themes/chartthemegrayscale_p.h @@ -12,20 +12,7 @@ public: m_seriesColors << QRgb(0xFFE8FFFC); m_seriesColors << QRgb(0xFFCCC2C2); - QLinearGradient g1; - g1.setColorAt(0.0, m_seriesColors.at(0)); - g1.setColorAt(1.0, m_seriesColors.at(1)); - m_seriesGradients << g1; - - QLinearGradient g2; - g2.setColorAt(0.0, m_seriesColors.at(1)); - g2.setColorAt(1.0, m_seriesColors.at(2)); - m_seriesGradients << g2; - - QLinearGradient g3; - g3.setColorAt(0.0, m_seriesColors.at(2)); - g3.setColorAt(1.0, m_seriesColors.at(3)); - m_seriesGradients << g3; + generateSeriesGradients(); QLinearGradient backgroundGradient; backgroundGradient.setColorAt(0.0, QRgb(0xffffffff)); diff --git a/src/themes/chartthemeicy_p.h b/src/themes/chartthemeicy_p.h index c33caf3..c59c11a 100644 --- a/src/themes/chartthemeicy_p.h +++ b/src/themes/chartthemeicy_p.h @@ -12,20 +12,7 @@ public: m_seriesColors << QRgb(0xFF3DADD9); m_seriesColors << QRgb(0xFF62C3D9); - QLinearGradient g1; - g1.setColorAt(0.0, m_seriesColors.at(0)); - g1.setColorAt(1.0, m_seriesColors.at(1)); - m_seriesGradients << g1; - - QLinearGradient g2; - g2.setColorAt(0.0, m_seriesColors.at(1)); - g2.setColorAt(1.0, m_seriesColors.at(2)); - m_seriesGradients << g2; - - QLinearGradient g3; - g3.setColorAt(0.0, m_seriesColors.at(2)); - g3.setColorAt(1.0, m_seriesColors.at(3)); - m_seriesGradients << g3; + generateSeriesGradients(); QLinearGradient backgroundGradient; backgroundGradient.setColorAt(0.0, QRgb(0xffBDE3F2)); diff --git a/src/themes/chartthemevanilla_p.h b/src/themes/chartthemevanilla_p.h index 29d1e7c..24b96be 100644 --- a/src/themes/chartthemevanilla_p.h +++ b/src/themes/chartthemevanilla_p.h @@ -13,25 +13,7 @@ public: m_seriesColors << QColor(210, 210, 52); m_seriesColors << QColor(136, 114, 58); - QLinearGradient g1; - g1.setColorAt(0.0, m_seriesColors.at(0)); - g1.setColorAt(1.0, m_seriesColors.at(1)); - m_seriesGradients << g1; - - QLinearGradient g2; - g2.setColorAt(0.0, m_seriesColors.at(1)); - g2.setColorAt(1.0, m_seriesColors.at(2)); - m_seriesGradients << g2; - - QLinearGradient g3; - g3.setColorAt(0.0, m_seriesColors.at(2)); - g3.setColorAt(1.0, m_seriesColors.at(3)); - m_seriesGradients << g3; - - QLinearGradient g4; - g4.setColorAt(0.0, m_seriesColors.at(3)); - g4.setColorAt(1.0, m_seriesColors.at(4)); - m_seriesGradients << g4; + generateSeriesGradients(); QLinearGradient backgroundGradient; backgroundGradient.setColorAt(0.0, QRgb(0xff9d844d));