##// END OF EJS Templates
removed warning from chartthemes demo
sauimone -
r1675:592f4e5c298a
parent child
Show More
@@ -1,378 +1,379
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 "themewidget.h"
22 22
23 23 #include <QChartView>
24 24 #include <QPieSeries>
25 25 #include <QPieSlice>
26 26 #include <QAbstractBarSeries>
27 27 #include <QPercentBarSeries>
28 28 #include <QStackedBarSeries>
29 29 #include <QBarSeries>
30 30 #include <QBarSet>
31 31 #include <QLineSeries>
32 32 #include <QSplineSeries>
33 33 #include <QScatterSeries>
34 34 #include <QAreaSeries>
35 35 #include <QLegend>
36 36 #include <QGridLayout>
37 37 #include <QFormLayout>
38 38 #include <QComboBox>
39 39 #include <QSpinBox>
40 40 #include <QCheckBox>
41 41 #include <QGroupBox>
42 42 #include <QLabel>
43 43 #include <QTime>
44 44 #include <QBarCategoriesAxis>
45 45
46 46 ThemeWidget::ThemeWidget(QWidget* parent) :
47 47 QWidget(parent),
48 48 m_listCount(3),
49 49 m_valueMax(10),
50 50 m_valueCount(7),
51 51 m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)),
52 52 m_themeComboBox(createThemeBox()),
53 53 m_antialiasCheckBox(new QCheckBox("Anti-aliasing")),
54 54 m_animatedComboBox(createAnimationBox()),
55 55 m_legendComboBox(createLegendBox())
56 56 {
57 57 connectSignals();
58 58 // create layout
59 59 QGridLayout* baseLayout = new QGridLayout();
60 60 QHBoxLayout *settingsLayout = new QHBoxLayout();
61 61 settingsLayout->addWidget(new QLabel("Theme:"));
62 62 settingsLayout->addWidget(m_themeComboBox);
63 63 settingsLayout->addWidget(new QLabel("Animation:"));
64 64 settingsLayout->addWidget(m_animatedComboBox);
65 65 settingsLayout->addWidget(new QLabel("Legend:"));
66 66 settingsLayout->addWidget(m_legendComboBox);
67 67 settingsLayout->addWidget(m_antialiasCheckBox);
68 68 settingsLayout->addStretch();
69 69 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
70 70
71 71 //create charts
72 72
73 73 QChartView *chartView;
74 74
75 75 chartView = new QChartView(createAreaChart());
76 76 baseLayout->addWidget(chartView, 1, 0);
77 77 m_charts << chartView;
78 78
79 79 chartView = new QChartView(createBarChart(m_valueCount));
80 80 baseLayout->addWidget(chartView, 1, 1);
81 81 m_charts << chartView;
82 82
83 83 chartView = new QChartView(createLineChart());
84 84 baseLayout->addWidget(chartView, 1, 2);
85 85 m_charts << chartView;
86 86
87 87 chartView = new QChartView(createPieChart());
88 88 chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen...
89 89 baseLayout->addWidget(chartView, 2, 0);
90 90 m_charts << chartView;
91 91
92 92 chartView = new QChartView(createSplineChart());
93 93 baseLayout->addWidget(chartView, 2, 1);
94 94 m_charts << chartView;
95 95
96 96 chartView = new QChartView(createScatterChart());
97 97 baseLayout->addWidget(chartView, 2, 2);
98 98 m_charts << chartView;
99 99
100 100 setLayout(baseLayout);
101 101
102 102 // Set defaults
103 103 m_antialiasCheckBox->setChecked(true);
104 104 updateUI();
105 105 }
106 106
107 107 ThemeWidget::~ThemeWidget()
108 108 {
109 109 }
110 110
111 111 void ThemeWidget::connectSignals()
112 112 {
113 113 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
114 114 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
115 115 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
116 116 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
117 117 }
118 118
119 119 DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const
120 120 {
121 121 DataTable dataTable;
122 122
123 123 // set seed for random stuff
124 124 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
125 125
126 126 // generate random data
127 127 for (int i(0); i < listCount; i++) {
128 128 DataList dataList;
129 129 qreal yValue(0);
130 130 for (int j(0); j < valueCount; j++) {
131 131 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
132 132 QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount),
133 133 yValue);
134 134 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
135 135 dataList << Data(value, label);
136 136 }
137 137 dataTable << dataList;
138 138 }
139 139
140 140 return dataTable;
141 141 }
142 142
143 143 QComboBox* ThemeWidget::createThemeBox() const
144 144 {
145 145 // settings layout
146 146 QComboBox* themeComboBox = new QComboBox();
147 147 themeComboBox->addItem("Light", QChart::ChartThemeLight);
148 148 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
149 149 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
150 150 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
151 151 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
152 152 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
153 153 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
154 154 return themeComboBox;
155 155 }
156 156
157 157 QComboBox* ThemeWidget::createAnimationBox() const
158 158 {
159 159 // settings layout
160 160 QComboBox* animationComboBox = new QComboBox();
161 161 animationComboBox->addItem("No Animations", QChart::NoAnimation);
162 162 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
163 163 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
164 164 animationComboBox->addItem("All Animations", QChart::AllAnimations);
165 165 return animationComboBox;
166 166 }
167 167
168 168 QComboBox* ThemeWidget::createLegendBox() const
169 169 {
170 170 QComboBox* legendComboBox = new QComboBox();
171 171 legendComboBox->addItem("No Legend ", 0);
172 172 legendComboBox->addItem("Legend Top", Qt::AlignTop);
173 173 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
174 174 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
175 175 legendComboBox->addItem("Legend Right", Qt::AlignRight);
176 176 return legendComboBox;
177 177 }
178 178
179 179 QChart* ThemeWidget::createAreaChart() const
180 180 {
181 181 QChart *chart = new QChart();
182 182 // chart->axisX()->setNiceNumbersEnabled(true);
183 183 // chart->axisY()->setNiceNumbersEnabled(true);
184 184 chart->setTitle("Area chart");
185 185
186 186 // The lower series initialized to zero values
187 187 QLineSeries *lowerSeries = 0;
188 188 QString name("Series ");
189 189 int nameIndex = 0;
190 190 for (int i(0); i < m_dataTable.count(); i++) {
191 191 QLineSeries *upperSeries = new QLineSeries(chart);
192 192 for (int j(0); j < m_dataTable[i].count(); j++) {
193 193 Data data = m_dataTable[i].at(j);
194 194 if (lowerSeries){
195 195 const QList<QPointF>& points = lowerSeries->points();
196 196 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
197 197 }else
198 198 upperSeries->append(QPointF(j, data.first.y()));
199 199 }
200 200 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
201 201 area->setName(name + QString::number(nameIndex));
202 202 nameIndex++;
203 203 chart->addSeries(area);
204 204 chart->createDefaultAxes();
205 205 lowerSeries = upperSeries;
206 206 }
207 207
208 208 return chart;
209 209 }
210 210
211 211 QChart* ThemeWidget::createBarChart(int valueCount) const
212 212 {
213 Q_UNUSED(valueCount);
213 214 QChart* chart = new QChart();
214 215 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
215 216 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
216 217 chart->setTitle("Bar chart");
217 218
218 219 QStackedBarSeries* series = new QStackedBarSeries(chart);
219 220 for (int i(0); i < m_dataTable.count(); i++) {
220 221 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
221 222 foreach (Data data, m_dataTable[i])
222 223 *set << data.first.y();
223 224 series->append(set);
224 225 }
225 226 chart->addSeries(series);
226 227 chart->createDefaultAxes();
227 228
228 229 return chart;
229 230 }
230 231
231 232 QChart* ThemeWidget::createLineChart() const
232 233 {
233 234 QChart* chart = new QChart();
234 235 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
235 236 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
236 237 chart->setTitle("Line chart");
237 238
238 239 QString name("Series ");
239 240 int nameIndex = 0;
240 241 foreach (DataList list, m_dataTable) {
241 242 QLineSeries *series = new QLineSeries(chart);
242 243 foreach (Data data, list)
243 244 series->append(data.first);
244 245 series->setName(name + QString::number(nameIndex));
245 246 nameIndex++;
246 247 chart->addSeries(series);
247 248 }
248 249 chart->createDefaultAxes();
249 250
250 251 return chart;
251 252 }
252 253
253 254 QChart* ThemeWidget::createPieChart() const
254 255 {
255 256 QChart* chart = new QChart();
256 257 chart->setTitle("Pie chart");
257 258
258 259 qreal pieSize = 1.0 / m_dataTable.count();
259 260 for (int i = 0; i < m_dataTable.count(); i++) {
260 261 QPieSeries *series = new QPieSeries(chart);
261 262 foreach (Data data, m_dataTable[i]) {
262 263 QPieSlice *slice = series->append(data.second, data.first.y());
263 264 if (data == m_dataTable[i].first()) {
264 265 slice->setLabelVisible();
265 266 slice->setExploded();
266 267 }
267 268 }
268 269 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
269 270 series->setPieSize(pieSize);
270 271 series->setHorizontalPosition(hPos);
271 272 series->setVerticalPosition(0.5);
272 273 chart->addSeries(series);
273 274 }
274 275
275 276 return chart;
276 277 }
277 278
278 279 QChart* ThemeWidget::createSplineChart() const
279 280 { // spine chart
280 281 QChart* chart = new QChart();
281 282 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
282 283 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
283 284 chart->setTitle("Spline chart");
284 285 QString name("Series ");
285 286 int nameIndex = 0;
286 287 foreach (DataList list, m_dataTable) {
287 288 QSplineSeries *series = new QSplineSeries(chart);
288 289 foreach (Data data, list)
289 290 series->append(data.first);
290 291 series->setName(name + QString::number(nameIndex));
291 292 nameIndex++;
292 293 chart->addSeries(series);
293 294 }
294 295 chart->createDefaultAxes();
295 296 return chart;
296 297 }
297 298
298 299 QChart* ThemeWidget::createScatterChart() const
299 300 { // scatter chart
300 301 QChart* chart = new QChart();
301 302 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
302 303 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
303 304 chart->setTitle("Scatter chart");
304 305 QString name("Series ");
305 306 int nameIndex = 0;
306 307 foreach (DataList list, m_dataTable) {
307 308 QScatterSeries *series = new QScatterSeries(chart);
308 309 foreach (Data data, list)
309 310 series->append(data.first);
310 311 series->setName(name + QString::number(nameIndex));
311 312 nameIndex++;
312 313 chart->addSeries(series);
313 314 }
314 315 chart->createDefaultAxes();
315 316 return chart;
316 317 }
317 318
318 319 void ThemeWidget::updateUI()
319 320 {
320 321 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
321 322
322 323 if (m_charts.at(0)->chart()->theme() != theme) {
323 324 foreach (QChartView *chartView, m_charts)
324 325 chartView->chart()->setTheme(theme);
325 326
326 327 QPalette pal = window()->palette();
327 328 if (theme == QChart::ChartThemeLight) {
328 329 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
329 330 pal.setColor(QPalette::WindowText, QRgb(0x404044));
330 331 } else if (theme == QChart::ChartThemeDark) {
331 332 pal.setColor(QPalette::Window, QRgb(0x121218));
332 333 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
333 334 } else if (theme == QChart::ChartThemeBlueCerulean) {
334 335 pal.setColor(QPalette::Window, QRgb(0x40434a));
335 336 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
336 337 } else if (theme == QChart::ChartThemeBrownSand) {
337 338 pal.setColor(QPalette::Window, QRgb(0x9e8965));
338 339 pal.setColor(QPalette::WindowText, QRgb(0x404044));
339 340 } else if (theme == QChart::ChartThemeBlueNcs) {
340 341 pal.setColor(QPalette::Window, QRgb(0x018bba));
341 342 pal.setColor(QPalette::WindowText, QRgb(0x404044));
342 343 } else if (theme == QChart::ChartThemeHighContrast) {
343 344 pal.setColor(QPalette::Window, QRgb(0xffab03));
344 345 pal.setColor(QPalette::WindowText, QRgb(0x181818));
345 346 } else if (theme == QChart::ChartThemeBlueIcy) {
346 347 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
347 348 pal.setColor(QPalette::WindowText, QRgb(0x404044));
348 349 } else {
349 350 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
350 351 pal.setColor(QPalette::WindowText, QRgb(0x404044));
351 352 }
352 353 window()->setPalette(pal);
353 354 }
354 355
355 356 bool checked = m_antialiasCheckBox->isChecked();
356 357 foreach (QChartView *chart, m_charts)
357 358 chart->setRenderHint(QPainter::Antialiasing, checked);
358 359
359 360 QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
360 361 if (m_charts.at(0)->chart()->animationOptions() != options) {
361 362 foreach (QChartView *chartView, m_charts)
362 363 chartView->chart()->setAnimationOptions(options);
363 364 }
364 365
365 366 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
366 367
367 368 if (!alignment) {
368 369 foreach (QChartView *chartView, m_charts) {
369 370 chartView->chart()->legend()->hide();
370 371 }
371 372 } else {
372 373 foreach (QChartView *chartView, m_charts) {
373 374 chartView->chart()->legend()->setAlignment(alignment);
374 375 chartView->chart()->legend()->show();
375 376 }
376 377 }
377 378 }
378 379
General Comments 0
You need to be logged in to leave comments. Login now