##// END OF EJS Templates
Adds chartviewer demo
Michal Klocek -
r1746:7ec01b86b1ec
parent child
Show More
@@ -0,0 +1,5
1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
2 TARGET = chartviewer
3 QT += opengl
4 SOURCES = main.cpp chartwindow.cpp
5 HEADERS = chartwindow.h
@@ -0,0 +1,438
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "chartwindow.h"
22
23 #include <QChartView>
24 #include <QPieSeries>
25 #include <QPieSlice>
26 #include <QPercentBarSeries>
27 #include <QStackedBarSeries>
28 #include <QBarSeries>
29 #include <QBarSet>
30 #include <QLineSeries>
31 #include <QSplineSeries>
32 #include <QScatterSeries>
33 #include <QAreaSeries>
34 #include <QLegend>
35 #include <QGridLayout>
36 #include <QFormLayout>
37 #include <QComboBox>
38 #include <QSpinBox>
39 #include <QCheckBox>
40 #include <QGroupBox>
41 #include <QLabel>
42 #include <QTime>
43 #include <QBarCategoriesAxis>
44 #include <QGraphicsScene>
45 #include <QGraphicsGridLayout>
46 #include <QGraphicsLinearLayout>
47 #include <QGraphicsProxyWidget>
48 #include <QGLWidget>
49 #include <QApplication>
50 #include <QDebug>
51
52 ChartWindow::ChartWindow(QWidget* parent) :
53 QMainWindow(parent), m_listCount(3), m_valueMax(10), m_valueCount(7), m_scene(
54 new QGraphicsScene(this)), m_view(0), m_dataTable(
55 generateRandomData(m_listCount, m_valueMax, m_valueCount)), m_form(0), m_themeComboBox(0), m_antialiasCheckBox(
56 0), m_animatedComboBox(0), m_legendComboBox(0), m_openGLCheckBox(0)
57 {
58 createProxyWidgets();
59 connectSignals();
60
61 // create layout
62 QGraphicsGridLayout* baseLayout = new QGraphicsGridLayout();
63 QGraphicsLinearLayout *settingsLayout = new QGraphicsLinearLayout();
64 settingsLayout->setOrientation(Qt::Vertical);
65 settingsLayout->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
66 settingsLayout->addItem(m_widgetHash["openGLCheckBox"]);
67 settingsLayout->addItem(m_widgetHash["antialiasCheckBox"]);
68 settingsLayout->addItem(m_widgetHash["themeLabel"]);
69 settingsLayout->addItem(m_widgetHash["themeComboBox"]);
70 settingsLayout->addItem(m_widgetHash["animationsLabel"]);
71 settingsLayout->addItem(m_widgetHash["animatedComboBox"]);
72 settingsLayout->addItem(m_widgetHash["legendLabel"]);
73 settingsLayout->addItem(m_widgetHash["legendComboBox"]);
74 settingsLayout->addStretch();
75 baseLayout->addItem(settingsLayout, 0, 3, 2, 1);
76 //create charts
77
78 int i = m_widgetHash.count();
79 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
80 widget->setZValue(i--);
81 widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
82 }
83
84 QChart* chart;
85
86 chart = createAreaChart();
87 baseLayout->addItem(chart, 0, 0);
88 m_chartList << chart;
89
90 chart = createBarChart(m_valueCount);
91 baseLayout->addItem(chart, 0, 1);
92 m_chartList << chart;
93
94 chart = createLineChart();
95 baseLayout->addItem(chart, 0, 2);
96 m_chartList << chart;
97
98 chart = createPieChart();
99 baseLayout->addItem(chart, 1, 0);
100 m_chartList << chart;
101
102 chart = createSplineChart();
103 baseLayout->addItem(chart, 1, 1);
104 m_chartList << chart;
105
106 chart = createScatterChart();
107 baseLayout->addItem(chart, 1, 2);
108 m_chartList << chart;
109
110 // Set defaults
111 //m_antialiasCheckBox->setChecked(true);
112
113 m_form = new QGraphicsWidget();
114 m_form->setLayout(baseLayout);
115 m_scene->addItem(m_form);
116
117 m_view = new GraphicsView(m_scene, m_form);
118 m_view->setMinimumSize(m_form->preferredSize().toSize());
119
120 updateUI();
121 setCentralWidget(m_view);
122 }
123
124 ChartWindow::~ChartWindow()
125 {
126 }
127
128 void ChartWindow::connectSignals()
129 {
130 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
131 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
132 connect(m_openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
133 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
134 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
135 }
136
137 DataTable ChartWindow::generateRandomData(int listCount, int valueMax, int valueCount) const
138 {
139 DataTable dataTable;
140
141 // set seed for random stuff
142 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
143
144 // generate random data
145 for (int i(0); i < listCount; i++) {
146 DataList dataList;
147 qreal yValue(0);
148 for (int j(0); j < valueCount; j++) {
149 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
150 QPointF value(
151 (j + (qreal) qrand() / (qreal) RAND_MAX)
152 * ((qreal) m_valueMax / (qreal) valueCount), yValue);
153 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
154 dataList << Data(value, label);
155 }
156 dataTable << dataList;
157 }
158
159 return dataTable;
160 }
161
162 void ChartWindow::createProxyWidgets()
163 {
164 m_themeComboBox = createThemeBox();
165 m_antialiasCheckBox = new QCheckBox(tr("Anti-aliasing"));
166 m_animatedComboBox = createAnimationBox();
167 m_legendComboBox = createLegendBox();
168 m_openGLCheckBox = new QCheckBox(tr("OpenGL"));
169 m_widgetHash["themeComboBox"] = m_scene->addWidget(m_themeComboBox);
170 m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(m_antialiasCheckBox);
171 m_widgetHash["animatedComboBox"] = m_scene->addWidget(m_animatedComboBox);
172 m_widgetHash["legendComboBox"] = m_scene->addWidget(m_legendComboBox);
173 m_widgetHash["openGLCheckBox"] = m_scene->addWidget(m_openGLCheckBox);
174 m_widgetHash["themeLabel"] = m_scene->addWidget(new QLabel("Theme"));
175 m_widgetHash["animationsLabel"] = m_scene->addWidget(new QLabel("Animations"));
176 m_widgetHash["legendLabel"] = m_scene->addWidget(new QLabel("Legend"));
177 }
178
179 QComboBox* ChartWindow::createThemeBox() const
180 {
181 // settings layoutQGLWidget’
182 QComboBox* themeComboBox = new QComboBox();
183 themeComboBox->addItem("Light", QChart::ChartThemeLight);
184 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
185 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
186 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
187 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
188 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
189 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
190 return themeComboBox;
191 }
192
193 QComboBox* ChartWindow::createAnimationBox() const
194 {
195 // settings layout
196 QComboBox* animationComboBox = new QComboBox();
197 animationComboBox->addItem("No Animations", QChart::NoAnimation);
198 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
199 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
200 animationComboBox->addItem("All Animations", QChart::AllAnimations);
201 return animationComboBox;
202 }
203
204 QComboBox* ChartWindow::createLegendBox() const
205 {
206 QComboBox* legendComboBox = new QComboBox();
207 legendComboBox->addItem("No Legend ", 0);
208 legendComboBox->addItem("Legend Top", Qt::AlignTop);
209 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
210 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
211 legendComboBox->addItem("Legend Right", Qt::AlignRight);
212 return legendComboBox;
213 }
214
215 QChart* ChartWindow::createAreaChart() const
216 {
217 QChart *chart = new QChart();
218 // chart->axisX()->setNiceNumbersEnabled(true);
219 // chart->axisY()->setNiceNumbersEnabled(true);
220 chart->setTitle("Area chart");
221
222 // The lower series initialized to zero values
223 QLineSeries *lowerSeries = 0;
224 QString name("Series ");
225 int nameIndex = 0;
226 for (int i(0); i < m_dataTable.count(); i++) {
227 QLineSeries *upperSeries = new QLineSeries(chart);
228 for (int j(0); j < m_dataTable[i].count(); j++) {
229 Data data = m_dataTable[i].at(j);
230 if (lowerSeries) {
231 const QList<QPointF>& points = lowerSeries->points();
232 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
233 }
234 else
235 upperSeries->append(QPointF(j, data.first.y()));
236 }
237 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
238 area->setName(name + QString::number(nameIndex));
239 nameIndex++;
240 chart->addSeries(area);
241 chart->createDefaultAxes();
242 lowerSeries = upperSeries;
243 }
244
245 return chart;
246 }
247
248 QChart* ChartWindow::createBarChart(int valueCount) const
249 {
250 Q_UNUSED(valueCount);
251 QChart* chart = new QChart();
252 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
253 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
254 chart->setTitle("Bar chart");
255
256 QStackedBarSeries* series = new QStackedBarSeries(chart);
257 for (int i(0); i < m_dataTable.count(); i++) {
258 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
259 foreach (Data data, m_dataTable[i])
260 *set << data.first.y();
261 series->append(set);
262 }
263 chart->addSeries(series);
264 chart->createDefaultAxes();
265
266 return chart;
267 }
268
269 QChart* ChartWindow::createLineChart() const
270 {
271 QChart* chart = new QChart();
272 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
273 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
274 chart->setTitle("Line chart");
275
276 QString name("Series ");
277 int nameIndex = 0;
278 foreach (DataList list, m_dataTable) {
279 QLineSeries *series = new QLineSeries(chart);
280 foreach (Data data, list)
281 series->append(data.first);
282 series->setName(name + QString::number(nameIndex));
283 nameIndex++;
284 chart->addSeries(series);
285 }
286 chart->createDefaultAxes();
287
288 return chart;
289 }
290
291 QChart* ChartWindow::createPieChart() const
292 {
293 QChart* chart = new QChart();
294 chart->setTitle("Pie chart");
295
296 qreal pieSize = 1.0 / m_dataTable.count();
297 for (int i = 0; i < m_dataTable.count(); i++) {
298 QPieSeries *series = new QPieSeries(chart);
299 foreach (Data data, m_dataTable[i]) {
300 QPieSlice *slice = series->append(data.second, data.first.y());
301 if (data == m_dataTable[i].first()) {
302 slice->setLabelVisible();
303 slice->setExploded();
304 }
305 }
306 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
307 series->setPieSize(pieSize);
308 series->setHorizontalPosition(hPos);
309 series->setVerticalPosition(0.5);
310 chart->addSeries(series);
311 }
312
313 return chart;
314 }
315
316 QChart* ChartWindow::createSplineChart() const
317 {
318 QChart* chart = new QChart();
319 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
320 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
321 chart->setTitle("Spline chart");
322 QString name("Series ");
323 int nameIndex = 0;
324 foreach (DataList list, m_dataTable) {
325 QSplineSeries *series = new QSplineSeries(chart);
326 foreach (Data data, list)
327 series->append(data.first);
328 series->setName(name + QString::number(nameIndex));
329 nameIndex++;
330 chart->addSeries(series);
331 }
332 chart->createDefaultAxes();
333 return chart;
334 }
335
336 QChart* ChartWindow::createScatterChart() const
337 {
338 QChart* chart = new QChart();
339 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
340 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
341 chart->setTitle("Scatter chart");
342 QString name("Series ");
343 int nameIndex = 0;
344 foreach (DataList list, m_dataTable) {
345 QScatterSeries *series = new QScatterSeries(chart);
346 foreach (Data data, list)
347 series->append(data.first);
348 series->setName(name + QString::number(nameIndex));
349 nameIndex++;
350 chart->addSeries(series);
351 }
352 chart->createDefaultAxes();
353 return chart;
354 }
355
356 void ChartWindow::updateUI()
357 {
358 bool opengl = m_openGLCheckBox->isChecked();
359 bool isOpengl = qobject_cast<QGLWidget*>(m_view->viewport());
360 if ((isOpengl && !opengl) || (!isOpengl && opengl)) {
361 m_view->deleteLater();
362 m_view = new GraphicsView(m_scene, m_form);
363 m_view->setViewport(!opengl ? new QWidget() : new QGLWidget());
364 setCentralWidget(m_view);
365 }
366
367 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(
368 m_themeComboBox->currentIndex()).toInt();
369
370 foreach (QChart *chart, m_chartList)
371 chart->setTheme(theme);
372
373 QPalette pal = window()->palette();
374 if (theme == QChart::ChartThemeLight) {
375 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
376 pal.setColor(QPalette::WindowText, QRgb(0x404044));
377 }
378 else if (theme == QChart::ChartThemeDark) {
379 pal.setColor(QPalette::Window, QRgb(0x121218));
380 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
381 }
382 else if (theme == QChart::ChartThemeBlueCerulean) {
383 pal.setColor(QPalette::Window, QRgb(0x40434a));
384 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
385 }
386 else if (theme == QChart::ChartThemeBrownSand) {
387 pal.setColor(QPalette::Window, QRgb(0x9e8965));
388 pal.setColor(QPalette::WindowText, QRgb(0x404044));
389 }
390 else if (theme == QChart::ChartThemeBlueNcs) {
391 pal.setColor(QPalette::Window, QRgb(0x018bba));
392 pal.setColor(QPalette::WindowText, QRgb(0x404044));
393 }
394 else if (theme == QChart::ChartThemeHighContrast) {
395 pal.setColor(QPalette::Window, QRgb(0xffab03));
396 pal.setColor(QPalette::WindowText, QRgb(0x181818));
397 }
398 else if (theme == QChart::ChartThemeBlueIcy) {
399 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
400 pal.setColor(QPalette::WindowText, QRgb(0x404044));
401 }
402 else {
403 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
404 pal.setColor(QPalette::WindowText, QRgb(0x404044));
405 }
406 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
407 widget->setPalette(pal);
408 }
409 m_view->setBackgroundBrush(pal.color((QPalette::Window)));
410
411 QChart::AnimationOptions options(
412 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
413 if (m_chartList.at(0)->animationOptions() != options) {
414 foreach (QChart *chart, m_chartList)
415 chart->setAnimationOptions(options);
416 }
417
418 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
419
420 if (!alignment) {
421 foreach (QChart *chart, m_chartList) {
422 chart->legend()->hide();
423 }
424 }
425 else {
426 foreach (QChart *chart, m_chartList) {
427 chart->legend()->setAlignment(alignment);
428 chart->legend()->show();
429 }
430 }
431
432 bool checked = m_antialiasCheckBox->isChecked();
433 if (opengl)
434 m_view->setRenderHint(QPainter::HighQualityAntialiasing, checked);
435 else
436 m_view->setRenderHint(QPainter::Antialiasing, checked);
437 }
438
@@ -0,0 +1,110
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef CHARTWINDOW_H_
22 #define CHARTWINDOW_H_
23 #include <QMainWindow>
24 #include <QChartGlobal>
25 #include <QHash>
26 #include <QGraphicsView>
27 #include <QResizeEvent>
28 #include <QGraphicsWidget>
29
30 class QComboBox;
31 class QCheckBox;
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 class QChart;
35 QTCOMMERCIALCHART_END_NAMESPACE
36
37 typedef QPair<QPointF, QString> Data;
38 typedef QList<Data> DataList;
39 typedef QList<DataList> DataTable;
40
41 class QGraphicsScene;
42
43 QTCOMMERCIALCHART_USE_NAMESPACE
44
45
46 class GraphicsView: public QGraphicsView
47 {
48 public:
49 GraphicsView(QGraphicsScene *scene, QGraphicsWidget *form , QWidget *parent = 0):QGraphicsView(scene,parent), m_form(form)
50 {
51 setWindowTitle(tr("Charts"));
52 }
53
54 protected:
55 void resizeEvent(QResizeEvent *event)
56 {
57 if (scene())
58 scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
59 if (m_form)
60 m_form->resize(QSizeF(event->size()));
61 QGraphicsView::resizeEvent(event);
62 }
63 private:
64 QGraphicsWidget *m_form;
65 };
66
67
68 class ChartWindow: public QMainWindow
69 {
70 Q_OBJECT
71 public:
72 explicit ChartWindow(QWidget *parent = 0);
73 ~ChartWindow();
74
75 private Q_SLOTS:
76 void updateUI();
77
78 private:
79 DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
80 QComboBox* createThemeBox() const;
81 QComboBox* createAnimationBox() const;
82 QComboBox* createLegendBox() const;
83 void connectSignals();
84 QChart* createAreaChart() const;
85 QChart* createBarChart(int valueCount) const;
86 QChart* createPieChart() const;
87 QChart* createLineChart() const;
88 QChart* createSplineChart() const;
89 QChart* createScatterChart() const;
90 void createProxyWidgets();
91
92 private:
93 int m_listCount;
94 int m_valueMax;
95 int m_valueCount;
96 QGraphicsScene* m_scene;
97 GraphicsView* m_view;
98 QHash<QString, QGraphicsProxyWidget*> m_widgetHash;
99 QList<QChart*> m_chartList;
100 DataTable m_dataTable;
101
102 QGraphicsWidget *m_form;
103 QComboBox *m_themeComboBox;
104 QCheckBox *m_antialiasCheckBox;
105 QComboBox *m_animatedComboBox;
106 QComboBox *m_legendComboBox;
107 QCheckBox *m_openGLCheckBox;
108 };
109
110 #endif
@@ -0,0 +1,32
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "chartwindow.h"
22 #include <QApplication>
23 #include <QMainWindow>
24
25 int main(int argc, char *argv[])
26 {
27 QApplication a(argc, argv);
28 ChartWindow window;
29 window.show();
30 return a.exec();
31 }
32
@@ -11,4 +11,5 SUBDIRS += chartthemes \
11 qmlweather \
11 qmlweather \
12 qmlf1legends \
12 qmlf1legends \
13 qmlcustomizations \
13 qmlcustomizations \
14 qmlcustommodel
14 qmlcustommodel \
15 chartviewer No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now