##// END OF EJS Templates
sauimone -
r66:f1cd31b8c179 merge
parent child
Show More
@@ -18,12 +18,14
18 18 QTCOMMERCIALCHART_BEGIN_NAMESPACE
19 19
20 20 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
21 m_axisX(new Axis(this)),
22 m_axisY(new Axis(this)),
23 m_grid(new XYGrid(this)),
24 m_plotDataIndex(0),
25 m_marginSize(0)
21 m_axisX(new Axis(this)),
22 m_axisY(new Axis(this)),
23 m_grid(new XYGrid(this)),
24 m_plotDataIndex(0),
25 m_marginSize(0)
26 26 {
27 // TODO: the default theme?
28 setTheme(QChart::ChartThemeVanilla);
27 29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
28 30 // set axis
29 31 m_axisY->rotate(90);
@@ -47,6 +49,9 void QChart::addSeries(QChartSeries* series)
47 49 case QChartSeries::SeriesTypeLine: {
48 50
49 51 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
52 // Use color defined by theme in case the series does not define a custom color
53 if (!xyseries->color().isValid() && m_themeColors.count())
54 xyseries->setColor(m_themeColors.takeFirst());
50 55
51 56 XYPlotDomain domain;
52 57 //TODO "nice numbers algorithm"
@@ -69,16 +74,6 void QChart::addSeries(QChartSeries* series)
69 74 m_xyLineChartItems<<item;
70 75 break;
71 76 }
72 // TODO: Not tested:
73 // case QChartSeries::SeriesTypeScatter: {
74 // QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
75 // if (scatter) {
76 // scatter->d->setParentItem(this);
77 // scene()->addItem(scatter->d);
78 // }
79 // break;
80 // }
81
82 77 case QChartSeries::SeriesTypeBar: {
83 78
84 79 qDebug() << "barSeries added";
@@ -90,6 +85,23 void QChart::addSeries(QChartSeries* series)
90 85 m_BarGroupItems.append(group); // If we need to access group later
91 86 break;
92 87 }
88 case QChartSeries::SeriesTypeScatter: {
89 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
90 connect(this, SIGNAL(sizeChanged(QRectF)),
91 scatterSeries, SLOT(chartSizeChanged(QRectF)));
92 scatterSeries->d->setParentItem(this);
93 QColor nextColor = m_themeColors.takeFirst();
94 nextColor.setAlpha(150); // TODO: default opacity?
95 scatterSeries->setMarkerColor(nextColor);
96 }
97 case QChartSeries::SeriesTypePie: {
98 // TODO: we now have also a list of y values as a parameter, it is ignored
99 // we should use a generic data class instead of list of x and y values
100 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
101 connect(this, SIGNAL(sizeChanged(QRectF)),
102 pieSeries, SLOT(chartSizeChanged(QRectF)));
103 // TODO: how to define the color for all the slices of a pie?
104 }
93 105 }
94 106 }
95 107
@@ -97,28 +109,32 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
97 109 {
98 110 // TODO: support also other types; not only scatter and pie
99 111
112 QChartSeries *series(0);
113
100 114 switch (type) {
115 case QChartSeries::SeriesTypeLine: {
116 series = QXYChartSeries::create();
117 break;
118 }
119 case QChartSeries::SeriesTypeBar: {
120 series = new BarChartSeries(this);
121 break;
122 }
101 123 case QChartSeries::SeriesTypeScatter: {
102 QScatterSeries *scatterSeries = new QScatterSeries(this);
103 connect(this, SIGNAL(sizeChanged(QRectF)),
104 scatterSeries, SLOT(chartSizeChanged(QRectF)));
105 scatterSeries->d->setParentItem(this);
106 return scatterSeries;
124 series = new QScatterSeries(this);
125 break;
107 126 }
108 127 case QChartSeries::SeriesTypePie: {
109 // TODO: we now have also a list of y values as a parameter, it is ignored
110 // we should use a generic data class instead of list of x and y values
111 QPieSeries *pieSeries = new QPieSeries(this);
112 connect(this, SIGNAL(sizeChanged(QRectF)),
113 pieSeries, SLOT(chartSizeChanged(QRectF)));
114 return pieSeries;
128 series = new QPieSeries(this);
129 break;
115 130 }
116 131 default:
117 132 Q_ASSERT(false);
118 133 break;
119 134 }
120 135
121 return 0;
136 addSeries(series);
137 return series;
122 138 }
123 139
124 140 void QChart::setSize(const QSizeF& size)
@@ -159,7 +175,39 void QChart::setMargin(int margin)
159 175 m_marginSize = margin;
160 176 }
161 177
162 #include "moc_qchart.cpp"
178 void QChart::setTheme(QChart::ChartTheme theme)
179 {
180 // TODO: define color themes
181 switch (theme) {
182 case ChartThemeVanilla:
183 m_themeColors.append(QColor(255, 238, 174));
184 m_themeColors.append(QColor(228, 228, 160));
185 m_themeColors.append(QColor(228, 179, 160));
186 m_themeColors.append(QColor(180, 151, 18));
187 m_themeColors.append(QColor(252, 252, 37));
188 break;
189 case ChartThemeIcy:
190 m_themeColors.append(QColor(255, 238, 174));
191 m_themeColors.append(QColor(228, 228, 160));
192 m_themeColors.append(QColor(228, 179, 160));
193 m_themeColors.append(QColor(180, 151, 18));
194 m_themeColors.append(QColor(252, 252, 37));
195 break;
196 case ChartThemeGrayscale:
197 m_themeColors.append(QColor(255, 238, 174));
198 m_themeColors.append(QColor(228, 228, 160));
199 m_themeColors.append(QColor(228, 179, 160));
200 m_themeColors.append(QColor(180, 151, 18));
201 m_themeColors.append(QColor(252, 252, 37));
202 break;
203 default:
204 Q_ASSERT(false);
205 break;
206 }
163 207
208 // TODO: update coloring of different elements to match the selected theme
209 }
210
211 #include "moc_qchart.cpp"
164 212
165 213 QTCOMMERCIALCHART_END_NAMESPACE
@@ -26,6 +26,13 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
26 26 {
27 27 Q_OBJECT
28 28 public:
29 enum ChartTheme {
30 ChartThemeVanilla = 0,
31 ChartThemeIcy,
32 ChartThemeGrayscale
33 };
34
35 public:
29 36 QChart(QGraphicsObject* parent = 0);
30 37 ~QChart();
31 38
@@ -41,6 +48,7 public:
41 48 virtual void setSize(const QSizeF& rect);
42 49 void setMargin(int margin);
43 50 int margin() const;
51 void setTheme(QChart::ChartTheme theme);
44 52
45 53 signals:
46 54 void sizeChanged(QRectF rect);
@@ -58,6 +66,7 private:
58 66 QList<QGraphicsItem*> m_items;
59 67 int m_plotDataIndex;
60 68 int m_marginSize;
69 QList<QColor> m_themeColors;
61 70
62 71 QList<BarGroup*> m_BarGroupItems;
63 72 };
@@ -46,6 +46,12 QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type)
46 46 {
47 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 55 #include "moc_qchartwidget.cpp"
50 56
51 57 QTCOMMERCIALCHART_END_NAMESPACE
@@ -28,6 +28,12 public:
28 28 void addSeries(QChartSeries* series);
29 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 37 private:
32 38 Q_DISABLE_COPY(QChartWidget)
33 39 QGraphicsScene *m_scene;
@@ -12,7 +12,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12 QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) :
13 13 QGraphicsItem(parent),
14 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 42 QPen pen = painter->pen();
42 43 QBrush brush = pen.brush();
43 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 47 pen.setBrush(brush);
46 48 pen.setWidth(4);
47 49 painter->setPen(pen);
@@ -80,6 +82,11 void QScatterSeries::chartSizeChanged(QRectF rect)
80 82 d->resize(rect);
81 83 }
82 84
85 void QScatterSeries::setMarkerColor(QColor color)
86 {
87 d->m_markerColor = color;
88 }
89
83 90 // TODO:
84 91 //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale)
85 92 //{
@@ -3,6 +3,7
3 3
4 4 #include "qchartseries.h"
5 5 #include <QRectF>
6 #include <QColor>
6 7
7 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 9 class QScatterSeriesPrivate;
@@ -21,6 +22,7 public: // from QChartSeries
21 22
22 23 public Q_SLOTS:
23 24 void chartSizeChanged(QRectF rect);
25 void setMarkerColor(QColor color);
24 26 //void chartScaleChanged(qreal xscale, qreal yscale);
25 27
26 28 private:
@@ -26,6 +26,7 public: // from QGraphicsItem
26 26 qreal m_scaley;
27 27 QList<qreal> m_scenex;
28 28 QList<qreal> m_sceney;
29 QColor m_markerColor;
29 30 };
30 31
31 32 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,7 +3,7
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 QXYChartSeries::QXYChartSeries(QObject* parent):QChartSeries(parent),
6 m_color(Qt::black)
6 m_color()
7 7 {
8 8 }
9 9
@@ -58,6 +58,14 MainWidget::MainWidget(QWidget *parent) :
58 58 m_yMaxSpin->setValue(10);
59 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 69 QGridLayout *grid = new QGridLayout();
62 70 QGridLayout *mainLayout = new QGridLayout();
63 71 //grid->addWidget(new QLabel("Add series:"), 0, 0);
@@ -73,9 +81,11 MainWidget::MainWidget(QWidget *parent) :
73 81 grid->addWidget(m_yMinSpin, 6, 1);
74 82 grid->addWidget(new QLabel("y max:"), 7, 0);
75 83 grid->addWidget(m_yMaxSpin, 7, 1);
84 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
85 grid->addWidget(chartTheme, 8, 1);
76 86 // add row with empty label to make all the other rows static
77 grid->addWidget(new QLabel(""), 8, 0);
78 grid->setRowStretch(8, 1);
87 grid->addWidget(new QLabel(""), 9, 0);
88 grid->setRowStretch(9, 1);
79 89
80 90 mainLayout->addLayout(grid, 0, 0);
81 91
@@ -160,8 +170,19 void MainWidget::addSeries(QString series, QString data)
160 170 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
161 171 Q_ASSERT(newSeries->setData(y));
162 172 } else if (series == "Line") {
163 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
164 Q_ASSERT(newSeries->setData(x, y));
173 // TODO: adding data to an existing line series does not give any visuals for some reason
174 // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine);
175 // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries);
176 // lineSeries->setColor(Qt::blue);
177 // for (int i(0); i < x.count() && i < y.count(); i++) {
178 // lineSeries->add(x.at(i), y.at(i));
179 // }
180 //Q_ASSERT(newSeries->setData(x, y));
181 QXYChartSeries* series0 = QXYChartSeries::create();
182 for (int i(0); i < x.count() && i < y.count(); i++)
183 series0->add(x.at(i), y.at(i));
184 m_chartWidget->addSeries(series0);
185 newSeries = series0;
165 186 } else {
166 187 // TODO
167 188 }
@@ -279,6 +300,12 void MainWidget::yMaxChanged(int value)
279 300 qDebug() << "yMaxChanged: " << value;
280 301 }
281 302
303 void MainWidget::changeChartTheme(int themeIndex)
304 {
305 qDebug() << "changeChartTheme: " << themeIndex;
306 m_chartWidget->setTheme((QChart::ChartTheme) themeIndex);
307 }
308
282 309 void MainWidget::setPieSizeFactor(double size)
283 310 {
284 311 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
@@ -30,6 +30,7 private slots:
30 30 void yMinChanged(int value);
31 31 void yMaxChanged(int value);
32 32 void setCurrentSeries(QChartSeries *series);
33 void changeChartTheme(int themeIndex);
33 34 void setPieSizeFactor(double margin);
34 35
35 36 private:
General Comments 0
You need to be logged in to leave comments. Login now