##// END OF EJS Templates
Draft implementation for setting color themes for a chart
Tero Ahola -
r64:e24eaf728304
parent child
Show More
@@ -18,12 +18,14
18 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18 QTCOMMERCIALCHART_BEGIN_NAMESPACE
19
19
20 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
21 m_axisX(new Axis(this)),
21 m_axisX(new Axis(this)),
22 m_axisY(new Axis(this)),
22 m_axisY(new Axis(this)),
23 m_grid(new XYGrid(this)),
23 m_grid(new XYGrid(this)),
24 m_plotDataIndex(0),
24 m_plotDataIndex(0),
25 m_marginSize(0)
25 m_marginSize(0)
26 {
26 {
27 // TODO: the default theme?
28 setTheme(QChart::ChartThemeVanilla);
27 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
28 // set axis
30 // set axis
29 m_axisY->rotate(90);
31 m_axisY->rotate(90);
@@ -103,6 +105,9 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
103 connect(this, SIGNAL(sizeChanged(QRectF)),
105 connect(this, SIGNAL(sizeChanged(QRectF)),
104 scatterSeries, SLOT(chartSizeChanged(QRectF)));
106 scatterSeries, SLOT(chartSizeChanged(QRectF)));
105 scatterSeries->d->setParentItem(this);
107 scatterSeries->d->setParentItem(this);
108 QColor nextColor = m_themeColors.takeFirst();
109 nextColor.setAlpha(150); // TODO: default opacity?
110 scatterSeries->setMarkerColor(nextColor);
106 return scatterSeries;
111 return scatterSeries;
107 }
112 }
108 case QChartSeries::SeriesTypePie: {
113 case QChartSeries::SeriesTypePie: {
@@ -111,6 +116,7 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
111 QPieSeries *pieSeries = new QPieSeries(this);
116 QPieSeries *pieSeries = new QPieSeries(this);
112 connect(this, SIGNAL(sizeChanged(QRectF)),
117 connect(this, SIGNAL(sizeChanged(QRectF)),
113 pieSeries, SLOT(chartSizeChanged(QRectF)));
118 pieSeries, SLOT(chartSizeChanged(QRectF)));
119 // TODO: how to define the color for all the slices of a pie?
114 return pieSeries;
120 return pieSeries;
115 }
121 }
116 default:
122 default:
@@ -159,7 +165,39 void QChart::setMargin(int margin)
159 m_marginSize = margin;
165 m_marginSize = margin;
160 }
166 }
161
167
162 #include "moc_qchart.cpp"
168 void QChart::setTheme(QChart::ChartTheme theme)
169 {
170 // TODO: define color themes
171 switch (theme) {
172 case ChartThemeVanilla:
173 m_themeColors.append(QColor(255, 238, 174));
174 m_themeColors.append(QColor(228, 228, 160));
175 m_themeColors.append(QColor(228, 179, 160));
176 m_themeColors.append(QColor(180, 151, 18));
177 m_themeColors.append(QColor(252, 252, 37));
178 break;
179 case ChartThemeIcy:
180 m_themeColors.append(QColor(255, 238, 174));
181 m_themeColors.append(QColor(228, 228, 160));
182 m_themeColors.append(QColor(228, 179, 160));
183 m_themeColors.append(QColor(180, 151, 18));
184 m_themeColors.append(QColor(252, 252, 37));
185 break;
186 case ChartThemeGrayscale:
187 m_themeColors.append(QColor(255, 238, 174));
188 m_themeColors.append(QColor(228, 228, 160));
189 m_themeColors.append(QColor(228, 179, 160));
190 m_themeColors.append(QColor(180, 151, 18));
191 m_themeColors.append(QColor(252, 252, 37));
192 break;
193 default:
194 Q_ASSERT(false);
195 break;
196 }
163
197
198 // TODO: update coloring of different elements to match the selected theme
199 }
200
201 #include "moc_qchart.cpp"
164
202
165 QTCOMMERCIALCHART_END_NAMESPACE
203 QTCOMMERCIALCHART_END_NAMESPACE
@@ -26,6 +26,13 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
26 {
26 {
27 Q_OBJECT
27 Q_OBJECT
28 public:
28 public:
29 enum ChartTheme {
30 ChartThemeVanilla = 0,
31 ChartThemeIcy,
32 ChartThemeGrayscale
33 };
34
35 public:
29 QChart(QGraphicsObject* parent = 0);
36 QChart(QGraphicsObject* parent = 0);
30 ~QChart();
37 ~QChart();
31
38
@@ -41,6 +48,7 public:
41 virtual void setSize(const QSizeF& rect);
48 virtual void setSize(const QSizeF& rect);
42 void setMargin(int margin);
49 void setMargin(int margin);
43 int margin() const;
50 int margin() const;
51 void setTheme(QChart::ChartTheme theme);
44
52
45 signals:
53 signals:
46 void sizeChanged(QRectF rect);
54 void sizeChanged(QRectF rect);
@@ -58,6 +66,7 private:
58 QList<QGraphicsItem*> m_items;
66 QList<QGraphicsItem*> m_items;
59 int m_plotDataIndex;
67 int m_plotDataIndex;
60 int m_marginSize;
68 int m_marginSize;
69 QList<QColor> m_themeColors;
61
70
62 QList<BarGroup*> m_BarGroupItems;
71 QList<BarGroup*> m_BarGroupItems;
63 };
72 };
@@ -46,6 +46,12 QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type)
46 {
46 {
47 return m_chart->createSeries(type);
47 return m_chart->createSeries(type);
48 }
48 }
49
50 void QChartWidget::setTheme(QChart::ChartTheme theme)
51 {
52 m_chart->setTheme(theme);
53 }
54
49 #include "moc_qchartwidget.cpp"
55 #include "moc_qchartwidget.cpp"
50
56
51 QTCOMMERCIALCHART_END_NAMESPACE
57 QTCOMMERCIALCHART_END_NAMESPACE
@@ -28,6 +28,12 public:
28 void addSeries(QChartSeries* series);
28 void addSeries(QChartSeries* series);
29 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
29 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
30
30
31 /*!
32 * Set color theme for the chart. Themes define harmonic colors for the graphical elements of
33 * the chart.
34 */
35 void setTheme(QChart::ChartTheme theme);
36
31 private:
37 private:
32 Q_DISABLE_COPY(QChartWidget)
38 Q_DISABLE_COPY(QChartWidget)
33 QGraphicsScene *m_scene;
39 QGraphicsScene *m_scene;
@@ -12,7 +12,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) :
12 QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) :
13 QGraphicsItem(parent),
13 QGraphicsItem(parent),
14 m_scalex(100), // TODO: let the use define the scale (or autoscaled)
14 m_scalex(100), // TODO: let the use define the scale (or autoscaled)
15 m_scaley(100)
15 m_scaley(100),
16 m_markerColor(QColor())
16 {
17 {
17 }
18 }
18
19
@@ -41,7 +42,8 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsI
41 QPen pen = painter->pen();
42 QPen pen = painter->pen();
42 QBrush brush = pen.brush();
43 QBrush brush = pen.brush();
43 // TODO: The opacity should be user definable...
44 // TODO: The opacity should be user definable...
44 brush.setColor(QColor(255, 82, 0, 100));
45 //brush.setColor(QColor(255, 82, 0, 100));
46 brush.setColor(m_markerColor);
45 pen.setBrush(brush);
47 pen.setBrush(brush);
46 pen.setWidth(4);
48 pen.setWidth(4);
47 painter->setPen(pen);
49 painter->setPen(pen);
@@ -80,6 +82,11 void QScatterSeries::chartSizeChanged(QRectF rect)
80 d->resize(rect);
82 d->resize(rect);
81 }
83 }
82
84
85 void QScatterSeries::setMarkerColor(QColor color)
86 {
87 d->m_markerColor = color;
88 }
89
83 // TODO:
90 // TODO:
84 //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale)
91 //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale)
85 //{
92 //{
@@ -3,6 +3,7
3
3
4 #include "qchartseries.h"
4 #include "qchartseries.h"
5 #include <QRectF>
5 #include <QRectF>
6 #include <QColor>
6
7
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 class QScatterSeriesPrivate;
9 class QScatterSeriesPrivate;
@@ -21,6 +22,7 public: // from QChartSeries
21
22
22 public Q_SLOTS:
23 public Q_SLOTS:
23 void chartSizeChanged(QRectF rect);
24 void chartSizeChanged(QRectF rect);
25 void setMarkerColor(QColor color);
24 //void chartScaleChanged(qreal xscale, qreal yscale);
26 //void chartScaleChanged(qreal xscale, qreal yscale);
25
27
26 private:
28 private:
@@ -26,6 +26,7 public: // from QGraphicsItem
26 qreal m_scaley;
26 qreal m_scaley;
27 QList<qreal> m_scenex;
27 QList<qreal> m_scenex;
28 QList<qreal> m_sceney;
28 QList<qreal> m_sceney;
29 QColor m_markerColor;
29 };
30 };
30
31
31 QTCOMMERCIALCHART_END_NAMESPACE
32 QTCOMMERCIALCHART_END_NAMESPACE
@@ -58,6 +58,14 MainWidget::MainWidget(QWidget *parent) :
58 m_yMaxSpin->setValue(10);
58 m_yMaxSpin->setValue(10);
59 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
59 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
60
60
61 QComboBox *chartTheme = new QComboBox();
62 chartTheme->addItem("Vanilla");
63 chartTheme->addItem("Icy");
64 chartTheme->addItem("Grayscale");
65 chartTheme->addItem("Tobedefined");
66 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
67 this, SLOT(changeChartTheme(int)));
68
61 QGridLayout *grid = new QGridLayout();
69 QGridLayout *grid = new QGridLayout();
62 QGridLayout *mainLayout = new QGridLayout();
70 QGridLayout *mainLayout = new QGridLayout();
63 //grid->addWidget(new QLabel("Add series:"), 0, 0);
71 //grid->addWidget(new QLabel("Add series:"), 0, 0);
@@ -73,9 +81,11 MainWidget::MainWidget(QWidget *parent) :
73 grid->addWidget(m_yMinSpin, 6, 1);
81 grid->addWidget(m_yMinSpin, 6, 1);
74 grid->addWidget(new QLabel("y max:"), 7, 0);
82 grid->addWidget(new QLabel("y max:"), 7, 0);
75 grid->addWidget(m_yMaxSpin, 7, 1);
83 grid->addWidget(m_yMaxSpin, 7, 1);
84 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
85 grid->addWidget(chartTheme, 8, 1);
76 // add row with empty label to make all the other rows static
86 // add row with empty label to make all the other rows static
77 grid->addWidget(new QLabel(""), 8, 0);
87 grid->addWidget(new QLabel(""), 9, 0);
78 grid->setRowStretch(8, 1);
88 grid->setRowStretch(9, 1);
79
89
80 mainLayout->addLayout(grid, 0, 0);
90 mainLayout->addLayout(grid, 0, 0);
81
91
@@ -276,6 +286,12 void MainWidget::yMaxChanged(int value)
276 qDebug() << "yMaxChanged: " << value;
286 qDebug() << "yMaxChanged: " << value;
277 }
287 }
278
288
289 void MainWidget::changeChartTheme(int themeIndex)
290 {
291 qDebug() << "changeChartTheme: " << themeIndex;
292 m_chartWidget->setTheme((QChart::ChartTheme) themeIndex);
293 }
294
279 void MainWidget::setPieSizeFactor(double size)
295 void MainWidget::setPieSizeFactor(double size)
280 {
296 {
281 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
297 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
@@ -30,6 +30,7 private slots:
30 void yMinChanged(int value);
30 void yMinChanged(int value);
31 void yMaxChanged(int value);
31 void yMaxChanged(int value);
32 void setCurrentSeries(QChartSeries *series);
32 void setCurrentSeries(QChartSeries *series);
33 void changeChartTheme(int themeIndex);
33 void setPieSizeFactor(double margin);
34 void setPieSizeFactor(double margin);
34
35
35 private:
36 private:
General Comments 0
You need to be logged in to leave comments. Login now