##// END OF EJS Templates
Fixed line series usage in the test app
Tero Ahola -
r65:51e529f06d1a
parent child
Show More
@@ -1,203 +1,213
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartseries.h"
2 #include "qchartseries.h"
3 #include "qscatterseries.h"
3 #include "qscatterseries.h"
4 #include "qscatterseries_p.h"
4 #include "qscatterseries_p.h"
5 #include "qpieseries.h"
5 #include "qpieseries.h"
6 #include "qxychartseries.h"
6 #include "qxychartseries.h"
7
7
8 #include "barchartseries.h"
8 #include "barchartseries.h"
9 #include "bargroup.h"
9 #include "bargroup.h"
10
10
11 #include "xylinechartitem_p.h"
11 #include "xylinechartitem_p.h"
12 #include "xyplotdomain_p.h"
12 #include "xyplotdomain_p.h"
13 #include "axis_p.h"
13 #include "axis_p.h"
14 #include "xygrid_p.h"
14 #include "xygrid_p.h"
15 #include <QGraphicsScene>
15 #include <QGraphicsScene>
16 #include <QDebug>
16 #include <QDebug>
17
17
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?
27 // TODO: the default theme?
28 setTheme(QChart::ChartThemeVanilla);
28 setTheme(QChart::ChartThemeVanilla);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
30 // set axis
30 // set axis
31 m_axisY->rotate(90);
31 m_axisY->rotate(90);
32 }
32 }
33
33
34 QChart::~QChart(){}
34 QChart::~QChart(){}
35
35
36 QRectF QChart::boundingRect() const
36 QRectF QChart::boundingRect() const
37 {
37 {
38 return m_rect;
38 return m_rect;
39 }
39 }
40
40
41 void QChart::addSeries(QChartSeries* series)
41 void QChart::addSeries(QChartSeries* series)
42 {
42 {
43 // TODO: we should check the series not already added
43 // TODO: we should check the series not already added
44
44
45 m_series<<series;
45 m_series<<series;
46
46
47 switch(series->type())
47 switch(series->type())
48 {
48 {
49 case QChartSeries::SeriesTypeLine: {
49 case QChartSeries::SeriesTypeLine: {
50
50
51 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
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());
52
55
53 XYPlotDomain domain;
56 XYPlotDomain domain;
54 //TODO "nice numbers algorithm"
57 //TODO "nice numbers algorithm"
55 domain.m_ticksX=4;
58 domain.m_ticksX=4;
56 domain.m_ticksY=4;
59 domain.m_ticksY=4;
57
60
58 for (int i = 0 ; i < xyseries->count() ; i++)
61 for (int i = 0 ; i < xyseries->count() ; i++)
59 {
62 {
60 qreal x = xyseries->x(i);
63 qreal x = xyseries->x(i);
61 qreal y = xyseries->y(i);
64 qreal y = xyseries->y(i);
62 domain.m_minX = qMin(domain.m_minX,x);
65 domain.m_minX = qMin(domain.m_minX,x);
63 domain.m_minY = qMin(domain.m_minY,y);
66 domain.m_minY = qMin(domain.m_minY,y);
64 domain.m_maxX = qMax(domain.m_maxX,x);
67 domain.m_maxX = qMax(domain.m_maxX,x);
65 domain.m_maxY = qMax(domain.m_maxY,y);
68 domain.m_maxY = qMax(domain.m_maxY,y);
66 }
69 }
67
70
68 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
71 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
69 item->updateXYPlotDomain(domain);
72 item->updateXYPlotDomain(domain);
70 m_plotDomainList<<domain;
73 m_plotDomainList<<domain;
71 m_xyLineChartItems<<item;
74 m_xyLineChartItems<<item;
72 break;
75 break;
73 }
76 }
74 // TODO: Not tested:
75 // case QChartSeries::SeriesTypeScatter: {
76 // QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
77 // if (scatter) {
78 // scatter->d->setParentItem(this);
79 // scene()->addItem(scatter->d);
80 // }
81 // break;
82 // }
83
84 case QChartSeries::SeriesTypeBar: {
77 case QChartSeries::SeriesTypeBar: {
85
78
86 qDebug() << "barSeries added";
79 qDebug() << "barSeries added";
87 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
80 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
88
81
89 // Who owns the series?
82 // Who owns the series?
90 BarGroup* group = new BarGroup(*barSeries, this);
83 BarGroup* group = new BarGroup(*barSeries, this);
91 scene()->addItem(group);
84 scene()->addItem(group);
92 m_BarGroupItems.append(group); // If we need to access group later
85 m_BarGroupItems.append(group); // If we need to access group later
93 break;
86 break;
94 }
87 }
95 }
96 }
97
98 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
99 {
100 // TODO: support also other types; not only scatter and pie
101
102 switch (type) {
103 case QChartSeries::SeriesTypeScatter: {
88 case QChartSeries::SeriesTypeScatter: {
104 QScatterSeries *scatterSeries = new QScatterSeries(this);
89 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
105 connect(this, SIGNAL(sizeChanged(QRectF)),
90 connect(this, SIGNAL(sizeChanged(QRectF)),
106 scatterSeries, SLOT(chartSizeChanged(QRectF)));
91 scatterSeries, SLOT(chartSizeChanged(QRectF)));
107 scatterSeries->d->setParentItem(this);
92 scatterSeries->d->setParentItem(this);
108 QColor nextColor = m_themeColors.takeFirst();
93 QColor nextColor = m_themeColors.takeFirst();
109 nextColor.setAlpha(150); // TODO: default opacity?
94 nextColor.setAlpha(150); // TODO: default opacity?
110 scatterSeries->setMarkerColor(nextColor);
95 scatterSeries->setMarkerColor(nextColor);
111 return scatterSeries;
96 }
112 }
113 case QChartSeries::SeriesTypePie: {
97 case QChartSeries::SeriesTypePie: {
114 // TODO: we now have also a list of y values as a parameter, it is ignored
98 // TODO: we now have also a list of y values as a parameter, it is ignored
115 // we should use a generic data class instead of list of x and y values
99 // we should use a generic data class instead of list of x and y values
116 QPieSeries *pieSeries = new QPieSeries(this);
100 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
117 connect(this, SIGNAL(sizeChanged(QRectF)),
101 connect(this, SIGNAL(sizeChanged(QRectF)),
118 pieSeries, SLOT(chartSizeChanged(QRectF)));
102 pieSeries, SLOT(chartSizeChanged(QRectF)));
119 // TODO: how to define the color for all the slices of a pie?
103 // TODO: how to define the color for all the slices of a pie?
120 return pieSeries;
104 }
105 }
106 }
107
108 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
109 {
110 // TODO: support also other types; not only scatter and pie
111
112 QChartSeries *series(0);
113
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 }
123 case QChartSeries::SeriesTypeScatter: {
124 series = new QScatterSeries(this);
125 break;
126 }
127 case QChartSeries::SeriesTypePie: {
128 series = new QPieSeries(this);
129 break;
121 }
130 }
122 default:
131 default:
123 Q_ASSERT(false);
132 Q_ASSERT(false);
124 break;
133 break;
125 }
134 }
126
135
127 return 0;
136 addSeries(series);
137 return series;
128 }
138 }
129
139
130 void QChart::setSize(const QSizeF& size)
140 void QChart::setSize(const QSizeF& size)
131 {
141 {
132 m_rect = QRect(QPoint(0,0),size.toSize());
142 m_rect = QRect(QPoint(0,0),size.toSize());
133 m_rect.adjust(margin(),margin(), -margin(), -margin());
143 m_rect.adjust(margin(),margin(), -margin(), -margin());
134 m_grid->setPos(m_rect.topLeft());
144 m_grid->setPos(m_rect.topLeft());
135 m_grid->setSize(m_rect.size());
145 m_grid->setSize(m_rect.size());
136
146
137 // TODO: TTD for setting scale
147 // TODO: TTD for setting scale
138 //emit scaleChanged(100, 100);
148 //emit scaleChanged(100, 100);
139 // TODO: calculate the origo
149 // TODO: calculate the origo
140 // TODO: not sure if emitting a signal here is the best from performance point of view
150 // TODO: not sure if emitting a signal here is the best from performance point of view
141 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
151 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
142
152
143 for (int i(0); i < m_plotDomainList.size(); i++)
153 for (int i(0); i < m_plotDomainList.size(); i++)
144 m_plotDomainList[i].m_viewportRect = m_rect;
154 m_plotDomainList[i].m_viewportRect = m_rect;
145
155
146 // TODO: line chart items are updated separately as they don't support update
156 // TODO: line chart items are updated separately as they don't support update
147 // via sizeChanged signal
157 // via sizeChanged signal
148 foreach(XYLineChartItem* item ,m_xyLineChartItems)
158 foreach(XYLineChartItem* item ,m_xyLineChartItems)
149 item->updateXYPlotDomain(m_plotDomainList.at(m_plotDataIndex));
159 item->updateXYPlotDomain(m_plotDomainList.at(m_plotDataIndex));
150
160
151
161
152 if (m_plotDomainList.count())
162 if (m_plotDomainList.count())
153 m_grid->setXYPlotData(m_plotDomainList.at(m_plotDataIndex));
163 m_grid->setXYPlotData(m_plotDomainList.at(m_plotDataIndex));
154
164
155 update();
165 update();
156 }
166 }
157
167
158 int QChart::margin() const
168 int QChart::margin() const
159 {
169 {
160 return m_marginSize;
170 return m_marginSize;
161 }
171 }
162
172
163 void QChart::setMargin(int margin)
173 void QChart::setMargin(int margin)
164 {
174 {
165 m_marginSize = margin;
175 m_marginSize = margin;
166 }
176 }
167
177
168 void QChart::setTheme(QChart::ChartTheme theme)
178 void QChart::setTheme(QChart::ChartTheme theme)
169 {
179 {
170 // TODO: define color themes
180 // TODO: define color themes
171 switch (theme) {
181 switch (theme) {
172 case ChartThemeVanilla:
182 case ChartThemeVanilla:
173 m_themeColors.append(QColor(255, 238, 174));
183 m_themeColors.append(QColor(255, 238, 174));
174 m_themeColors.append(QColor(228, 228, 160));
184 m_themeColors.append(QColor(228, 228, 160));
175 m_themeColors.append(QColor(228, 179, 160));
185 m_themeColors.append(QColor(228, 179, 160));
176 m_themeColors.append(QColor(180, 151, 18));
186 m_themeColors.append(QColor(180, 151, 18));
177 m_themeColors.append(QColor(252, 252, 37));
187 m_themeColors.append(QColor(252, 252, 37));
178 break;
188 break;
179 case ChartThemeIcy:
189 case ChartThemeIcy:
180 m_themeColors.append(QColor(255, 238, 174));
190 m_themeColors.append(QColor(255, 238, 174));
181 m_themeColors.append(QColor(228, 228, 160));
191 m_themeColors.append(QColor(228, 228, 160));
182 m_themeColors.append(QColor(228, 179, 160));
192 m_themeColors.append(QColor(228, 179, 160));
183 m_themeColors.append(QColor(180, 151, 18));
193 m_themeColors.append(QColor(180, 151, 18));
184 m_themeColors.append(QColor(252, 252, 37));
194 m_themeColors.append(QColor(252, 252, 37));
185 break;
195 break;
186 case ChartThemeGrayscale:
196 case ChartThemeGrayscale:
187 m_themeColors.append(QColor(255, 238, 174));
197 m_themeColors.append(QColor(255, 238, 174));
188 m_themeColors.append(QColor(228, 228, 160));
198 m_themeColors.append(QColor(228, 228, 160));
189 m_themeColors.append(QColor(228, 179, 160));
199 m_themeColors.append(QColor(228, 179, 160));
190 m_themeColors.append(QColor(180, 151, 18));
200 m_themeColors.append(QColor(180, 151, 18));
191 m_themeColors.append(QColor(252, 252, 37));
201 m_themeColors.append(QColor(252, 252, 37));
192 break;
202 break;
193 default:
203 default:
194 Q_ASSERT(false);
204 Q_ASSERT(false);
195 break;
205 break;
196 }
206 }
197
207
198 // TODO: update coloring of different elements to match the selected theme
208 // TODO: update coloring of different elements to match the selected theme
199 }
209 }
200
210
201 #include "moc_qchart.cpp"
211 #include "moc_qchart.cpp"
202
212
203 QTCOMMERCIALCHART_END_NAMESPACE
213 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,68 +1,68
1 #include "qxychartseries.h"
1 #include "qxychartseries.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 QXYChartSeries::QXYChartSeries(QObject* parent):QChartSeries(parent),
5 QXYChartSeries::QXYChartSeries(QObject* parent):QChartSeries(parent),
6 m_color(Qt::black)
6 m_color()
7 {
7 {
8 }
8 }
9
9
10 QXYChartSeries::~QXYChartSeries()
10 QXYChartSeries::~QXYChartSeries()
11 {
11 {
12 }
12 }
13
13
14 QXYChartSeries* QXYChartSeries::create(QObject* parent)
14 QXYChartSeries* QXYChartSeries::create(QObject* parent)
15 {
15 {
16 //TODO: here we take QChartData when it is ready
16 //TODO: here we take QChartData when it is ready
17 // return null if malformed;
17 // return null if malformed;
18 return new QXYChartSeries(parent);
18 return new QXYChartSeries(parent);
19 }
19 }
20
20
21 void QXYChartSeries::setColor(const QColor& color)
21 void QXYChartSeries::setColor(const QColor& color)
22 {
22 {
23 m_color = color;
23 m_color = color;
24 }
24 }
25
25
26 void QXYChartSeries::add(qreal x,qreal y)
26 void QXYChartSeries::add(qreal x,qreal y)
27 {
27 {
28 m_x<<x;
28 m_x<<x;
29 m_y<<y;
29 m_y<<y;
30 }
30 }
31
31
32 void QXYChartSeries::clear()
32 void QXYChartSeries::clear()
33 {
33 {
34 m_x.clear();
34 m_x.clear();
35 m_y.clear();
35 m_y.clear();
36 }
36 }
37
37
38 qreal QXYChartSeries::x(int pos) const
38 qreal QXYChartSeries::x(int pos) const
39 {
39 {
40 return m_x.at(pos);
40 return m_x.at(pos);
41 }
41 }
42
42
43 qreal QXYChartSeries::y(int pos) const
43 qreal QXYChartSeries::y(int pos) const
44 {
44 {
45 return m_y.at(pos);
45 return m_y.at(pos);
46 }
46 }
47
47
48 int QXYChartSeries::count() const
48 int QXYChartSeries::count() const
49 {
49 {
50 Q_ASSERT(m_x.size() == m_y.size());
50 Q_ASSERT(m_x.size() == m_y.size());
51
51
52 return m_x.size();
52 return m_x.size();
53
53
54 }
54 }
55
55
56 QDebug operator<< (QDebug debug, const QXYChartSeries series)
56 QDebug operator<< (QDebug debug, const QXYChartSeries series)
57 {
57 {
58 Q_ASSERT(series.m_x.size() == series.m_y.size());
58 Q_ASSERT(series.m_x.size() == series.m_y.size());
59
59
60 int size = series.m_x.size();
60 int size = series.m_x.size();
61
61
62 for (int i=0;i<size;i++) {
62 for (int i=0;i<size;i++) {
63 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
63 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
64 }
64 }
65 return debug.space();
65 return debug.space();
66 }
66 }
67
67
68 QTCOMMERCIALCHART_END_NAMESPACE
68 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,300 +1,311
1 #include "mainwidget.h"
1 #include "mainwidget.h"
2 #include "dataseriedialog.h"
2 #include "dataseriedialog.h"
3 #include "qchartseries.h"
3 #include "qchartseries.h"
4 #include "qpieseries.h"
4 #include "qpieseries.h"
5 #include <qxychartseries.h>
5 #include <qxychartseries.h>
6 #include <barchartseries.h>
6 #include <barchartseries.h>
7 #include <QPushButton>
7 #include <QPushButton>
8 #include <QComboBox>
8 #include <QComboBox>
9 #include <QSpinBox>
9 #include <QSpinBox>
10 #include <QCheckBox>
10 #include <QCheckBox>
11 #include <QGridLayout>
11 #include <QGridLayout>
12 #include <QHBoxLayout>
12 #include <QHBoxLayout>
13 #include <QLabel>
13 #include <QLabel>
14 #include <QSpacerItem>
14 #include <QSpacerItem>
15 #include <QMessageBox>
15 #include <QMessageBox>
16 #include <cmath>
16 #include <cmath>
17 #include <QDebug>
17 #include <QDebug>
18
18
19 QTCOMMERCIALCHART_USE_NAMESPACE
19 QTCOMMERCIALCHART_USE_NAMESPACE
20
20
21 MainWidget::MainWidget(QWidget *parent) :
21 MainWidget::MainWidget(QWidget *parent) :
22 QWidget(parent)
22 QWidget(parent)
23 {
23 {
24 QPushButton *addSeriesButton = new QPushButton("Add series");
24 QPushButton *addSeriesButton = new QPushButton("Add series");
25 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
25 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
26
26
27 // Chart background
27 // Chart background
28 QComboBox *backgroundCombo = new QComboBox(this);
28 QComboBox *backgroundCombo = new QComboBox(this);
29 backgroundCombo->addItem("None");
29 backgroundCombo->addItem("None");
30 backgroundCombo->addItem("TODO Grid");
30 backgroundCombo->addItem("TODO Grid");
31 backgroundCombo->addItem("TODO Image");
31 backgroundCombo->addItem("TODO Image");
32 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
32 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
33 this, SLOT(backgroundChanged(int)));
33 this, SLOT(backgroundChanged(int)));
34
34
35 // Axis
35 // Axis
36 // TODO: multiple axes?
36 // TODO: multiple axes?
37 m_autoScaleCheck = new QCheckBox("Automatic scaling");
37 m_autoScaleCheck = new QCheckBox("Automatic scaling");
38 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
38 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
39 // Allow setting also non-sense values (like -2147483648 and 2147483647)
39 // Allow setting also non-sense values (like -2147483648 and 2147483647)
40 m_xMinSpin = new QSpinBox();
40 m_xMinSpin = new QSpinBox();
41 m_xMinSpin->setMinimum(INT_MIN);
41 m_xMinSpin->setMinimum(INT_MIN);
42 m_xMinSpin->setMaximum(INT_MAX);
42 m_xMinSpin->setMaximum(INT_MAX);
43 m_xMinSpin->setValue(0);
43 m_xMinSpin->setValue(0);
44 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
44 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
45 m_xMaxSpin = new QSpinBox();
45 m_xMaxSpin = new QSpinBox();
46 m_xMaxSpin->setMinimum(INT_MIN);
46 m_xMaxSpin->setMinimum(INT_MIN);
47 m_xMaxSpin->setMaximum(INT_MAX);
47 m_xMaxSpin->setMaximum(INT_MAX);
48 m_xMaxSpin->setValue(10);
48 m_xMaxSpin->setValue(10);
49 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
49 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
50 m_yMinSpin = new QSpinBox();
50 m_yMinSpin = new QSpinBox();
51 m_yMinSpin->setMinimum(INT_MIN);
51 m_yMinSpin->setMinimum(INT_MIN);
52 m_yMinSpin->setMaximum(INT_MAX);
52 m_yMinSpin->setMaximum(INT_MAX);
53 m_yMinSpin->setValue(0);
53 m_yMinSpin->setValue(0);
54 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
54 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
55 m_yMaxSpin = new QSpinBox();
55 m_yMaxSpin = new QSpinBox();
56 m_yMaxSpin->setMinimum(INT_MIN);
56 m_yMaxSpin->setMinimum(INT_MIN);
57 m_yMaxSpin->setMaximum(INT_MAX);
57 m_yMaxSpin->setMaximum(INT_MAX);
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();
61 QComboBox *chartTheme = new QComboBox();
62 chartTheme->addItem("Vanilla");
62 chartTheme->addItem("Vanilla");
63 chartTheme->addItem("Icy");
63 chartTheme->addItem("Icy");
64 chartTheme->addItem("Grayscale");
64 chartTheme->addItem("Grayscale");
65 chartTheme->addItem("Tobedefined");
65 chartTheme->addItem("Tobedefined");
66 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
66 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
67 this, SLOT(changeChartTheme(int)));
67 this, SLOT(changeChartTheme(int)));
68
68
69 QGridLayout *grid = new QGridLayout();
69 QGridLayout *grid = new QGridLayout();
70 QGridLayout *mainLayout = new QGridLayout();
70 QGridLayout *mainLayout = new QGridLayout();
71 //grid->addWidget(new QLabel("Add series:"), 0, 0);
71 //grid->addWidget(new QLabel("Add series:"), 0, 0);
72 grid->addWidget(addSeriesButton, 0, 1);
72 grid->addWidget(addSeriesButton, 0, 1);
73 grid->addWidget(new QLabel("Background:"), 2, 0);
73 grid->addWidget(new QLabel("Background:"), 2, 0);
74 grid->addWidget(backgroundCombo, 2, 1);
74 grid->addWidget(backgroundCombo, 2, 1);
75 grid->addWidget(m_autoScaleCheck, 3, 0);
75 grid->addWidget(m_autoScaleCheck, 3, 0);
76 grid->addWidget(new QLabel("x min:"), 4, 0);
76 grid->addWidget(new QLabel("x min:"), 4, 0);
77 grid->addWidget(m_xMinSpin, 4, 1);
77 grid->addWidget(m_xMinSpin, 4, 1);
78 grid->addWidget(new QLabel("x max:"), 5, 0);
78 grid->addWidget(new QLabel("x max:"), 5, 0);
79 grid->addWidget(m_xMaxSpin, 5, 1);
79 grid->addWidget(m_xMaxSpin, 5, 1);
80 grid->addWidget(new QLabel("y min:"), 6, 0);
80 grid->addWidget(new QLabel("y min:"), 6, 0);
81 grid->addWidget(m_yMinSpin, 6, 1);
81 grid->addWidget(m_yMinSpin, 6, 1);
82 grid->addWidget(new QLabel("y max:"), 7, 0);
82 grid->addWidget(new QLabel("y max:"), 7, 0);
83 grid->addWidget(m_yMaxSpin, 7, 1);
83 grid->addWidget(m_yMaxSpin, 7, 1);
84 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
84 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
85 grid->addWidget(chartTheme, 8, 1);
85 grid->addWidget(chartTheme, 8, 1);
86 // 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
87 grid->addWidget(new QLabel(""), 9, 0);
87 grid->addWidget(new QLabel(""), 9, 0);
88 grid->setRowStretch(9, 1);
88 grid->setRowStretch(9, 1);
89
89
90 mainLayout->addLayout(grid, 0, 0);
90 mainLayout->addLayout(grid, 0, 0);
91
91
92 // Scatter specific settings
92 // Scatter specific settings
93 m_scatterLayout = new QGridLayout();
93 m_scatterLayout = new QGridLayout();
94 m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
94 m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
95 m_scatterLayout->setEnabled(false);
95 m_scatterLayout->setEnabled(false);
96
96
97 // Pie specific settings
97 // Pie specific settings
98 m_pieLayout = new QGridLayout();
98 m_pieLayout = new QGridLayout();
99 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
99 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
100 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
100 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
101 pieSizeSpin->setMinimum(LONG_MIN);
101 pieSizeSpin->setMinimum(LONG_MIN);
102 pieSizeSpin->setMaximum(LONG_MAX);
102 pieSizeSpin->setMaximum(LONG_MAX);
103 pieSizeSpin->setValue(1.0);
103 pieSizeSpin->setValue(1.0);
104 pieSizeSpin->setSingleStep(0.1);
104 pieSizeSpin->setSingleStep(0.1);
105 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
105 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
106 m_pieLayout->setEnabled(false);
106 m_pieLayout->setEnabled(false);
107 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
107 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
108
108
109 mainLayout->addLayout(m_scatterLayout, 1, 0);
109 mainLayout->addLayout(m_scatterLayout, 1, 0);
110 mainLayout->addLayout(m_pieLayout, 2, 0);
110 mainLayout->addLayout(m_pieLayout, 2, 0);
111
111
112 m_chartWidget = new QChartWidget(this);
112 m_chartWidget = new QChartWidget(this);
113 //m_chartWidget->setColor(Qt::red);
113 //m_chartWidget->setColor(Qt::red);
114 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
114 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
115 // hbox->setStretch(1, 1);
115 // hbox->setStretch(1, 1);
116
116
117 setLayout(mainLayout);
117 setLayout(mainLayout);
118
118
119 m_autoScaleCheck->setChecked(true);
119 m_autoScaleCheck->setChecked(true);
120 testDataChanged(0);
120 testDataChanged(0);
121 }
121 }
122
122
123 void MainWidget::addSeries()
123 void MainWidget::addSeries()
124 {
124 {
125 DataSerieDialog dialog(m_defaultSeriesName, this);
125 DataSerieDialog dialog(m_defaultSeriesName, this);
126 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
126 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
127 dialog.exec();
127 dialog.exec();
128 }
128 }
129
129
130 void MainWidget::addSeries(QString series, QString data)
130 void MainWidget::addSeries(QString series, QString data)
131 {
131 {
132 qDebug() << "addSeries: " << series << " data: " << data;
132 qDebug() << "addSeries: " << series << " data: " << data;
133 m_defaultSeriesName = series;
133 m_defaultSeriesName = series;
134
134
135 // TODO: a dedicated data class for storing x and y values
135 // TODO: a dedicated data class for storing x and y values
136 QList<qreal> x;
136 QList<qreal> x;
137 QList<qreal> y;
137 QList<qreal> y;
138
138
139 if (data == "linear") {
139 if (data == "linear") {
140 for (int i = 0; i < 20; i++) {
140 for (int i = 0; i < 20; i++) {
141 x.append(i);
141 x.append(i);
142 y.append(i);
142 y.append(i);
143 }
143 }
144 } else if (data == "linear, 1M") {
144 } else if (data == "linear, 1M") {
145 for (int i = 0; i < 10000; i++) {
145 for (int i = 0; i < 10000; i++) {
146 x.append(i);
146 x.append(i);
147 y.append(20);
147 y.append(20);
148 }
148 }
149 } else if (data == "SIN") {
149 } else if (data == "SIN") {
150 for (int i = 0; i < 100; i++) {
150 for (int i = 0; i < 100; i++) {
151 x.append(i);
151 x.append(i);
152 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
152 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
153 }
153 }
154 } else if (data == "SIN + random") {
154 } else if (data == "SIN + random") {
155 for (qreal i = 0; i < 100; i += 0.1) {
155 for (qreal i = 0; i < 100; i += 0.1) {
156 x.append(i + (rand() % 5));
156 x.append(i + (rand() % 5));
157 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
157 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
158 }
158 }
159 } else {
159 } else {
160 // TODO: check if data has a valid file name
160 // TODO: check if data has a valid file name
161 Q_ASSERT(false);
161 Q_ASSERT(false);
162 }
162 }
163
163
164 // TODO: color of the series
164 // TODO: color of the series
165 QChartSeries *newSeries = 0;
165 QChartSeries *newSeries = 0;
166 if (series == "Scatter") {
166 if (series == "Scatter") {
167 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter);
167 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter);
168 Q_ASSERT(newSeries->setData(x, y));
168 Q_ASSERT(newSeries->setData(x, y));
169 } else if (series == "Pie") {
169 } else if (series == "Pie") {
170 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
170 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
171 Q_ASSERT(newSeries->setData(y));
171 Q_ASSERT(newSeries->setData(y));
172 } else if (series == "Line") {
172 } else if (series == "Line") {
173 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
173 // TODO: adding data to an existing line series does not give any visuals for some reason
174 Q_ASSERT(newSeries->setData(x, y));
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;
175 } else {
186 } else {
176 // TODO
187 // TODO
177 }
188 }
178
189
179 // BarChart
190 // BarChart
180 if (series == "Bar") {
191 if (series == "Bar") {
181 qDebug() << "Bar chart series";
192 qDebug() << "Bar chart series";
182 QChartSeries* barSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this);
193 QChartSeries* barSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this);
183 QList<int> barData;
194 QList<int> barData;
184 barData << 1;
195 barData << 1;
185 barData << 12;
196 barData << 12;
186 barData << 5;
197 barData << 5;
187 barData << 8;
198 barData << 8;
188 barData << 17;
199 barData << 17;
189 barData << 9;
200 barData << 9;
190 barSeries->setData(barData);
201 barSeries->setData(barData);
191 m_chartWidget->addSeries(barSeries);
202 m_chartWidget->addSeries(barSeries);
192
203
193 }
204 }
194
205
195 setCurrentSeries(newSeries);
206 setCurrentSeries(newSeries);
196 }
207 }
197
208
198 void MainWidget::setCurrentSeries(QChartSeries *series)
209 void MainWidget::setCurrentSeries(QChartSeries *series)
199 {
210 {
200 m_currentSeries = series;
211 m_currentSeries = series;
201 switch (m_currentSeries->type()) {
212 switch (m_currentSeries->type()) {
202 case QChartSeries::SeriesTypeLine:
213 case QChartSeries::SeriesTypeLine:
203 break;
214 break;
204 case QChartSeries::SeriesTypeScatter:
215 case QChartSeries::SeriesTypeScatter:
205 break;
216 break;
206 case QChartSeries::SeriesTypePie:
217 case QChartSeries::SeriesTypePie:
207 break;
218 break;
208 default:
219 default:
209 Q_ASSERT(false);
220 Q_ASSERT(false);
210 break;
221 break;
211 }
222 }
212 }
223 }
213
224
214 void MainWidget::testDataChanged(int itemIndex)
225 void MainWidget::testDataChanged(int itemIndex)
215 {
226 {
216 qDebug() << "testDataChanged: " << itemIndex;
227 qDebug() << "testDataChanged: " << itemIndex;
217
228
218 // switch (itemIndex) {
229 // switch (itemIndex) {
219 // case 0: {
230 // case 0: {
220 // QList<QChartDataPoint> data;
231 // QList<QChartDataPoint> data;
221 // for (int x = 0; x < 20; x++) {
232 // for (int x = 0; x < 20; x++) {
222 // data.append(QChartDataPoint() << x << x / 2);
233 // data.append(QChartDataPoint() << x << x / 2);
223 // }
234 // }
224 // m_chartWidget->setData(data);
235 // m_chartWidget->setData(data);
225 // break;
236 // break;
226 // }
237 // }
227 // case 1: {
238 // case 1: {
228 // QList<QChartDataPoint> data;
239 // QList<QChartDataPoint> data;
229 // for (int x = 0; x < 100; x++) {
240 // for (int x = 0; x < 100; x++) {
230 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
241 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
231 // }
242 // }
232 // m_chartWidget->setData(data);
243 // m_chartWidget->setData(data);
233 // break;
244 // break;
234 // }
245 // }
235 // case 2: {
246 // case 2: {
236 // QList<QChartDataPoint> data;
247 // QList<QChartDataPoint> data;
237 // for (int x = 0; x < 1000; x++) {
248 // for (int x = 0; x < 1000; x++) {
238 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
249 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
239 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
250 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
240 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
251 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
241 // }
252 // }
242 // m_chartWidget->setData(data);
253 // m_chartWidget->setData(data);
243 // break;
254 // break;
244 // }
255 // }
245 // default:
256 // default:
246 // break;
257 // break;
247 // }
258 // }
248 }
259 }
249
260
250 void MainWidget::backgroundChanged(int itemIndex)
261 void MainWidget::backgroundChanged(int itemIndex)
251 {
262 {
252 qDebug() << "backgroundChanged: " << itemIndex;
263 qDebug() << "backgroundChanged: " << itemIndex;
253 }
264 }
254
265
255 void MainWidget::autoScaleChanged(int value)
266 void MainWidget::autoScaleChanged(int value)
256 {
267 {
257 if (value) {
268 if (value) {
258 // TODO: enable auto scaling
269 // TODO: enable auto scaling
259 } else {
270 } else {
260 // TODO: set scaling manually (and disable auto scaling)
271 // TODO: set scaling manually (and disable auto scaling)
261 }
272 }
262
273
263 m_xMinSpin->setEnabled(!value);
274 m_xMinSpin->setEnabled(!value);
264 m_xMaxSpin->setEnabled(!value);
275 m_xMaxSpin->setEnabled(!value);
265 m_yMinSpin->setEnabled(!value);
276 m_yMinSpin->setEnabled(!value);
266 m_yMaxSpin->setEnabled(!value);
277 m_yMaxSpin->setEnabled(!value);
267 }
278 }
268
279
269 void MainWidget::xMinChanged(int value)
280 void MainWidget::xMinChanged(int value)
270 {
281 {
271 qDebug() << "xMinChanged: " << value;
282 qDebug() << "xMinChanged: " << value;
272 }
283 }
273
284
274 void MainWidget::xMaxChanged(int value)
285 void MainWidget::xMaxChanged(int value)
275 {
286 {
276 qDebug() << "xMaxChanged: " << value;
287 qDebug() << "xMaxChanged: " << value;
277 }
288 }
278
289
279 void MainWidget::yMinChanged(int value)
290 void MainWidget::yMinChanged(int value)
280 {
291 {
281 qDebug() << "yMinChanged: " << value;
292 qDebug() << "yMinChanged: " << value;
282 }
293 }
283
294
284 void MainWidget::yMaxChanged(int value)
295 void MainWidget::yMaxChanged(int value)
285 {
296 {
286 qDebug() << "yMaxChanged: " << value;
297 qDebug() << "yMaxChanged: " << value;
287 }
298 }
288
299
289 void MainWidget::changeChartTheme(int themeIndex)
300 void MainWidget::changeChartTheme(int themeIndex)
290 {
301 {
291 qDebug() << "changeChartTheme: " << themeIndex;
302 qDebug() << "changeChartTheme: " << themeIndex;
292 m_chartWidget->setTheme((QChart::ChartTheme) themeIndex);
303 m_chartWidget->setTheme((QChart::ChartTheme) themeIndex);
293 }
304 }
294
305
295 void MainWidget::setPieSizeFactor(double size)
306 void MainWidget::setPieSizeFactor(double size)
296 {
307 {
297 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
308 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
298 Q_ASSERT(pie);
309 Q_ASSERT(pie);
299 pie->setSizeFactor(qreal(size));
310 pie->setSizeFactor(qreal(size));
300 }
311 }
General Comments 0
You need to be logged in to leave comments. Login now