##// END OF EJS Templates
Refactored series creation.
sauimone -
r62:d89198258a93
parent child
Show More
@@ -0,0 +1,32
1 #include "qchartglobal.h"
2 #include "qchartseries.h"
3
4 #include "barchartseries.h"
5 #include "qxychartseries.h"
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 QChartSeries* QChartSeries::create(QChartSeriesType type, QObject* parent)
10 {
11 // TODO: Other types
12 switch (type) {
13 case QChartSeries::SeriesTypeLine: {
14 QXYChartSeries* s = QXYChartSeries::create(parent); // TODO: do we need create method for derived implementations?
15 return s;
16 }
17 case QChartSeries::SeriesTypePie: {
18 return 0;
19 }
20 case QChartSeries::SeriesTypeScatter: {
21 return 0;
22 }
23 case QChartSeries::SeriesTypeBar: {
24 BarChartSeries* s = new BarChartSeries(parent);
25 return s;
26 }
27 default:
28 return 0;
29 }
30 }
31
32 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,46 +1,55
1 1 #include "barchartseries.h"
2 2 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 3
4 BarChartSeries::BarChartSeries(QList<int> data, QObject *parent)
4 BarChartSeries::BarChartSeries(QObject *parent)
5 5 : QChartSeries(parent)
6 , mData(data)
7 6 {
8 7 }
9 8
9 bool BarChartSeries::setData(QList<int> data)
10 {
11 mData = data;
12 return true;
13 }
14
10 15 int BarChartSeries::min()
11 16 {
17 Q_ASSERT(mData.count() > 0);
18
12 19 // TODO: make min and max members and update them when data changes.
13 20 // This is slower since they are checked every time, even if data is same since previous call.
14 21 int min = mData.at(0);
15 22
16 23 for (int i=0; i <mData.count(); i++) {
17 24 if (mData.at(i) < min) {
18 25 min = mData.at(i);
19 26 }
20 27 }
21 28 return min;
22 29 }
23 30
24 31 int BarChartSeries::max()
25 32 {
33 Q_ASSERT(mData.count() > 0);
34
26 35 int max = mData.at(0);
27 36
28 37 for (int i=0; i <mData.count(); i++) {
29 38 if (mData.at(i) > max) {
30 39 max = mData.at(i);
31 40 }
32 41 }
33 42 return max;
34 43 }
35 44
36 45 int BarChartSeries::count()
37 46 {
38 47 return mData.count();
39 48 }
40 49
41 50 int BarChartSeries::valueAt(int i)
42 51 {
43 52 return mData.at(i);
44 53 }
45 54
46 55 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,34 +1,39
1 1 #ifndef BARCHARTSERIES_H
2 2 #define BARCHARTSERIES_H
3 3
4 4 #include <QList>
5 5 #include "qchartseries.h"
6 6 #include "qchartglobal.h"
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 // Container for series
11 11 class QTCOMMERCIALCHART_EXPORT BarChartSeries : public QChartSeries
12 12 {
13
13 // TODO:
14 // Q_OBJECT
14 15 public:
15 BarChartSeries(QList<int> data, QObject* parent=0);
16 BarChartSeries(QObject* parent=0);
16 17
17 18 // from QChartSeries
18 static QChartSeries* create(QObject* parent = 0 );
19 19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
20 20
21 virtual bool setData(QList<int> data);
22 virtual bool setData(QList<qreal> data) {return false;}
23 virtual bool setData(QList<qreal> x, QList<qreal> y) {return false;}
24
25
21 26 // Methods to find out minimum and maximum values of data
22 27 int min();
23 28 int max();
24 29 int count();
25 30 int valueAt(int i);
26 31
27 32 private:
28 33
29 34 QList<int> mData;
30 35 };
31 36
32 37 QTCOMMERCIALCHART_END_NAMESPACE
33 38
34 39 #endif // BARCHARTSERIES_H
@@ -1,41 +1,43
1 1 #ifndef QCHARTSERIES_H
2 2 #define QCHARTSERIES_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include <QObject>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 class QTCOMMERCIALCHART_EXPORT QChartSeries : public QObject
10 10 {
11
11 //TODO:
12 //Q_OBJECT
12 13 public:
13 14 enum QChartSeriesType {
14 15 SeriesTypeLine = 0,
15 16 // SeriesTypeArea,
16 17 SeriesTypeBar,
17 18 SeriesTypePie,
18 19 SeriesTypeScatter
19 20 // SeriesTypeSpline
20 21 };
21 22
22 23 protected:
23 24 QChartSeries(QObject *parent = 0):QObject(parent){};
24 25
25 26 public:
26 27 virtual ~QChartSeries(){};
27 28
28 29 // Factory method
29 static QChartSeries* create(QObject* parent = 0 ){ return 0;}
30 static QChartSeries* create(QChartSeriesType type, QObject* parent = 0 );
31
30 32 // Pure virtual
31 33 virtual QChartSeriesType type() const = 0;
32 34
33 35 virtual bool setData(QList<int> data) { return false; }
34 36 virtual bool setData(QList<qreal> data) { return false; }
35 37 virtual bool setData(QList<qreal> x, QList<qreal> y){ return false; }
36 38 };
37 39
38 40 QTCOMMERCIALCHART_END_NAMESPACE
39 41
40 42 #endif
41 43
@@ -1,89 +1,90
1 1 !include( ../common.pri ) {
2 2 error( Couldn't find the common.pri file! )
3 3 }
4 4
5 5 TARGET = QtCommercialChart
6 6 DESTDIR = $$CHART_BUILD_LIB_DIR
7 7 TEMPLATE = lib
8 8 QT += core \
9 9 gui
10 10
11 11 CONFIG += debug_and_release
12 12 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
13 13
14 14 SOURCES += \
15 15 barchart/barchartseries.cpp \
16 16 barchart/bargroup.cpp \
17 17 barchart/bar.cpp \
18 18 xylinechart/qxychartseries.cpp \
19 19 xylinechart/xylinechartitem.cpp \
20 20 xylinechart/xygrid.cpp \
21 21 xylinechart/xyplotdomain.cpp \
22 22 qscatterseries.cpp \
23 23 qpieseries.cpp \
24 24 qchart.cpp \
25 25 axis.cpp \
26 26 qchartwidget.cpp \
27 27 pieslice.cpp \
28 qchartview.cpp
28 qchartview.cpp \
29 qchartseries.cpp
29 30
30 31 PRIVATE_HEADERS += \
31 32 xylinechart/xylinechartitem_p.h \
32 33 xylinechart/xyplotdomain_p.h \
33 34 xylinechart/xygrid_p.h \
34 35 qscatterseries_p.h \
35 36 pieslice.h \
36 37 axis_p.h
37 38
38 39 PUBLIC_HEADERS += \
39 40 qchartseries.h \
40 41 qscatterseries.h \
41 42 qpieseries.h \
42 43 qchart.h \
43 44 qchartwidget.h \
44 45 qchartglobal.h \
45 46 xylinechart/qxychartseries.h \
46 47 barchart/barchartseries.h \
47 48 barchart/bargroup.h \
48 49 qchartview.h
49 50
50 51 HEADERS += $$PUBLIC_HEADERS
51 52 HEADERS += $$PRIVATE_HEADERS
52 53
53 54 INCLUDEPATH += xylinechart \
54 55 barchart \
55 56 .
56 57
57 58 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
58 59 MOC_DIR = $$CHART_BUILD_DIR/lib
59 60 UI_DIR = $$CHART_BUILD_DIR/lib
60 61 RCC_DIR = $$CHART_BUILD_DIR/lib
61 62
62 63
63 64 DEFINES += QTCOMMERCIALCHART_LIBRARY
64 65
65 66 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
66 67 public_headers.files = $$PUBLIC_HEADERS
67 68 target.path = $$[QT_INSTALL_LIBS]
68 69 INSTALLS += target \
69 70 public_headers
70 71
71 72
72 73 install_build_headers.name = bild_headers
73 74 install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h
74 75 install_build_headers.input = PUBLIC_HEADERS
75 76 install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR
76 77 install_build_headers.CONFIG += target_predeps no_link
77 78 QMAKE_EXTRA_COMPILERS += install_build_headers
78 79
79 80 chartversion.target = qchartversion_p.h
80 81 chartversion.commands = @echo "build_time" > $$chartversion.target;
81 82 chartversion.depends = $$HEADERS $$SOURCES
82 83 PRE_TARGETDEPS += qchartversion_p.h
83 84 QMAKE_CLEAN+= qchartversion_p.h
84 85 QMAKE_EXTRA_TARGETS += chartversion
85 86
86 87 unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
87 88 win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
88 89
89 90
@@ -1,40 +1,42
1 1 #ifndef QXYSERIES_H_
2 2 #define QXYSERIES_H_
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartseries.h"
6 6 #include <QDebug>
7 7 #include <QColor>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class QTCOMMERCIALCHART_EXPORT QXYChartSeries : public QChartSeries
12 12 {
13 //TODO:
14 // Q_OBJECT
13 15 private:
14 16 QXYChartSeries(QObject* parent=0);
15 17 public:
16 18 virtual ~QXYChartSeries();
17 19
18 20 //implemented from QChartSeries
19 21 static QXYChartSeries* create(QObject* parent=0);
20 22 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;}
21 23
22 24 void add(qreal x, qreal y);
23 25 void clear();
24 26 void setColor(const QColor& color);
25 27 const QColor& color() const { return m_color;}
26 28 int count() const;
27 29 qreal x(int pos) const;
28 30 qreal y(int pos) const;
29 31 friend QDebug operator<< (QDebug d, const QXYChartSeries series);
30 32
31 33 private:
32 34 QColor m_color;
33 35 QList<qreal> m_x;
34 36 QList<qreal> m_y;
35 37
36 38 };
37 39
38 40 QTCOMMERCIALCHART_END_NAMESPACE
39 41
40 42 #endif
@@ -1,267 +1,284
1 1 #include "mainwidget.h"
2 2 #include "dataseriedialog.h"
3 3 #include "qchartseries.h"
4 4 #include "qpieseries.h"
5 5 #include <qxychartseries.h>
6 #include <barchartseries.h>
6 7 #include <QPushButton>
7 8 #include <QComboBox>
8 9 #include <QSpinBox>
9 10 #include <QCheckBox>
10 11 #include <QGridLayout>
11 12 #include <QHBoxLayout>
12 13 #include <QLabel>
13 14 #include <QSpacerItem>
14 15 #include <QMessageBox>
15 16 #include <cmath>
16 17 #include <QDebug>
17 18
18 19 QTCOMMERCIALCHART_USE_NAMESPACE
19 20
20 21 MainWidget::MainWidget(QWidget *parent) :
21 22 QWidget(parent)
22 23 {
23 24 QPushButton *addSeriesButton = new QPushButton("Add series");
24 25 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
25 26
26 27 // Chart background
27 28 QComboBox *backgroundCombo = new QComboBox(this);
28 29 backgroundCombo->addItem("None");
29 30 backgroundCombo->addItem("TODO Grid");
30 31 backgroundCombo->addItem("TODO Image");
31 32 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
32 33 this, SLOT(backgroundChanged(int)));
33 34
34 35 // Axis
35 36 // TODO: multiple axes?
36 37 m_autoScaleCheck = new QCheckBox("Automatic scaling");
37 38 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
38 39 // Allow setting also non-sense values (like -2147483648 and 2147483647)
39 40 m_xMinSpin = new QSpinBox();
40 41 m_xMinSpin->setMinimum(INT_MIN);
41 42 m_xMinSpin->setMaximum(INT_MAX);
42 43 m_xMinSpin->setValue(0);
43 44 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
44 45 m_xMaxSpin = new QSpinBox();
45 46 m_xMaxSpin->setMinimum(INT_MIN);
46 47 m_xMaxSpin->setMaximum(INT_MAX);
47 48 m_xMaxSpin->setValue(10);
48 49 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
49 50 m_yMinSpin = new QSpinBox();
50 51 m_yMinSpin->setMinimum(INT_MIN);
51 52 m_yMinSpin->setMaximum(INT_MAX);
52 53 m_yMinSpin->setValue(0);
53 54 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
54 55 m_yMaxSpin = new QSpinBox();
55 56 m_yMaxSpin->setMinimum(INT_MIN);
56 57 m_yMaxSpin->setMaximum(INT_MAX);
57 58 m_yMaxSpin->setValue(10);
58 59 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
59 60
60 61 QGridLayout *grid = new QGridLayout();
61 62 QGridLayout *mainLayout = new QGridLayout();
62 63 //grid->addWidget(new QLabel("Add series:"), 0, 0);
63 64 grid->addWidget(addSeriesButton, 0, 1);
64 65 grid->addWidget(new QLabel("Background:"), 2, 0);
65 66 grid->addWidget(backgroundCombo, 2, 1);
66 67 grid->addWidget(m_autoScaleCheck, 3, 0);
67 68 grid->addWidget(new QLabel("x min:"), 4, 0);
68 69 grid->addWidget(m_xMinSpin, 4, 1);
69 70 grid->addWidget(new QLabel("x max:"), 5, 0);
70 71 grid->addWidget(m_xMaxSpin, 5, 1);
71 72 grid->addWidget(new QLabel("y min:"), 6, 0);
72 73 grid->addWidget(m_yMinSpin, 6, 1);
73 74 grid->addWidget(new QLabel("y max:"), 7, 0);
74 75 grid->addWidget(m_yMaxSpin, 7, 1);
75 76 // add row with empty label to make all the other rows static
76 77 grid->addWidget(new QLabel(""), 8, 0);
77 78 grid->setRowStretch(8, 1);
78 79
79 80 mainLayout->addLayout(grid, 0, 0);
80 81
81 82 // Scatter specific settings
82 83 m_scatterLayout = new QGridLayout();
83 84 m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
84 85 m_scatterLayout->setEnabled(false);
85 86
86 87 // Pie specific settings
87 88 m_pieLayout = new QGridLayout();
88 89 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
89 90 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
90 91 pieSizeSpin->setMinimum(LONG_MIN);
91 92 pieSizeSpin->setMaximum(LONG_MAX);
92 93 pieSizeSpin->setValue(1.0);
93 94 pieSizeSpin->setSingleStep(0.1);
94 95 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
95 96 m_pieLayout->setEnabled(false);
96 97 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
97 98
98 99 mainLayout->addLayout(m_scatterLayout, 1, 0);
99 100 mainLayout->addLayout(m_pieLayout, 2, 0);
100 101
101 102 m_chartWidget = new QChartWidget(this);
102 103 //m_chartWidget->setColor(Qt::red);
103 104 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
104 105 // hbox->setStretch(1, 1);
105 106
106 107 setLayout(mainLayout);
107 108
108 109 m_autoScaleCheck->setChecked(true);
109 110 testDataChanged(0);
110 111 }
111 112
112 113 void MainWidget::addSeries()
113 114 {
114 115 DataSerieDialog dialog(m_defaultSeriesName, this);
115 116 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
116 117 dialog.exec();
117 118 }
118 119
119 120 void MainWidget::addSeries(QString series, QString data)
120 121 {
121 122 qDebug() << "addSeries: " << series << " data: " << data;
122 123 m_defaultSeriesName = series;
123 124
124 125 // TODO: a dedicated data class for storing x and y values
125 126 QList<qreal> x;
126 127 QList<qreal> y;
127 128
128 129 if (data == "linear") {
129 130 for (int i = 0; i < 20; i++) {
130 131 x.append(i);
131 132 y.append(i);
132 133 }
133 134 } else if (data == "linear, 1M") {
134 135 for (int i = 0; i < 10000; i++) {
135 136 x.append(i);
136 137 y.append(20);
137 138 }
138 139 } else if (data == "SIN") {
139 140 for (int i = 0; i < 100; i++) {
140 141 x.append(i);
141 142 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
142 143 }
143 144 } else if (data == "SIN + random") {
144 145 for (qreal i = 0; i < 100; i += 0.1) {
145 146 x.append(i + (rand() % 5));
146 147 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
147 148 }
148 149 } else {
149 150 // TODO: check if data has a valid file name
150 151 Q_ASSERT(false);
151 152 }
152 153
153 154 // TODO: color of the series
154 155 QChartSeries *newSeries = 0;
155 156 if (series == "Scatter") {
156 157 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter);
157 158 Q_ASSERT(newSeries->setData(x, y));
158 159 } else if (series == "Pie") {
159 160 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
160 161 Q_ASSERT(newSeries->setData(y));
161 162 } else if (series == "Line") {
162 163 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
163 164 Q_ASSERT(newSeries->setData(x, y));
164 165 } else {
165 166 // TODO
166 167 }
167 168
169 // BarChart
170 if (series == "Bar") {
171 qDebug() << "Bar chart series";
172 QChartSeries* barSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this);
173 QList<int> barData;
174 barData << 1;
175 barData << 12;
176 barData << 5;
177 barData << 8;
178 barData << 17;
179 barData << 9;
180 barSeries->setData(barData);
181 m_chartWidget->addSeries(barSeries);
182
183 }
184
168 185 setCurrentSeries(newSeries);
169 186 }
170 187
171 188 void MainWidget::setCurrentSeries(QChartSeries *series)
172 189 {
173 190 m_currentSeries = series;
174 191 switch (m_currentSeries->type()) {
175 192 case QChartSeries::SeriesTypeLine:
176 193 break;
177 194 case QChartSeries::SeriesTypeScatter:
178 195 break;
179 196 case QChartSeries::SeriesTypePie:
180 197 break;
181 198 default:
182 199 Q_ASSERT(false);
183 200 break;
184 201 }
185 202 }
186 203
187 204 void MainWidget::testDataChanged(int itemIndex)
188 205 {
189 206 qDebug() << "testDataChanged: " << itemIndex;
190 207
191 208 // switch (itemIndex) {
192 209 // case 0: {
193 210 // QList<QChartDataPoint> data;
194 211 // for (int x = 0; x < 20; x++) {
195 212 // data.append(QChartDataPoint() << x << x / 2);
196 213 // }
197 214 // m_chartWidget->setData(data);
198 215 // break;
199 216 // }
200 217 // case 1: {
201 218 // QList<QChartDataPoint> data;
202 219 // for (int x = 0; x < 100; x++) {
203 220 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
204 221 // }
205 222 // m_chartWidget->setData(data);
206 223 // break;
207 224 // }
208 225 // case 2: {
209 226 // QList<QChartDataPoint> data;
210 227 // for (int x = 0; x < 1000; x++) {
211 228 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
212 229 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
213 230 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
214 231 // }
215 232 // m_chartWidget->setData(data);
216 233 // break;
217 234 // }
218 235 // default:
219 236 // break;
220 237 // }
221 238 }
222 239
223 240 void MainWidget::backgroundChanged(int itemIndex)
224 241 {
225 242 qDebug() << "backgroundChanged: " << itemIndex;
226 243 }
227 244
228 245 void MainWidget::autoScaleChanged(int value)
229 246 {
230 247 if (value) {
231 248 // TODO: enable auto scaling
232 249 } else {
233 250 // TODO: set scaling manually (and disable auto scaling)
234 251 }
235 252
236 253 m_xMinSpin->setEnabled(!value);
237 254 m_xMaxSpin->setEnabled(!value);
238 255 m_yMinSpin->setEnabled(!value);
239 256 m_yMaxSpin->setEnabled(!value);
240 257 }
241 258
242 259 void MainWidget::xMinChanged(int value)
243 260 {
244 261 qDebug() << "xMinChanged: " << value;
245 262 }
246 263
247 264 void MainWidget::xMaxChanged(int value)
248 265 {
249 266 qDebug() << "xMaxChanged: " << value;
250 267 }
251 268
252 269 void MainWidget::yMinChanged(int value)
253 270 {
254 271 qDebug() << "yMinChanged: " << value;
255 272 }
256 273
257 274 void MainWidget::yMaxChanged(int value)
258 275 {
259 276 qDebug() << "yMaxChanged: " << value;
260 277 }
261 278
262 279 void MainWidget::setPieSizeFactor(double size)
263 280 {
264 281 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
265 282 Q_ASSERT(pie);
266 283 pie->setSizeFactor(qreal(size));
267 284 }
General Comments 0
You need to be logged in to leave comments. Login now