##// END OF EJS Templates
Color themes now enabled for scatter, pie and line series.
Tero Ahola -
r75:cdad8ac737ab
parent child
Show More
@@ -45,7 +45,7 void QChart::addSeries(QChartSeries* series)
45 {
45 {
46 // TODO: we should check the series not already added
46 // TODO: we should check the series not already added
47
47
48 m_series<<series;
48 m_chartSeries << series;
49
49
50 switch(series->type())
50 switch(series->type())
51 {
51 {
@@ -54,7 +54,7 void QChart::addSeries(QChartSeries* series)
54 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
54 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
55 // Use color defined by theme in case the series does not define a custom color
55 // Use color defined by theme in case the series does not define a custom color
56 if (!xyseries->color().isValid() && m_themeColors.count())
56 if (!xyseries->color().isValid() && m_themeColors.count())
57 xyseries->setColor(m_themeColors.takeFirst());
57 xyseries->setColor(nextColor());
58
58
59 m_plotDataIndex = 0 ;
59 m_plotDataIndex = 0 ;
60 m_plotDomainList.resize(1);
60 m_plotDomainList.resize(1);
@@ -72,7 +72,6 void QChart::addSeries(QChartSeries* series)
72 }
72 }
73
73
74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
75
76 m_chartItems<<item;
75 m_chartItems<<item;
77
76
78 foreach(ChartItem* i ,m_chartItems)
77 foreach(ChartItem* i ,m_chartItems)
@@ -91,20 +90,30 void QChart::addSeries(QChartSeries* series)
91 }
90 }
92 case QChartSeries::SeriesTypeScatter: {
91 case QChartSeries::SeriesTypeScatter: {
93 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
92 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
93 scatterSeries->d->setParentItem(this);
94 // Set pre-defined colors in case the series has no colors defined
95 if (!scatterSeries->markerColor().isValid())
96 scatterSeries->setMarkerColor(nextColor());
94 connect(this, SIGNAL(sizeChanged(QRectF)),
97 connect(this, SIGNAL(sizeChanged(QRectF)),
95 scatterSeries, SLOT(chartSizeChanged(QRectF)));
98 scatterSeries, SLOT(chartSizeChanged(QRectF)));
96 scatterSeries->d->setParentItem(this);
99 // QColor nextColor = m_themeColors.takeFirst();
97 QColor nextColor = m_themeColors.takeFirst();
100 // nextColor.setAlpha(150); // TODO: default opacity?
98 nextColor.setAlpha(150); // TODO: default opacity?
101 // scatterSeries->setMarkerColor(nextColor);
99 scatterSeries->setMarkerColor(nextColor);
102 break;
100 }
103 }
101 case QChartSeries::SeriesTypePie: {
104 case QChartSeries::SeriesTypePie: {
102 // TODO: we now have also a list of y values as a parameter, it is ignored
103 // we should use a generic data class instead of list of x and y values
104 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
105 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
106 for (int i(0); i < pieSeries->sliceCount(); i++) {
107 if (!pieSeries->sliceColor(i).isValid())
108 pieSeries->setSliceColor(i, nextColor());
109 }
105 connect(this, SIGNAL(sizeChanged(QRectF)),
110 connect(this, SIGNAL(sizeChanged(QRectF)),
106 pieSeries, SLOT(chartSizeChanged(QRectF)));
111 pieSeries, SLOT(chartSizeChanged(QRectF)));
112
113 // Set pre-defined colors in case the series has no colors defined
107 // TODO: how to define the color for all the slices of a pie?
114 // TODO: how to define the color for all the slices of a pie?
115 // for (int (i); i < pieSeries.sliceCount(); i++)
116 break;
108 }
117 }
109 }
118 }
110 }
119 }
@@ -191,37 +200,62 void QChart::setMargin(int margin)
191 m_marginSize = margin;
200 m_marginSize = margin;
192 }
201 }
193
202
194 void QChart::setTheme(QChart::ChartTheme theme)
203 void QChart::setTheme(QChart::ChartThemeId theme)
195 {
204 {
205 // if (theme != m_currentTheme) {
206 m_themeColors.clear();
207
196 // TODO: define color themes
208 // TODO: define color themes
197 switch (theme) {
209 switch (theme) {
198 case ChartThemeVanilla:
210 case QChart::ChartThemeVanilla:
199 m_themeColors.append(QColor(255, 238, 174));
211 m_themeColors.append(QColor(217, 197, 116));
200 m_themeColors.append(QColor(228, 228, 160));
212 m_themeColors.append(QColor(214, 168, 150));
201 m_themeColors.append(QColor(228, 179, 160));
213 m_themeColors.append(QColor(160, 160, 113));
202 m_themeColors.append(QColor(180, 151, 18));
214 m_themeColors.append(QColor(210, 210, 52));
203 m_themeColors.append(QColor(252, 252, 37));
215 m_themeColors.append(QColor(136, 114, 58));
204 break;
216 break;
205 case ChartThemeIcy:
217 case QChart::ChartThemeIcy:
206 m_themeColors.append(QColor(255, 238, 174));
218 m_themeColors.append(QColor(0, 3, 165));
207 m_themeColors.append(QColor(228, 228, 160));
219 m_themeColors.append(QColor(49, 52, 123));
208 m_themeColors.append(QColor(228, 179, 160));
220 m_themeColors.append(QColor(71, 114, 187));
209 m_themeColors.append(QColor(180, 151, 18));
221 m_themeColors.append(QColor(48, 97, 87));
210 m_themeColors.append(QColor(252, 252, 37));
222 m_themeColors.append(QColor(19, 71, 90));
223 m_themeColors.append(QColor(110, 70, 228));
211 break;
224 break;
212 case ChartThemeGrayscale:
225 case QChart::ChartThemeGrayscale:
213 m_themeColors.append(QColor(255, 238, 174));
226 m_themeColors.append(QColor(0, 0, 0));
214 m_themeColors.append(QColor(228, 228, 160));
227 m_themeColors.append(QColor(50, 50, 50));
215 m_themeColors.append(QColor(228, 179, 160));
228 m_themeColors.append(QColor(100, 100, 100));
216 m_themeColors.append(QColor(180, 151, 18));
229 m_themeColors.append(QColor(140, 140, 140));
217 m_themeColors.append(QColor(252, 252, 37));
230 m_themeColors.append(QColor(180, 180, 180));
218 break;
231 break;
219 default:
232 default:
220 Q_ASSERT(false);
233 Q_ASSERT(false);
221 break;
234 break;
222 }
235 }
223
236
224 // TODO: update coloring of different elements to match the selected theme
237 foreach(QChartSeries* series, m_chartSeries) {
238 // TODO: other series interested on themes?
239 if (series->type() == QChartSeries::SeriesTypeLine) {
240 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
241 lineseries->setColor(nextColor());
242 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
243 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
244 scatter->setMarkerColor(nextColor());
245 } else if (series->type() == QChartSeries::SeriesTypePie) {
246 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
247 for (int i(0); i < pieSeries->sliceCount(); i++)
248 pieSeries->setSliceColor(i, nextColor());
249 }
250 }
251 update();
252 }
253
254 QColor QChart::nextColor()
255 {
256 QColor nextColor = m_themeColors.first();
257 m_themeColors.move(0, m_themeColors.size() - 1);
258 return nextColor;
225 }
259 }
226
260
227 void QChart::zoomInToRect(const QRect& rectangle)
261 void QChart::zoomInToRect(const QRect& rectangle)
@@ -27,7 +27,7 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
27 {
27 {
28 Q_OBJECT
28 Q_OBJECT
29 public:
29 public:
30 enum ChartTheme {
30 enum ChartThemeId {
31 ChartThemeVanilla = 0,
31 ChartThemeVanilla = 0,
32 ChartThemeIcy,
32 ChartThemeIcy,
33 ChartThemeGrayscale
33 ChartThemeGrayscale
@@ -49,7 +49,7 public:
49 void setSize(const QSize& size);
49 void setSize(const QSize& size);
50 void setMargin(int margin);
50 void setMargin(int margin);
51 int margin() const;
51 int margin() const;
52 void setTheme(QChart::ChartTheme theme);
52 void setTheme(QChart::ChartThemeId theme);
53
53
54 void setTitle(const QString& title);
54 void setTitle(const QString& title);
55 void setBackgroundColor(const QColor& color);
55 void setBackgroundColor(const QColor& color);
@@ -69,6 +69,8 signals:
69 void scaleChanged(qreal xscale, qreal yscale);
69 void scaleChanged(qreal xscale, qreal yscale);
70
70
71 private:
71 private:
72 QColor nextColor();
73
72 Q_DISABLE_COPY(QChart)
74 Q_DISABLE_COPY(QChart)
73 QGraphicsRectItem* m_background;
75 QGraphicsRectItem* m_background;
74 QLinearGradient m_backgroundGradient;
76 QLinearGradient m_backgroundGradient;
@@ -76,11 +78,9 private:
76 AxisItem* m_axisX;
78 AxisItem* m_axisX;
77 AxisItem* m_axisY;
79 AxisItem* m_axisY;
78 QRect m_rect;
80 QRect m_rect;
79 QList<const QChartSeries*> m_series;
81 QList<QChartSeries*> m_chartSeries;
80 QVector<PlotDomain> m_plotDomainList;
82 QVector<PlotDomain> m_plotDomainList;
81 QList<ChartItem*> m_chartItems;
83 QList<ChartItem*> m_chartItems;
82 //TODO: remove
83 QList<QGraphicsItem*> m_items;
84 int m_plotDataIndex;
84 int m_plotDataIndex;
85 int m_marginSize;
85 int m_marginSize;
86 QList<QColor> m_themeColors;
86 QList<QColor> m_themeColors;
@@ -47,7 +47,7 QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type)
47 return m_chart->createSeries(type);
47 return m_chart->createSeries(type);
48 }
48 }
49
49
50 void QChartWidget::setTheme(QChart::ChartTheme theme)
50 void QChartWidget::setTheme(QChart::ChartThemeId theme)
51 {
51 {
52 m_chart->setTheme(theme);
52 m_chart->setTheme(theme);
53 }
53 }
@@ -32,7 +32,7 public:
32 * Set color theme for the chart. Themes define harmonic colors for the graphical elements of
32 * Set color theme for the chart. Themes define harmonic colors for the graphical elements of
33 * the chart.
33 * the chart.
34 */
34 */
35 void setTheme(QChart::ChartTheme theme);
35 void setTheme(QChart::ChartThemeId theme);
36
36
37 private:
37 private:
38 Q_DISABLE_COPY(QChartWidget)
38 Q_DISABLE_COPY(QChartWidget)
@@ -34,7 +34,7 bool QPieSeries::setData(QList<qreal> data)
34 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
34 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
35 foreach (qreal value, m_data) {
35 foreach (qreal value, m_data) {
36 qreal span = value / total * fullPie;
36 qreal span = value / total * fullPie;
37 PieSlice *slice = new PieSlice(randomColor(), angle, span, parentItem->boundingRect());
37 PieSlice *slice = new PieSlice(QColor(), angle, span, parentItem->boundingRect());
38 slice->setParentItem(parentItem);
38 slice->setParentItem(parentItem);
39 m_slices.append(slice);
39 m_slices.append(slice);
40 angle += span;
40 angle += span;
@@ -44,6 +44,25 bool QPieSeries::setData(QList<qreal> data)
44 return true;
44 return true;
45 }
45 }
46
46
47 void QPieSeries::setSliceColor(int index, QColor color)
48 {
49 if (index >= 0 && index < m_slices.count())
50 m_slices.at(index)->m_color = color;
51 }
52
53 QColor QPieSeries::sliceColor(int index)
54 {
55 if (index >= 0 && index < m_slices.count())
56 return m_slices.at(index)->m_color;
57 else
58 return QColor();
59 }
60
61 int QPieSeries::sliceCount()
62 {
63 return m_slices.count();
64 }
65
47 void QPieSeries::chartSizeChanged(QRectF chartRect)
66 void QPieSeries::chartSizeChanged(QRectF chartRect)
48 {
67 {
49 // TODO: allow user setting the size?
68 // TODO: allow user setting the size?
@@ -82,16 +101,6 void QPieSeries::setSizeFactor(qreal factor)
82 parentItem->update();
101 parentItem->update();
83 }
102 }
84
103
85 QColor QPieSeries::randomColor()
86 {
87 QColor c;
88 c.setRed(qrand() % 255);
89 c.setGreen(qrand() % 255);
90 c.setBlue(qrand() % 255);
91 return c;
92 }
93
94
95 #include "moc_qpieseries.cpp"
104 #include "moc_qpieseries.cpp"
96
105
97 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -18,7 +18,6 public:
18 // TODO: use a generic data class instead of x and y
18 // TODO: use a generic data class instead of x and y
19 QPieSeries(QGraphicsObject *parent = 0);
19 QPieSeries(QGraphicsObject *parent = 0);
20 ~QPieSeries();
20 ~QPieSeries();
21 QColor randomColor();
22 void setSizeFactor(qreal sizeFactor);
21 void setSizeFactor(qreal sizeFactor);
23 qreal sizeFactor() { return m_sizeFactor; }
22 qreal sizeFactor() { return m_sizeFactor; }
24
23
@@ -26,6 +25,11 public: // from QChartSeries
26 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
25 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
27 bool setData(QList<qreal> data);
26 bool setData(QList<qreal> data);
28
27
28 public:
29 void setSliceColor(int index, QColor color);
30 QColor sliceColor(int index);
31 int sliceCount();
32
29 public Q_SLOTS:
33 public Q_SLOTS:
30 void chartSizeChanged(QRectF rect);
34 void chartSizeChanged(QRectF rect);
31
35
@@ -33,6 +37,7 private:
33 void resizeSlices(QRectF rect);
37 void resizeSlices(QRectF rect);
34 //Q_DECLARE_PRIVATE(QPieSeries)
38 //Q_DECLARE_PRIVATE(QPieSeries)
35 Q_DISABLE_COPY(QPieSeries)
39 Q_DISABLE_COPY(QPieSeries)
40 friend class QChart;
36 // TODO: move the followin to internal impl
41 // TODO: move the followin to internal impl
37 QList<qreal> m_data;
42 QList<qreal> m_data;
38 QList<PieSlice*> m_slices;
43 QList<PieSlice*> m_slices;
@@ -87,6 +87,11 void QScatterSeries::setMarkerColor(QColor color)
87 d->m_markerColor = color;
87 d->m_markerColor = color;
88 }
88 }
89
89
90 QColor QScatterSeries::markerColor()
91 {
92 return d->m_markerColor;
93 }
94
90 // TODO:
95 // TODO:
91 //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale)
96 //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale)
92 //{
97 //{
@@ -23,6 +23,7 public: // from QChartSeries
23 public Q_SLOTS:
23 public Q_SLOTS:
24 void chartSizeChanged(QRectF rect);
24 void chartSizeChanged(QRectF rect);
25 void setMarkerColor(QColor color);
25 void setMarkerColor(QColor color);
26 QColor markerColor();
26 //void chartScaleChanged(qreal xscale, qreal yscale);
27 //void chartScaleChanged(qreal xscale, qreal yscale);
27
28
28 private:
29 private:
@@ -19,6 +19,7 public: // from QGraphicsItem
19 QRectF boundingRect() const;
19 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21
21
22 public:
22 // TODO: use the chart data class instead of list of x and y values?
23 // TODO: use the chart data class instead of list of x and y values?
23 QList<qreal> m_x;
24 QList<qreal> m_x;
24 QList<qreal> m_y;
25 QList<qreal> m_y;
@@ -303,12 +303,12 void MainWidget::yMaxChanged(int value)
303 void MainWidget::changeChartTheme(int themeIndex)
303 void MainWidget::changeChartTheme(int themeIndex)
304 {
304 {
305 qDebug() << "changeChartTheme: " << themeIndex;
305 qDebug() << "changeChartTheme: " << themeIndex;
306 // m_chartWidget->setTheme((QChart::ChartTheme) themeIndex);
306 m_chartWidget->setTheme((QChart::ChartThemeId) themeIndex);
307 }
307 }
308
308
309 void MainWidget::setPieSizeFactor(double size)
309 void MainWidget::setPieSizeFactor(double size)
310 {
310 {
311 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
311 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
312 Q_ASSERT(pie);
312 if (pie)
313 pie->setSizeFactor(qreal(size));
313 pie->setSizeFactor(qreal(size));
314 }
314 }
General Comments 0
You need to be logged in to leave comments. Login now