@@ -0,0 +1,331 | |||
|
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:BSD$ | |
|
10 | ** You may use this file under the terms of the BSD license as follows: | |
|
11 | ** | |
|
12 | ** "Redistribution and use in source and binary forms, with or without | |
|
13 | ** modification, are permitted provided that the following conditions are | |
|
14 | ** met: | |
|
15 | ** * Redistributions of source code must retain the above copyright | |
|
16 | ** notice, this list of conditions and the following disclaimer. | |
|
17 | ** * Redistributions in binary form must reproduce the above copyright | |
|
18 | ** notice, this list of conditions and the following disclaimer in | |
|
19 | ** the documentation and/or other materials provided with the | |
|
20 | ** distribution. | |
|
21 | ** * Neither the name of Digia nor the names of its contributors | |
|
22 | ** may be used to endorse or promote products derived from this | |
|
23 | ** software without specific prior written permission. | |
|
24 | ** | |
|
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
|
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
|
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
|
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
|
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
|
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
|
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
|
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
|
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | |
|
36 | ** $QT_END_LICENSE$ | |
|
37 | ** | |
|
38 | ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies). | |
|
39 | ** | |
|
40 | ****************************************************************************/ | |
|
41 | ||
|
42 | #include "themewidget.h" | |
|
43 | ||
|
44 | #include <QChartView> | |
|
45 | #include <QPieSeries> | |
|
46 | #include <QPieSlice> | |
|
47 | #include <QBarSeries> | |
|
48 | #include <QPercentBarSeries> | |
|
49 | #include <QStackedBarSeries> | |
|
50 | #include <QBarSet> | |
|
51 | #include <QLineSeries> | |
|
52 | #include <QSplineSeries> | |
|
53 | #include <QScatterSeries> | |
|
54 | #include <QAreaSeries> | |
|
55 | #include <QGridLayout> | |
|
56 | #include <QFormLayout> | |
|
57 | #include <QComboBox> | |
|
58 | #include <QSpinBox> | |
|
59 | #include <QCheckBox> | |
|
60 | #include <QGroupBox> | |
|
61 | #include <QLabel> | |
|
62 | #include <QTime> | |
|
63 | ||
|
64 | ThemeWidget::ThemeWidget(QWidget* parent) : | |
|
65 | QWidget(parent), | |
|
66 | m_listCount(3), | |
|
67 | m_valueMax(100), | |
|
68 | m_valueCount(11), | |
|
69 | m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)), | |
|
70 | m_themeComboBox(createThemeBox()), | |
|
71 | m_antialiasCheckBox(new QCheckBox("Anti aliasing")), | |
|
72 | m_animatedComboBox(createAnimationBox()) | |
|
73 | { | |
|
74 | ||
|
75 | connectSignals(); | |
|
76 | // create layout | |
|
77 | QGridLayout* baseLayout = new QGridLayout(); | |
|
78 | QHBoxLayout *settingsLayout = new QHBoxLayout(); | |
|
79 | settingsLayout->addWidget(new QLabel("Theme:")); | |
|
80 | settingsLayout->addWidget(m_themeComboBox); | |
|
81 | settingsLayout->addWidget(new QLabel("Animation:")); | |
|
82 | settingsLayout->addWidget(m_animatedComboBox); | |
|
83 | settingsLayout->addWidget(m_antialiasCheckBox); | |
|
84 | settingsLayout->addStretch(); | |
|
85 | baseLayout->addLayout(settingsLayout, 0, 0, 1, 3); | |
|
86 | ||
|
87 | //create charts | |
|
88 | ||
|
89 | QChartView *chartView; | |
|
90 | ||
|
91 | chartView = new QChartView(createAreaChart()); | |
|
92 | baseLayout->addWidget(chartView, 1, 0); | |
|
93 | m_charts << chartView; | |
|
94 | ||
|
95 | chartView = new QChartView(createBarChart(m_valueCount)); | |
|
96 | baseLayout->addWidget(chartView, 1, 1); | |
|
97 | m_charts << chartView; | |
|
98 | ||
|
99 | chartView = new QChartView(createLineChart()); | |
|
100 | baseLayout->addWidget(chartView, 1, 2); | |
|
101 | m_charts << chartView; | |
|
102 | ||
|
103 | chartView = new QChartView(createPieChart()); | |
|
104 | chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen... | |
|
105 | baseLayout->addWidget(chartView, 2, 0); | |
|
106 | m_charts << chartView; | |
|
107 | ||
|
108 | chartView = new QChartView(createSplineChart()); | |
|
109 | baseLayout->addWidget(chartView, 2, 1); | |
|
110 | m_charts << chartView; | |
|
111 | ||
|
112 | chartView = new QChartView(createSplineChart()); | |
|
113 | baseLayout->addWidget(chartView, 2, 2); | |
|
114 | m_charts << chartView; | |
|
115 | ||
|
116 | setLayout(baseLayout); | |
|
117 | } | |
|
118 | ||
|
119 | ThemeWidget::~ThemeWidget() | |
|
120 | { | |
|
121 | } | |
|
122 | ||
|
123 | void ThemeWidget::connectSignals() | |
|
124 | { | |
|
125 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | |
|
126 | connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); | |
|
127 | connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); | |
|
128 | } | |
|
129 | ||
|
130 | DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const | |
|
131 | { | |
|
132 | DataTable dataTable; | |
|
133 | ||
|
134 | // set seed for random stuff | |
|
135 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); | |
|
136 | ||
|
137 | // generate random data | |
|
138 | for (int i(0); i < listCount; i++) { | |
|
139 | DataList dataList; | |
|
140 | for (int j(0); j < valueCount; j++) { | |
|
141 | QPointF value(j + (qreal) rand() / (qreal) RAND_MAX, qrand() % valueMax); | |
|
142 | QString label = "Item " + QString::number(i) + ":" + QString::number(j); | |
|
143 | dataList << Data(value, label); | |
|
144 | } | |
|
145 | dataTable << dataList; | |
|
146 | } | |
|
147 | ||
|
148 | return dataTable; | |
|
149 | } | |
|
150 | ||
|
151 | QComboBox* ThemeWidget::createThemeBox() const | |
|
152 | { | |
|
153 | // settings layout | |
|
154 | QComboBox* themeComboBox = new QComboBox(); | |
|
155 | themeComboBox->addItem("Default", QChart::ChartThemeDefault); | |
|
156 | themeComboBox->addItem("Light", QChart::ChartThemeLight); | |
|
157 | themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean); | |
|
158 | themeComboBox->addItem("Dark", QChart::ChartThemeDark); | |
|
159 | themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand); | |
|
160 | themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs); | |
|
161 | themeComboBox->addItem("Icy", QChart::ChartThemeIcy); | |
|
162 | themeComboBox->addItem("Scientific", QChart::ChartThemeScientific); | |
|
163 | return themeComboBox; | |
|
164 | } | |
|
165 | ||
|
166 | QComboBox* ThemeWidget::createAnimationBox() const | |
|
167 | { | |
|
168 | // settings layout | |
|
169 | QComboBox* animationComboBox = new QComboBox(); | |
|
170 | animationComboBox->addItem("No Animations", QChart::NoAnimation); | |
|
171 | animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations); | |
|
172 | animationComboBox->addItem("Series Animations", QChart::SeriesAnimations); | |
|
173 | animationComboBox->addItem("All Animations", QChart::AllAnimations); | |
|
174 | return animationComboBox; | |
|
175 | } | |
|
176 | ||
|
177 | QChart* ThemeWidget::createAreaChart() const | |
|
178 | { | |
|
179 | // area chart | |
|
180 | QChart *chart = new QChart(); | |
|
181 | chart->setTitle("Area chart"); | |
|
182 | { | |
|
183 | for (int i(0); i < m_dataTable.count(); i++) { | |
|
184 | QLineSeries *series1 = new QLineSeries(chart); | |
|
185 | QLineSeries *series2 = new QLineSeries(chart); | |
|
186 | foreach (Data data, m_dataTable[i]) { | |
|
187 | series1->add(data.first); | |
|
188 | series2->add(QPointF(data.first.x(), 0.0)); | |
|
189 | } | |
|
190 | QAreaSeries *area = new QAreaSeries(series1, series2); | |
|
191 | chart->addSeries(area); | |
|
192 | } | |
|
193 | } | |
|
194 | return chart; | |
|
195 | } | |
|
196 | ||
|
197 | QChart* ThemeWidget::createBarChart(int valueCount) const | |
|
198 | { | |
|
199 | // bar chart | |
|
200 | QChart* chart = new QChart(); | |
|
201 | chart->setTitle("Bar chart"); | |
|
202 | { | |
|
203 | QBarCategories categories; | |
|
204 | // TODO: categories | |
|
205 | for (int i(0); i < valueCount; i++) | |
|
206 | categories << QString::number(i); | |
|
207 | // QBarSeries* series = new QBarSeries(categories, chart); | |
|
208 | // QPercentBarSeries* series = new QPercentBarSeries(categories, chart); | |
|
209 | QStackedBarSeries* series = new QStackedBarSeries(categories, chart); | |
|
210 | for (int i(0); i < m_dataTable.count(); i++) { | |
|
211 | QBarSet *set = new QBarSet("Set" + QString::number(i)); | |
|
212 | foreach (Data data, m_dataTable[i]) | |
|
213 | *set << data.first.y(); | |
|
214 | series->addBarSet(set); | |
|
215 | } | |
|
216 | chart->addSeries(series); | |
|
217 | } | |
|
218 | return chart; | |
|
219 | } | |
|
220 | ||
|
221 | QChart* ThemeWidget::createLineChart() const | |
|
222 | { | |
|
223 | // line chart | |
|
224 | QChart* chart = new QChart(); | |
|
225 | chart->setTitle("Line chart"); | |
|
226 | foreach (DataList list, m_dataTable) { | |
|
227 | QLineSeries *series = new QLineSeries(chart); | |
|
228 | foreach (Data data, list) | |
|
229 | series->add(data.first); | |
|
230 | chart->addSeries(series); | |
|
231 | } | |
|
232 | return chart; | |
|
233 | } | |
|
234 | ||
|
235 | QChart* ThemeWidget::createPieChart() const | |
|
236 | { | |
|
237 | // pie chart | |
|
238 | QChart* chart = new QChart(); | |
|
239 | chart->setTitle("Pie chart"); | |
|
240 | qreal pieSize = 1.0 / m_dataTable.count(); | |
|
241 | for (int i = 0; i < m_dataTable.count(); i++) { | |
|
242 | QPieSeries *series = new QPieSeries(chart); | |
|
243 | foreach (Data data, m_dataTable[i]) { | |
|
244 | QPieSlice *slice = series->add(data.first.y(), data.second); | |
|
245 | if (data == m_dataTable[i].first()) { | |
|
246 | slice->setLabelVisible(); | |
|
247 | slice->setExploded(); | |
|
248 | } | |
|
249 | } | |
|
250 | qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); | |
|
251 | series->setPieSize(pieSize); | |
|
252 | series->setPiePosition(hPos, 0.5); | |
|
253 | chart->addSeries(series); | |
|
254 | } | |
|
255 | ||
|
256 | return chart; | |
|
257 | } | |
|
258 | ||
|
259 | QChart* ThemeWidget::createSplineChart() const | |
|
260 | { // spine chart | |
|
261 | QChart* chart = new QChart(); | |
|
262 | chart->setTitle("Spline chart"); | |
|
263 | foreach (DataList list, m_dataTable) { | |
|
264 | QSplineSeries *series = new QSplineSeries(chart); | |
|
265 | foreach (Data data, list) | |
|
266 | series->add(data.first); | |
|
267 | chart->addSeries(series); | |
|
268 | } | |
|
269 | return chart; | |
|
270 | } | |
|
271 | ||
|
272 | QChart* ThemeWidget::createScatterChart() const | |
|
273 | { // scatter chart | |
|
274 | QChart* chart = new QChart(); | |
|
275 | chart->setTitle("Scatter chart"); | |
|
276 | foreach (DataList list, m_dataTable) { | |
|
277 | QScatterSeries *series = new QScatterSeries(chart); | |
|
278 | foreach (Data data, list) | |
|
279 | series->add(data.first); | |
|
280 | chart->addSeries(series); | |
|
281 | } | |
|
282 | return chart; | |
|
283 | } | |
|
284 | ||
|
285 | void ThemeWidget::updateUI() | |
|
286 | { | |
|
287 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); | |
|
288 | ||
|
289 | if (m_charts.at(0)->chart()->theme() != theme) { | |
|
290 | foreach (QChartView *chartView, m_charts) | |
|
291 | chartView->chart()->setTheme(theme); | |
|
292 | ||
|
293 | QPalette pal = window()->palette(); | |
|
294 | if (theme == QChart::ChartThemeLight) { | |
|
295 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); | |
|
296 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); | |
|
297 | } | |
|
298 | else if (theme == QChart::ChartThemeDark) { | |
|
299 | pal.setColor(QPalette::Window, QRgb(0x121218)); | |
|
300 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); | |
|
301 | } | |
|
302 | else if (theme == QChart::ChartThemeBlueCerulean) { | |
|
303 | pal.setColor(QPalette::Window, QRgb(0x40434a)); | |
|
304 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); | |
|
305 | } | |
|
306 | else if (theme == QChart::ChartThemeBrownSand) { | |
|
307 | pal.setColor(QPalette::Window, QRgb(0x9e8965)); | |
|
308 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); | |
|
309 | } | |
|
310 | else if (theme == QChart::ChartThemeBlueNcs) { | |
|
311 | pal.setColor(QPalette::Window, QRgb(0x018bba)); | |
|
312 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); | |
|
313 | } | |
|
314 | else { | |
|
315 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); | |
|
316 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); | |
|
317 | } | |
|
318 | window()->setPalette(pal); | |
|
319 | } | |
|
320 | ||
|
321 | bool checked = m_antialiasCheckBox->isChecked(); | |
|
322 | foreach (QChartView *chart, m_charts) | |
|
323 | chart->setRenderHint(QPainter::Antialiasing, checked); | |
|
324 | ||
|
325 | QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt()); | |
|
326 | if (m_charts.at(0)->chart()->animationOptions() != options) { | |
|
327 | foreach (QChartView *chartView, m_charts) | |
|
328 | chartView->chart()->setAnimationOptions(options); | |
|
329 | } | |
|
330 | } | |
|
331 |
@@ -0,0 +1,95 | |||
|
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:BSD$ | |
|
10 | ** You may use this file under the terms of the BSD license as follows: | |
|
11 | ** | |
|
12 | ** "Redistribution and use in source and binary forms, with or without | |
|
13 | ** modification, are permitted provided that the following conditions are | |
|
14 | ** met: | |
|
15 | ** * Redistributions of source code must retain the above copyright | |
|
16 | ** notice, this list of conditions and the following disclaimer. | |
|
17 | ** * Redistributions in binary form must reproduce the above copyright | |
|
18 | ** notice, this list of conditions and the following disclaimer in | |
|
19 | ** the documentation and/or other materials provided with the | |
|
20 | ** distribution. | |
|
21 | ** * Neither the name of Digia nor the names of its contributors | |
|
22 | ** may be used to endorse or promote products derived from this | |
|
23 | ** software without specific prior written permission. | |
|
24 | ** | |
|
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
|
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
|
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
|
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
|
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
|
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
|
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
|
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
|
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | |
|
36 | ** $QT_END_LICENSE$ | |
|
37 | ** | |
|
38 | ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies). | |
|
39 | ** | |
|
40 | ****************************************************************************/ | |
|
41 | ||
|
42 | #ifndef THEMEWINDOW_H_ | |
|
43 | #define THEMEWINDOW_H_ | |
|
44 | #include <QWidget> | |
|
45 | #include <QChartGlobal> | |
|
46 | ||
|
47 | class QComboBox; | |
|
48 | class QCheckBox; | |
|
49 | ||
|
50 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
51 | class QChartView; | |
|
52 | class QChart; | |
|
53 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
54 | ||
|
55 | typedef QPair<QPointF, QString> Data; | |
|
56 | typedef QList<Data> DataList; | |
|
57 | typedef QList<DataList> DataTable; | |
|
58 | ||
|
59 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
60 | ||
|
61 | class ThemeWidget: public QWidget | |
|
62 | { | |
|
63 | Q_OBJECT | |
|
64 | public: | |
|
65 | explicit ThemeWidget(QWidget *parent = 0); | |
|
66 | ~ThemeWidget(); | |
|
67 | ||
|
68 | public slots: | |
|
69 | void updateUI(); | |
|
70 | ||
|
71 | private: | |
|
72 | DataTable generateRandomData(int listCount,int valueMax,int valueCount) const; | |
|
73 | QComboBox* createThemeBox() const; | |
|
74 | QComboBox* createAnimationBox() const; | |
|
75 | void connectSignals(); | |
|
76 | QChart* createAreaChart() const; | |
|
77 | QChart* createBarChart(int valueCount) const; | |
|
78 | QChart* createPieChart() const; | |
|
79 | QChart* createLineChart() const; | |
|
80 | QChart* createSplineChart() const; | |
|
81 | QChart* createScatterChart() const; | |
|
82 | ||
|
83 | private: | |
|
84 | int m_listCount; | |
|
85 | int m_valueMax; | |
|
86 | int m_valueCount; | |
|
87 | QList<QChartView*> m_charts; | |
|
88 | DataTable m_dataTable; | |
|
89 | ||
|
90 | QComboBox *m_themeComboBox; | |
|
91 | QCheckBox *m_antialiasCheckBox; | |
|
92 | QComboBox *m_animatedComboBox; | |
|
93 | }; | |
|
94 | ||
|
95 | #endif /* THEMEWINDOW_H_ */ |
@@ -1,7 +1,4 | |||
|
1 | !include( ../demos.pri ) { | |
|
2 | error( "Couldn't find the examples.pri file!" ) | |
|
3 | } | |
|
1 | !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" ) | |
|
4 | 2 | TARGET = chartthemes |
|
5 |
SOURCES |
|
|
6 | ||
|
7 | ||
|
3 | SOURCES = main.cpp themewidget.cpp | |
|
4 | HEADERS = themewidget.h |
@@ -1,250 +1,56 | |||
|
1 | #include <QtGui/QApplication> | |
|
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:BSD$ | |
|
10 | ** You may use this file under the terms of the BSD license as follows: | |
|
11 | ** | |
|
12 | ** "Redistribution and use in source and binary forms, with or without | |
|
13 | ** modification, are permitted provided that the following conditions are | |
|
14 | ** met: | |
|
15 | ** * Redistributions of source code must retain the above copyright | |
|
16 | ** notice, this list of conditions and the following disclaimer. | |
|
17 | ** * Redistributions in binary form must reproduce the above copyright | |
|
18 | ** notice, this list of conditions and the following disclaimer in | |
|
19 | ** the documentation and/or other materials provided with the | |
|
20 | ** distribution. | |
|
21 | ** * Neither the name of Digia nor the names of its contributors | |
|
22 | ** may be used to endorse or promote products derived from this | |
|
23 | ** software without specific prior written permission. | |
|
24 | ** | |
|
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
|
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
|
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
|
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
|
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
|
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
|
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
|
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
|
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | |
|
36 | ** $QT_END_LICENSE$ | |
|
37 | ** | |
|
38 | ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies). | |
|
39 | ** | |
|
40 | ****************************************************************************/ | |
|
41 | ||
|
42 | #include "themewidget.h" | |
|
43 | #include <QApplication> | |
|
2 | 44 | #include <QMainWindow> |
|
3 | #include <qchartglobal.h> | |
|
4 | #include <qchartview.h> | |
|
5 | #include <qpieseries.h> | |
|
6 | #include <qpieslice.h> | |
|
7 | #include <qbarseries.h> | |
|
8 | #include <qpercentbarseries.h> | |
|
9 | #include <qstackedbarseries.h> | |
|
10 | #include <qbarset.h> | |
|
11 | #include <QGridLayout> | |
|
12 | #include <QFormLayout> | |
|
13 | #include <QComboBox> | |
|
14 | #include <QSpinBox> | |
|
15 | #include <QCheckBox> | |
|
16 | #include <QGroupBox> | |
|
17 | #include <QLabel> | |
|
18 | #include <QTime> | |
|
19 | #include <qlineseries.h> | |
|
20 | #include <qsplineseries.h> | |
|
21 | #include <qscatterseries.h> | |
|
22 | #include <qareaseries.h> | |
|
23 | ||
|
24 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
25 | ||
|
26 | typedef QPair<QPointF, QString> Data; | |
|
27 | typedef QList<Data> DataList; | |
|
28 | typedef QList<DataList> DataTable; | |
|
29 | ||
|
30 | ||
|
31 | class MainWidget : public QWidget | |
|
32 | { | |
|
33 | Q_OBJECT | |
|
34 | ||
|
35 | public: | |
|
36 | explicit MainWidget(QWidget* parent = 0) | |
|
37 | :QWidget(parent) | |
|
38 | { | |
|
39 | // set seed for random stuff | |
|
40 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
|
41 | ||
|
42 | // generate random data | |
|
43 | int listCount = 3; | |
|
44 | int valueMax = 100; | |
|
45 | int valueCount = 11; | |
|
46 | for (int i(0); i < listCount; i++) { | |
|
47 | DataList dataList; | |
|
48 | for (int j(0); j < valueCount; j++) { | |
|
49 | QPointF value(j + (qreal) rand() / (qreal) RAND_MAX, qrand() % valueMax); | |
|
50 | QString label = "Item " + QString::number(i) + ":" + QString::number(j); | |
|
51 | dataList << Data(value, label); | |
|
52 | } | |
|
53 | m_dataTable << dataList; | |
|
54 | } | |
|
55 | ||
|
56 | // create layout | |
|
57 | QGridLayout* baseLayout = new QGridLayout(); | |
|
58 | ||
|
59 | // settings layout | |
|
60 | m_themeComboBox = new QComboBox(); | |
|
61 | m_themeComboBox->addItem("Default", QChart::ChartThemeDefault); | |
|
62 | m_themeComboBox->addItem("Light", QChart::ChartThemeLight); | |
|
63 | m_themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean); | |
|
64 | m_themeComboBox->addItem("Dark", QChart::ChartThemeDark); | |
|
65 | m_themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand); | |
|
66 | m_themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs); | |
|
67 | m_themeComboBox->addItem("Icy", QChart::ChartThemeIcy); | |
|
68 | m_themeComboBox->addItem("Scientific", QChart::ChartThemeScientific); | |
|
69 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateTheme())); | |
|
70 | QCheckBox *antialiasCheckBox = new QCheckBox("Anti aliasing"); | |
|
71 | connect(antialiasCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateAntialiasing(bool))); | |
|
72 | QCheckBox *animatedCheckBox = new QCheckBox("Animated"); | |
|
73 | connect(animatedCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateAnimations(bool))); | |
|
74 | QHBoxLayout *settingsLayout = new QHBoxLayout(); | |
|
75 | settingsLayout->addWidget(new QLabel("Theme:")); | |
|
76 | settingsLayout->addWidget(m_themeComboBox); | |
|
77 | settingsLayout->addWidget(antialiasCheckBox); | |
|
78 | settingsLayout->addWidget(animatedCheckBox); | |
|
79 | settingsLayout->addStretch(); | |
|
80 | baseLayout->addLayout(settingsLayout, 0, 0, 1, 3); | |
|
81 | ||
|
82 | // area chart | |
|
83 | QChartView *chart = new QChartView(); | |
|
84 | chart->setChartTitle("Area chart"); | |
|
85 | baseLayout->addWidget(chart, 1, 0); | |
|
86 | { | |
|
87 | for (int i(0); i < m_dataTable.count(); i++) { | |
|
88 | QLineSeries *series1 = new QLineSeries(chart); | |
|
89 | QLineSeries *series2 = new QLineSeries(chart); | |
|
90 | foreach (Data data, m_dataTable[i]) { | |
|
91 | series1->add(data.first); | |
|
92 | series2->add(QPointF(data.first.x(), 0.0)); | |
|
93 | } | |
|
94 | QAreaSeries *area = new QAreaSeries(series1, series2); | |
|
95 | chart->addSeries(area); | |
|
96 | } | |
|
97 | } | |
|
98 | m_charts << chart; | |
|
99 | ||
|
100 | // bar chart | |
|
101 | chart = new QChartView(); | |
|
102 | chart->setChartTitle("Bar chart"); | |
|
103 | baseLayout->addWidget(chart, 1, 1); | |
|
104 | { | |
|
105 | QStringList categories; | |
|
106 | // TODO: categories | |
|
107 | for (int i(0); i < valueCount; i++) | |
|
108 | categories << QString::number(i); | |
|
109 | // QBarSeries* series = new QBarSeries(categories, chart); | |
|
110 | // QPercentBarSeries* series = new QPercentBarSeries(categories, chart); | |
|
111 | QStackedBarSeries* series = new QStackedBarSeries(categories, chart); | |
|
112 | for (int i(0); i < m_dataTable.count(); i++) { | |
|
113 | QBarSet *set = new QBarSet("Set" + QString::number(i)); | |
|
114 | foreach (Data data, m_dataTable[i]) | |
|
115 | *set << data.first.y(); | |
|
116 | series->addBarSet(set); | |
|
117 | } | |
|
118 | chart->addSeries(series); | |
|
119 | } | |
|
120 | m_charts << chart; | |
|
121 | ||
|
122 | // line chart | |
|
123 | chart = new QChartView(); | |
|
124 | chart->setChartTitle("Line chart"); | |
|
125 | baseLayout->addWidget(chart, 1, 2); | |
|
126 | foreach (DataList list, m_dataTable) { | |
|
127 | QLineSeries *series = new QLineSeries(chart); | |
|
128 | foreach (Data data, list) | |
|
129 | series->add(data.first); | |
|
130 | chart->addSeries(series); | |
|
131 | } | |
|
132 | m_charts << chart; | |
|
133 | ||
|
134 | // pie chart | |
|
135 | chart = new QChartView(); | |
|
136 | chart->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen... | |
|
137 | chart->setChartTitle("Pie chart"); | |
|
138 | baseLayout->addWidget(chart, 2, 0); | |
|
139 | qreal pieSize = 1.0 / m_dataTable.count(); | |
|
140 | for (int i=0; i<m_dataTable.count(); i++) { | |
|
141 | QPieSeries *series = new QPieSeries(chart); | |
|
142 | foreach (Data data, m_dataTable[i]) { | |
|
143 | QPieSlice *slice = series->add(data.first.y(), data.second); | |
|
144 | if (data == m_dataTable[i].first()) { | |
|
145 | slice->setLabelVisible(); | |
|
146 | slice->setExploded(); | |
|
147 | } | |
|
148 | } | |
|
149 | qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); | |
|
150 | series->setPieSize(pieSize); | |
|
151 | series->setPiePosition(hPos, 0.5); | |
|
152 | chart->addSeries(series); | |
|
153 | } | |
|
154 | m_charts << chart; | |
|
155 | ||
|
156 | // spine chart | |
|
157 | chart = new QChartView(); | |
|
158 | chart->setChartTitle("Spline chart"); | |
|
159 | baseLayout->addWidget(chart, 2, 1); | |
|
160 | foreach (DataList list, m_dataTable) { | |
|
161 | QSplineSeries *series = new QSplineSeries(chart); | |
|
162 | foreach (Data data, list) | |
|
163 | series->add(data.first); | |
|
164 | chart->addSeries(series); | |
|
165 | } | |
|
166 | m_charts << chart; | |
|
167 | ||
|
168 | // scatter chart | |
|
169 | chart = new QChartView(); | |
|
170 | chart->setChartTitle("Scatter chart"); | |
|
171 | baseLayout->addWidget(chart, 2, 2); | |
|
172 | foreach (DataList list, m_dataTable) { | |
|
173 | QScatterSeries *series = new QScatterSeries(chart); | |
|
174 | foreach (Data data, list) | |
|
175 | series->add(data.first); | |
|
176 | chart->addSeries(series); | |
|
177 | } | |
|
178 | m_charts << chart; | |
|
179 | ||
|
180 | setLayout(baseLayout); | |
|
181 | } | |
|
182 | ||
|
183 | public Q_SLOTS: | |
|
184 | ||
|
185 | void updateTheme() | |
|
186 | { | |
|
187 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); | |
|
188 | foreach (QChartView *chart, m_charts) | |
|
189 | chart->setChartTheme(theme); | |
|
190 | ||
|
191 | QPalette pal = window()->palette(); | |
|
192 | if (theme == QChart::ChartThemeLight) { | |
|
193 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); | |
|
194 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); | |
|
195 | } else if (theme == QChart::ChartThemeDark) { | |
|
196 | pal.setColor(QPalette::Window, QRgb(0x121218)); | |
|
197 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); | |
|
198 | } else if (theme == QChart::ChartThemeBlueCerulean) { | |
|
199 | pal.setColor(QPalette::Window, QRgb(0x40434a)); | |
|
200 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); | |
|
201 | } else if (theme == QChart::ChartThemeBrownSand) { | |
|
202 | pal.setColor(QPalette::Window, QRgb(0x9e8965)); | |
|
203 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); | |
|
204 | } else if (theme == QChart::ChartThemeBlueNcs) { | |
|
205 | pal.setColor(QPalette::Window, QRgb(0x018bba)); | |
|
206 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); | |
|
207 | } else { | |
|
208 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); | |
|
209 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); | |
|
210 | } | |
|
211 | window()->setPalette(pal); | |
|
212 | } | |
|
213 | ||
|
214 | void updateAntialiasing(bool enabled) | |
|
215 | { | |
|
216 | foreach (QChartView *chart, m_charts) | |
|
217 | chart->setRenderHint(QPainter::Antialiasing, enabled); | |
|
218 | } | |
|
219 | ||
|
220 | void updateAnimations(bool animated) | |
|
221 | { | |
|
222 | QChart::AnimationOptions options = QChart::NoAnimation; | |
|
223 | if (animated) | |
|
224 | options = QChart::AllAnimations; | |
|
225 | ||
|
226 | foreach (QChartView *chart, m_charts) | |
|
227 | chart->setAnimationOptions(options); | |
|
228 | } | |
|
229 | ||
|
230 | private: | |
|
231 | QList<QChartView*> m_charts; | |
|
232 | QComboBox *m_themeComboBox; | |
|
233 | DataTable m_dataTable; | |
|
234 | }; | |
|
235 | 45 | |
|
236 | 46 | int main(int argc, char *argv[]) |
|
237 | 47 | { |
|
238 | 48 | QApplication a(argc, argv); |
|
239 | ||
|
240 | 49 | QMainWindow window; |
|
241 | ||
|
242 | MainWidget* widget = new MainWidget(); | |
|
243 | ||
|
50 | ThemeWidget* widget = new ThemeWidget(); | |
|
244 | 51 | window.setCentralWidget(widget); |
|
245 | 52 | window.resize(900, 600); |
|
246 | 53 | window.show(); |
|
247 | ||
|
248 | 54 | return a.exec(); |
|
249 | 55 | } |
|
250 | 56 |
General Comments 0
You need to be logged in to leave comments.
Login now