##// END OF EJS Templates
Adds templete to chartviewer to simplyfy chart type creation
Michal Klocek -
r1839:16961447933b
parent child
Show More
@@ -1,86 +1,93
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
22 22 #ifndef CHARTS_H
23 23 #define CHARTS_H
24 24 #include "model.h"
25 25 #include <QList>
26 26 #include <QString>
27 27 #include <qchartglobal.h>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 class QChart;
31 31 QTCOMMERCIALCHART_END_NAMESPACE
32 32
33 33 QTCOMMERCIALCHART_USE_NAMESPACE
34 34
35 35 class Chart
36 36 {
37 37 public:
38 Chart();
39
40 virtual ~Chart();
41
42 virtual void initialize();
38 virtual ~Chart(){};
43 39 virtual QChart* createChart(const DataTable& table) = 0;
44 40 virtual QString name() = 0;
45 41 virtual QString category() = 0;
46 42 virtual QString subCategory() = 0;
47 43
48 44 };
49 45
50 46 namespace Charts
51 47 {
52 48
53 49 typedef QList<Chart*> ChartList;
54 50
55 51 inline ChartList& chartList()
56 52 {
57 53 static ChartList list;
58 54 return list;
59 55 }
60 56
61 57 inline bool findChart(Chart* chart)
62 58 {
63 59 ChartList& list = chartList();
64 60 if (list.contains(chart)) {
65 61 return true;
66 62 }
67 63 foreach (Chart* item, list) {
68 64 if (item->name() == chart->name() && item->category() == chart->category() && item->subCategory() == chart->subCategory()) {
69 65 return true;
70 66 }
71 67 }
72 68 return false;
73 69 }
74 70
75 71 inline void addChart(Chart* chart)
76 72 {
77 73 ChartList& list = chartList();
78 74 if (!findChart(chart)) {
79 75 list.append(chart);
80 76 }
81 77 }
82 78 }
83 79
84 #define DECLARE_CHART(chartName) static chartName t;
80 template <class T>
81 class ChartWrapper
82 {
83 public:
84 QSharedPointer<T> chart;
85 ChartWrapper() : chart(new T)
86 {
87 Charts::addChart(chart.data());
88 }
89 };
90
91 #define DECLARE_CHART(chartName) static ChartWrapper<chartName> t;
85 92
86 93 #endif
@@ -1,74 +1,69
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 "charts.h"
22 22 #include "qchart.h"
23 23 #include "qareaseries.h"
24 24 #include "qlineseries.h"
25 25
26 26 class AreaChart: public Chart
27 27 {
28 28 public:
29
30 AreaChart(){
31 initialize();
32 }
33
34 29 QString name() { return QObject::tr("AreaChart"); }
35 30 QString category() { return QObject::tr("XYSeries"); }
36 31 QString subCategory() { return QString::null; }
37 32
38 33 QChart* createChart(const DataTable& table)
39 34 {
40 35
41 36 QChart *chart = new QChart();
42 37 chart->setTitle("Area chart");
43 38
44 39 // The lower series initialized to zero values
45 40 QLineSeries *lowerSeries = 0;
46 41 QString name("Series ");
47 42 int nameIndex = 0;
48 43 for (int i(0); i < table.count(); i++) {
49 44 QLineSeries *upperSeries = new QLineSeries(chart);
50 45 for (int j(0); j < table[i].count(); j++) {
51 46 Data data = table[i].at(j);
52 47 if (lowerSeries) {
53 48 const QList<QPointF>& points = lowerSeries->points();
54 49 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
55 50 }
56 51 else
57 52 upperSeries->append(QPointF(j, data.first.y()));
58 53 }
59 54 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
60 55 area->setName(name + QString::number(nameIndex));
61 56 nameIndex++;
62 57 chart->addSeries(area);
63 58 chart->createDefaultAxes();
64 59 lowerSeries = upperSeries;
65 60 }
66 61
67 62 return chart;
68 63
69 64 }
70 65
71 66 };
72 67
73 68 DECLARE_CHART(AreaChart)
74 69
@@ -1,60 +1,55
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 "charts.h"
22 22 #include "qchart.h"
23 23 #include "qhorizontalbarseries.h"
24 24 #include "qbarset.h"
25 25
26 26 class HorizontalBarChart: public Chart
27 27 {
28 28 public:
29
30 HorizontalBarChart(){
31 initialize();
32 }
33
34 29 QString name() { return QObject::tr("HorizontalBarChart"); }
35 30 QString category() { return QObject::tr("BarSeries"); }
36 31 QString subCategory() { return QObject::tr("Vertical"); }
37 32
38 33 QChart* createChart(const DataTable& table)
39 34 {
40 35
41 36 QChart* chart = new QChart();
42 37
43 38 chart->setTitle("Horizontal bar chart");
44 39
45 40 QHorizontalBarSeries* series = new QHorizontalBarSeries(chart);
46 41 for (int i(0); i < table.count(); i++) {
47 42 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
48 43 foreach (Data data, table[i])
49 44 *set << data.first.y();
50 45 series->append(set);
51 46 }
52 47 chart->addSeries(series);
53 48 chart->createDefaultAxes();
54 49 return chart;
55 50 }
56 51
57 52 };
58 53
59 54 DECLARE_CHART(HorizontalBarChart)
60 55
@@ -1,60 +1,55
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 "charts.h"
22 22 #include "qchart.h"
23 23 #include "qhorizontalstackedbarseries.h"
24 24 #include "qbarset.h"
25 25
26 26 class HorizontalStackedBarChart: public Chart
27 27 {
28 28 public:
29
30 HorizontalStackedBarChart(){
31 initialize();
32 }
33
34 29 QString name() { return QObject::tr("HorizontalStackedBarChart"); }
35 30 QString category() { return QObject::tr("BarSeries"); }
36 31 QString subCategory() { return QObject::tr("Vertical"); }
37 32
38 33 QChart* createChart(const DataTable& table)
39 34 {
40 35
41 36 QChart* chart = new QChart();
42 37
43 38 chart->setTitle("Horizontal stacked chart");
44 39
45 40 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries(chart);
46 41 for (int i(0); i < table.count(); i++) {
47 42 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
48 43 foreach (Data data, table[i])
49 44 *set << data.first.y();
50 45 series->append(set);
51 46 }
52 47 chart->addSeries(series);
53 48 chart->createDefaultAxes();
54 49 return chart;
55 50 }
56 51
57 52 };
58 53
59 54 DECLARE_CHART(HorizontalStackedBarChart)
60 55
@@ -1,61 +1,56
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 "charts.h"
22 22 #include "qchart.h"
23 23 #include "qlineseries.h"
24 24
25 25 class LineChart: public Chart
26 26 {
27 27 public:
28
29 LineChart(){
30 initialize();
31 }
32
33 28 QString name() { return QObject::tr("LineChart"); }
34 29 QString category() { return QObject::tr("XYSeries"); }
35 30 QString subCategory() { return QString::null; }
36 31
37 32 QChart* createChart(const DataTable& table) {
38 33
39 34 QChart* chart = new QChart();
40 35 chart->setTitle("Line chart");
41 36
42 37 QString name("Series ");
43 38 int nameIndex = 0;
44 39 foreach (DataList list, table) {
45 40 QLineSeries *series = new QLineSeries(chart);
46 41 foreach (Data data, list)
47 42 series->append(data.first);
48 43 series->setName(name + QString::number(nameIndex));
49 44 nameIndex++;
50 45 chart->addSeries(series);
51 46 }
52 47
53 48 chart->createDefaultAxes();
54 49
55 50 return chart;
56 51 }
57 52
58 53 };
59 54
60 55 DECLARE_CHART(LineChart)
61 56
@@ -1,64 +1,59
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 "charts.h"
22 22 #include "qchart.h"
23 23 #include "qpieseries.h"
24 24
25 25 class PieChart: public Chart
26 26 {
27 27 public:
28
29 PieChart(){
30 initialize();
31 }
32
33 28 QString name() { return QObject::tr("PieChart"); }
34 29 QString category() { return QString::null; }
35 30 QString subCategory() { return QString::null; }
36 31
37 32 QChart* createChart(const DataTable& table)
38 33 {
39 34 QChart* chart = new QChart();
40 35 chart->setTitle("Pie chart");
41 36
42 37 qreal pieSize = 1.0 / table.count();
43 38 for (int i = 0; i < table.count(); i++) {
44 39 QPieSeries *series = new QPieSeries(chart);
45 40 foreach (Data data, table[i]) {
46 41 QPieSlice *slice = series->append(data.second, data.first.y());
47 42 if (data == table[i].first()) {
48 43 slice->setLabelVisible();
49 44 slice->setExploded();
50 45 }
51 46 }
52 47 qreal hPos = (pieSize / 2) + (i / (qreal) table.count());
53 48 series->setPieSize(pieSize);
54 49 series->setHorizontalPosition(hPos);
55 50 series->setVerticalPosition(0.5);
56 51 chart->addSeries(series);
57 52 }
58 53 return chart;
59 54 }
60 55
61 56 };
62 57
63 58 DECLARE_CHART(PieChart)
64 59
@@ -1,59 +1,54
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 "charts.h"
22 22 #include "qchart.h"
23 23 #include "qscatterseries.h"
24 24
25 25 class ScatterChart: public Chart
26 26 {
27 27 public:
28
29 ScatterChart(){
30 initialize();
31 }
32
33 28 QString name() { return QObject::tr("ScatterChart"); }
34 29 QString category() { return QObject::tr("XYSeries"); }
35 30 QString subCategory() { return QString::null; }
36 31
37 32 QChart* createChart(const DataTable& table)
38 33 {
39 34
40 35 QChart* chart = new QChart();
41 36 chart->setTitle("Scatter chart");
42 37 QString name("Series ");
43 38 int nameIndex = 0;
44 39 foreach (DataList list, table) {
45 40 QScatterSeries *series = new QScatterSeries(chart);
46 41 foreach (Data data, list)
47 42 series->append(data.first);
48 43 series->setName(name + QString::number(nameIndex));
49 44 nameIndex++;
50 45 chart->addSeries(series);
51 46 }
52 47 chart->createDefaultAxes();
53 48
54 49 return chart;
55 50 }
56 51
57 52 };
58 53
59 54 DECLARE_CHART(ScatterChart)
@@ -1,60 +1,55
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 "charts.h"
22 22 #include "qchart.h"
23 23 #include "qsplineseries.h"
24 24
25 25 class SplineChart: public Chart
26 26 {
27 27 public:
28
29 SplineChart(){
30 initialize();
31 }
32
33 28 QString name() { return QObject::tr("SplineChart"); }
34 29 QString category() { return QObject::tr("XYSeries"); }
35 30 QString subCategory() { return QString::null; }
36 31
37 32 QChart* createChart(const DataTable& table)
38 33 {
39 34
40 35 QChart* chart = new QChart();
41 36
42 37 chart->setTitle("Spline chart");
43 38 QString name("Series ");
44 39 int nameIndex = 0;
45 40 foreach (DataList list, table) {
46 41 QSplineSeries *series = new QSplineSeries(chart);
47 42 foreach (Data data, list)
48 43 series->append(data.first);
49 44 series->setName(name + QString::number(nameIndex));
50 45 nameIndex++;
51 46 chart->addSeries(series);
52 47 }
53 48 chart->createDefaultAxes();
54 49 return chart;
55 50 }
56 51
57 52 };
58 53
59 54 DECLARE_CHART(SplineChart)
60 55
@@ -1,60 +1,55
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 "charts.h"
22 22 #include "qchart.h"
23 23 #include "qbarseries.h"
24 24 #include "qbarset.h"
25 25
26 26 class VerticalBarChart: public Chart
27 27 {
28 28 public:
29
30 VerticalBarChart(){
31 initialize();
32 }
33
34 29 QString name() { return QObject::tr("VerticalBarChart"); }
35 30 QString category() { return QObject::tr("BarSeries"); }
36 31 QString subCategory() { return QObject::tr("Vertical"); }
37 32
38 33 QChart* createChart(const DataTable& table)
39 34 {
40 35
41 36 QChart* chart = new QChart();
42 37
43 38 chart->setTitle("Vertical bar chart");
44 39
45 40 QBarSeries* series = new QBarSeries(chart);
46 41 for (int i(0); i < table.count(); i++) {
47 42 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
48 43 foreach (Data data, table[i])
49 44 *set << data.first.y();
50 45 series->append(set);
51 46 }
52 47 chart->addSeries(series);
53 48 chart->createDefaultAxes();
54 49 return chart;
55 50 }
56 51
57 52 };
58 53
59 54 DECLARE_CHART(VerticalBarChart)
60 55
@@ -1,60 +1,55
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 "charts.h"
22 22 #include "qchart.h"
23 23 #include "qstackedbarseries.h"
24 24 #include "qbarset.h"
25 25
26 26 class VerticalStackedBarChart: public Chart
27 27 {
28 28 public:
29
30 VerticalStackedBarChart(){
31 initialize();
32 }
33
34 29 QString name() { return QObject::tr("VerticalStackedBarChart"); }
35 30 QString category() { return QObject::tr("BarSeries"); }
36 31 QString subCategory() { return QObject::tr("Vertical"); }
37 32
38 33 QChart* createChart(const DataTable& table)
39 34 {
40 35
41 36 QChart* chart = new QChart();
42 37
43 38 chart->setTitle("Stacked bar chart");
44 39
45 40 QStackedBarSeries* series = new QStackedBarSeries(chart);
46 41 for (int i(0); i < table.count(); i++) {
47 42 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
48 43 foreach (Data data, table[i])
49 44 *set << data.first.y();
50 45 series->append(set);
51 46 }
52 47 chart->addSeries(series);
53 48 chart->createDefaultAxes();
54 49 return chart;
55 50 }
56 51
57 52 };
58 53
59 54 DECLARE_CHART(VerticalStackedBarChart)
60 55
@@ -1,8 +1,8
1 1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
2 2 include(charts/charts.pri)
3 3 TARGET = chartviewer
4 4 QT += opengl
5 5 INCLUDEPATH += .
6 SOURCES += main.cpp window.cpp view.cpp charts.cpp
6 SOURCES += main.cpp window.cpp view.cpp
7 7 HEADERS += window.h view.h charts.h model.h
8 8
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now