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