Debian Codegraph @

tests/manual/barcharttester/chart-widget.cpp

raw
1 // Copyright (C) 2021 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "chart-widget.h"
5
6 #include <QtCharts/QLineSeries>
7 #include <QtCharts/QLegend>
8 #include <QtCharts/QBarCategoryAxis>
9 #include <QtCharts/QValueAxis>
10 #include <QtCharts/QLogValueAxis>
11 #include <QtCharts/QBarSet>
12 #include <QtCharts/QHorizontalBarSeries>
13 #include <QtCharts/QHorizontalPercentBarSeries>
14 #include <QtCharts/QHorizontalStackedBarSeries>
15 #include <QtCharts/QPercentBarSeries>
16 #include <QtCharts/QStackedBarSeries>
17 #include <QElapsedTimer>
18 #include <QDebug>
19
20 //#define VALUE_LOGGING 1
21
22 const int initialCount = 100000;
23 const int visibleCount = 20;
24 const int storeCount = 100000000;
25 const int interval = 500;
26
27 const int initialSeriesCount = 2;
28 const int maxSeriesCount = 2;
29 const bool removeSeries = false;
30 const int extraSeriesFrequency = 7;
31
32 const int initialSetCount = 3;
33 const int maxSetCount = 3;
34 const bool removeSets = false;
35 const int extraSetFrequency = 3;
36
37 const bool initialLabels = true;
38 const int labelsTrigger = -1;
39 const int visibleTrigger = -1;
40 const int appendFrequency = 1;
41 const int animationTrigger = -1;
42 const bool sameNumberOfBars = false;
43
44 const bool animation = true;
45 const int animationDuration = 300;
46
47 const bool horizontal = false;
48 const bool percent = false;
49 const bool stacked = true;
50
51 const bool negativeValues = false;
52 const bool mixedValues = false;
53
54 // Negative indexes are counted from end of the set.
55 const bool doReplace = false;
56 const bool doRemove = false;
57 const bool doInsert = false;
58 const bool singleReplace = false;
59 const bool singleRemove = false;
60 const bool singleInsert = false;
61 const int removeIndex = -7;
62 const int replaceIndex = -3;
63 const int insertIndex = -5;
64
65 const bool logarithmic = false;
66 const bool barCategories = false;
67
68 const qreal barWidth = 0.9;
69
70 // Note: reverse axes are not fully supported for bars (animation and label positioning break a bit)
71 const bool reverseBar = false;
72 const bool reverseValue = false;
73
74 static int counter = 1;
75 static const QString nameTemplate = QStringLiteral("Set %1/%2");
76
77 ChartWidget::ChartWidget(QWidget *parent) :
78 QWidget(parent),
79 m_chart(new QChart()),
80 m_chartView(new QChartView(this)),
81 m_setCount(initialSetCount),
82 m_seriesCount(initialSeriesCount),
83 m_extraScroll(0.0)
84 {
85 m_elapsedTimer.start();
86
87 if (logarithmic) {
88 QLogValueAxis *logAxis = new QLogValueAxis;
89 logAxis->setBase(2);
90 m_valueAxis = logAxis;
91 } else {
92 m_valueAxis = new QValueAxis;
93 }
94
95 if (barCategories)
96 m_barAxis = new QBarCategoryAxis;
97 else
98 m_barAxis = new QValueAxis;
99
100 m_barAxis->setReverse(reverseBar);
101 m_valueAxis->setReverse(reverseValue);
102
103 for (int i = 0; i < maxSeriesCount; i++) {
104 if (horizontal) {
105 if (percent)
106 m_series.append(new QHorizontalPercentBarSeries);
107 else if (stacked)
108 m_series.append(new QHorizontalStackedBarSeries);
109 else
110 m_series.append(new QHorizontalBarSeries);
111 } else {
112 if (percent)
113 m_series.append(new QPercentBarSeries);
114 else if (stacked)
115 m_series.append(new QStackedBarSeries);
116 else
117 m_series.append(new QBarSeries);
118 }
119 QAbstractBarSeries *series =
120 qobject_cast<QAbstractBarSeries *>(m_series.at(m_series.size() - 1));
121 QString seriesNameTemplate = QStringLiteral("bar %1");
122 series->setName(seriesNameTemplate.arg(i));
123 series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd);
124 series->setLabelsVisible(initialLabels);
125 series->setBarWidth(barWidth);
126 }
127
128 resize(800, 300);
129 m_horizontalLayout = new QHBoxLayout(this);
130 m_horizontalLayout->setSpacing(6);
131 m_horizontalLayout->setContentsMargins(11, 11, 11, 11);
132 m_horizontalLayout->addWidget(m_chartView);
133
134 qDebug("UI setup time: %lld", m_elapsedTimer.restart());
135
136 m_chartView->setRenderHint(QPainter::Antialiasing);
137
138 createChart();
139 qDebug("Chart creation time: %lld", m_elapsedTimer.restart());
140
141 QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
142
143 m_timer.setInterval(interval);
144 m_timer.start();
145 }
146
147 ChartWidget::~ChartWidget()
148 {
149 }
150
151 void ChartWidget::handleTimeout()
152 {
153 qDebug() << "Intervening time:" << m_elapsedTimer.restart();
154
155 qDebug() << "----------" << counter << "----------";
156
157 bool doScroll = false;
158
159 if (counter % appendFrequency == 0) {
160 for (int i = 0; i < maxSeriesCount; i++) {
161 for (int j = 0; j < maxSetCount; j++) {
162 QBarSet *set = m_sets.value(m_series.at(i)).at(j);
163 qreal value = 5 * i + (counter % 100) / qreal(j+1);
164 if (negativeValues)
165 set->append(-value);
166 else if (mixedValues)
167 set->append(counter % 2 ? value : -value);
168 else
169 set->append(value);
170 #ifdef VALUE_LOGGING
171 qDebug() << "Appended value:" << set->at(set->count() - 1)
172 << "to series:" << i
173 << "to set:" << j
174 << "at index:" << set->count() - 1;
175 #endif
176 if (set->count() > storeCount)
177 set->remove(0, set->count() - storeCount);
178 if (set->count() > visibleCount)
179 doScroll = true;
180 }
181 }
182 qDebug() << "Append time:" << m_elapsedTimer.restart();
183 }
184
185 if (doScroll) {
186 doScroll = false;
187 qreal scrollAmount = horizontal ? m_chart->plotArea().height() / visibleCount
188 : m_chart->plotArea().width() / visibleCount;
189 // Charts can't scroll without any series, so store the required scroll in those cases
190 if (m_seriesCount == 0) {
191 m_extraScroll += scrollAmount;
192 } else {
193 if (horizontal)
194 m_chart->scroll(0, scrollAmount + m_extraScroll);
195 else
196 m_chart->scroll(scrollAmount + m_extraScroll, 0);
197 m_extraScroll = 0.0;
198 }
199 qDebug() << "Scroll time:" << m_elapsedTimer.restart();
200 }
201
202 if (doRemove || doReplace || doInsert) {
203 for (int i = 0; i < maxSeriesCount; i++) {
204 for (int j = 0; j < m_setCount; j++) {
205 QBarSet *set = m_sets.value(m_series.at(i)).at(j);
206 qreal value = ((counter + 20) % 100) / qreal(j + 1);
207 if (doReplace && (!singleReplace || j == 0)) {
208 int index = replaceIndex < 0 ? set->count() + replaceIndex : replaceIndex;
209 set->replace(index, value);
210 }
211 if (doRemove && (!singleRemove || j == 0)) {
212 int index = removeIndex < 0 ? set->count() + removeIndex : removeIndex;
213 set->remove(index, 1);
214 }
215 if (doInsert && (!singleInsert || j == 0)) {
216 int index = insertIndex < 0 ? set->count() + insertIndex : insertIndex;
217 set->insert(index, value);
218 }
219 }
220 }
221 qDebug() << "R/R time:" << m_elapsedTimer.restart();
222 }
223
224 if (counter % extraSeriesFrequency == 0) {
225 if (m_seriesCount <= maxSeriesCount) {
226 qDebug() << "Adjusting series count, current count:" << m_seriesCount;
227 static int seriesCountAdder = 1;
228 if (m_seriesCount == maxSeriesCount) {
229 if (removeSeries)
230 seriesCountAdder = -1;
231 else
232 seriesCountAdder = 0;
233 } else if (m_seriesCount == 0) {
234 seriesCountAdder = 1;
235 }
236 if (seriesCountAdder < 0)
237 m_chart->removeSeries(m_series.at(m_seriesCount - 1));
238 else if (m_seriesCount < maxSeriesCount)
239 addSeriesToChart(m_series.at(m_seriesCount));
240 m_seriesCount += seriesCountAdder;
241 }
242 }
243
244 if (counter % extraSetFrequency == 0) {
245 if (m_setCount <= maxSetCount) {
246 qDebug() << "Adjusting setcount, current count:" << m_setCount;
247 static int setCountAdder = 1;
248 if (m_setCount == maxSetCount) {
249 if (removeSets)
250 setCountAdder = -1;
251 else
252 setCountAdder = 0;
253 } else if (m_setCount == 0) {
254 setCountAdder = 1;
255 }
256 for (int i = 0; i < maxSeriesCount; i++) {
257 if (setCountAdder < 0) {
258 int barCount = m_sets.value(m_series.at(i)).at(m_setCount - 1)->count();
259 m_series.at(i)->remove(m_sets.value(m_series.at(i)).at(m_setCount - 1));
260 // Since remove deletes the set, recreate it in our list
261 QBarSet *set = new QBarSet(nameTemplate.arg(i).arg(m_setCount - 1));
262 m_sets[m_series.at(i)][m_setCount - 1] = set;
263 set->setLabelBrush(QColor("black"));
264 set->setPen(QPen(QColor("black"), 0.3));
265 QList<qreal> valueList;
266 valueList.reserve(barCount);
267 for (int j = 0; j < barCount; ++j) {
268 qreal value = counter % 100;
269 if (negativeValues)
270 valueList.append(-value);
271 else if (mixedValues)
272 valueList.append(counter % 2 ? value : -value);
273 else
274 valueList.append(value);
275 }
276 set->append(valueList);
277
278 } else if (m_setCount < maxSetCount) {
279 m_series.at(i)->append(m_sets.value(m_series.at(i)).at(m_setCount));
280 }
281 }
282 m_setCount += setCountAdder;
283 }
284 }
285
286 if (labelsTrigger > 0 && counter % labelsTrigger == 0) {
287 m_series.at(0)->setLabelsVisible(!m_series.at(0)->isLabelsVisible());
288 qDebug() << "Label visibility changed";
289 }
290
291 if (visibleTrigger > 0 && counter % visibleTrigger == 0) {
292 m_series.at(0)->setVisible(!m_series.at(0)->isVisible());
293 qDebug() << "Series visibility changed";
294 }
295
296 if (animationTrigger > 0 && counter % animationTrigger == 0) {
297 if (m_chart->animationOptions() == QChart::SeriesAnimations)
298 m_chart->setAnimationOptions(QChart::NoAnimation);
299 else
300 m_chart->setAnimationOptions(QChart::SeriesAnimations);
301 qDebug() << "Series animation changed";
302 }
303
304 qDebug() << "Rest of time:" << m_elapsedTimer.restart();
305
306 qDebug() << "GraphicsItem Count:" << m_chart->scene()->items().size();
307 counter++;
308 }
309
310 void ChartWidget::createChart()
311 {
312 qDebug() << "Initial bar count:" << initialCount;
313
314 QList<qreal> valueList;
315 valueList.reserve(initialCount);
316 for (int j = 0; j < initialCount; ++j) {
317 qreal value = counter++ % 100;
318 if (negativeValues)
319 valueList.append(-value);
320 else if (mixedValues)
321 valueList.append(counter % 2 ? value : -value);
322 else
323 valueList.append(value);
324 }
325
326 for (int i = 0; i < maxSeriesCount; i++) {
327 for (int j = 0; j < maxSetCount; j++) {
328 QBarSet *set = new QBarSet(nameTemplate.arg(i).arg(j));
329 m_sets[m_series.at(i)].append(set);
330 set->setLabelBrush(QColor("black"));
331 set->setPen(QPen(QColor("black"), 0.3));
332 if (sameNumberOfBars) {
333 set->append(valueList);
334 } else {
335 QList<qreal> tempList = valueList;
336 for (int k = 0; k < j; k++)
337 tempList.removeLast();
338 set->append(tempList);
339 }
340 if (j < m_setCount)
341 m_series.at(i)->append(set);
342 }
343 }
344 for (int i = 0; i < initialSeriesCount; i++)
345 addSeriesToChart(m_series.at(i));
346
347 m_chart->setTitle("Chart");
348 if (animation) {
349 m_chart->setAnimationOptions(QChart::SeriesAnimations);
350 m_chart->setAnimationDuration(animationDuration);
351 }
352
353 if (barCategories) {
354 QBarCategoryAxis *barCatAxis = qobject_cast<QBarCategoryAxis *>(m_barAxis);
355 QStringList categories;
356 const int count = qMax(initialCount, visibleCount);
357 for (int i = 0; i < count; i++)
358 categories.append(QString::number(i));
359 barCatAxis->setCategories(categories);
360 } else {
361 qobject_cast<QValueAxis *>(m_barAxis)->setTickCount(11);
362 }
363
364 if (initialCount > visibleCount)
365 m_barAxis->setRange(initialCount - visibleCount, initialCount);
366 else
367 m_barAxis->setRange(0, visibleCount);
368
369 qreal rangeValue = stacked ? 200.0 : 100.0;
370 m_valueAxis->setRange(logarithmic ? 1.0 : (negativeValues || mixedValues) ? -rangeValue : 0.0,
371 (!negativeValues || mixedValues) ? rangeValue : 0.0);
372
373 m_chartView->setChart(m_chart);
374 }
375
376 void ChartWidget::addSeriesToChart(QAbstractBarSeries *series)
377 {
378 qDebug() << "Adding series:" << series->name();
379
380 // HACK: Temporarily take the sets out of the series until axes are set.
381 // This is done because added series defaults to a domain that displays all bars, which can
382 // get extremely slow as the bar count increases.
383 QList<QBarSet *> sets = series->barSets();
384 for (auto set : sets)
385 series->take(set);
386
387 m_chart->addSeries(series);
388 if (horizontal) {
389 m_chart->setAxisX(m_valueAxis,series);
390 m_chart->setAxisY(m_barAxis, series);
391 } else {
392 m_chart->setAxisX(m_barAxis, series);
393 m_chart->setAxisY(m_valueAxis, series);
394 }
395
396 series->append(sets);
397 }