##// END OF EJS Templates
Adds charts to chartsviewer
Michal Klocek -
r1753:6c2ac002bf7e
parent child
Show More
@@ -0,0 +1,29
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 "charts.h"
22
23 Chart::Chart(){};
24 Chart::~Chart(){};
25
26 void Chart::initialize()
27 {
28 Charts::addChart(this);
29 }
@@ -0,0 +1,86
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
22 #ifndef CHARTS_H
23 #define CHARTS_H
24 #include "window.h"
25 #include <QList>
26 #include <QString>
27 #include <qchartglobal.h>
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QChart;
31 QTCOMMERCIALCHART_END_NAMESPACE
32
33 QTCOMMERCIALCHART_USE_NAMESPACE
34
35 class Chart
36 {
37 public:
38 Chart();
39
40 virtual ~Chart();
41
42 virtual void initialize();
43 virtual QChart* createChart(const DataTable& table) = 0;
44 virtual QString name() = 0;
45 virtual QString category() = 0;
46 virtual QString subCategory() = 0;
47
48 };
49
50 namespace Charts
51 {
52
53 typedef QList<Chart*> ChartList;
54
55 inline ChartList& chartList()
56 {
57 static ChartList list;
58 return list;
59 }
60
61 inline bool findChart(Chart* chart)
62 {
63 ChartList& list = chartList();
64 if (list.contains(chart)) {
65 return true;
66 }
67 foreach (Chart* item, list) {
68 if (item->name() == chart->name() && item->category() == chart->category() && item->subCategory() == chart->subCategory()) {
69 return true;
70 }
71 }
72 return false;
73 }
74
75 inline void addChart(Chart* chart)
76 {
77 ChartList& list = chartList();
78 if (!findChart(chart)) {
79 list.append(chart);
80 }
81 }
82 }
83
84 #define DECLARE_CHART(chartName) static chartName t;
85
86 #endif
@@ -0,0 +1,74
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 "charts.h"
22 #include "qchart.h"
23 #include "qareaseries.h"
24 #include "qlineseries.h"
25
26 class AreaChart: public Chart
27 {
28 public:
29
30 AreaChart(){
31 initialize();
32 }
33
34 QString name() { return QObject::tr("AreaChart"); }
35 QString category() { return QObject::tr("XYSeries"); }
36 QString subCategory() { return QString::null; }
37
38 QChart* createChart(const DataTable& table)
39 {
40
41 QChart *chart = new QChart();
42 chart->setTitle("Area chart");
43
44 // The lower series initialized to zero values
45 QLineSeries *lowerSeries = 0;
46 QString name("Series ");
47 int nameIndex = 0;
48 for (int i(0); i < table.count(); i++) {
49 QLineSeries *upperSeries = new QLineSeries(chart);
50 for (int j(0); j < table[i].count(); j++) {
51 Data data = table[i].at(j);
52 if (lowerSeries) {
53 const QList<QPointF>& points = lowerSeries->points();
54 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
55 }
56 else
57 upperSeries->append(QPointF(j, data.first.y()));
58 }
59 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
60 area->setName(name + QString::number(nameIndex));
61 nameIndex++;
62 chart->addSeries(area);
63 chart->createDefaultAxes();
64 lowerSeries = upperSeries;
65 }
66
67 return chart;
68
69 }
70
71 };
72
73 DECLARE_CHART(AreaChart)
74
@@ -0,0 +1,60
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 "charts.h"
22 #include "qchart.h"
23 #include "qstackedbarseries.h"
24 #include "qbarset.h"
25
26 class BarChart: public Chart
27 {
28 public:
29
30 BarChart(){
31 initialize();
32 }
33
34 QString name() { return QObject::tr("VerticalStackedBarChart"); }
35 QString category() { return QObject::tr("BarSeries"); }
36 QString subCategory() { return QObject::tr("Vertical"); }
37
38 QChart* createChart(const DataTable& table)
39 {
40
41 QChart* chart = new QChart();
42
43 chart->setTitle("Bar chart");
44
45 QStackedBarSeries* series = new QStackedBarSeries(chart);
46 for (int i(0); i < table.count(); i++) {
47 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
48 foreach (Data data, table[i])
49 *set << data.first.y();
50 series->append(set);
51 }
52 chart->addSeries(series);
53 chart->createDefaultAxes();
54 return chart;
55 }
56
57 };
58
59 DECLARE_CHART(BarChart)
60
@@ -0,0 +1,4
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
3
4 SOURCES+= linechart.cpp scatterchart.cpp splinechart.cpp piechart.cpp barchart.cpp areachart.cpp No newline at end of file
@@ -0,0 +1,61
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 "charts.h"
22 #include "qchart.h"
23 #include "qlineseries.h"
24
25 class LineChart: public Chart
26 {
27 public:
28
29 LineChart(){
30 initialize();
31 }
32
33 QString name() { return QObject::tr("LineChart"); }
34 QString category() { return QObject::tr("XYSeries"); }
35 QString subCategory() { return QString::null; }
36
37 QChart* createChart(const DataTable& table) {
38
39 QChart* chart = new QChart();
40 chart->setTitle("Line chart");
41
42 QString name("Series ");
43 int nameIndex = 0;
44 foreach (DataList list, table) {
45 QLineSeries *series = new QLineSeries(chart);
46 foreach (Data data, list)
47 series->append(data.first);
48 series->setName(name + QString::number(nameIndex));
49 nameIndex++;
50 chart->addSeries(series);
51 }
52
53 chart->createDefaultAxes();
54
55 return chart;
56 }
57
58 };
59
60 DECLARE_CHART(LineChart)
61
@@ -0,0 +1,64
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 "charts.h"
22 #include "qchart.h"
23 #include "qpieseries.h"
24
25 class PieChart: public Chart
26 {
27 public:
28
29 PieChart(){
30 initialize();
31 }
32
33 QString name() { return QObject::tr("PieChart"); }
34 QString category() { return QString::null; }
35 QString subCategory() { return QString::null; }
36
37 QChart* createChart(const DataTable& table)
38 {
39 QChart* chart = new QChart();
40 chart->setTitle("Pie chart");
41
42 qreal pieSize = 1.0 / table.count();
43 for (int i = 0; i < table.count(); i++) {
44 QPieSeries *series = new QPieSeries(chart);
45 foreach (Data data, table[i]) {
46 QPieSlice *slice = series->append(data.second, data.first.y());
47 if (data == table[i].first()) {
48 slice->setLabelVisible();
49 slice->setExploded();
50 }
51 }
52 qreal hPos = (pieSize / 2) + (i / (qreal) table.count());
53 series->setPieSize(pieSize);
54 series->setHorizontalPosition(hPos);
55 series->setVerticalPosition(0.5);
56 chart->addSeries(series);
57 }
58 return chart;
59 }
60
61 };
62
63 DECLARE_CHART(PieChart)
64
@@ -0,0 +1,59
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 "charts.h"
22 #include "qchart.h"
23 #include "qscatterseries.h"
24
25 class ScatterChart: public Chart
26 {
27 public:
28
29 ScatterChart(){
30 initialize();
31 }
32
33 QString name() { return QObject::tr("ScatterChart"); }
34 QString category() { return QObject::tr("XYSeries"); }
35 QString subCategory() { return QString::null; }
36
37 QChart* createChart(const DataTable& table)
38 {
39
40 QChart* chart = new QChart();
41 chart->setTitle("Scatter chart");
42 QString name("Series ");
43 int nameIndex = 0;
44 foreach (DataList list, table) {
45 QScatterSeries *series = new QScatterSeries(chart);
46 foreach (Data data, list)
47 series->append(data.first);
48 series->setName(name + QString::number(nameIndex));
49 nameIndex++;
50 chart->addSeries(series);
51 }
52 chart->createDefaultAxes();
53
54 return chart;
55 }
56
57 };
58
59 DECLARE_CHART(ScatterChart)
@@ -0,0 +1,60
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 "charts.h"
22 #include "qchart.h"
23 #include "qsplineseries.h"
24
25 class SplineChart: public Chart
26 {
27 public:
28
29 SplineChart(){
30 initialize();
31 }
32
33 QString name() { return QObject::tr("SplineChart"); }
34 QString category() { return QObject::tr("XYSeries"); }
35 QString subCategory() { return QString::null; }
36
37 QChart* createChart(const DataTable& table)
38 {
39
40 QChart* chart = new QChart();
41
42 chart->setTitle("Spline chart");
43 QString name("Series ");
44 int nameIndex = 0;
45 foreach (DataList list, table) {
46 QSplineSeries *series = new QSplineSeries(chart);
47 foreach (Data data, list)
48 series->append(data.first);
49 series->setName(name + QString::number(nameIndex));
50 nameIndex++;
51 chart->addSeries(series);
52 }
53 chart->createDefaultAxes();
54 return chart;
55 }
56
57 };
58
59 DECLARE_CHART(SplineChart)
60
@@ -1,5 +1,7
1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
2 include(charts/charts.pri)
2 TARGET = chartviewer
3 TARGET = chartviewer
3 QT += opengl
4 QT += opengl
4 SOURCES = main.cpp window.cpp view.cpp
5 SOURCES += main.cpp window.cpp view.cpp charts.cpp
5 HEADERS = window.h view.h No newline at end of file
6 HEADERS += window.h view.h charts.h
7
@@ -20,6 +20,7
20
20
21 #include "window.h"
21 #include "window.h"
22 #include "view.h"
22 #include "view.h"
23 #include "charts.h"
23
24
24 #include <QChartView>
25 #include <QChartView>
25 #include <QPieSeries>
26 #include <QPieSeries>
@@ -100,31 +101,16 Window::Window(QWidget* parent) :
100 widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
101 widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
101 }
102 }
102
103
103 QChart* chart;
104
104
105 chart = createAreaChart();
105 Charts::ChartList list = Charts::chartList();
106 baseLayout->addItem(chart, 0, 0);
107 m_chartList << chart;
108
106
109 chart = createBarChart(m_valueCount);
107 for(int i = 0 ; i < 6 ; ++i)
110 baseLayout->addItem(chart, 0, 1);
108 {
111 m_chartList << chart;
109 if(!(i<list.size()) || list.isEmpty()) break;
112
110 QChart* chart = list.at(i)->createChart(m_dataTable);
113 chart = createLineChart();
111 baseLayout->addItem(chart, i/3, i%3);
114 baseLayout->addItem(chart, 0, 2);
112 m_chartList << chart;
115 m_chartList << chart;
113 }
116
117 chart = createPieChart();
118 baseLayout->addItem(chart, 1, 0);
119 m_chartList << chart;
120
121 chart = createSplineChart();
122 baseLayout->addItem(chart, 1, 1);
123 m_chartList << chart;
124
125 chart = createScatterChart();
126 baseLayout->addItem(chart, 1, 2);
127 m_chartList << chart;
128
114
129 m_form = new QGraphicsWidget();
115 m_form = new QGraphicsWidget();
130 m_form->setLayout(baseLayout);
116 m_form->setLayout(baseLayout);
@@ -238,147 +224,6 QComboBox* Window::createLegendBox() const
238 return legendComboBox;
224 return legendComboBox;
239 }
225 }
240
226
241 QChart* Window::createAreaChart() const
242 {
243 QChart *chart = new QChart();
244 // chart->axisX()->setNiceNumbersEnabled(true);
245 // chart->axisY()->setNiceNumbersEnabled(true);
246 chart->setTitle("Area chart");
247
248 // The lower series initialized to zero values
249 QLineSeries *lowerSeries = 0;
250 QString name("Series ");
251 int nameIndex = 0;
252 for (int i(0); i < m_dataTable.count(); i++) {
253 QLineSeries *upperSeries = new QLineSeries(chart);
254 for (int j(0); j < m_dataTable[i].count(); j++) {
255 Data data = m_dataTable[i].at(j);
256 if (lowerSeries) {
257 const QList<QPointF>& points = lowerSeries->points();
258 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
259 }
260 else
261 upperSeries->append(QPointF(j, data.first.y()));
262 }
263 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
264 area->setName(name + QString::number(nameIndex));
265 nameIndex++;
266 chart->addSeries(area);
267 chart->createDefaultAxes();
268 lowerSeries = upperSeries;
269 }
270
271 return chart;
272 }
273
274 QChart* Window::createBarChart(int valueCount) const
275 {
276 Q_UNUSED(valueCount);
277 QChart* chart = new QChart();
278 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
279 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
280 chart->setTitle("Bar chart");
281
282 QStackedBarSeries* series = new QStackedBarSeries(chart);
283 for (int i(0); i < m_dataTable.count(); i++) {
284 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
285 foreach (Data data, m_dataTable[i])
286 *set << data.first.y();
287 series->append(set);
288 }
289 chart->addSeries(series);
290 chart->createDefaultAxes();
291
292 return chart;
293 }
294
295 QChart* Window::createLineChart() const
296 {
297 QChart* chart = new QChart();
298 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
299 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
300 chart->setTitle("Line chart");
301
302 QString name("Series ");
303 int nameIndex = 0;
304 foreach (DataList list, m_dataTable) {
305 QLineSeries *series = new QLineSeries(chart);
306 foreach (Data data, list)
307 series->append(data.first);
308 series->setName(name + QString::number(nameIndex));
309 nameIndex++;
310 chart->addSeries(series);
311 }
312 chart->createDefaultAxes();
313
314 return chart;
315 }
316
317 QChart* Window::createPieChart() const
318 {
319 QChart* chart = new QChart();
320 chart->setTitle("Pie chart");
321
322 qreal pieSize = 1.0 / m_dataTable.count();
323 for (int i = 0; i < m_dataTable.count(); i++) {
324 QPieSeries *series = new QPieSeries(chart);
325 foreach (Data data, m_dataTable[i]) {
326 QPieSlice *slice = series->append(data.second, data.first.y());
327 if (data == m_dataTable[i].first()) {
328 slice->setLabelVisible();
329 slice->setExploded();
330 }
331 }
332 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
333 series->setPieSize(pieSize);
334 series->setHorizontalPosition(hPos);
335 series->setVerticalPosition(0.5);
336 chart->addSeries(series);
337 }
338
339 return chart;
340 }
341
342 QChart* Window::createSplineChart() const
343 {
344 QChart* chart = new QChart();
345 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
346 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
347 chart->setTitle("Spline chart");
348 QString name("Series ");
349 int nameIndex = 0;
350 foreach (DataList list, m_dataTable) {
351 QSplineSeries *series = new QSplineSeries(chart);
352 foreach (Data data, list)
353 series->append(data.first);
354 series->setName(name + QString::number(nameIndex));
355 nameIndex++;
356 chart->addSeries(series);
357 }
358 chart->createDefaultAxes();
359 return chart;
360 }
361
362 QChart* Window::createScatterChart() const
363 {
364 QChart* chart = new QChart();
365 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
366 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
367 chart->setTitle("Scatter chart");
368 QString name("Series ");
369 int nameIndex = 0;
370 foreach (DataList list, m_dataTable) {
371 QScatterSeries *series = new QScatterSeries(chart);
372 foreach (Data data, list)
373 series->append(data.first);
374 series->setName(name + QString::number(nameIndex));
375 nameIndex++;
376 chart->addSeries(series);
377 }
378 chart->createDefaultAxes();
379 return chart;
380 }
381
382 void Window::updateUI()
227 void Window::updateUI()
383 {
228 {
384 bool opengl = m_openGLCheckBox->isChecked();
229 bool opengl = m_openGLCheckBox->isChecked();
@@ -437,7 +282,7 void Window::updateUI()
437
282
438 QChart::AnimationOptions options(
283 QChart::AnimationOptions options(
439 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
284 m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
440 if (m_chartList.at(0)->animationOptions() != options) {
285 if (!m_chartList.isEmpty() && m_chartList.at(0)->animationOptions() != options) {
441 foreach (QChart *chart, m_chartList)
286 foreach (QChart *chart, m_chartList)
442 chart->setAnimationOptions(options);
287 chart->setAnimationOptions(options);
443 }
288 }
@@ -58,12 +58,6 private:
58 QComboBox* createAnimationBox() const;
58 QComboBox* createAnimationBox() const;
59 QComboBox* createLegendBox() const;
59 QComboBox* createLegendBox() const;
60 void connectSignals();
60 void connectSignals();
61 QChart* createAreaChart() const;
62 QChart* createBarChart(int valueCount) const;
63 QChart* createPieChart() const;
64 QChart* createLineChart() const;
65 QChart* createSplineChart() const;
66 QChart* createScatterChart() const;
67 void createProxyWidgets();
61 void createProxyWidgets();
68
62
69 protected:
63 protected:
General Comments 0
You need to be logged in to leave comments. Login now