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