@@ -1,385 +1,377 | |||
|
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 <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 | #include <QCategoriesAxis> |
|
44 | 44 | |
|
45 | 45 | ThemeWidget::ThemeWidget(QWidget* parent) : |
|
46 | 46 | QWidget(parent), |
|
47 | 47 | m_listCount(3), |
|
48 | 48 | m_valueMax(10), |
|
49 | 49 | m_valueCount(7), |
|
50 | 50 | m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)), |
|
51 | 51 | m_themeComboBox(createThemeBox()), |
|
52 | 52 | m_antialiasCheckBox(new QCheckBox("Anti-aliasing")), |
|
53 | 53 | m_animatedComboBox(createAnimationBox()), |
|
54 | 54 | m_legendComboBox(createLegendBox()) |
|
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(new QLabel("Legend:")); |
|
65 | 65 | settingsLayout->addWidget(m_legendComboBox); |
|
66 | 66 | settingsLayout->addWidget(m_antialiasCheckBox); |
|
67 | 67 | settingsLayout->addStretch(); |
|
68 | 68 | baseLayout->addLayout(settingsLayout, 0, 0, 1, 3); |
|
69 | 69 | |
|
70 | 70 | //create charts |
|
71 | 71 | |
|
72 | 72 | QChartView *chartView; |
|
73 | 73 | |
|
74 | 74 | chartView = new QChartView(createAreaChart()); |
|
75 | 75 | baseLayout->addWidget(chartView, 1, 0); |
|
76 | 76 | m_charts << chartView; |
|
77 | 77 | |
|
78 | 78 | chartView = new QChartView(createBarChart(m_valueCount)); |
|
79 | 79 | baseLayout->addWidget(chartView, 1, 1); |
|
80 | 80 | m_charts << chartView; |
|
81 | 81 | |
|
82 | 82 | chartView = new QChartView(createLineChart()); |
|
83 | 83 | baseLayout->addWidget(chartView, 1, 2); |
|
84 | 84 | m_charts << chartView; |
|
85 | 85 | |
|
86 | 86 | chartView = new QChartView(createPieChart()); |
|
87 | 87 | chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen... |
|
88 | 88 | baseLayout->addWidget(chartView, 2, 0); |
|
89 | 89 | m_charts << chartView; |
|
90 | 90 | |
|
91 | 91 | chartView = new QChartView(createSplineChart()); |
|
92 | 92 | baseLayout->addWidget(chartView, 2, 1); |
|
93 | 93 | m_charts << chartView; |
|
94 | 94 | |
|
95 | 95 | chartView = new QChartView(createScatterChart()); |
|
96 | 96 | baseLayout->addWidget(chartView, 2, 2); |
|
97 | 97 | m_charts << chartView; |
|
98 | 98 | |
|
99 | 99 | setLayout(baseLayout); |
|
100 | 100 | |
|
101 | 101 | // Set defaults |
|
102 | 102 | m_antialiasCheckBox->setChecked(true); |
|
103 | 103 | updateUI(); |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | ThemeWidget::~ThemeWidget() |
|
107 | 107 | { |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | void ThemeWidget::connectSignals() |
|
111 | 111 | { |
|
112 | 112 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); |
|
113 | 113 | connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); |
|
114 | 114 | connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); |
|
115 | 115 | connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const |
|
119 | 119 | { |
|
120 | 120 | DataTable dataTable; |
|
121 | 121 | |
|
122 | 122 | // set seed for random stuff |
|
123 | 123 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); |
|
124 | 124 | |
|
125 | 125 | // generate random data |
|
126 | 126 | for (int i(0); i < listCount; i++) { |
|
127 | 127 | DataList dataList; |
|
128 | 128 | qreal yValue(0); |
|
129 | 129 | for (int j(0); j < valueCount; j++) { |
|
130 | 130 | yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount; |
|
131 | 131 | QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount), |
|
132 | 132 | yValue); |
|
133 | 133 | QString label = "Slice " + QString::number(i) + ":" + QString::number(j); |
|
134 | 134 | dataList << Data(value, label); |
|
135 | 135 | } |
|
136 | 136 | dataTable << dataList; |
|
137 | 137 | } |
|
138 | 138 | |
|
139 | 139 | return dataTable; |
|
140 | 140 | } |
|
141 | 141 | |
|
142 | 142 | QComboBox* ThemeWidget::createThemeBox() const |
|
143 | 143 | { |
|
144 | 144 | // settings layout |
|
145 | 145 | QComboBox* themeComboBox = new QComboBox(); |
|
146 | 146 | themeComboBox->addItem("Light", QChart::ChartThemeLight); |
|
147 | 147 | themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean); |
|
148 | 148 | themeComboBox->addItem("Dark", QChart::ChartThemeDark); |
|
149 | 149 | themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand); |
|
150 | 150 | themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs); |
|
151 | 151 | themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast); |
|
152 | 152 | themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy); |
|
153 | 153 | return themeComboBox; |
|
154 | 154 | } |
|
155 | 155 | |
|
156 | 156 | QComboBox* ThemeWidget::createAnimationBox() const |
|
157 | 157 | { |
|
158 | 158 | // settings layout |
|
159 | 159 | QComboBox* animationComboBox = new QComboBox(); |
|
160 | 160 | animationComboBox->addItem("No Animations", QChart::NoAnimation); |
|
161 | 161 | animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations); |
|
162 | 162 | animationComboBox->addItem("Series Animations", QChart::SeriesAnimations); |
|
163 | 163 | animationComboBox->addItem("All Animations", QChart::AllAnimations); |
|
164 | 164 | return animationComboBox; |
|
165 | 165 | } |
|
166 | 166 | |
|
167 | 167 | QComboBox* ThemeWidget::createLegendBox() const |
|
168 | 168 | { |
|
169 | 169 | QComboBox* legendComboBox = new QComboBox(); |
|
170 | 170 | legendComboBox->addItem("No Legend ", 0); |
|
171 | 171 | legendComboBox->addItem("Legend Top", Qt::AlignTop); |
|
172 | 172 | legendComboBox->addItem("Legend Bottom", Qt::AlignBottom); |
|
173 | 173 | legendComboBox->addItem("Legend Left", Qt::AlignLeft); |
|
174 | 174 | legendComboBox->addItem("Legend Right", Qt::AlignRight); |
|
175 | 175 | return legendComboBox; |
|
176 | 176 | } |
|
177 | 177 | |
|
178 | 178 | QChart* ThemeWidget::createAreaChart() const |
|
179 | 179 | { |
|
180 | 180 | QChart *chart = new QChart(); |
|
181 | 181 | // chart->axisX()->setNiceNumbersEnabled(true); |
|
182 | 182 | // chart->axisY()->setNiceNumbersEnabled(true); |
|
183 | 183 | chart->setTitle("Area chart"); |
|
184 | 184 | |
|
185 | 185 | // The lower series initialized to zero values |
|
186 | 186 | QLineSeries *lowerSeries = 0; |
|
187 | 187 | QString name("Series "); |
|
188 | 188 | int nameIndex = 0; |
|
189 | 189 | for (int i(0); i < m_dataTable.count(); i++) { |
|
190 | 190 | QLineSeries *upperSeries = new QLineSeries(chart); |
|
191 | 191 | for (int j(0); j < m_dataTable[i].count(); j++) { |
|
192 | 192 | Data data = m_dataTable[i].at(j); |
|
193 | 193 | if (lowerSeries){ |
|
194 | 194 | const QList<QPointF>& points = lowerSeries->points(); |
|
195 | 195 | upperSeries->append(QPointF(j, points[i].y() + data.first.y())); |
|
196 | 196 | }else |
|
197 | 197 | upperSeries->append(QPointF(j, data.first.y())); |
|
198 | 198 | } |
|
199 | 199 | QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries); |
|
200 | 200 | area->setName(name + QString::number(nameIndex)); |
|
201 | 201 | nameIndex++; |
|
202 | 202 | chart->addSeries(area); |
|
203 | 203 | chart->createDefaultAxes(); |
|
204 | 204 | lowerSeries = upperSeries; |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | return chart; |
|
208 | 208 | } |
|
209 | 209 | |
|
210 | 210 | QChart* ThemeWidget::createBarChart(int valueCount) const |
|
211 | 211 | { |
|
212 | 212 | QChart* chart = new QChart(); |
|
213 | 213 | //TODO: chart->axisX()->setNiceNumbersEnabled(true); |
|
214 | 214 | //TODO: chart->axisY()->setNiceNumbersEnabled(true); |
|
215 | 215 | chart->setTitle("Bar chart"); |
|
216 | 216 | |
|
217 | QStringList categories; | |
|
218 | for (int i(0); i < valueCount; i++) | |
|
219 | categories << QString::number(i); | |
|
220 | ||
|
221 | QCategoriesAxis* axis = new QCategoriesAxis(); | |
|
222 | axis->append(categories); | |
|
223 | ||
|
224 | 217 | QStackedBarSeries* series = new QStackedBarSeries(chart); |
|
225 | 218 | for (int i(0); i < m_dataTable.count(); i++) { |
|
226 | 219 | QBarSet *set = new QBarSet("Bar set " + QString::number(i)); |
|
227 | 220 | foreach (Data data, m_dataTable[i]) |
|
228 | 221 | *set << data.first.y(); |
|
229 | 222 | series->append(set); |
|
230 | 223 | } |
|
231 | 224 | chart->addSeries(series); |
|
232 | 225 | chart->createDefaultAxes(); |
|
233 | chart->setAxisX(axis, series); | |
|
234 | 226 | |
|
235 | 227 | return chart; |
|
236 | 228 | } |
|
237 | 229 | |
|
238 | 230 | QChart* ThemeWidget::createLineChart() const |
|
239 | 231 | { |
|
240 | 232 | QChart* chart = new QChart(); |
|
241 | 233 | //TODO: chart->axisX()->setNiceNumbersEnabled(true); |
|
242 | 234 | //TODO: chart->axisY()->setNiceNumbersEnabled(true); |
|
243 | 235 | chart->setTitle("Line chart"); |
|
244 | 236 | |
|
245 | 237 | QString name("Series "); |
|
246 | 238 | int nameIndex = 0; |
|
247 | 239 | foreach (DataList list, m_dataTable) { |
|
248 | 240 | QLineSeries *series = new QLineSeries(chart); |
|
249 | 241 | foreach (Data data, list) |
|
250 | 242 | series->append(data.first); |
|
251 | 243 | series->setName(name + QString::number(nameIndex)); |
|
252 | 244 | nameIndex++; |
|
253 | 245 | chart->addSeries(series); |
|
254 | 246 | } |
|
255 | 247 | chart->createDefaultAxes(); |
|
256 | 248 | |
|
257 | 249 | return chart; |
|
258 | 250 | } |
|
259 | 251 | |
|
260 | 252 | QChart* ThemeWidget::createPieChart() const |
|
261 | 253 | { |
|
262 | 254 | QChart* chart = new QChart(); |
|
263 | 255 | chart->setTitle("Pie chart"); |
|
264 | 256 | |
|
265 | 257 | qreal pieSize = 1.0 / m_dataTable.count(); |
|
266 | 258 | for (int i = 0; i < m_dataTable.count(); i++) { |
|
267 | 259 | QPieSeries *series = new QPieSeries(chart); |
|
268 | 260 | foreach (Data data, m_dataTable[i]) { |
|
269 | 261 | QPieSlice *slice = series->append(data.second, data.first.y()); |
|
270 | 262 | if (data == m_dataTable[i].first()) { |
|
271 | 263 | slice->setLabelVisible(); |
|
272 | 264 | slice->setExploded(); |
|
273 | 265 | } |
|
274 | 266 | } |
|
275 | 267 | qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); |
|
276 | 268 | series->setPieSize(pieSize); |
|
277 | 269 | series->setHorizontalPosition(hPos); |
|
278 | 270 | series->setVerticalPosition(0.5); |
|
279 | 271 | chart->addSeries(series); |
|
280 | 272 | } |
|
281 | 273 | |
|
282 | 274 | return chart; |
|
283 | 275 | } |
|
284 | 276 | |
|
285 | 277 | QChart* ThemeWidget::createSplineChart() const |
|
286 | 278 | { // spine chart |
|
287 | 279 | QChart* chart = new QChart(); |
|
288 | 280 | //TODO: chart->axisX()->setNiceNumbersEnabled(true); |
|
289 | 281 | //TODO: chart->axisY()->setNiceNumbersEnabled(true); |
|
290 | 282 | chart->setTitle("Spline chart"); |
|
291 | 283 | QString name("Series "); |
|
292 | 284 | int nameIndex = 0; |
|
293 | 285 | foreach (DataList list, m_dataTable) { |
|
294 | 286 | QSplineSeries *series = new QSplineSeries(chart); |
|
295 | 287 | foreach (Data data, list) |
|
296 | 288 | series->append(data.first); |
|
297 | 289 | series->setName(name + QString::number(nameIndex)); |
|
298 | 290 | nameIndex++; |
|
299 | 291 | chart->addSeries(series); |
|
300 | 292 | } |
|
301 | 293 | chart->createDefaultAxes(); |
|
302 | 294 | return chart; |
|
303 | 295 | } |
|
304 | 296 | |
|
305 | 297 | QChart* ThemeWidget::createScatterChart() const |
|
306 | 298 | { // scatter chart |
|
307 | 299 | QChart* chart = new QChart(); |
|
308 | 300 | //TODO: chart->axisX()->setNiceNumbersEnabled(true); |
|
309 | 301 | //TODO: chart->axisY()->setNiceNumbersEnabled(true); |
|
310 | 302 | chart->setTitle("Scatter chart"); |
|
311 | 303 | QString name("Series "); |
|
312 | 304 | int nameIndex = 0; |
|
313 | 305 | foreach (DataList list, m_dataTable) { |
|
314 | 306 | QScatterSeries *series = new QScatterSeries(chart); |
|
315 | 307 | foreach (Data data, list) |
|
316 | 308 | series->append(data.first); |
|
317 | 309 | series->setName(name + QString::number(nameIndex)); |
|
318 | 310 | nameIndex++; |
|
319 | 311 | chart->addSeries(series); |
|
320 | 312 | } |
|
321 | 313 | chart->createDefaultAxes(); |
|
322 | 314 | return chart; |
|
323 | 315 | } |
|
324 | 316 | |
|
325 | 317 | void ThemeWidget::updateUI() |
|
326 | 318 | { |
|
327 | 319 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); |
|
328 | 320 | |
|
329 | 321 | if (m_charts.at(0)->chart()->theme() != theme) { |
|
330 | 322 | foreach (QChartView *chartView, m_charts) |
|
331 | 323 | chartView->chart()->setTheme(theme); |
|
332 | 324 | |
|
333 | 325 | QPalette pal = window()->palette(); |
|
334 | 326 | if (theme == QChart::ChartThemeLight) { |
|
335 | 327 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); |
|
336 | 328 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
337 | 329 | } else if (theme == QChart::ChartThemeDark) { |
|
338 | 330 | pal.setColor(QPalette::Window, QRgb(0x121218)); |
|
339 | 331 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); |
|
340 | 332 | } else if (theme == QChart::ChartThemeBlueCerulean) { |
|
341 | 333 | pal.setColor(QPalette::Window, QRgb(0x40434a)); |
|
342 | 334 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); |
|
343 | 335 | } else if (theme == QChart::ChartThemeBrownSand) { |
|
344 | 336 | pal.setColor(QPalette::Window, QRgb(0x9e8965)); |
|
345 | 337 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
346 | 338 | } else if (theme == QChart::ChartThemeBlueNcs) { |
|
347 | 339 | pal.setColor(QPalette::Window, QRgb(0x018bba)); |
|
348 | 340 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
349 | 341 | } else if (theme == QChart::ChartThemeHighContrast) { |
|
350 | 342 | pal.setColor(QPalette::Window, QRgb(0xffab03)); |
|
351 | 343 | pal.setColor(QPalette::WindowText, QRgb(0x181818)); |
|
352 | 344 | } else if (theme == QChart::ChartThemeBlueIcy) { |
|
353 | 345 | pal.setColor(QPalette::Window, QRgb(0xcee7f0)); |
|
354 | 346 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
355 | 347 | } else { |
|
356 | 348 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); |
|
357 | 349 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
358 | 350 | } |
|
359 | 351 | window()->setPalette(pal); |
|
360 | 352 | } |
|
361 | 353 | |
|
362 | 354 | bool checked = m_antialiasCheckBox->isChecked(); |
|
363 | 355 | foreach (QChartView *chart, m_charts) |
|
364 | 356 | chart->setRenderHint(QPainter::Antialiasing, checked); |
|
365 | 357 | |
|
366 | 358 | QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt()); |
|
367 | 359 | if (m_charts.at(0)->chart()->animationOptions() != options) { |
|
368 | 360 | foreach (QChartView *chartView, m_charts) |
|
369 | 361 | chartView->chart()->setAnimationOptions(options); |
|
370 | 362 | } |
|
371 | 363 | |
|
372 | 364 | Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt()); |
|
373 | 365 | |
|
374 | 366 | if (!alignment) { |
|
375 | 367 | foreach (QChartView *chartView, m_charts) { |
|
376 | 368 | chartView->chart()->legend()->hide(); |
|
377 | 369 | } |
|
378 | 370 | } else { |
|
379 | 371 | foreach (QChartView *chartView, m_charts) { |
|
380 | 372 | chartView->chart()->legend()->setAlignment(alignment); |
|
381 | 373 | chartView->chart()->legend()->show(); |
|
382 | 374 | } |
|
383 | 375 | } |
|
384 | 376 | } |
|
385 | 377 |
@@ -1,735 +1,743 | |||
|
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 "qabstractbarseries.h" |
|
22 | 22 | #include "qabstractbarseries_p.h" |
|
23 | 23 | #include "qbarset.h" |
|
24 | 24 | #include "qbarset_p.h" |
|
25 | 25 | #include "domain_p.h" |
|
26 | 26 | #include "legendmarker_p.h" |
|
27 | 27 | #include "chartdataset_p.h" |
|
28 | 28 | #include "charttheme_p.h" |
|
29 | 29 | #include "chartanimator_p.h" |
|
30 | 30 | #include "qvaluesaxis.h" |
|
31 | 31 | #include "qcategoriesaxis.h" |
|
32 | 32 | |
|
33 | 33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
34 | 34 | |
|
35 | 35 | /*! |
|
36 | 36 | \class QAbstractBarSeries |
|
37 | 37 | \brief Series for creating a bar chart |
|
38 | 38 | \mainclass |
|
39 | 39 | |
|
40 | 40 | QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to |
|
41 | 41 | the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar |
|
42 | 42 | and y-value is the height of the bar. The category names are ignored with this series and x-axis |
|
43 | 43 | shows the x-values. |
|
44 | 44 | |
|
45 | 45 | See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart. |
|
46 | 46 | \image examples_barchart.png |
|
47 | 47 | |
|
48 | 48 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries |
|
49 | 49 | */ |
|
50 | 50 | /*! |
|
51 | 51 | \qmlclass AbstractBarSeries QAbstractBarSeries |
|
52 | 52 | \inherits QAbstractSeries |
|
53 | 53 | |
|
54 | 54 | The following QML shows how to create a simple bar chart: |
|
55 | 55 | \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1 |
|
56 | 56 | |
|
57 | 57 | \beginfloatleft |
|
58 | 58 | \image demos_qmlchart6.png |
|
59 | 59 | \endfloat |
|
60 | 60 | \clearfloat |
|
61 | 61 | */ |
|
62 | 62 | |
|
63 | 63 | /*! |
|
64 | 64 | \property QAbstractBarSeries::barWidth |
|
65 | 65 | The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars |
|
66 | 66 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
67 | 67 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
68 | 68 | Note that with QBarSeries this value means the width of one group of bars instead of just one bar. |
|
69 | 69 | \sa QBarSeries |
|
70 | 70 | */ |
|
71 | 71 | /*! |
|
72 | 72 | \qmlproperty real AbstractBarSeries::barWidth |
|
73 | 73 | The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars |
|
74 | 74 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
75 | 75 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
76 | 76 | Note that with QBarSeries this value means the width of one group of bars instead of just one bar. |
|
77 | 77 | */ |
|
78 | 78 | |
|
79 | 79 | /*! |
|
80 | 80 | \property QAbstractBarSeries::count |
|
81 | 81 | Holds the number of sets in series. |
|
82 | 82 | */ |
|
83 | 83 | /*! |
|
84 | 84 | \qmlproperty int AbstractBarSeries::count |
|
85 | 85 | Holds the number of sets in series. |
|
86 | 86 | */ |
|
87 | 87 | |
|
88 | 88 | /*! |
|
89 | 89 | \property QAbstractBarSeries::labelsVisible |
|
90 | 90 | Defines the visibility of the labels in series |
|
91 | 91 | */ |
|
92 | 92 | /*! |
|
93 | 93 | \qmlproperty bool AbstractBarSeries::labelsVisible |
|
94 | 94 | Defines the visibility of the labels in series |
|
95 | 95 | */ |
|
96 | 96 | |
|
97 | 97 | /*! |
|
98 | 98 | \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset) |
|
99 | 99 | The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset. |
|
100 | 100 | Clicked bar inside set is indexed by \a index |
|
101 | 101 | */ |
|
102 | 102 | /*! |
|
103 | 103 | \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset) |
|
104 | 104 | The signal is emitted if the user clicks with a mouse on top of BarSet. |
|
105 | 105 | Clicked bar inside set is indexed by \a index |
|
106 | 106 | */ |
|
107 | 107 | |
|
108 | 108 | /*! |
|
109 | 109 | \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset) |
|
110 | 110 | |
|
111 | 111 | The signal is emitted if mouse is hovered on top of series. |
|
112 | 112 | Parameter \a barset is the pointer of barset, where hover happened. |
|
113 | 113 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
114 | 114 | */ |
|
115 | 115 | /*! |
|
116 | 116 | \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset) |
|
117 | 117 | |
|
118 | 118 | The signal is emitted if mouse is hovered on top of series. |
|
119 | 119 | Parameter \a barset is the pointer of barset, where hover happened. |
|
120 | 120 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
121 | 121 | */ |
|
122 | 122 | |
|
123 | 123 | /*! |
|
124 | 124 | \fn void QAbstractBarSeries::countChanged() |
|
125 | 125 | This signal is emitted when barset count has been changed, for example by append or remove. |
|
126 | 126 | */ |
|
127 | 127 | /*! |
|
128 | 128 | \qmlsignal AbstractBarSeries::onCountChanged() |
|
129 | 129 | This signal is emitted when barset count has been changed, for example by append or remove. |
|
130 | 130 | */ |
|
131 | 131 | |
|
132 | 132 | /*! |
|
133 | 133 | \fn void QAbstractBarSeries::labelsVisibleChanged() |
|
134 | 134 | This signal is emitted when labels visibility have changed. |
|
135 | 135 | \sa isLabelsVisible(), setLabelsVisible() |
|
136 | 136 | */ |
|
137 | 137 | |
|
138 | 138 | /*! |
|
139 | 139 | \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets) |
|
140 | 140 | This signal is emitted when \a sets have been added to the series. |
|
141 | 141 | \sa append(), insert() |
|
142 | 142 | */ |
|
143 | 143 | /*! |
|
144 | 144 | \qmlsignal AbstractBarSeries::onAdded(BarSet barset) |
|
145 | 145 | Emitted when \a barset has been added to the series. |
|
146 | 146 | */ |
|
147 | 147 | |
|
148 | 148 | /*! |
|
149 | 149 | \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets) |
|
150 | 150 | This signal is emitted when \a sets have been removed from the series. |
|
151 | 151 | \sa remove() |
|
152 | 152 | */ |
|
153 | 153 | /*! |
|
154 | 154 | \qmlsignal AbstractBarSeries::onRemoved(BarSet barset) |
|
155 | 155 | Emitted when \a barset has been removed from the series. |
|
156 | 156 | */ |
|
157 | 157 | |
|
158 | 158 | /*! |
|
159 | 159 | \qmlmethod BarSet AbstractBarSeries::at(int index) |
|
160 | 160 | Returns bar set at \a index. Returns null if the index is not valid. |
|
161 | 161 | */ |
|
162 | 162 | |
|
163 | 163 | /*! |
|
164 | 164 | \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values) |
|
165 | 165 | Adds a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints. |
|
166 | 166 | For example: |
|
167 | 167 | \code |
|
168 | 168 | myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]); |
|
169 | 169 | myBarSeries.append("set 2", [Qt.point(0, 1), Qt.point(2, 2.5), Qt.point(3.5, 2.2)]); |
|
170 | 170 | \endcode |
|
171 | 171 | */ |
|
172 | 172 | |
|
173 | 173 | /*! |
|
174 | 174 | \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values) |
|
175 | 175 | Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints. |
|
176 | 176 | If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is |
|
177 | 177 | appended. |
|
178 | 178 | \sa AbstractBarSeries::append() |
|
179 | 179 | */ |
|
180 | 180 | |
|
181 | 181 | /*! |
|
182 | 182 | \qmlmethod bool AbstractBarSeries::remove(BarSet barset) |
|
183 | 183 | Removes the barset from the series. Returns true if successfull, false otherwise. |
|
184 | 184 | */ |
|
185 | 185 | |
|
186 | 186 | /*! |
|
187 | 187 | \qmlmethod AbstractBarSeries::clear() |
|
188 | 188 | Removes all barsets from the series. |
|
189 | 189 | */ |
|
190 | 190 | |
|
191 | 191 | /*! |
|
192 | 192 | Constructs empty QAbstractBarSeries. |
|
193 | 193 | QAbstractBarSeries is QObject which is a child of a \a parent. |
|
194 | 194 | */ |
|
195 | 195 | QAbstractBarSeries::QAbstractBarSeries(QObject *parent) : |
|
196 | 196 | QAbstractSeries(*new QAbstractBarSeriesPrivate(this),parent) |
|
197 | 197 | { |
|
198 | 198 | } |
|
199 | 199 | |
|
200 | 200 | /*! |
|
201 | 201 | Destructs abstractbarseries and owned barsets. |
|
202 | 202 | */ |
|
203 | 203 | QAbstractBarSeries::~QAbstractBarSeries() |
|
204 | 204 | { |
|
205 | 205 | Q_D(QAbstractBarSeries); |
|
206 | 206 | if(d->m_dataset){ |
|
207 | 207 | d->m_dataset->removeSeries(this); |
|
208 | 208 | } |
|
209 | 209 | } |
|
210 | 210 | |
|
211 | 211 | /*! |
|
212 | 212 | \internal |
|
213 | 213 | */ |
|
214 | 214 | QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent) : |
|
215 | 215 | QAbstractSeries(d,parent) |
|
216 | 216 | { |
|
217 | 217 | } |
|
218 | 218 | |
|
219 | 219 | /*! |
|
220 | 220 | Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars |
|
221 | 221 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
222 | 222 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
223 | 223 | Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar. |
|
224 | 224 | */ |
|
225 | 225 | void QAbstractBarSeries::setBarWidth(qreal width) |
|
226 | 226 | { |
|
227 | 227 | Q_D(QAbstractBarSeries); |
|
228 | 228 | d->setBarWidth(width); |
|
229 | 229 | } |
|
230 | 230 | |
|
231 | 231 | /*! |
|
232 | 232 | Returns the width of the bars of the series. |
|
233 | 233 | \sa setBarWidth() |
|
234 | 234 | */ |
|
235 | 235 | qreal QAbstractBarSeries::barWidth() const |
|
236 | 236 | { |
|
237 | 237 | Q_D(const QAbstractBarSeries); |
|
238 | 238 | return d->barWidth(); |
|
239 | 239 | } |
|
240 | 240 | |
|
241 | 241 | /*! |
|
242 | 242 | Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended. |
|
243 | 243 | Returns true, if appending succeeded. |
|
244 | 244 | */ |
|
245 | 245 | bool QAbstractBarSeries::append(QBarSet *set) |
|
246 | 246 | { |
|
247 | 247 | Q_D(QAbstractBarSeries); |
|
248 | 248 | bool success = d->append(set); |
|
249 | 249 | if (success) { |
|
250 | 250 | QList<QBarSet*> sets; |
|
251 | 251 | sets.append(set); |
|
252 | 252 | emit barsetsAdded(sets); |
|
253 | 253 | emit countChanged(); |
|
254 | 254 | } |
|
255 | 255 | return success; |
|
256 | 256 | } |
|
257 | 257 | |
|
258 | 258 | /*! |
|
259 | 259 | Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set. |
|
260 | 260 | Returns true, if set was removed. |
|
261 | 261 | */ |
|
262 | 262 | bool QAbstractBarSeries::remove(QBarSet *set) |
|
263 | 263 | { |
|
264 | 264 | Q_D(QAbstractBarSeries); |
|
265 | 265 | bool success = d->remove(set); |
|
266 | 266 | if (success) { |
|
267 | 267 | QList<QBarSet*> sets; |
|
268 | 268 | sets.append(set); |
|
269 | 269 | emit barsetsRemoved(sets); |
|
270 | 270 | emit countChanged(); |
|
271 | 271 | } |
|
272 | 272 | return success; |
|
273 | 273 | } |
|
274 | 274 | |
|
275 | 275 | /*! |
|
276 | 276 | Adds a list of barsets to series. Takes ownership of \a sets. |
|
277 | 277 | Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series, |
|
278 | 278 | nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended |
|
279 | 279 | and function returns false. |
|
280 | 280 | */ |
|
281 | 281 | bool QAbstractBarSeries::append(QList<QBarSet* > sets) |
|
282 | 282 | { |
|
283 | 283 | Q_D(QAbstractBarSeries); |
|
284 | 284 | bool success = d->append(sets); |
|
285 | 285 | if (success) { |
|
286 | 286 | emit barsetsAdded(sets); |
|
287 | 287 | emit countChanged(); |
|
288 | 288 | } |
|
289 | 289 | return success; |
|
290 | 290 | } |
|
291 | 291 | |
|
292 | 292 | /*! |
|
293 | 293 | Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended. |
|
294 | 294 | Returns true, if inserting succeeded. |
|
295 | 295 | |
|
296 | 296 | */ |
|
297 | 297 | bool QAbstractBarSeries::insert(int index, QBarSet *set) |
|
298 | 298 | { |
|
299 | 299 | Q_D(QAbstractBarSeries); |
|
300 | 300 | bool success = d->insert(index, set); |
|
301 | 301 | if (success) { |
|
302 | 302 | QList<QBarSet*> sets; |
|
303 | 303 | sets.append(set); |
|
304 | 304 | emit barsetsAdded(sets); |
|
305 | 305 | emit countChanged(); |
|
306 | 306 | } |
|
307 | 307 | return success; |
|
308 | 308 | } |
|
309 | 309 | |
|
310 | 310 | /*! |
|
311 | 311 | Removes all of the bar sets from the series |
|
312 | 312 | */ |
|
313 | 313 | void QAbstractBarSeries::clear() |
|
314 | 314 | { |
|
315 | 315 | Q_D(QAbstractBarSeries); |
|
316 | 316 | QList<QBarSet *> sets = barSets(); |
|
317 | 317 | bool success = d->remove(sets); |
|
318 | 318 | if (success) { |
|
319 | 319 | emit barsetsRemoved(sets); |
|
320 | 320 | emit countChanged(); |
|
321 | 321 | } |
|
322 | 322 | } |
|
323 | 323 | |
|
324 | 324 | /*! |
|
325 | 325 | Returns number of sets in series. |
|
326 | 326 | */ |
|
327 | 327 | int QAbstractBarSeries::count() const |
|
328 | 328 | { |
|
329 | 329 | Q_D(const QAbstractBarSeries); |
|
330 | 330 | return d->m_barSets.count(); |
|
331 | 331 | } |
|
332 | 332 | |
|
333 | 333 | /*! |
|
334 | 334 | Returns a list of sets in series. Keeps ownership of sets. |
|
335 | 335 | */ |
|
336 | 336 | QList<QBarSet*> QAbstractBarSeries::barSets() const |
|
337 | 337 | { |
|
338 | 338 | Q_D(const QAbstractBarSeries); |
|
339 | 339 | return d->m_barSets; |
|
340 | 340 | } |
|
341 | 341 | |
|
342 | 342 | /*! |
|
343 | 343 | Sets the visibility of labels in series to \a visible |
|
344 | 344 | */ |
|
345 | 345 | void QAbstractBarSeries::setLabelsVisible(bool visible) |
|
346 | 346 | { |
|
347 | 347 | Q_D(QAbstractBarSeries); |
|
348 | 348 | if (d->m_labelsVisible != visible) { |
|
349 | 349 | d->setLabelsVisible(visible); |
|
350 | 350 | emit labelsVisibleChanged(); |
|
351 | 351 | } |
|
352 | 352 | } |
|
353 | 353 | |
|
354 | 354 | /*! |
|
355 | 355 | Returns the visibility of labels |
|
356 | 356 | */ |
|
357 | 357 | bool QAbstractBarSeries::isLabelsVisible() const |
|
358 | 358 | { |
|
359 | 359 | Q_D(const QAbstractBarSeries); |
|
360 | 360 | return d->m_labelsVisible; |
|
361 | 361 | } |
|
362 | 362 | |
|
363 | 363 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
364 | 364 | |
|
365 | 365 | QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) : |
|
366 | 366 | QAbstractSeriesPrivate(q), |
|
367 | 367 | m_barWidth(0.5), // Default value is 50% of category width |
|
368 | 368 | m_labelsVisible(false), |
|
369 | 369 | m_visible(true) |
|
370 | 370 | { |
|
371 | 371 | } |
|
372 | 372 | |
|
373 | 373 | int QAbstractBarSeriesPrivate::categoryCount() const |
|
374 | 374 | { |
|
375 | 375 | // No categories defined. return count of longest set. |
|
376 | 376 | int count = 0; |
|
377 | 377 | for (int i=0; i<m_barSets.count(); i++) { |
|
378 | 378 | if (m_barSets.at(i)->count() > count) { |
|
379 | 379 | count = m_barSets.at(i)->count(); |
|
380 | 380 | } |
|
381 | 381 | } |
|
382 | 382 | |
|
383 | 383 | return count; |
|
384 | 384 | } |
|
385 | 385 | |
|
386 | 386 | void QAbstractBarSeriesPrivate::setBarWidth(qreal width) |
|
387 | 387 | { |
|
388 | 388 | if (width < 0.0) { |
|
389 | 389 | width = 0.0; |
|
390 | 390 | } |
|
391 | 391 | m_barWidth = width; |
|
392 | 392 | emit updatedBars(); |
|
393 | 393 | } |
|
394 | 394 | |
|
395 | 395 | qreal QAbstractBarSeriesPrivate::barWidth() const |
|
396 | 396 | { |
|
397 | 397 | return m_barWidth; |
|
398 | 398 | } |
|
399 | 399 | |
|
400 | 400 | QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index) |
|
401 | 401 | { |
|
402 | 402 | return m_barSets.at(index); |
|
403 | 403 | } |
|
404 | 404 | |
|
405 | 405 | void QAbstractBarSeriesPrivate::setVisible(bool visible) |
|
406 | 406 | { |
|
407 | 407 | m_visible = visible; |
|
408 | 408 | emit updatedBars(); |
|
409 | 409 | } |
|
410 | 410 | |
|
411 | 411 | void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible) |
|
412 | 412 | { |
|
413 | 413 | m_labelsVisible = visible; |
|
414 | 414 | emit labelsVisibleChanged(visible); |
|
415 | 415 | } |
|
416 | 416 | |
|
417 | 417 | qreal QAbstractBarSeriesPrivate::min() |
|
418 | 418 | { |
|
419 | 419 | if (m_barSets.count() <= 0) { |
|
420 | 420 | return 0; |
|
421 | 421 | } |
|
422 | 422 | qreal min = INT_MAX; |
|
423 | 423 | |
|
424 | 424 | for (int i = 0; i < m_barSets.count(); i++) { |
|
425 | 425 | int categoryCount = m_barSets.at(i)->count(); |
|
426 | 426 | for (int j = 0; j < categoryCount; j++) { |
|
427 | 427 | qreal temp = m_barSets.at(i)->at(j); |
|
428 | 428 | if (temp < min) |
|
429 | 429 | min = temp; |
|
430 | 430 | } |
|
431 | 431 | } |
|
432 | 432 | return min; |
|
433 | 433 | } |
|
434 | 434 | |
|
435 | 435 | qreal QAbstractBarSeriesPrivate::max() |
|
436 | 436 | { |
|
437 | 437 | if (m_barSets.count() <= 0) { |
|
438 | 438 | return 0; |
|
439 | 439 | } |
|
440 | 440 | qreal max = INT_MIN; |
|
441 | 441 | |
|
442 | 442 | for (int i = 0; i < m_barSets.count(); i++) { |
|
443 | 443 | int categoryCount = m_barSets.at(i)->count(); |
|
444 | 444 | for (int j = 0; j < categoryCount; j++) { |
|
445 | 445 | qreal temp = m_barSets.at(i)->at(j); |
|
446 | 446 | if (temp > max) |
|
447 | 447 | max = temp; |
|
448 | 448 | } |
|
449 | 449 | } |
|
450 | 450 | |
|
451 | 451 | return max; |
|
452 | 452 | } |
|
453 | 453 | |
|
454 | 454 | qreal QAbstractBarSeriesPrivate::valueAt(int set, int category) |
|
455 | 455 | { |
|
456 | 456 | if ((set < 0) || (set >= m_barSets.count())) { |
|
457 | 457 | // No set, no value. |
|
458 | 458 | return 0; |
|
459 | 459 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
460 | 460 | // No category, no value. |
|
461 | 461 | return 0; |
|
462 | 462 | } |
|
463 | 463 | |
|
464 | 464 | return m_barSets.at(set)->at(category); |
|
465 | 465 | } |
|
466 | 466 | |
|
467 | 467 | qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category) |
|
468 | 468 | { |
|
469 | 469 | if ((set < 0) || (set >= m_barSets.count())) { |
|
470 | 470 | // No set, no value. |
|
471 | 471 | return 0; |
|
472 | 472 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
473 | 473 | // No category, no value. |
|
474 | 474 | return 0; |
|
475 | 475 | } |
|
476 | 476 | |
|
477 | 477 | qreal value = m_barSets.at(set)->at(category); |
|
478 | 478 | qreal sum = categorySum(category); |
|
479 | 479 | if ( qFuzzyIsNull(sum) ) { |
|
480 | 480 | return 0; |
|
481 | 481 | } |
|
482 | 482 | |
|
483 | 483 | return value / sum; |
|
484 | 484 | } |
|
485 | 485 | |
|
486 | 486 | qreal QAbstractBarSeriesPrivate::categorySum(int category) |
|
487 | 487 | { |
|
488 | 488 | qreal sum(0); |
|
489 | 489 | int count = m_barSets.count(); // Count sets |
|
490 | 490 | for (int set = 0; set < count; set++) { |
|
491 | 491 | if (category < m_barSets.at(set)->count()) |
|
492 | 492 | sum += m_barSets.at(set)->at(category); |
|
493 | 493 | } |
|
494 | 494 | return sum; |
|
495 | 495 | } |
|
496 | 496 | |
|
497 | 497 | qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category) |
|
498 | 498 | { |
|
499 | 499 | qreal sum(0); |
|
500 | 500 | int count = m_barSets.count(); // Count sets |
|
501 | 501 | for (int set = 0; set < count; set++) { |
|
502 | 502 | if (category < m_barSets.at(set)->count()) |
|
503 | 503 | sum += qAbs(m_barSets.at(set)->at(category)); |
|
504 | 504 | } |
|
505 | 505 | return sum; |
|
506 | 506 | } |
|
507 | 507 | |
|
508 | 508 | qreal QAbstractBarSeriesPrivate::maxCategorySum() |
|
509 | 509 | { |
|
510 | 510 | qreal max = INT_MIN; |
|
511 | 511 | int count = categoryCount(); |
|
512 | 512 | for (int i = 0; i < count; i++) { |
|
513 | 513 | qreal sum = categorySum(i); |
|
514 | 514 | if (sum > max) |
|
515 | 515 | max = sum; |
|
516 | 516 | } |
|
517 | 517 | return max; |
|
518 | 518 | } |
|
519 | 519 | |
|
520 | 520 | qreal QAbstractBarSeriesPrivate::minX() |
|
521 | 521 | { |
|
522 | 522 | if (m_barSets.count() <= 0) { |
|
523 | 523 | return 0; |
|
524 | 524 | } |
|
525 | 525 | qreal min = INT_MAX; |
|
526 | 526 | |
|
527 | 527 | for (int i = 0; i < m_barSets.count(); i++) { |
|
528 | 528 | int categoryCount = m_barSets.at(i)->count(); |
|
529 | 529 | for (int j = 0; j < categoryCount; j++) { |
|
530 | 530 | qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x(); |
|
531 | 531 | if (temp < min) |
|
532 | 532 | min = temp; |
|
533 | 533 | } |
|
534 | 534 | } |
|
535 | 535 | return min; |
|
536 | 536 | } |
|
537 | 537 | |
|
538 | 538 | qreal QAbstractBarSeriesPrivate::maxX() |
|
539 | 539 | { |
|
540 | 540 | if (m_barSets.count() <= 0) { |
|
541 | 541 | return 0; |
|
542 | 542 | } |
|
543 | 543 | qreal max = INT_MIN; |
|
544 | 544 | |
|
545 | 545 | for (int i = 0; i < m_barSets.count(); i++) { |
|
546 | 546 | int categoryCount = m_barSets.at(i)->count(); |
|
547 | 547 | for (int j = 0; j < categoryCount; j++) { |
|
548 | 548 | qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x(); |
|
549 | 549 | if (temp > max) |
|
550 | 550 | max = temp; |
|
551 | 551 | } |
|
552 | 552 | } |
|
553 | 553 | |
|
554 | 554 | return max; |
|
555 | 555 | } |
|
556 | 556 | |
|
557 | 557 | |
|
558 | 558 | void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain) |
|
559 | 559 | { |
|
560 | 560 | qreal minX(domain.minX()); |
|
561 | 561 | qreal minY(domain.minY()); |
|
562 | 562 | qreal maxX(domain.maxX()); |
|
563 | 563 | qreal maxY(domain.maxY()); |
|
564 | 564 | int tickXCount(domain.tickXCount()); |
|
565 | 565 | int tickYCount(domain.tickYCount()); |
|
566 | 566 | |
|
567 | 567 | qreal seriesMinX = this->minX(); |
|
568 | 568 | qreal seriesMaxX = this->maxX(); |
|
569 | 569 | qreal y = max(); |
|
570 | 570 | minX = qMin(minX, seriesMinX - 0.5); |
|
571 | 571 | minY = qMin(minY, y); |
|
572 | 572 | maxX = qMax(maxX, seriesMaxX + 0.5); |
|
573 | 573 | maxY = qMax(maxY, y); |
|
574 | 574 | tickXCount = categoryCount()+1; |
|
575 | 575 | |
|
576 | 576 | domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); |
|
577 | 577 | } |
|
578 | 578 | |
|
579 | 579 | Chart* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter) |
|
580 | 580 | { |
|
581 | 581 | Q_Q(QAbstractBarSeries); |
|
582 | 582 | |
|
583 | 583 | BarChartItem* bar = new BarChartItem(q,presenter); |
|
584 | 584 | if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { |
|
585 | 585 | presenter->animator()->addAnimation(bar); |
|
586 | 586 | } |
|
587 | 587 | presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); |
|
588 | 588 | return bar; |
|
589 | 589 | |
|
590 | 590 | } |
|
591 | 591 | |
|
592 | 592 | QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* legend) |
|
593 | 593 | { |
|
594 | 594 | Q_Q(QAbstractBarSeries); |
|
595 | 595 | QList<LegendMarker*> markers; |
|
596 | 596 | foreach(QBarSet* set, q->barSets()) { |
|
597 | 597 | BarLegendMarker* marker = new BarLegendMarker(q,set,legend); |
|
598 | 598 | markers << marker; |
|
599 | 599 | } |
|
600 | 600 | |
|
601 | 601 | return markers; |
|
602 | 602 | } |
|
603 | 603 | |
|
604 | 604 | bool QAbstractBarSeriesPrivate::append(QBarSet *set) |
|
605 | 605 | { |
|
606 | 606 | Q_Q(QAbstractBarSeries); |
|
607 | 607 | if ((m_barSets.contains(set)) || (set == 0)) { |
|
608 | 608 | // Fail if set is already in list or set is null. |
|
609 | 609 | return false; |
|
610 | 610 | } |
|
611 | 611 | m_barSets.append(set); |
|
612 | 612 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
613 | 613 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
614 | 614 | emit restructuredBars(); // this notifies barchartitem |
|
615 | 615 | if (m_dataset) { |
|
616 | 616 | m_dataset->updateSeries(q); // this notifies legend |
|
617 | 617 | } |
|
618 | 618 | return true; |
|
619 | 619 | } |
|
620 | 620 | |
|
621 | 621 | bool QAbstractBarSeriesPrivate::remove(QBarSet *set) |
|
622 | 622 | { |
|
623 | 623 | Q_Q(QAbstractBarSeries); |
|
624 | 624 | if (!m_barSets.contains(set)) { |
|
625 | 625 | // Fail if set is not in list |
|
626 | 626 | return false; |
|
627 | 627 | } |
|
628 | 628 | m_barSets.removeOne(set); |
|
629 | 629 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
630 | 630 | QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
631 | 631 | emit restructuredBars(); // this notifies barchartitem |
|
632 | 632 | if (m_dataset) { |
|
633 | 633 | m_dataset->updateSeries(q); // this notifies legend |
|
634 | 634 | } |
|
635 | 635 | return true; |
|
636 | 636 | } |
|
637 | 637 | |
|
638 | 638 | bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets) |
|
639 | 639 | { |
|
640 | 640 | Q_Q(QAbstractBarSeries); |
|
641 | 641 | foreach (QBarSet* set, sets) { |
|
642 | 642 | if ((set == 0) || (m_barSets.contains(set))) { |
|
643 | 643 | // Fail if any of the sets is null or is already appended. |
|
644 | 644 | return false; |
|
645 | 645 | } |
|
646 | 646 | if (sets.count(set) != 1) { |
|
647 | 647 | // Also fail if same set is more than once in given list. |
|
648 | 648 | return false; |
|
649 | 649 | } |
|
650 | 650 | } |
|
651 | 651 | |
|
652 | 652 | foreach (QBarSet* set, sets) { |
|
653 | 653 | m_barSets.append(set); |
|
654 | 654 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
655 | 655 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
656 | 656 | } |
|
657 | 657 | emit restructuredBars(); // this notifies barchartitem |
|
658 | 658 | if (m_dataset) { |
|
659 | 659 | m_dataset->updateSeries(q); // this notifies legend |
|
660 | 660 | } |
|
661 | 661 | return true; |
|
662 | 662 | } |
|
663 | 663 | |
|
664 | 664 | bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets) |
|
665 | 665 | { |
|
666 | 666 | Q_Q(QAbstractBarSeries); |
|
667 | 667 | if (sets.count() == 0) { |
|
668 | 668 | return false; |
|
669 | 669 | } |
|
670 | 670 | foreach (QBarSet* set, sets) { |
|
671 | 671 | if ((set == 0) || (!m_barSets.contains(set))) { |
|
672 | 672 | // Fail if any of the sets is null or is not in series |
|
673 | 673 | return false; |
|
674 | 674 | } |
|
675 | 675 | if (sets.count(set) != 1) { |
|
676 | 676 | // Also fail if same set is more than once in given list. |
|
677 | 677 | return false; |
|
678 | 678 | } |
|
679 | 679 | } |
|
680 | 680 | |
|
681 | 681 | foreach (QBarSet* set, sets) { |
|
682 | 682 | m_barSets.removeOne(set); |
|
683 | 683 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
684 | 684 | QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
685 | 685 | } |
|
686 | 686 | |
|
687 | 687 | emit restructuredBars(); // this notifies barchartitem |
|
688 | 688 | if (m_dataset) { |
|
689 | 689 | m_dataset->updateSeries(q); // this notifies legend |
|
690 | 690 | } |
|
691 | 691 | return true; |
|
692 | 692 | } |
|
693 | 693 | |
|
694 | 694 | bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set) |
|
695 | 695 | { |
|
696 | 696 | Q_Q(QAbstractBarSeries); |
|
697 | 697 | if ((m_barSets.contains(set)) || (set == 0)) { |
|
698 | 698 | // Fail if set is already in list or set is null. |
|
699 | 699 | return false; |
|
700 | 700 | } |
|
701 | 701 | m_barSets.insert(index, set); |
|
702 | 702 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
703 | 703 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
704 | 704 | emit restructuredBars(); // this notifies barchartitem |
|
705 | 705 | if (m_dataset) { |
|
706 | 706 | m_dataset->updateSeries(q); // this notifies legend |
|
707 | 707 | } |
|
708 | 708 | return true; |
|
709 | 709 | } |
|
710 | 710 | |
|
711 | 711 | void QAbstractBarSeriesPrivate::initializeAxisX(QAbstractAxis* axis) |
|
712 | 712 | { |
|
713 | Q_UNUSED(axis); | |
|
713 | if(axis->type()==QAbstractAxis::AxisTypeCategories) | |
|
714 | { | |
|
715 | QCategoriesAxis* cataxis = qobject_cast<QCategoriesAxis*>(axis); | |
|
716 | Q_ASSERT(cataxis); | |
|
717 | QStringList categories; | |
|
718 | for (int i(1); i < categoryCount()+1; i++) | |
|
719 | categories << QString::number(i); | |
|
720 | cataxis->append(categories); | |
|
721 | } | |
|
714 | 722 | } |
|
715 | 723 | |
|
716 | 724 | void QAbstractBarSeriesPrivate::initializeAxisY(QAbstractAxis* axis) |
|
717 | 725 | { |
|
718 | 726 | Q_UNUSED(axis) |
|
719 | 727 | } |
|
720 | 728 | |
|
721 | 729 | QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisXType() const |
|
722 | 730 | { |
|
723 | 731 | return QAbstractAxis::AxisTypeCategories; |
|
724 | 732 | } |
|
725 | 733 | |
|
726 | 734 | QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisYType() const |
|
727 | 735 | { |
|
728 | 736 | return QAbstractAxis::AxisTypeValues; |
|
729 | 737 | } |
|
730 | 738 | |
|
731 | 739 | #include "moc_qabstractbarseries.cpp" |
|
732 | 740 | #include "moc_qabstractbarseries_p.cpp" |
|
733 | 741 | |
|
734 | 742 | |
|
735 | 743 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now