##// 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 1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
2 include(charts/charts.pri)
2 3 TARGET = chartviewer
3 4 QT += opengl
4 SOURCES = main.cpp window.cpp view.cpp
5 HEADERS = window.h view.h No newline at end of file
5 SOURCES += main.cpp window.cpp view.cpp charts.cpp
6 HEADERS += window.h view.h charts.h
7
@@ -1,585 +1,430
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 "window.h"
22 22 #include "view.h"
23 #include "charts.h"
23 24
24 25 #include <QChartView>
25 26 #include <QPieSeries>
26 27 #include <QPieSlice>
27 28 #include <QPercentBarSeries>
28 29 #include <QStackedBarSeries>
29 30 #include <QBarSeries>
30 31 #include <QBarSet>
31 32 #include <QLineSeries>
32 33 #include <QSplineSeries>
33 34 #include <QScatterSeries>
34 35 #include <QAreaSeries>
35 36 #include <QLegend>
36 37 #include <QGridLayout>
37 38 #include <QFormLayout>
38 39 #include <QComboBox>
39 40 #include <QSpinBox>
40 41 #include <QCheckBox>
41 42 #include <QGroupBox>
42 43 #include <QLabel>
43 44 #include <QTime>
44 45 #include <QBarCategoriesAxis>
45 46 #include <QGraphicsScene>
46 47 #include <QGraphicsGridLayout>
47 48 #include <QGraphicsLinearLayout>
48 49 #include <QGraphicsProxyWidget>
49 50 #include <QGLWidget>
50 51 #include <QApplication>
51 52 #include <QDebug>
52 53
53 54 Window::Window(QWidget* parent) :
54 55 QMainWindow(parent),
55 56 m_listCount(3),
56 57 m_valueMax(10),
57 58 m_valueCount(7),
58 59 m_scene(new QGraphicsScene(this)),
59 60 m_view(0),
60 61 m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
61 62 m_form(0),
62 63 m_themeComboBox(0),
63 64 m_antialiasCheckBox(0),
64 65 m_animatedComboBox(0),
65 66 m_legendComboBox(0),
66 67 m_openGLCheckBox(0),
67 68 m_zoomCheckBox(0),
68 69 m_scrollCheckBox(0),
69 70 m_rubberBand(new QGraphicsRectItem()),
70 71 m_isScrolling(false),
71 72 m_isZooming(false),
72 73 m_scroll(false),
73 74 m_zoom(false)
74 75 {
75 76 createProxyWidgets();
76 77 connectSignals();
77 78
78 79 // create layout
79 80 QGraphicsGridLayout* baseLayout = new QGraphicsGridLayout();
80 81 QGraphicsLinearLayout *settingsLayout = new QGraphicsLinearLayout();
81 82 settingsLayout->setOrientation(Qt::Vertical);
82 83 settingsLayout->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
83 84 settingsLayout->addItem(m_widgetHash["openGLCheckBox"]);
84 85 settingsLayout->addItem(m_widgetHash["antialiasCheckBox"]);
85 86 settingsLayout->addItem(m_widgetHash["themeLabel"]);
86 87 settingsLayout->addItem(m_widgetHash["themeComboBox"]);
87 88 settingsLayout->addItem(m_widgetHash["animationsLabel"]);
88 89 settingsLayout->addItem(m_widgetHash["animatedComboBox"]);
89 90 settingsLayout->addItem(m_widgetHash["legendLabel"]);
90 91 settingsLayout->addItem(m_widgetHash["legendComboBox"]);
91 92 settingsLayout->addItem(m_widgetHash["scrollCheckBox"]);
92 93 settingsLayout->addItem(m_widgetHash["zoomCheckBox"]);
93 94 settingsLayout->addStretch();
94 95 baseLayout->addItem(settingsLayout, 0, 3, 2, 1);
95 96 //create charts
96 97
97 98 int i = m_widgetHash.count();
98 99 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
99 100 widget->setZValue(i--);
100 101 widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
101 102 }
102 103
103 QChart* chart;
104 104
105 chart = createAreaChart();
106 baseLayout->addItem(chart, 0, 0);
107 m_chartList << chart;
105 Charts::ChartList list = Charts::chartList();
108 106
109 chart = createBarChart(m_valueCount);
110 baseLayout->addItem(chart, 0, 1);
111 m_chartList << chart;
112
113 chart = createLineChart();
114 baseLayout->addItem(chart, 0, 2);
115 m_chartList << chart;
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;
107 for(int i = 0 ; i < 6 ; ++i)
108 {
109 if(!(i<list.size()) || list.isEmpty()) break;
110 QChart* chart = list.at(i)->createChart(m_dataTable);
111 baseLayout->addItem(chart, i/3, i%3);
112 m_chartList << chart;
113 }
128 114
129 115 m_form = new QGraphicsWidget();
130 116 m_form->setLayout(baseLayout);
131 117 m_scene->addItem(m_form);
132 118 m_scene->addItem(m_rubberBand);
133 119 m_rubberBand->setVisible(false);
134 120
135 121 m_view = new View(m_scene, m_form);
136 122 m_view->setMinimumSize(m_form->preferredSize().toSize());
137 123
138 124 // Set defaults
139 125 m_antialiasCheckBox->setChecked(true);
140 126 updateUI();
141 127 setCentralWidget(m_view);
142 128 }
143 129
144 130 Window::~Window()
145 131 {
146 132 }
147 133
148 134 void Window::connectSignals()
149 135 {
150 136 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
151 137 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
152 138 connect(m_openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
153 139 connect(m_zoomCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
154 140 connect(m_scrollCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
155 141 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
156 142 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
157 143 }
158 144
159 145 DataTable Window::generateRandomData(int listCount, int valueMax, int valueCount) const
160 146 {
161 147 DataTable dataTable;
162 148
163 149 // set seed for random stuff
164 150 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
165 151
166 152 // generate random data
167 153 for (int i(0); i < listCount; i++) {
168 154 DataList dataList;
169 155 qreal yValue(0);
170 156 for (int j(0); j < valueCount; j++) {
171 157 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
172 158 QPointF value(
173 159 (j + (qreal) qrand() / (qreal) RAND_MAX)
174 160 * ((qreal) m_valueMax / (qreal) valueCount), yValue);
175 161 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
176 162 dataList << Data(value, label);
177 163 }
178 164 dataTable << dataList;
179 165 }
180 166
181 167 return dataTable;
182 168 }
183 169
184 170 void Window::createProxyWidgets()
185 171 {
186 172 m_themeComboBox = createThemeBox();
187 173 m_antialiasCheckBox = new QCheckBox(tr("Anti-aliasing"));
188 174 m_animatedComboBox = createAnimationBox();
189 175 m_legendComboBox = createLegendBox();
190 176 m_openGLCheckBox = new QCheckBox(tr("OpenGL"));
191 177 m_zoomCheckBox = new QCheckBox(tr("Zoom"));
192 178 m_scrollCheckBox = new QCheckBox(tr("Scroll"));
193 179 m_widgetHash["themeComboBox"] = m_scene->addWidget(m_themeComboBox);
194 180 m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(m_antialiasCheckBox);
195 181 m_widgetHash["animatedComboBox"] = m_scene->addWidget(m_animatedComboBox);
196 182 m_widgetHash["legendComboBox"] = m_scene->addWidget(m_legendComboBox);
197 183 m_widgetHash["openGLCheckBox"] = m_scene->addWidget(m_openGLCheckBox);
198 184 m_widgetHash["themeLabel"] = m_scene->addWidget(new QLabel("Theme"));
199 185 m_widgetHash["animationsLabel"] = m_scene->addWidget(new QLabel("Animations"));
200 186 m_widgetHash["legendLabel"] = m_scene->addWidget(new QLabel("Legend"));
201 187 m_widgetHash["zoomCheckBox"] = m_scene->addWidget(m_zoomCheckBox);
202 188 m_widgetHash["scrollCheckBox"] = m_scene->addWidget(m_scrollCheckBox);
203 189 }
204 190
205 191 QComboBox* Window::createThemeBox() const
206 192 {
207 193 // settings layoutQGLWidget’
208 194 QComboBox* themeComboBox = new QComboBox();
209 195 themeComboBox->addItem("Light", QChart::ChartThemeLight);
210 196 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
211 197 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
212 198 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
213 199 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
214 200 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
215 201 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
216 202 return themeComboBox;
217 203 }
218 204
219 205 QComboBox* Window::createAnimationBox() const
220 206 {
221 207 // settings layout
222 208 QComboBox* animationComboBox = new QComboBox();
223 209 animationComboBox->addItem("No Animations", QChart::NoAnimation);
224 210 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
225 211 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
226 212 animationComboBox->addItem("All Animations", QChart::AllAnimations);
227 213 return animationComboBox;
228 214 }
229 215
230 216 QComboBox* Window::createLegendBox() const
231 217 {
232 218 QComboBox* legendComboBox = new QComboBox();
233 219 legendComboBox->addItem("No Legend ", 0);
234 220 legendComboBox->addItem("Legend Top", Qt::AlignTop);
235 221 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
236 222 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
237 223 legendComboBox->addItem("Legend Right", Qt::AlignRight);
238 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 227 void Window::updateUI()
383 228 {
384 229 bool opengl = m_openGLCheckBox->isChecked();
385 230 bool isOpengl = qobject_cast<QGLWidget*>(m_view->viewport());
386 231 if ((isOpengl && !opengl) || (!isOpengl && opengl)) {
387 232 m_view->deleteLater();
388 233 m_view = new View(m_scene, m_form);
389 234 m_view->setViewport(!opengl ? new QWidget() : new QGLWidget());
390 235 setCentralWidget(m_view);
391 236 }
392 237
393 238 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(
394 239 m_themeComboBox->currentIndex()).toInt();
395 240
396 241 foreach (QChart *chart, m_chartList)
397 242 chart->setTheme(theme);
398 243
399 244 QPalette pal = window()->palette();
400 245 if (theme == QChart::ChartThemeLight) {
401 246 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
402 247 pal.setColor(QPalette::WindowText, QRgb(0x404044));
403 248 }
404 249 else if (theme == QChart::ChartThemeDark) {
405 250 pal.setColor(QPalette::Window, QRgb(0x121218));
406 251 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
407 252 }
408 253 else if (theme == QChart::ChartThemeBlueCerulean) {
409 254 pal.setColor(QPalette::Window, QRgb(0x40434a));
410 255 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
411 256 }
412 257 else if (theme == QChart::ChartThemeBrownSand) {
413 258 pal.setColor(QPalette::Window, QRgb(0x9e8965));
414 259 pal.setColor(QPalette::WindowText, QRgb(0x404044));
415 260 }
416 261 else if (theme == QChart::ChartThemeBlueNcs) {
417 262 pal.setColor(QPalette::Window, QRgb(0x018bba));
418 263 pal.setColor(QPalette::WindowText, QRgb(0x404044));
419 264 }
420 265 else if (theme == QChart::ChartThemeHighContrast) {
421 266 pal.setColor(QPalette::Window, QRgb(0xffab03));
422 267 pal.setColor(QPalette::WindowText, QRgb(0x181818));
423 268 }
424 269 else if (theme == QChart::ChartThemeBlueIcy) {
425 270 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
426 271 pal.setColor(QPalette::WindowText, QRgb(0x404044));
427 272 }
428 273 else {
429 274 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
430 275 pal.setColor(QPalette::WindowText, QRgb(0x404044));
431 276 }
432 277 foreach(QGraphicsProxyWidget* widget , m_widgetHash) {
433 278 widget->setPalette(pal);
434 279 }
435 280 m_view->setBackgroundBrush(pal.color((QPalette::Window)));
436 281 m_rubberBand->setPen(pal.color((QPalette::WindowText)));
437 282
438 283 QChart::AnimationOptions options(
439 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 286 foreach (QChart *chart, m_chartList)
442 287 chart->setAnimationOptions(options);
443 288 }
444 289
445 290 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
446 291
447 292 if (!alignment) {
448 293 foreach (QChart *chart, m_chartList) {
449 294 chart->legend()->hide();
450 295 }
451 296 }
452 297 else {
453 298 foreach (QChart *chart, m_chartList) {
454 299 chart->legend()->setAlignment(alignment);
455 300 chart->legend()->show();
456 301 }
457 302 }
458 303
459 304 bool antialias = m_antialiasCheckBox->isChecked();
460 305
461 306 if (opengl)
462 307 m_view->setRenderHint(QPainter::HighQualityAntialiasing, antialias);
463 308 else
464 309 m_view->setRenderHint(QPainter::Antialiasing, antialias);
465 310
466 311 bool scroll = m_scrollCheckBox->isChecked();
467 312
468 313 if(!m_scroll & scroll){
469 314 m_scroll=true;
470 315 m_zoom=false;
471 316 m_zoomCheckBox->setChecked(false);
472 317 }
473 318
474 319 bool zoom = m_zoomCheckBox->isChecked();
475 320
476 321 if(!m_zoom & zoom){
477 322 m_scroll=false;
478 323 m_zoom=true;
479 324 m_scrollCheckBox->setChecked(false);
480 325 }
481 326
482 327 }
483 328
484 329 void Window::mousePressEvent(QMouseEvent *event)
485 330 {
486 331 if (event->button() == Qt::LeftButton) {
487 332
488 333 m_origin = event->pos();
489 334 m_isScrolling = false;
490 335 m_isZooming = false;
491 336
492 337 foreach (QChart *chart, m_chartList) {
493 338
494 339 QRectF geometryRect = chart->geometry();
495 340 QRectF plotArea = chart->plotArea();
496 341 plotArea.translate(geometryRect.topLeft());
497 342 if (plotArea.contains(m_origin)) {
498 343 m_isScrolling = m_scroll;
499 344 m_isZooming = m_zoom;
500 345 break;
501 346 }
502 347 }
503 348
504 349 if (m_isZooming) {
505 350 m_rubberBand->setRect(QRectF(m_origin, QSize()));
506 351 m_rubberBand->setVisible(true);
507 352 }
508 353 event->accept();
509 354 }
510 355
511 356 if (event->button() == Qt::RightButton) {
512 357 m_origin = event->pos();
513 358 m_isZooming = m_zoom;
514 359 }
515 360 }
516 361
517 362 void Window::mouseMoveEvent(QMouseEvent *event)
518 363 {
519 364 if (m_isScrolling || m_isZooming) {
520 365
521 366 foreach (QChart *chart, m_chartList) {
522 367
523 368 QRectF geometryRect = chart->geometry();
524 369 QRectF plotArea = chart->plotArea();
525 370 plotArea.translate(geometryRect.topLeft());
526 371
527 372 if (plotArea.contains(m_origin)) {
528 373 if (m_isScrolling) {
529 374 QPointF delta = m_origin - event->pos();
530 375 chart->scroll(delta.x(), -delta.y());
531 376 }
532 377 if (m_isZooming && plotArea.contains(event->pos())) {
533 378 m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized());
534 379 }
535 380 break;
536 381 }
537 382 }
538 383 if(m_isScrolling) m_origin = event->pos();
539 384 event->accept();
540 385 }
541 386 }
542 387
543 388 void Window::mouseReleaseEvent(QMouseEvent *event)
544 389 {
545 390 if (event->button() == Qt::LeftButton) {
546 391 m_isScrolling = false;
547 392 if (m_isZooming) {
548 393 m_isZooming = false;
549 394 m_rubberBand->setVisible(false);
550 395
551 396 foreach (QChart *chart, m_chartList) {
552 397
553 398 QRectF geometryRect = chart->geometry();
554 399 QRectF plotArea = chart->plotArea();
555 400 plotArea.translate(geometryRect.topLeft());
556 401
557 402 if (plotArea.contains(m_origin)) {
558 403 QRectF rect = m_rubberBand->rect();
559 404 rect.translate(-geometryRect.topLeft());
560 405 chart->zoomIn(rect);
561 406 break;
562 407 }
563 408 }
564 409 }
565 410 event->accept();
566 411 }
567 412
568 413 if (event->button() == Qt::RightButton) {
569 414
570 415 if (m_isZooming) {
571 416 foreach (QChart *chart, m_chartList) {
572 417
573 418 QRectF geometryRect = chart->geometry();
574 419 QRectF plotArea = chart->plotArea();
575 420 plotArea.translate(geometryRect.topLeft());
576 421
577 422 if (plotArea.contains(m_origin)) {
578 423 QRectF rect = m_rubberBand->rect();
579 424 chart->zoomOut();
580 425 break;
581 426 }
582 427 }
583 428 }
584 429 }
585 430 }
@@ -1,102 +1,96
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 #ifndef WINDOW_H_
22 22 #define WINDOW_H_
23 23 #include <QMainWindow>
24 24 #include <QChartGlobal>
25 25 #include <QHash>
26 26
27 27 class QComboBox;
28 28 class QCheckBox;
29 29 class QGraphicsRectItem;
30 30 class QGraphicsScene;
31 31 class QGraphicsWidget;
32 32 class View;
33 33
34 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 35 class QChart;
36 36 QTCOMMERCIALCHART_END_NAMESPACE
37 37
38 38 typedef QPair<QPointF, QString> Data;
39 39 typedef QList<Data> DataList;
40 40 typedef QList<DataList> DataTable;
41 41
42 42 QTCOMMERCIALCHART_USE_NAMESPACE
43 43
44 44 class Window: public QMainWindow
45 45 {
46 46 Q_OBJECT
47 47 public:
48 48 explicit Window(QWidget *parent = 0);
49 49 ~Window();
50 50
51 51 private Q_SLOTS:
52 52 void updateUI();
53 53
54 54
55 55 private:
56 56 DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
57 57 QComboBox* createThemeBox() const;
58 58 QComboBox* createAnimationBox() const;
59 59 QComboBox* createLegendBox() const;
60 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 61 void createProxyWidgets();
68 62
69 63 protected:
70 64 void mousePressEvent(QMouseEvent *event);
71 65 void mouseMoveEvent(QMouseEvent *event);
72 66 void mouseReleaseEvent(QMouseEvent *event);
73 67
74 68
75 69 private:
76 70 int m_listCount;
77 71 int m_valueMax;
78 72 int m_valueCount;
79 73 QGraphicsScene* m_scene;
80 74 View* m_view;
81 75 QHash<QString, QGraphicsProxyWidget*> m_widgetHash;
82 76 QList<QChart*> m_chartList;
83 77 DataTable m_dataTable;
84 78
85 79 QGraphicsWidget *m_form;
86 80 QComboBox *m_themeComboBox;
87 81 QCheckBox *m_antialiasCheckBox;
88 82 QComboBox *m_animatedComboBox;
89 83 QComboBox *m_legendComboBox;
90 84 QCheckBox *m_openGLCheckBox;
91 85 QCheckBox *m_zoomCheckBox;
92 86 QCheckBox *m_scrollCheckBox;
93 87 QPoint m_origin;
94 88 QGraphicsRectItem* m_rubberBand;
95 89
96 90 bool m_isScrolling;
97 91 bool m_isZooming;
98 92 bool m_scroll;
99 93 bool m_zoom;
100 94 };
101 95
102 96 #endif
General Comments 0
You need to be logged in to leave comments. Login now