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