##// END OF EJS Templates
chartwidgettest updated
Marek Rosa -
r1583:ccda934c8c27
parent child
Show More
@@ -1,111 +1,112
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "tablewidget.h"
22 22 #include "customtablemodel.h"
23 23 #include <QGridLayout>
24 24 #include <QTableView>
25 25 #include <QChart>
26 26 #include <QChartView>
27 27 #include <QLineSeries>
28 28 #include <QVXYModelMapper>
29 29 #include <QHeaderView>
30 30
31 31 QTCOMMERCIALCHART_USE_NAMESPACE
32 32
33 33 TableWidget::TableWidget(QWidget *parent)
34 34 : QWidget(parent)
35 35 {
36 36 // create simple model for storing data
37 37 // user's table data model
38 38 //! [1]
39 39 CustomTableModel *model = new CustomTableModel;
40 40 //! [1]
41 41
42 42 //! [2]
43 43 // create table view and add model to it
44 44 QTableView *tableView = new QTableView;
45 45 tableView->setModel(model);
46 46 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
47 47 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
48 48 //! [2]
49 49
50 50 //! [3]
51 51 QChart *chart = new QChart;
52 52 chart->setAnimationOptions(QChart::AllAnimations);
53 53 //! [3]
54 54
55 55 // series 1
56 56 //! [4]
57 57 QLineSeries *series = new QLineSeries;
58 58 series->setName("Line 1");
59 59 QVXYModelMapper *mapper = new QVXYModelMapper(this);
60 60 mapper->setXColumn(0);
61 61 mapper->setYColumn(1);
62 62 mapper->setSeries(series);
63 63 mapper->setModel(model);
64 64 chart->addSeries(series);
65 65 //! [4]
66 66
67 67 //! [5]
68 68 // for storing color hex from the series
69 69 QString seriesColorHex = "#000000";
70 70
71 71 // get the color of the series and use it for showing the mapped area
72 72 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
73 73 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
74 74 //! [5]
75 75
76 76
77 77 // series 2
78 78 //! [6]
79 79 series = new QLineSeries;
80 80 series->setName("Line 2");
81 81
82 82 mapper = new QVXYModelMapper(this);
83 83 mapper->setXColumn(2);
84 84 mapper->setYColumn(3);
85 85 mapper->setSeries(series);
86 86 mapper->setModel(model);
87 87 chart->addSeries(series);
88 88 //! [6]
89 89
90 90 //! [7]
91 91 // get the color of the series and use it for showing the mapped area
92 92 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
93 93 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
94 94 //! [7]
95 95
96 96 //! [8]
97 chart->createDefaultAxes();
97 98 QChartView *chartView = new QChartView(chart);
98 99 chartView->setRenderHint(QPainter::Antialiasing);
99 100 chartView->setMinimumSize(640, 480);
100 101 //! [8]
101 102
102 103 //! [9]
103 104 // create main layout
104 105 QGridLayout* mainLayout = new QGridLayout;
105 106 mainLayout->addWidget(tableView, 1, 0);
106 107 mainLayout->addWidget(chartView, 1, 1);
107 108 mainLayout->setColumnStretch(1, 1);
108 109 mainLayout->setColumnStretch(0, 0);
109 110 setLayout(mainLayout);
110 111 //! [9]
111 112 }
@@ -1,363 +1,364
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "mainwidget.h"
22 22 #include "dataseriedialog.h"
23 23 #include "qchartview.h"
24 24 #include "qpieseries.h"
25 25 #include "qscatterseries.h"
26 26 #include "qlineseries.h"
27 27 #include <qareaseries.h>
28 28 #include <qsplineseries.h>
29 29 #include <qbarset.h>
30 30 #include <qbarseries.h>
31 31 #include <qgroupedbarseries.h>
32 32 #include <qstackedbarseries.h>
33 33 #include <qpercentbarseries.h>
34 34 #include <QPushButton>
35 35 #include <QComboBox>
36 36 #include <QSpinBox>
37 37 #include <QCheckBox>
38 38 #include <QGridLayout>
39 39 #include <QHBoxLayout>
40 40 #include <QLabel>
41 41 #include <QSpacerItem>
42 42 #include <QMessageBox>
43 43 #include <cmath>
44 44 #include <QDebug>
45 45 #include <QStandardItemModel>
46 46 #include <QCategoriesAxis>
47 47
48 48
49 49 QTCOMMERCIALCHART_USE_NAMESPACE
50 50
51 51 MainWidget::MainWidget(QWidget *parent) :
52 52 QWidget(parent),
53 53 m_addSerieDialog(0),
54 54 m_chart(0)
55 55 {
56 56 m_chart = new QChart();
57 57
58 58 // Grid layout for the controls for configuring the chart widget
59 59 QGridLayout *grid = new QGridLayout();
60 60 QPushButton *addSeriesButton = new QPushButton("Add series");
61 61 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
62 62 grid->addWidget(addSeriesButton, 0, 1);
63 63 initBackroundCombo(grid);
64 64 initScaleControls(grid);
65 65 initThemeCombo(grid);
66 66 initCheckboxes(grid);
67 67
68 68 // add row with empty label to make all the other rows static
69 69 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
70 70 grid->setRowStretch(grid->rowCount() - 1, 1);
71 71
72 72 // Create chart view with the chart
73 73 m_chartView = new QChartView(m_chart, this);
74 74 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
75 75
76 76 // Another grid layout as a main layout
77 77 QGridLayout *mainLayout = new QGridLayout();
78 78 mainLayout->addLayout(grid, 0, 0);
79 79 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
80 80 setLayout(mainLayout);
81 81 }
82 82
83 83 // Combo box for selecting the chart's background
84 84 void MainWidget::initBackroundCombo(QGridLayout *grid)
85 85 {
86 86 QComboBox *backgroundCombo = new QComboBox(this);
87 87 backgroundCombo->addItem("Color");
88 88 backgroundCombo->addItem("Gradient");
89 89 backgroundCombo->addItem("Image");
90 90 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
91 91 this, SLOT(backgroundChanged(int)));
92 92
93 93 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
94 94 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
95 95 }
96 96
97 97 // Scale related controls (auto-scale vs. manual min-max values)
98 98 void MainWidget::initScaleControls(QGridLayout *grid)
99 99 {
100 100 m_autoScaleCheck = new QCheckBox("Automatic scaling");
101 101 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
102 102 // Allow setting also non-sense values (like -2147483648 and 2147483647)
103 103 m_xMinSpin = new QSpinBox();
104 104 m_xMinSpin->setMinimum(INT_MIN);
105 105 m_xMinSpin->setMaximum(INT_MAX);
106 106 m_xMinSpin->setValue(0);
107 107 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
108 108 m_xMaxSpin = new QSpinBox();
109 109 m_xMaxSpin->setMinimum(INT_MIN);
110 110 m_xMaxSpin->setMaximum(INT_MAX);
111 111 m_xMaxSpin->setValue(10);
112 112 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
113 113 m_yMinSpin = new QSpinBox();
114 114 m_yMinSpin->setMinimum(INT_MIN);
115 115 m_yMinSpin->setMaximum(INT_MAX);
116 116 m_yMinSpin->setValue(0);
117 117 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
118 118 m_yMaxSpin = new QSpinBox();
119 119 m_yMaxSpin->setMinimum(INT_MIN);
120 120 m_yMaxSpin->setMaximum(INT_MAX);
121 121 m_yMaxSpin->setValue(10);
122 122 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
123 123
124 124 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
125 125 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
126 126 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
127 127 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
128 128 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
129 129 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
130 130 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
131 131 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
132 132 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
133 133
134 134 m_autoScaleCheck->setChecked(true);
135 135 }
136 136
137 137 // Combo box for selecting theme
138 138 void MainWidget::initThemeCombo(QGridLayout *grid)
139 139 {
140 140 QComboBox *chartTheme = new QComboBox();
141 141 chartTheme->addItem("Default");
142 142 chartTheme->addItem("Light");
143 143 chartTheme->addItem("Blue Cerulean");
144 144 chartTheme->addItem("Dark");
145 145 chartTheme->addItem("Brown Sand");
146 146 chartTheme->addItem("Blue NCS");
147 147 chartTheme->addItem("High Contrast");
148 148 chartTheme->addItem("Blue Icy");
149 149 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
150 150 this, SLOT(changeChartTheme(int)));
151 151 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
152 152 grid->addWidget(chartTheme, 8, 1);
153 153 }
154 154
155 155 // Different check boxes for customizing chart
156 156 void MainWidget::initCheckboxes(QGridLayout *grid)
157 157 {
158 158 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
159 159 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
160 160 // connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
161 161 zoomCheckBox->setChecked(true);
162 162 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
163 163
164 164 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
165 165 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
166 166 aliasCheckBox->setChecked(false);
167 167 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
168 168 }
169 169
170 170 void MainWidget::antiAliasToggled(bool enabled)
171 171 {
172 172 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
173 173 }
174 174
175 175 void MainWidget::addSeries()
176 176 {
177 177 if (!m_addSerieDialog) {
178 178 m_addSerieDialog = new DataSerieDialog(this);
179 179 connect(m_addSerieDialog, SIGNAL(accepted(QString,int,int,QString,bool)),
180 180 this, SLOT(addSeries(QString,int,int,QString,bool)));
181 181 }
182 182 m_addSerieDialog->exec();
183 183 }
184 184
185 185 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
186 186 {
187 187 // TODO: dataCharacteristics
188 188 QList<RealList> testData;
189 189 for (int j(0); j < columnCount; j++) {
190 190 QList <qreal> newColumn;
191 191 for (int i(0); i < rowCount; i++) {
192 192 if (dataCharacteristics == "Sin") {
193 193 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
194 194 } else if (dataCharacteristics == "Sin + random") {
195 195 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
196 196 } else if (dataCharacteristics == "Random") {
197 197 newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX);
198 198 } else if (dataCharacteristics == "Linear") {
199 199 //newColumn.append(i * (j + 1.0));
200 200 // TODO: temporary hack to make pie work; prevent zero values:
201 201 newColumn.append(i * (j + 1.0) + 0.1);
202 202 } else { // "constant"
203 203 newColumn.append((j + 1.0));
204 204 }
205 205 }
206 206 testData.append(newColumn);
207 207 }
208 208 return testData;
209 209 }
210 210
211 211 QStringList MainWidget::generateLabels(int count)
212 212 {
213 213 QStringList result;
214 214 for (int i(0); i < count; i++)
215 215 result.append("label" + QString::number(i));
216 216 return result;
217 217 }
218 218
219 219 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
220 220 {
221 221 qDebug() << "addSeries: " << seriesName
222 222 << " columnCount: " << columnCount
223 223 << " rowCount: " << rowCount
224 224 << " dataCharacteristics: " << dataCharacteristics
225 225 << " labels enabled: " << labelsEnabled;
226 226 m_defaultSeriesName = seriesName;
227 227
228 228 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
229 229
230 230 // Line series and scatter series use similar data
231 231 if (seriesName == "Line") {
232 232 for (int j(0); j < data.count(); j ++) {
233 233 QList<qreal> column = data.at(j);
234 234 QLineSeries *series = new QLineSeries();
235 235 series->setName("line" + QString::number(j));
236 236 for (int i(0); i < column.count(); i++)
237 237 series->append(i, column.at(i));
238 238 m_chart->addSeries(series);
239 239 }
240 240 } else if (seriesName == "Area") {
241 241 // TODO: lower series for the area?
242 242 for (int j(0); j < data.count(); j ++) {
243 243 QList<qreal> column = data.at(j);
244 244 QLineSeries *lineSeries = new QLineSeries();
245 245 for (int i(0); i < column.count(); i++)
246 246 lineSeries->append(i, column.at(i));
247 247 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
248 248 areaSeries->setName("area" + QString::number(j));
249 249 m_chart->addSeries(areaSeries);
250 250 }
251 251 } else if (seriesName == "Scatter") {
252 252 for (int j(0); j < data.count(); j++) {
253 253 QList<qreal> column = data.at(j);
254 254 QScatterSeries *series = new QScatterSeries();
255 255 series->setName("scatter" + QString::number(j));
256 256 for (int i(0); i < column.count(); i++)
257 257 series->append(i, column.at(i));
258 258 m_chart->addSeries(series);
259 259 }
260 260 } else if (seriesName == "Pie") {
261 261 QStringList labels = generateLabels(rowCount);
262 262 for (int j(0); j < data.count(); j++) {
263 263 QPieSeries *series = new QPieSeries();
264 264 QList<qreal> column = data.at(j);
265 265 for (int i(0); i < column.count(); i++)
266 266 series->append(labels.at(i), column.at(i));
267 267 m_chart->addSeries(series);
268 268 }
269 269 } else if (seriesName == "Bar"
270 270 || seriesName == "Grouped bar"
271 271 || seriesName == "Stacked bar"
272 272 || seriesName == "Percent bar") {
273 273 QStringList category;
274 274 QStringList labels = generateLabels(rowCount);
275 275 foreach(QString label, labels)
276 276 category << label;
277 277 QBarSeries* series = 0;
278 278 if (seriesName == "Bar") {
279 279 series = new QBarSeries(this);
280 280 QCategoriesAxis* axis = new QCategoriesAxis();
281 281 axis->append(category);
282 282 m_chart->setAxisX(axis,series);
283 283 } else if (seriesName == "Grouped bar") {
284 284 series = new QGroupedBarSeries(this);
285 285 QCategoriesAxis* axis = new QCategoriesAxis();
286 286 axis->append(category);
287 287 m_chart->setAxisX(axis,series);
288 288 } else if (seriesName == "Stacked bar") {
289 289 series = new QStackedBarSeries(this);
290 290 QCategoriesAxis* axis = new QCategoriesAxis();
291 291 axis->append(category);
292 292 m_chart->setAxisX(axis,series);
293 293 } else {
294 294 series = new QPercentBarSeries(this);
295 295 QCategoriesAxis* axis = new QCategoriesAxis();
296 296 axis->append(category);
297 297 m_chart->setAxisX(axis,series);
298 298 }
299 299
300 300 for (int j(0); j < data.count(); j++) {
301 301 QList<qreal> column = data.at(j);
302 302 QBarSet *set = new QBarSet("set" + QString::number(j));
303 303 for (int i(0); i < column.count(); i++)
304 304 *set << column.at(i);
305 305 series->append(set);
306 306 }
307 307
308 308 m_chart->addSeries(series);
309 309 } else if (seriesName == "Spline") {
310 310 for (int j(0); j < data.count(); j ++) {
311 311 QList<qreal> column = data.at(j);
312 312 QSplineSeries *series = new QSplineSeries();
313 313 for (int i(0); i < column.count(); i++)
314 314 series->append(i, column.at(i));
315 315 m_chart->addSeries(series);
316 316 }
317 317 }
318 m_chart->createDefaultAxes();
318 319 }
319 320
320 321 void MainWidget::backgroundChanged(int itemIndex)
321 322 {
322 323 qDebug() << "backgroundChanged: " << itemIndex;
323 324 }
324 325
325 326 void MainWidget::autoScaleChanged(int value)
326 327 {
327 328 if (value) {
328 329 // TODO: enable auto scaling
329 330 } else {
330 331 // TODO: set scaling manually (and disable auto scaling)
331 332 }
332 333
333 334 m_xMinSpin->setEnabled(!value);
334 335 m_xMaxSpin->setEnabled(!value);
335 336 m_yMinSpin->setEnabled(!value);
336 337 m_yMaxSpin->setEnabled(!value);
337 338 }
338 339
339 340 void MainWidget::xMinChanged(int value)
340 341 {
341 342 qDebug() << "xMinChanged: " << value;
342 343 }
343 344
344 345 void MainWidget::xMaxChanged(int value)
345 346 {
346 347 qDebug() << "xMaxChanged: " << value;
347 348 }
348 349
349 350 void MainWidget::yMinChanged(int value)
350 351 {
351 352 qDebug() << "yMinChanged: " << value;
352 353 }
353 354
354 355 void MainWidget::yMaxChanged(int value)
355 356 {
356 357 qDebug() << "yMaxChanged: " << value;
357 358 }
358 359
359 360 void MainWidget::changeChartTheme(int themeIndex)
360 361 {
361 362 qDebug() << "changeChartTheme: " << themeIndex;
362 363 m_chart->setTheme((QChart::ChartTheme) themeIndex);
363 364 }
General Comments 0
You need to be logged in to leave comments. Login now