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