diff --git a/src/qchart.cpp b/src/qchart.cpp index bb73d17..3b5e7aa 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -45,7 +45,7 @@ void QChart::addSeries(QChartSeries* series) { // TODO: we should check the series not already added - m_series<type()) { @@ -54,7 +54,7 @@ void QChart::addSeries(QChartSeries* series) QXYChartSeries* xyseries = static_cast(series); // Use color defined by theme in case the series does not define a custom color if (!xyseries->color().isValid() && m_themeColors.count()) - xyseries->setColor(m_themeColors.takeFirst()); + xyseries->setColor(nextColor()); m_plotDataIndex = 0 ; m_plotDomainList.resize(1); @@ -72,7 +72,6 @@ void QChart::addSeries(QChartSeries* series) } XYLineChartItem* item = new XYLineChartItem(xyseries,this); - m_chartItems<(series); + scatterSeries->d->setParentItem(this); + // Set pre-defined colors in case the series has no colors defined + if (!scatterSeries->markerColor().isValid()) + scatterSeries->setMarkerColor(nextColor()); connect(this, SIGNAL(sizeChanged(QRectF)), scatterSeries, SLOT(chartSizeChanged(QRectF))); - scatterSeries->d->setParentItem(this); - QColor nextColor = m_themeColors.takeFirst(); - nextColor.setAlpha(150); // TODO: default opacity? - scatterSeries->setMarkerColor(nextColor); +// QColor nextColor = m_themeColors.takeFirst(); +// nextColor.setAlpha(150); // TODO: default opacity? +// scatterSeries->setMarkerColor(nextColor); + break; } case QChartSeries::SeriesTypePie: { - // TODO: we now have also a list of y values as a parameter, it is ignored - // we should use a generic data class instead of list of x and y values QPieSeries *pieSeries = qobject_cast(series); + for (int i(0); i < pieSeries->sliceCount(); i++) { + if (!pieSeries->sliceColor(i).isValid()) + pieSeries->setSliceColor(i, nextColor()); + } connect(this, SIGNAL(sizeChanged(QRectF)), pieSeries, SLOT(chartSizeChanged(QRectF))); + + // Set pre-defined colors in case the series has no colors defined // TODO: how to define the color for all the slices of a pie? +// for (int (i); i < pieSeries.sliceCount(); i++) + break; } } } @@ -191,37 +200,62 @@ void QChart::setMargin(int margin) m_marginSize = margin; } -void QChart::setTheme(QChart::ChartTheme theme) +void QChart::setTheme(QChart::ChartThemeId theme) { +// if (theme != m_currentTheme) { + m_themeColors.clear(); + // TODO: define color themes switch (theme) { - case ChartThemeVanilla: - m_themeColors.append(QColor(255, 238, 174)); - m_themeColors.append(QColor(228, 228, 160)); - m_themeColors.append(QColor(228, 179, 160)); - m_themeColors.append(QColor(180, 151, 18)); - m_themeColors.append(QColor(252, 252, 37)); + case QChart::ChartThemeVanilla: + m_themeColors.append(QColor(217, 197, 116)); + m_themeColors.append(QColor(214, 168, 150)); + m_themeColors.append(QColor(160, 160, 113)); + m_themeColors.append(QColor(210, 210, 52)); + m_themeColors.append(QColor(136, 114, 58)); break; - case ChartThemeIcy: - m_themeColors.append(QColor(255, 238, 174)); - m_themeColors.append(QColor(228, 228, 160)); - m_themeColors.append(QColor(228, 179, 160)); - m_themeColors.append(QColor(180, 151, 18)); - m_themeColors.append(QColor(252, 252, 37)); + case QChart::ChartThemeIcy: + m_themeColors.append(QColor(0, 3, 165)); + m_themeColors.append(QColor(49, 52, 123)); + m_themeColors.append(QColor(71, 114, 187)); + m_themeColors.append(QColor(48, 97, 87)); + m_themeColors.append(QColor(19, 71, 90)); + m_themeColors.append(QColor(110, 70, 228)); break; - case ChartThemeGrayscale: - m_themeColors.append(QColor(255, 238, 174)); - m_themeColors.append(QColor(228, 228, 160)); - m_themeColors.append(QColor(228, 179, 160)); - m_themeColors.append(QColor(180, 151, 18)); - m_themeColors.append(QColor(252, 252, 37)); + case QChart::ChartThemeGrayscale: + m_themeColors.append(QColor(0, 0, 0)); + m_themeColors.append(QColor(50, 50, 50)); + m_themeColors.append(QColor(100, 100, 100)); + m_themeColors.append(QColor(140, 140, 140)); + m_themeColors.append(QColor(180, 180, 180)); break; default: Q_ASSERT(false); break; } - // TODO: update coloring of different elements to match the selected theme + foreach(QChartSeries* series, m_chartSeries) { + // TODO: other series interested on themes? + if (series->type() == QChartSeries::SeriesTypeLine) { + QXYChartSeries *lineseries = reinterpret_cast(series); + lineseries->setColor(nextColor()); + } else if (series->type() == QChartSeries::SeriesTypeScatter) { + QScatterSeries *scatter = qobject_cast(series); + scatter->setMarkerColor(nextColor()); + } else if (series->type() == QChartSeries::SeriesTypePie) { + QPieSeries *pieSeries = qobject_cast(series); + for (int i(0); i < pieSeries->sliceCount(); i++) + pieSeries->setSliceColor(i, nextColor()); + } + } + update(); +} + +QColor QChart::nextColor() +{ + QColor nextColor = m_themeColors.first(); + m_themeColors.move(0, m_themeColors.size() - 1); + return nextColor; } void QChart::zoomInToRect(const QRect& rectangle) diff --git a/src/qchart.h b/src/qchart.h index 2fa86d2..b2d05d6 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -27,7 +27,7 @@ class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject { Q_OBJECT public: - enum ChartTheme { + enum ChartThemeId { ChartThemeVanilla = 0, ChartThemeIcy, ChartThemeGrayscale @@ -49,7 +49,7 @@ public: void setSize(const QSize& size); void setMargin(int margin); int margin() const; - void setTheme(QChart::ChartTheme theme); + void setTheme(QChart::ChartThemeId theme); void setTitle(const QString& title); void setBackgroundColor(const QColor& color); @@ -69,6 +69,8 @@ signals: void scaleChanged(qreal xscale, qreal yscale); private: + QColor nextColor(); + Q_DISABLE_COPY(QChart) QGraphicsRectItem* m_background; QLinearGradient m_backgroundGradient; @@ -76,11 +78,9 @@ private: AxisItem* m_axisX; AxisItem* m_axisY; QRect m_rect; - QList m_series; + QList m_chartSeries; QVector m_plotDomainList; QList m_chartItems; - //TODO: remove - QList m_items; int m_plotDataIndex; int m_marginSize; QList m_themeColors; diff --git a/src/qchartwidget.cpp b/src/qchartwidget.cpp index ecd3f1b..e46f462 100644 --- a/src/qchartwidget.cpp +++ b/src/qchartwidget.cpp @@ -47,7 +47,7 @@ QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type) return m_chart->createSeries(type); } -void QChartWidget::setTheme(QChart::ChartTheme theme) +void QChartWidget::setTheme(QChart::ChartThemeId theme) { m_chart->setTheme(theme); } diff --git a/src/qchartwidget.h b/src/qchartwidget.h index b5d2347..6cc4321 100644 --- a/src/qchartwidget.h +++ b/src/qchartwidget.h @@ -32,7 +32,7 @@ public: * Set color theme for the chart. Themes define harmonic colors for the graphical elements of * the chart. */ - void setTheme(QChart::ChartTheme theme); + void setTheme(QChart::ChartThemeId theme); private: Q_DISABLE_COPY(QChartWidget) diff --git a/src/qpieseries.cpp b/src/qpieseries.cpp index a37c90b..064aa92 100644 --- a/src/qpieseries.cpp +++ b/src/qpieseries.cpp @@ -34,7 +34,7 @@ bool QPieSeries::setData(QList data) // TODO: no need to create new slices in case size changed; we should re-use the existing ones foreach (qreal value, m_data) { qreal span = value / total * fullPie; - PieSlice *slice = new PieSlice(randomColor(), angle, span, parentItem->boundingRect()); + PieSlice *slice = new PieSlice(QColor(), angle, span, parentItem->boundingRect()); slice->setParentItem(parentItem); m_slices.append(slice); angle += span; @@ -44,6 +44,25 @@ bool QPieSeries::setData(QList data) return true; } +void QPieSeries::setSliceColor(int index, QColor color) +{ + if (index >= 0 && index < m_slices.count()) + m_slices.at(index)->m_color = color; +} + +QColor QPieSeries::sliceColor(int index) +{ + if (index >= 0 && index < m_slices.count()) + return m_slices.at(index)->m_color; + else + return QColor(); +} + +int QPieSeries::sliceCount() +{ + return m_slices.count(); +} + void QPieSeries::chartSizeChanged(QRectF chartRect) { // TODO: allow user setting the size? @@ -82,16 +101,6 @@ void QPieSeries::setSizeFactor(qreal factor) parentItem->update(); } -QColor QPieSeries::randomColor() -{ - QColor c; - c.setRed(qrand() % 255); - c.setGreen(qrand() % 255); - c.setBlue(qrand() % 255); - return c; -} - - #include "moc_qpieseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qpieseries.h b/src/qpieseries.h index 0d9d7d9..d2ee898 100644 --- a/src/qpieseries.h +++ b/src/qpieseries.h @@ -18,7 +18,6 @@ public: // TODO: use a generic data class instead of x and y QPieSeries(QGraphicsObject *parent = 0); ~QPieSeries(); - QColor randomColor(); void setSizeFactor(qreal sizeFactor); qreal sizeFactor() { return m_sizeFactor; } @@ -26,6 +25,11 @@ public: // from QChartSeries QChartSeriesType type() const { return QChartSeries::SeriesTypePie; } bool setData(QList data); +public: + void setSliceColor(int index, QColor color); + QColor sliceColor(int index); + int sliceCount(); + public Q_SLOTS: void chartSizeChanged(QRectF rect); @@ -33,6 +37,7 @@ private: void resizeSlices(QRectF rect); //Q_DECLARE_PRIVATE(QPieSeries) Q_DISABLE_COPY(QPieSeries) + friend class QChart; // TODO: move the followin to internal impl QList m_data; QList m_slices; diff --git a/src/qscatterseries.cpp b/src/qscatterseries.cpp index 471da73..3c712af 100644 --- a/src/qscatterseries.cpp +++ b/src/qscatterseries.cpp @@ -87,6 +87,11 @@ void QScatterSeries::setMarkerColor(QColor color) d->m_markerColor = color; } +QColor QScatterSeries::markerColor() +{ + return d->m_markerColor; +} + // TODO: //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale) //{ diff --git a/src/qscatterseries.h b/src/qscatterseries.h index a134b1e..d889008 100644 --- a/src/qscatterseries.h +++ b/src/qscatterseries.h @@ -23,6 +23,7 @@ public: // from QChartSeries public Q_SLOTS: void chartSizeChanged(QRectF rect); void setMarkerColor(QColor color); + QColor markerColor(); //void chartScaleChanged(qreal xscale, qreal yscale); private: diff --git a/src/qscatterseries_p.h b/src/qscatterseries_p.h index e39c4af..18bb986 100644 --- a/src/qscatterseries_p.h +++ b/src/qscatterseries_p.h @@ -19,6 +19,7 @@ public: // from QGraphicsItem QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); +public: // TODO: use the chart data class instead of list of x and y values? QList m_x; QList m_y; diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index 9f9a918..41575e3 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -303,12 +303,12 @@ void MainWidget::yMaxChanged(int value) void MainWidget::changeChartTheme(int themeIndex) { qDebug() << "changeChartTheme: " << themeIndex; -// m_chartWidget->setTheme((QChart::ChartTheme) themeIndex); + m_chartWidget->setTheme((QChart::ChartThemeId) themeIndex); } void MainWidget::setPieSizeFactor(double size) { QPieSeries *pie = qobject_cast(m_currentSeries); - Q_ASSERT(pie); - pie->setSizeFactor(qreal(size)); + if (pie) + pie->setSizeFactor(qreal(size)); }