##// END OF EJS Templates
Line and area series in chartwidgettest
Tero Ahola -
r468:baa18f282b3d
parent child
Show More
@@ -1,357 +1,369
1 1 #include "mainwidget.h"
2 2 #include "dataseriedialog.h"
3 #include "qpieseries.h"
4 #include "qscatterseries.h"
3 #include <qpieseries.h>
4 #include <qscatterseries.h>
5 5 #include <qlineseries.h>
6 #include "qsplineseries.h"
6 #include <qareaseries.h>
7 #include <qsplineseries.h>
7 8 #include <qbarset.h>
8 9 #include <qbarseries.h>
9 10 #include <qstackedbarseries.h>
10 11 #include <qpercentbarseries.h>
11 12 #include <QPushButton>
12 13 #include <QComboBox>
13 14 #include <QSpinBox>
14 15 #include <QCheckBox>
15 16 #include <QGridLayout>
16 17 #include <QHBoxLayout>
17 18 #include <QLabel>
18 19 #include <QSpacerItem>
19 20 #include <QMessageBox>
20 21 #include <cmath>
21 22 #include <QDebug>
22 23 #include <QStandardItemModel>
23 24
24 25
25 26 QTCOMMERCIALCHART_USE_NAMESPACE
26 27
27 28 MainWidget::MainWidget(QWidget *parent) :
28 29 QWidget(parent),
29 30 m_addSerieDialog(0),
30 31 m_chartView(0)
31 32 {
32 33 m_chartView = new QChartView(this);
33 34 m_chartView->setRubberBandPolicy(QChartView::HorizonalRubberBand);
34 35
35 36 // Grid layout for the controls for configuring the chart widget
36 37 QGridLayout *grid = new QGridLayout();
37 38 QPushButton *addSeriesButton = new QPushButton("Add series");
38 39 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
39 40 grid->addWidget(addSeriesButton, 0, 1);
40 41 initBackroundCombo(grid);
41 42 initScaleControls(grid);
42 43 initThemeCombo(grid);
43 44 initCheckboxes(grid);
44 45
45 46 // add row with empty label to make all the other rows static
46 47 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
47 48 grid->setRowStretch(grid->rowCount() - 1, 1);
48 49
49 50 // Another grid layout as a main layout
50 51 QGridLayout *mainLayout = new QGridLayout();
51 52 mainLayout->addLayout(grid, 0, 0);
52 53
53 54 // Add layouts and the chart widget to the main layout
54 55 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
55 56 setLayout(mainLayout);
56 57 }
57 58
58 59 // Combo box for selecting the chart's background
59 60 void MainWidget::initBackroundCombo(QGridLayout *grid)
60 61 {
61 62 QComboBox *backgroundCombo = new QComboBox(this);
62 63 backgroundCombo->addItem("Color");
63 64 backgroundCombo->addItem("Gradient");
64 65 backgroundCombo->addItem("Image");
65 66 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
66 67 this, SLOT(backgroundChanged(int)));
67 68
68 69 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
69 70 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
70 71 }
71 72
72 73 // Scale related controls (auto-scale vs. manual min-max values)
73 74 void MainWidget::initScaleControls(QGridLayout *grid)
74 75 {
75 76 m_autoScaleCheck = new QCheckBox("Automatic scaling");
76 77 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
77 78 // Allow setting also non-sense values (like -2147483648 and 2147483647)
78 79 m_xMinSpin = new QSpinBox();
79 80 m_xMinSpin->setMinimum(INT_MIN);
80 81 m_xMinSpin->setMaximum(INT_MAX);
81 82 m_xMinSpin->setValue(0);
82 83 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
83 84 m_xMaxSpin = new QSpinBox();
84 85 m_xMaxSpin->setMinimum(INT_MIN);
85 86 m_xMaxSpin->setMaximum(INT_MAX);
86 87 m_xMaxSpin->setValue(10);
87 88 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
88 89 m_yMinSpin = new QSpinBox();
89 90 m_yMinSpin->setMinimum(INT_MIN);
90 91 m_yMinSpin->setMaximum(INT_MAX);
91 92 m_yMinSpin->setValue(0);
92 93 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
93 94 m_yMaxSpin = new QSpinBox();
94 95 m_yMaxSpin->setMinimum(INT_MIN);
95 96 m_yMaxSpin->setMaximum(INT_MAX);
96 97 m_yMaxSpin->setValue(10);
97 98 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
98 99
99 100 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
100 101 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
101 102 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
102 103 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
103 104 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
104 105 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
105 106 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
106 107 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
107 108 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
108 109
109 110 m_autoScaleCheck->setChecked(true);
110 111 }
111 112
112 113 // Combo box for selecting theme
113 114 void MainWidget::initThemeCombo(QGridLayout *grid)
114 115 {
115 116 QComboBox *chartTheme = new QComboBox();
116 117 chartTheme->addItem("Default");
117 118 chartTheme->addItem("Vanilla");
118 119 chartTheme->addItem("Icy");
119 120 chartTheme->addItem("Grayscale");
120 121 chartTheme->addItem("Scientific");
121 122 chartTheme->addItem("Unnamed1");
122 123 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
123 124 this, SLOT(changeChartTheme(int)));
124 125 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
125 126 grid->addWidget(chartTheme, 8, 1);
126 127 }
127 128
128 129 // Different check boxes for customizing chart
129 130 void MainWidget::initCheckboxes(QGridLayout *grid)
130 131 {
131 132 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
132 133 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
133 134 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
134 135 zoomCheckBox->setChecked(true);
135 136 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
136 137
137 138 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
138 139 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
139 140 aliasCheckBox->setChecked(false);
140 141 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
141 142 }
142 143
143 144 void MainWidget::antiAliasToggled(bool enabled)
144 145 {
145 146 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
146 147 }
147 148
148 149 void MainWidget::addSeries()
149 150 {
150 151 if (!m_addSerieDialog) {
151 152 m_addSerieDialog = new DataSerieDialog(this);
152 153 connect(m_addSerieDialog, SIGNAL(accepted(QString, int, int, QString, bool)),
153 154 this, SLOT(addSeries(QString, int, int, QString, bool)));
154 155 }
155 156 m_addSerieDialog->exec();
156 157 }
157 158
158 159 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
159 160 {
160 161 // TODO: dataCharacteristics
161 162 QList<RealList> testData;
162 163 for (int j(0); j < columnCount; j++) {
163 164 QList <qreal> newColumn;
164 165 for (int i(0); i < rowCount; i++) {
165 166 if (dataCharacteristics == "Sin") {
166 167 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
167 168 } else if (dataCharacteristics == "Sin + random") {
168 169 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
169 170 } else if (dataCharacteristics == "Random") {
170 171 newColumn.append(rand() % 5);
171 172 } else if (dataCharacteristics == "Linear") {
172 173 //newColumn.append(i * (j + 1.0));
173 174 // TODO: temporary hack to make pie work; prevent zero values:
174 175 newColumn.append(i * (j + 1.0) + 0.1);
175 176 } else { // "constant"
176 177 newColumn.append((j + 1.0));
177 178 }
178 179 }
179 180 testData.append(newColumn);
180 181 }
181 182 return testData;
182 183 }
183 184
184 185 QStringList MainWidget::generateLabels(int count)
185 186 {
186 187 QStringList result;
187 188 for (int i(0); i < count; i++)
188 189 result.append("label" + QString::number(i));
189 190 return result;
190 191 }
191 192
192 193 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
193 194 {
194 195 qDebug() << "addSeries: " << seriesName
195 196 << " columnCount: " << columnCount
196 197 << " rowCount: " << rowCount
197 198 << " dataCharacteristics: " << dataCharacteristics
198 199 << " labels enabled: " << labelsEnabled;
199 200 m_defaultSeriesName = seriesName;
200 201
201 202 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
202 203
203 204 // Line series and scatter series use similar data
204 if (seriesName.contains("qline", Qt::CaseInsensitive)) {
205 if (seriesName == "Line") {
205 206 for (int j(0); j < data.count(); j ++) {
206 207 QList<qreal> column = data.at(j);
207 208 QLineSeries *series = new QLineSeries();
208 209 for (int i(0); i < column.count(); i++) {
209 210 series->add(i, column.at(i));
210 211 }
211 212 m_chartView->addSeries(series);
212 213 setCurrentSeries(series);
213 214 }
214 } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) {
215 } if (seriesName == "Area") {
216 // TODO: lower series for the area?
217 for (int j(0); j < data.count(); j ++) {
218 QList<qreal> column = data.at(j);
219 QLineSeries *lineSeries = new QLineSeries();
220 for (int i(0); i < column.count(); i++) {
221 lineSeries->add(i, column.at(i));
222 }
223 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
224 m_chartView->addSeries(areaSeries);
225 setCurrentSeries(areaSeries);
226 }
227 } else if (seriesName == "Scatter") {
215 228 for (int j(0); j < data.count(); j++) {
216 229 QList<qreal> column = data.at(j);
217 230 QScatterSeries *series = new QScatterSeries();
218 231 for (int i(0); i < column.count(); i++) {
219 232 (*series) << QPointF(i, column.at(i));
220 233 }
221 234 m_chartView->addSeries(series);
222 235 setCurrentSeries(series);
223 236 }
224 } else if (seriesName.contains("pie", Qt::CaseInsensitive)) {
237 } else if (seriesName == "Pie") {
225 238 QStringList labels = generateLabels(rowCount);
226 239 for (int j(0); j < data.count(); j++) {
227 240 QPieSeries *series = new QPieSeries();
228 241 QList<qreal> column = data.at(j);
229 242 for (int i(0); i < column.count(); i++) {
230 243 series->add(column.at(i), labels.at(i));
231 244 }
232 245 m_chartView->addSeries(series);
233 246 setCurrentSeries(series);
234 247 }
235 248 } else if (seriesName == "Bar"
236 249 || seriesName == "Stacked bar"
237 250 || seriesName == "Percent bar") {
238 251 QStringList category;
239 252 QStringList labels = generateLabels(rowCount);
240 253 foreach(QString label, labels)
241 254 category << label;
242 255 QBarSeries* series = 0;
243 256 if (seriesName == "Bar")
244 257 series = new QBarSeries(category, this);
245 258 else if (seriesName == "Stacked bar")
246 259 series = new QStackedBarSeries(category, this);
247 260 else
248 261 series = new QPercentBarSeries(category, this);
249 262
250 263 for (int j(0); j < data.count(); j++) {
251 264 QList<qreal> column = data.at(j);
252 265 QBarSet *set = new QBarSet("set" + QString::number(j));
253 266 for (int i(0); i < column.count(); i++) {
254 267 *set << column.at(i);
255 268 }
256 269 series->addBarSet(set);
257 270 }
258 271
259 272 // TODO: new implementation of setFloatingValuesEnabled with signals
260 273 //series->setFloatingValuesEnabled(true);
261 274 series->setToolTipEnabled(true);
262 275 series->setSeparatorsEnabled(false);
263 276 m_chartView->addSeries(series);
264 277 setCurrentSeries(series);
265 }
266 else if (seriesName.contains("spline", Qt::CaseInsensitive)) {
278 } else if (seriesName == "Spline") {
267 279 for (int j(0); j < data.count(); j ++) {
268 280 QList<qreal> column = data.at(j);
269 281 QSplineSeries *series = new QSplineSeries();
270 282 for (int i(0); i < column.count(); i++) {
271 283 series->add(i, column.at(i));
272 284 }
273 285 m_chartView->addSeries(series);
274 286 setCurrentSeries(series);
275 287 }
276 288
277 289 // TODO: area
278 290 }
279 291 }
280 292
281 293 void MainWidget::setCurrentSeries(QSeries *series)
282 294 {
283 295 if (series) {
284 296 m_currentSeries = series;
285 297 switch (m_currentSeries->type()) {
286 298 case QSeries::SeriesTypeLine:
287 299 break;
288 300 case QSeries::SeriesTypeScatter:
289 301 break;
290 302 case QSeries::SeriesTypePie:
291 303 break;
292 304 case QSeries::SeriesTypeBar:
293 305 qDebug() << "setCurrentSeries (bar)";
294 306 break;
295 307 case QSeries::SeriesTypeStackedBar:
296 308 qDebug() << "setCurrentSeries (Stackedbar)";
297 309 break;
298 310 case QSeries::SeriesTypePercentBar:
299 311 qDebug() << "setCurrentSeries (Percentbar)";
300 312 break;
301 313 case QSeries::SeriesTypeSpline:
302 314 break;
303 315 default:
304 316 Q_ASSERT(false);
305 317 break;
306 318 }
307 319 }
308 320 }
309 321
310 322 void MainWidget::backgroundChanged(int itemIndex)
311 323 {
312 324 qDebug() << "backgroundChanged: " << itemIndex;
313 325 }
314 326
315 327 void MainWidget::autoScaleChanged(int value)
316 328 {
317 329 if (value) {
318 330 // TODO: enable auto scaling
319 331 } else {
320 332 // TODO: set scaling manually (and disable auto scaling)
321 333 }
322 334
323 335 m_xMinSpin->setEnabled(!value);
324 336 m_xMaxSpin->setEnabled(!value);
325 337 m_yMinSpin->setEnabled(!value);
326 338 m_yMaxSpin->setEnabled(!value);
327 339 }
328 340
329 341 void MainWidget::xMinChanged(int value)
330 342 {
331 343 qDebug() << "xMinChanged: " << value;
332 344 }
333 345
334 346 void MainWidget::xMaxChanged(int value)
335 347 {
336 348 qDebug() << "xMaxChanged: " << value;
337 349 }
338 350
339 351 void MainWidget::yMinChanged(int value)
340 352 {
341 353 qDebug() << "yMinChanged: " << value;
342 354 }
343 355
344 356 void MainWidget::yMaxChanged(int value)
345 357 {
346 358 qDebug() << "yMaxChanged: " << value;
347 359 }
348 360
349 361 void MainWidget::changeChartTheme(int themeIndex)
350 362 {
351 363 qDebug() << "changeChartTheme: " << themeIndex;
352 364 m_chartView->setChartTheme((QChart::ChartTheme) themeIndex);
353 365 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
354 366 QSize s = size();
355 367 s.setWidth(s.width()+1);
356 368 resize(s);
357 369 }
General Comments 0
You need to be logged in to leave comments. Login now