| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses |
| 5 |
|
| 6 |
#include "mainwindow.h" |
| 7 |
#include "chartview.h" |
| 8 |
#include <QtCharts/QScatterSeries> |
| 9 |
#include <QtCharts/QLineSeries> |
| 10 |
#include <QtCharts/QValueAxis> |
| 11 |
#include <QtCharts/QLogValueAxis> |
| 12 |
#include <QtCharts/QDateTimeAxis> |
| 13 |
#include <QtCharts/QCategoryAxis> |
| 14 |
#include <QtCharts/QChart> |
| 15 |
#include <QtCore/QRandomGenerator> |
| 16 |
#include <QtCore/QDebug> |
| 17 |
#include <QtCore/QDateTime> |
| 18 |
|
| 19 |
QT_USE_NAMESPACE |
| 20 |
#include "ui_mainwindow.h" |
| 21 |
|
| 22 |
MainWindow::MainWindow(QWidget *parent) : |
| 23 |
QMainWindow(parent), |
| 24 |
ui(new Ui::MainWindow), |
| 25 |
m_xMin(0.0), |
| 26 |
m_xMax(20.0), |
| 27 |
m_yMin(0.0), |
| 28 |
m_yMax(10.0), |
| 29 |
m_backgroundBrush(new QBrush(Qt::white)), |
| 30 |
m_plotAreaBackgroundBrush(new QBrush(Qt::NoBrush)), |
| 31 |
m_backgroundPen(new QPen(Qt::NoPen)), |
| 32 |
m_plotAreaBackgroundPen(new QPen(Qt::NoPen)), |
| 33 |
m_animationOptions(QChart::NoAnimation), |
| 34 |
m_chart(0), |
| 35 |
m_xAxis(0), |
| 36 |
m_yAxis(0), |
| 37 |
m_xAxisMode(AxisModeValue), |
| 38 |
m_yAxisMode(AxisModeValue), |
| 39 |
m_pointCount(100) |
| 40 |
{ |
| 41 |
ui->setupUi(this); |
| 42 |
|
| 43 |
ui->yMinSpin->setValue(m_yMin); |
| 44 |
ui->yMaxSpin->setValue(m_yMax); |
| 45 |
ui->xMinSpin->setValue(m_xMin); |
| 46 |
ui->xMaxSpin->setValue(m_xMax); |
| 47 |
|
| 48 |
initXYValueChart(); |
| 49 |
setXAxis(AxisModeValue); |
| 50 |
setYAxis(AxisModeValue); |
| 51 |
|
| 52 |
connect(ui->yMinSpin, SIGNAL(valueChanged(double)), |
| 53 |
this, SLOT(yMinChanged(double))); |
| 54 |
connect(ui->yMaxSpin, SIGNAL(valueChanged(double)), |
| 55 |
this, SLOT(yMaxChanged(double))); |
| 56 |
connect(ui->xMinSpin, SIGNAL(valueChanged(double)), |
| 57 |
this, SLOT(xMinChanged(double))); |
| 58 |
connect(ui->xMaxSpin, SIGNAL(valueChanged(double)), |
| 59 |
this, SLOT(xMaxChanged(double))); |
| 60 |
connect(ui->animationsComboBox, SIGNAL(currentIndexChanged(int)), |
| 61 |
this, SLOT(animationIndexChanged(int))); |
| 62 |
connect(ui->xAxisComboBox, SIGNAL(currentIndexChanged(int)), |
| 63 |
this, SLOT(xAxisIndexChanged(int))); |
| 64 |
connect(ui->yAxisComboBox, SIGNAL(currentIndexChanged(int)), |
| 65 |
this, SLOT(yAxisIndexChanged(int))); |
| 66 |
connect(ui->backgroundComboBox, SIGNAL(currentIndexChanged(int)), |
| 67 |
this, SLOT(backgroundIndexChanged(int))); |
| 68 |
connect(ui->plotAreaComboBox, SIGNAL(currentIndexChanged(int)), |
| 69 |
this, SLOT(plotAreaIndexChanged(int))); |
| 70 |
connect(ui->themeComboBox, SIGNAL(currentIndexChanged(int)), |
| 71 |
this, SLOT(themeIndexChanged(int))); |
| 72 |
connect(ui->addSeriesButton, SIGNAL(clicked(bool)), |
| 73 |
this, SLOT(addSeriesClicked())); |
| 74 |
connect(ui->addGLSeriesButton, SIGNAL(clicked(bool)), |
| 75 |
this, SLOT(addGLSeriesClicked())); |
| 76 |
connect(ui->removeSeriesButton, SIGNAL(clicked(bool)), |
| 77 |
this, SLOT(removeSeriesClicked())); |
| 78 |
connect(ui->countComboBox, SIGNAL(currentIndexChanged(int)), |
| 79 |
this, SLOT(countIndexChanged(int))); |
| 80 |
connect(ui->colorsComboBox, SIGNAL(currentIndexChanged(int)), |
| 81 |
this, SLOT(colorIndexChanged(int))); |
| 82 |
connect(ui->widthComboBox, SIGNAL(currentIndexChanged(int)), |
| 83 |
this, SLOT(widthIndexChanged(int))); |
| 84 |
connect(ui->antiAliasCheckBox, SIGNAL(clicked(bool)), |
| 85 |
this, SLOT(antiAliasCheckBoxClicked(bool))); |
| 86 |
connect(ui->intervalSpinbox, SIGNAL(valueChanged(int)), |
| 87 |
&m_dataSource, SLOT(setInterval(int))); |
| 88 |
|
| 89 |
ui->chartView->setChart(m_chart); |
| 90 |
ui->chartView->setRenderHint(QPainter::Antialiasing); |
| 91 |
|
| 92 |
QObject::connect(m_chart->scene(), &QGraphicsScene::changed, |
| 93 |
&m_dataSource, &DataSource::handleSceneChanged); |
| 94 |
|
| 95 |
m_dataSource.startUpdates(m_seriesList, ui->fpsLabel, ui->intervalSpinbox->value()); |
| 96 |
} |
| 97 |
|
| 98 |
MainWindow::~MainWindow() |
| 99 |
{ |
| 100 |
delete ui; |
| 101 |
} |
| 102 |
|
| 103 |
void MainWindow::initXYValueChart() |
| 104 |
{ |
| 105 |
m_chart = new QChart(); |
| 106 |
m_chart->setTitle("Use arrow keys to scroll and +/- to zoom"); |
| 107 |
m_chart->setAnimationOptions(m_animationOptions); |
| 108 |
m_chart->setBackgroundBrush(*m_backgroundBrush); |
| 109 |
m_chart->setBackgroundPen(*m_backgroundPen); |
| 110 |
m_chart->setPlotAreaBackgroundBrush(*m_plotAreaBackgroundBrush); |
| 111 |
m_chart->setPlotAreaBackgroundPen(*m_plotAreaBackgroundPen); |
| 112 |
} |
| 113 |
|
| 114 |
void MainWindow::setXAxis(MainWindow::AxisMode mode) |
| 115 |
{ |
| 116 |
if (m_xAxis) { |
| 117 |
m_chart->removeAxis(m_xAxis); |
| 118 |
delete m_xAxis; |
| 119 |
m_xAxis = 0; |
| 120 |
} |
| 121 |
|
| 122 |
m_xAxisMode = mode; |
| 123 |
|
| 124 |
switch (m_xAxisMode) { |
| 125 |
case AxisModeNone: |
| 126 |
return; |
| 127 |
case AxisModeValue: |
| 128 |
m_xAxis = new QValueAxis(); |
| 129 |
break; |
| 130 |
case AxisModeLogValue: |
| 131 |
m_xAxis = new QLogValueAxis(); |
| 132 |
break; |
| 133 |
case AxisModeDateTime: |
| 134 |
m_xAxis = new QDateTimeAxis(); |
| 135 |
break; |
| 136 |
case AxisModeCategory: |
| 137 |
m_xAxis = new QCategoryAxis(); |
| 138 |
applyCategories(); |
| 139 |
break; |
| 140 |
default: |
| 141 |
qWarning() << "Unsupported AxisMode"; |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
m_chart->addAxis(m_xAxis, Qt::AlignBottom); |
| 146 |
|
| 147 |
foreach (QAbstractSeries *series, m_seriesList) |
| 148 |
series->attachAxis(m_xAxis); |
| 149 |
|
| 150 |
applyRanges(); |
| 151 |
} |
| 152 |
|
| 153 |
void MainWindow::setYAxis(MainWindow::AxisMode mode) |
| 154 |
{ |
| 155 |
if (m_yAxis) { |
| 156 |
m_chart->removeAxis(m_yAxis); |
| 157 |
delete m_yAxis; |
| 158 |
m_yAxis = 0; |
| 159 |
} |
| 160 |
|
| 161 |
m_yAxisMode = mode; |
| 162 |
|
| 163 |
switch (m_yAxisMode) { |
| 164 |
case AxisModeNone: |
| 165 |
return; |
| 166 |
case AxisModeValue: |
| 167 |
m_yAxis = new QValueAxis(); |
| 168 |
break; |
| 169 |
case AxisModeLogValue: |
| 170 |
m_yAxis = new QLogValueAxis(); |
| 171 |
break; |
| 172 |
case AxisModeDateTime: |
| 173 |
m_yAxis = new QDateTimeAxis(); |
| 174 |
break; |
| 175 |
case AxisModeCategory: |
| 176 |
m_yAxis = new QCategoryAxis(); |
| 177 |
applyCategories(); |
| 178 |
break; |
| 179 |
default: |
| 180 |
qWarning() << "Unsupported AxisMode"; |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
m_chart->addAxis(m_yAxis, Qt::AlignLeft); |
| 185 |
|
| 186 |
foreach (QAbstractSeries *series, m_seriesList) |
| 187 |
series->attachAxis(m_yAxis); |
| 188 |
|
| 189 |
applyRanges(); |
| 190 |
} |
| 191 |
|
| 192 |
void MainWindow::applyRanges() |
| 193 |
{ |
| 194 |
if (m_xAxis) { |
| 195 |
if (m_xAxisMode == AxisModeLogValue) { |
| 196 |
if (m_xMin <= 0) |
| 197 |
m_xMin = 1.0; |
| 198 |
if (m_xMax <= m_xMin) |
| 199 |
m_xMax = m_xMin + 1.0; |
| 200 |
} |
| 201 |
if (m_xAxisMode == AxisModeDateTime) { |
| 202 |
QDateTime dateTimeMin; |
| 203 |
QDateTime dateTimeMax; |
| 204 |
dateTimeMin.setMSecsSinceEpoch(qint64(m_xMin)); |
| 205 |
dateTimeMax.setMSecsSinceEpoch(qint64(m_xMax)); |
| 206 |
m_xAxis->setRange(dateTimeMin, dateTimeMax); |
| 207 |
} else { |
| 208 |
m_xAxis->setRange(m_xMin, m_xMax); |
| 209 |
} |
| 210 |
ui->xMinSpin->setValue(m_xMin); |
| 211 |
ui->xMaxSpin->setValue(m_xMax); |
| 212 |
} |
| 213 |
if (m_yAxis) { |
| 214 |
if (m_yAxisMode == AxisModeLogValue) { |
| 215 |
if (m_yMin <= 0) |
| 216 |
m_yMin = 1.0; |
| 217 |
if (m_yMax <= m_yMin) |
| 218 |
m_yMax = m_yMin + 1.0; |
| 219 |
} |
| 220 |
if (m_yAxisMode == AxisModeDateTime) { |
| 221 |
QDateTime dateTimeMin; |
| 222 |
QDateTime dateTimeMax; |
| 223 |
dateTimeMin.setMSecsSinceEpoch(qint64(m_yMin)); |
| 224 |
dateTimeMax.setMSecsSinceEpoch(qint64(m_yMax)); |
| 225 |
m_yAxis->setRange(dateTimeMin, dateTimeMax); |
| 226 |
} else { |
| 227 |
m_yAxis->setRange(m_yMin, m_yMax); |
| 228 |
} |
| 229 |
ui->yMinSpin->setValue(m_yMin); |
| 230 |
ui->yMaxSpin->setValue(m_yMax); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
void MainWindow::xMinChanged(double value) |
| 235 |
{ |
| 236 |
m_xMin = value; |
| 237 |
applyRanges(); |
| 238 |
} |
| 239 |
|
| 240 |
void MainWindow::xMaxChanged(double value) |
| 241 |
{ |
| 242 |
m_xMax = value; |
| 243 |
applyRanges(); |
| 244 |
} |
| 245 |
|
| 246 |
void MainWindow::yMinChanged(double value) |
| 247 |
{ |
| 248 |
m_yMin = value; |
| 249 |
applyRanges(); |
| 250 |
} |
| 251 |
|
| 252 |
void MainWindow::yMaxChanged(double value) |
| 253 |
{ |
| 254 |
m_yMax = value; |
| 255 |
applyRanges(); |
| 256 |
} |
| 257 |
|
| 258 |
void MainWindow::animationIndexChanged(int index) |
| 259 |
{ |
| 260 |
switch (index) { |
| 261 |
case 0: |
| 262 |
m_animationOptions = QChart::NoAnimation; |
| 263 |
break; |
| 264 |
case 1: |
| 265 |
m_animationOptions = QChart::SeriesAnimations; |
| 266 |
break; |
| 267 |
case 2: |
| 268 |
m_animationOptions = QChart::GridAxisAnimations; |
| 269 |
break; |
| 270 |
case 3: |
| 271 |
m_animationOptions = QChart::AllAnimations; |
| 272 |
break; |
| 273 |
default: |
| 274 |
break; |
| 275 |
} |
| 276 |
|
| 277 |
m_chart->setAnimationOptions(m_animationOptions); |
| 278 |
} |
| 279 |
|
| 280 |
void MainWindow::xRangeChanged(qreal min, qreal max) |
| 281 |
{ |
| 282 |
if (!qFuzzyCompare(qreal(ui->xMinSpin->value()), min)) |
| 283 |
ui->xMinSpin->setValue(min); |
| 284 |
if (!qFuzzyCompare(qreal(ui->xMaxSpin->value()), max)) |
| 285 |
ui->xMaxSpin->setValue(max); |
| 286 |
} |
| 287 |
|
| 288 |
void MainWindow::yRangeChanged(qreal min, qreal max) |
| 289 |
{ |
| 290 |
if (!qFuzzyCompare(qreal(ui->yMinSpin->value()), min)) |
| 291 |
ui->yMinSpin->setValue(min); |
| 292 |
if (!qFuzzyCompare(qreal(ui->yMaxSpin->value()), max)) |
| 293 |
ui->yMaxSpin->setValue(max); |
| 294 |
} |
| 295 |
|
| 296 |
void MainWindow::xAxisIndexChanged(int index) |
| 297 |
{ |
| 298 |
switch (index) { |
| 299 |
case 0: |
| 300 |
setXAxis(AxisModeNone); |
| 301 |
break; |
| 302 |
case 1: |
| 303 |
setXAxis(AxisModeValue); |
| 304 |
break; |
| 305 |
case 2: |
| 306 |
setXAxis(AxisModeLogValue); |
| 307 |
break; |
| 308 |
case 3: |
| 309 |
setXAxis(AxisModeDateTime); |
| 310 |
break; |
| 311 |
case 4: |
| 312 |
setXAxis(AxisModeCategory); |
| 313 |
break; |
| 314 |
default: |
| 315 |
qWarning("Invalid Index!"); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
void MainWindow::yAxisIndexChanged(int index) |
| 320 |
{ |
| 321 |
switch (index) { |
| 322 |
case 0: |
| 323 |
setYAxis(AxisModeNone); |
| 324 |
break; |
| 325 |
case 1: |
| 326 |
setYAxis(AxisModeValue); |
| 327 |
break; |
| 328 |
case 2: |
| 329 |
setYAxis(AxisModeLogValue); |
| 330 |
break; |
| 331 |
case 3: |
| 332 |
setYAxis(AxisModeDateTime); |
| 333 |
break; |
| 334 |
case 4: |
| 335 |
setYAxis(AxisModeCategory); |
| 336 |
break; |
| 337 |
default: |
| 338 |
qWarning("Invalid Index!"); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
void MainWindow::themeIndexChanged(int index) |
| 343 |
{ |
| 344 |
m_chart->setTheme(QChart::ChartTheme(index)); |
| 345 |
} |
| 346 |
|
| 347 |
void MainWindow::addSeriesClicked() |
| 348 |
{ |
| 349 |
addSeries(false); |
| 350 |
} |
| 351 |
|
| 352 |
void MainWindow::removeSeriesClicked() |
| 353 |
{ |
| 354 |
if (m_seriesList.size()) { |
| 355 |
QXYSeries *series = m_seriesList.takeAt(m_seriesList.size() - 1); |
| 356 |
m_chart->removeSeries(series); |
| 357 |
delete series; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
void MainWindow::addGLSeriesClicked() |
| 362 |
{ |
| 363 |
addSeries(true); |
| 364 |
} |
| 365 |
|
| 366 |
void MainWindow::countIndexChanged(int index) |
| 367 |
{ |
| 368 |
m_pointCount = ui->countComboBox->itemText(index).toInt(); |
| 369 |
for (int i = 0; i < m_seriesList.size(); i++) { |
| 370 |
m_dataSource.generateData(i, 2, m_pointCount); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
void MainWindow::colorIndexChanged(int index) |
| 375 |
{ |
| 376 |
QColor color = QColor(ui->colorsComboBox->itemText(index).toLower()); |
| 377 |
foreach (QXYSeries *series, m_seriesList) { |
| 378 |
if (series->type() == QAbstractSeries::SeriesTypeScatter) { |
| 379 |
QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
| 380 |
scatterSeries->setBorderColor(color); |
| 381 |
scatterSeries->setColor(color); |
| 382 |
} else { |
| 383 |
series->setColor(color); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
void MainWindow::widthIndexChanged(int index) |
| 389 |
{ |
| 390 |
int width = ui->widthComboBox->itemText(index).toInt(); |
| 391 |
foreach (QXYSeries *series, m_seriesList) { |
| 392 |
if (series->type() == QAbstractSeries::SeriesTypeScatter) { |
| 393 |
QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
| 394 |
scatterSeries->setMarkerSize(width); |
| 395 |
} else { |
| 396 |
QColor color = QColor(ui->colorsComboBox->itemText( |
| 397 |
ui->colorsComboBox->currentIndex()).toLower()); |
| 398 |
series->setPen(QPen(QBrush(color), width)); |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
void MainWindow::antiAliasCheckBoxClicked(bool checked) |
| 404 |
{ |
| 405 |
ui->chartView->setRenderHint(QPainter::Antialiasing, checked); |
| 406 |
} |
| 407 |
|
| 408 |
void MainWindow::handleHovered(const QPointF &point, bool state) |
| 409 |
{ |
| 410 |
QAbstractSeries *series = qobject_cast<QAbstractSeries *>(sender()); |
| 411 |
if (series) { |
| 412 |
qDebug() << __FUNCTION__ << series->name() << point << state; |
| 413 |
const QString labelTemplate = QStringLiteral("%3: %1 x %2 - %4"); |
| 414 |
ui->coordinatesLabel->setText( |
| 415 |
labelTemplate.arg(point.x()).arg(point.y()).arg(series->name()) |
| 416 |
.arg(state ? QStringLiteral("enter") : QStringLiteral("leave"))); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
void MainWindow::handleClicked(const QPointF &point) |
| 421 |
{ |
| 422 |
QAbstractSeries *series = qobject_cast<QAbstractSeries *>(sender()); |
| 423 |
qDebug() << __FUNCTION__ << series->name() << point; |
| 424 |
} |
| 425 |
|
| 426 |
void MainWindow::handlePressed(const QPointF &point) |
| 427 |
{ |
| 428 |
QAbstractSeries *series = qobject_cast<QAbstractSeries *>(sender()); |
| 429 |
qDebug() << __FUNCTION__ << series->name() << point; |
| 430 |
} |
| 431 |
|
| 432 |
void MainWindow::handleReleased(const QPointF &point) |
| 433 |
{ |
| 434 |
QAbstractSeries *series = qobject_cast<QAbstractSeries *>(sender()); |
| 435 |
qDebug() << __FUNCTION__ << series->name() << point; |
| 436 |
} |
| 437 |
|
| 438 |
void MainWindow::handleDoubleClicked(const QPointF &point) |
| 439 |
{ |
| 440 |
QAbstractSeries *series = qobject_cast<QAbstractSeries *>(sender()); |
| 441 |
qDebug() << __FUNCTION__ << series->name() << point; |
| 442 |
} |
| 443 |
|
| 444 |
void MainWindow::backgroundIndexChanged(int index) |
| 445 |
{ |
| 446 |
delete m_backgroundBrush; |
| 447 |
delete m_backgroundPen; |
| 448 |
|
| 449 |
switch (index) { |
| 450 |
case 0: |
| 451 |
m_backgroundBrush = new QBrush(Qt::white); |
| 452 |
m_backgroundPen = new QPen(Qt::NoPen); |
| 453 |
break; |
| 454 |
case 1: |
| 455 |
m_backgroundBrush = new QBrush(Qt::blue); |
| 456 |
m_backgroundPen = new QPen(Qt::NoPen); |
| 457 |
break; |
| 458 |
case 2: |
| 459 |
m_backgroundBrush = new QBrush(Qt::yellow); |
| 460 |
m_backgroundPen = new QPen(Qt::black, 2); |
| 461 |
break; |
| 462 |
default: |
| 463 |
break; |
| 464 |
} |
| 465 |
m_chart->setBackgroundBrush(*m_backgroundBrush); |
| 466 |
m_chart->setBackgroundPen(*m_backgroundPen); |
| 467 |
} |
| 468 |
|
| 469 |
void MainWindow::plotAreaIndexChanged(int index) |
| 470 |
{ |
| 471 |
delete m_plotAreaBackgroundBrush; |
| 472 |
delete m_plotAreaBackgroundPen; |
| 473 |
|
| 474 |
switch (index) { |
| 475 |
case 0: |
| 476 |
m_plotAreaBackgroundBrush = new QBrush(Qt::green); |
| 477 |
m_plotAreaBackgroundPen = new QPen(Qt::green); |
| 478 |
m_chart->setPlotAreaBackgroundVisible(false); |
| 479 |
break; |
| 480 |
case 1: |
| 481 |
m_plotAreaBackgroundBrush = new QBrush(Qt::magenta); |
| 482 |
m_plotAreaBackgroundPen = new QPen(Qt::NoPen); |
| 483 |
m_chart->setPlotAreaBackgroundVisible(true); |
| 484 |
break; |
| 485 |
case 2: |
| 486 |
m_plotAreaBackgroundBrush = new QBrush(Qt::lightGray); |
| 487 |
m_plotAreaBackgroundPen = new QPen(Qt::red, 6); |
| 488 |
m_chart->setPlotAreaBackgroundVisible(true); |
| 489 |
break; |
| 490 |
default: |
| 491 |
break; |
| 492 |
} |
| 493 |
m_chart->setPlotAreaBackgroundBrush(*m_plotAreaBackgroundBrush); |
| 494 |
m_chart->setPlotAreaBackgroundPen(*m_plotAreaBackgroundPen); |
| 495 |
} |
| 496 |
|
| 497 |
void MainWindow::applyCategories() |
| 498 |
{ |
| 499 |
// Basic layout is three categories, extended has five |
| 500 |
if (m_xAxisMode == AxisModeCategory) { |
| 501 |
QCategoryAxis *angCatAxis = static_cast<QCategoryAxis *>(m_xAxis); |
| 502 |
if (angCatAxis->count() == 0) { |
| 503 |
angCatAxis->setStartValue(2); |
| 504 |
angCatAxis->append("Category A", 6); |
| 505 |
angCatAxis->append("Category B", 12); |
| 506 |
angCatAxis->append("Category C", 18); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
if (m_yAxisMode == AxisModeCategory) { |
| 511 |
QCategoryAxis *radCatAxis = static_cast<QCategoryAxis *>(m_yAxis); |
| 512 |
if (radCatAxis->count() == 0) { |
| 513 |
radCatAxis->setStartValue(1); |
| 514 |
radCatAxis->append("Category 1", 3); |
| 515 |
radCatAxis->append("Category 2", 4); |
| 516 |
radCatAxis->append("Category 3", 8); |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
void MainWindow::addSeries(bool gl) |
| 522 |
{ |
| 523 |
QColor color = QColor(ui->colorsComboBox->itemText(ui->colorsComboBox->currentIndex()).toLower()); |
| 524 |
int width = ui->widthComboBox->itemText(ui->widthComboBox->currentIndex()).toInt(); |
| 525 |
|
| 526 |
if (m_seriesList.size() < maxSeriesCount) { |
| 527 |
QXYSeries *series; |
| 528 |
if (QRandomGenerator::global()->bounded(2)) { |
| 529 |
series = new QLineSeries; |
| 530 |
series->setPen(QPen(QBrush(color), width)); |
| 531 |
} else { |
| 532 |
QScatterSeries *scatterSeries = new QScatterSeries; |
| 533 |
scatterSeries->setMarkerSize(width); |
| 534 |
scatterSeries->setBorderColor(color); |
| 535 |
scatterSeries->setBrush(QBrush(color)); |
| 536 |
series = scatterSeries; |
| 537 |
} |
| 538 |
series->setUseOpenGL(gl); |
| 539 |
const QString glNameTemplate = QStringLiteral("GL_%1"); |
| 540 |
const QString rasterNameTemplate = QStringLiteral("Raster_%1"); |
| 541 |
if (gl) |
| 542 |
series->setName(glNameTemplate.arg(m_seriesList.size())); |
| 543 |
else |
| 544 |
series->setName(rasterNameTemplate.arg(m_seriesList.size())); |
| 545 |
m_dataSource.generateData(m_seriesList.size(), 2, m_pointCount); |
| 546 |
m_seriesList.append(series); |
| 547 |
m_chart->addSeries(series); |
| 548 |
if (m_xAxis) |
| 549 |
series->attachAxis(m_xAxis); |
| 550 |
if (m_yAxis) |
| 551 |
series->attachAxis(m_yAxis); |
| 552 |
applyRanges(); |
| 553 |
|
| 554 |
QObject::connect(series, &QXYSeries::hovered, this, &MainWindow::handleHovered); |
| 555 |
QObject::connect(series, &QXYSeries::clicked, this, &MainWindow::handleClicked); |
| 556 |
QObject::connect(series, &QXYSeries::pressed, this, &MainWindow::handlePressed); |
| 557 |
QObject::connect(series, &QXYSeries::released, this, &MainWindow::handleReleased); |
| 558 |
QObject::connect(series, &QXYSeries::doubleClicked, this, &MainWindow::handleDoubleClicked); |
| 559 |
} |
| 560 |
} |