| 1 | // Copyright (C) 2023 The Qt Company Ltd. | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only | 
| 3 |  | 
| 4 | #include <QtGraphs/QValueAxis> | 
| 5 | #include <private/qvalueaxis_p.h> | 
| 6 | #include <private/charthelpers_p.h> | 
| 7 | #include <QtCore/QtMath> | 
| 8 |  | 
| 9 | QT_BEGIN_NAMESPACE | 
| 10 | /*! | 
| 11 |     \class QValueAxis | 
| 12 |     \inmodule QtGraphs | 
| 13 |     \ingroup graphs_2D | 
| 14 |     \brief The QValueAxis class adds values to a graph's axes. | 
| 15 |  | 
| 16 |     A value axis can be set up to show an axis line with tick marks, grid lines, and shades. | 
| 17 |     The values on the axis are drawn at the positions of tick marks. | 
| 18 | */ | 
| 19 | /*! | 
| 20 |     \qmltype ValueAxis | 
| 21 |     \nativetype QValueAxis | 
| 22 |     \inqmlmodule QtGraphs | 
| 23 |     \ingroup graphs_qml_2D | 
| 24 |     \inherits AbstractAxis | 
| 25 |     \brief Adds values to a graph's axes. | 
| 26 |  | 
| 27 |     The ValueAxis type can be set up to show an axis line with tick marks, grid lines, and shades. | 
| 28 |     The values on the axis are drawn at the positions of tick marks. | 
| 29 |  | 
| 30 |     The following example code illustrates how to use the ValueAxis type: | 
| 31 |     \code | 
| 32 |     GraphsView { | 
| 33 |         axisX: ValueAxis { | 
| 34 |             max: 10 | 
| 35 |             tickInterval: 1 | 
| 36 |         } | 
| 37 |         axisY: ValueAxis { | 
| 38 |             min -20 | 
| 39 |             max: 40 | 
| 40 |         } | 
| 41 |         LineSeries { | 
| 42 |             // Add a few XYPoint data... | 
| 43 |         } | 
| 44 |     } | 
| 45 |     \endcode | 
| 46 | */ | 
| 47 |  | 
| 48 | /*! | 
| 49 |   \property QValueAxis::min | 
| 50 |   \brief The minimum value on the axis. | 
| 51 |  | 
| 52 |   When setting this property, the maximum value is adjusted if necessary, to ensure that | 
| 53 |   the range remains valid. | 
| 54 |   The default value is 0.0 | 
| 55 | */ | 
| 56 | /*! | 
| 57 |   \qmlproperty real ValueAxis::min | 
| 58 |   The minimum value on the axis. | 
| 59 |  | 
| 60 |   When setting this property, the maximum value is adjusted if necessary, to ensure that | 
| 61 |   the range remains valid. | 
| 62 |   The default value is 0.0 | 
| 63 | */ | 
| 64 |  | 
| 65 | /*! | 
| 66 |   \property QValueAxis::max | 
| 67 |   \brief The maximum value on the axis. | 
| 68 |  | 
| 69 |   When setting this property, the minimum value is adjusted if necessary, to ensure that | 
| 70 |   the range remains valid. | 
| 71 |   The default value is 10.0 | 
| 72 | */ | 
| 73 | /*! | 
| 74 |   \qmlproperty real ValueAxis::max | 
| 75 |   The maximum value on the axis. | 
| 76 |  | 
| 77 |   When setting this property, the minimum value is adjusted if necessary, to ensure that | 
| 78 |   the range remains valid. | 
| 79 |   The default value is 10.0 | 
| 80 | */ | 
| 81 |  | 
| 82 | /*! | 
| 83 |   \property QValueAxis::subTickCount | 
| 84 |   \brief The number of subticks on the axis. This indicates how many subticks are drawn | 
| 85 |   between major lines on the graph. Labels are not drawn for subticks. The default value is 0. | 
| 86 | */ | 
| 87 | /*! | 
| 88 |   \qmlproperty int ValueAxis::subTickCount | 
| 89 |   The number of subticks on the axis. This indicates how many subticks are drawn | 
| 90 |   between major lines on the graph. Labels are not drawn for subticks. The default value is 0. | 
| 91 | */ | 
| 92 |  | 
| 93 | /*! | 
| 94 |   \property QValueAxis::tickAnchor | 
| 95 |   \brief The base value where the dynamically placed tick marks and labels are started from. | 
| 96 |   The default value is 0. | 
| 97 |  */ | 
| 98 | /*! | 
| 99 |   \qmlproperty real ValueAxis::tickAnchor | 
| 100 |   The base value where the dynamically placed tick marks and labels are started from. | 
| 101 |   The default value is 0. | 
| 102 |  */ | 
| 103 |  | 
| 104 | /*! | 
| 105 |   \property QValueAxis::tickInterval | 
| 106 |   \brief The interval between dynamically placed tick marks and labels. | 
| 107 |   The default value is 0, which means that intervals are automatically calculated | 
| 108 |   based on the min and max range. | 
| 109 | */ | 
| 110 | /*! | 
| 111 |   \qmlproperty real ValueAxis::tickInterval | 
| 112 |   The interval between dynamically placed tick marks and labels. | 
| 113 |   The default value is 0, which means that intervals are automatically calculated | 
| 114 |   based on the min and max range. | 
| 115 | */ | 
| 116 |  | 
| 117 | /*! | 
| 118 |   \property QValueAxis::labelFormat | 
| 119 |   \brief The label format of the axis. | 
| 120 |  | 
| 121 |   The format string supports the following conversion specifiers, length modifiers, and flags | 
| 122 |   provided by \c printf() in the standard C++ library: d, i, o, x, X, f, F, e, E, g, G, c. | 
| 123 |  | 
| 124 |   The default value is empty, in which case 'f' format is used. | 
| 125 |  | 
| 126 |   \sa QString::asprintf() | 
| 127 | */ | 
| 128 | /*! | 
| 129 |   \qmlproperty string ValueAxis::labelFormat | 
| 130 |  | 
| 131 |   The format string supports the following conversion specifiers, length modifiers, and flags | 
| 132 |   provided by \c printf() in the standard C++ library: d, i, o, x, X, f, F, e, E, g, G, c. | 
| 133 |  | 
| 134 |   The default value is empty, in which case 'f' format is used. | 
| 135 |  | 
| 136 |   \sa QString::asprintf() | 
| 137 | */ | 
| 138 |  | 
| 139 | /*! | 
| 140 |   \property QValueAxis::labelDecimals | 
| 141 |   \brief The number of decimals used for showing the labels. When set to -1, decimal amount | 
| 142 |   is adjusted automatically based on the values range. The default value is -1. | 
| 143 | */ | 
| 144 | /*! | 
| 145 |   \qmlproperty int ValueAxis::labelDecimals | 
| 146 |   The number of decimals used for showing the labels. When set to -1, decimal amount | 
| 147 |   is adjusted automatically based on the values range. The default value is -1. | 
| 148 | */ | 
| 149 |  | 
| 150 | /*! | 
| 151 |   \qmlsignal ValueAxis::minChanged(real min) | 
| 152 |   This signal is emitted when the minimum value of the axis changes to \a min. | 
| 153 | */ | 
| 154 |  | 
| 155 | /*! | 
| 156 |   \qmlsignal ValueAxis::maxChanged(real max) | 
| 157 |   This signal is emitted when the maximum value of the axis changes to \a max. | 
| 158 | */ | 
| 159 |  | 
| 160 | /*! | 
| 161 |   \qmlsignal ValueAxis::subTickCountChanged(int subTickCount) | 
| 162 |   This signal is emitted when the number of subticks on the axis, specified by | 
| 163 |   \a subTickCount, changes. | 
| 164 | */ | 
| 165 |  | 
| 166 | /*! | 
| 167 |     \qmlsignal ValueAxis::rangeChanged(real min, real max) | 
| 168 |   This signal is emitted when the minimum or maximum value of the axis | 
| 169 |   changes to \a min and \a max, respectively. | 
| 170 | */ | 
| 171 |  | 
| 172 | /*! | 
| 173 |   \qmlsignal ValueAxis::labelFormatChanged(string format) | 
| 174 |   This signal is emitted when the format of axis labels changes to \a format. | 
| 175 | */ | 
| 176 |  | 
| 177 | /*! | 
| 178 |   \qmlsignal ValueAxis::labelDecimalsChanged(int decimals) | 
| 179 |   This signal is emitted when the amount of axis label decimals changes to \a decimals. | 
| 180 | */ | 
| 181 |  | 
| 182 | /*! | 
| 183 |   \qmlsignal ValueAxis::tickAnchorChanged(real tickAnchor) | 
| 184 |   This signal is emitted when the tick anchoring value changes to \a tickAnchor. | 
| 185 | */ | 
| 186 |  | 
| 187 | /*! | 
| 188 |   \qmlsignal ValueAxis::tickIntervalChanged(real tickInterval) | 
| 189 |   This signal is emitted when the tick interval value, changes to | 
| 190 |   \a tickInterval. | 
| 191 | */ | 
| 192 |  | 
| 193 | /*! | 
| 194 |     Constructs an axis object that is a child of \a parent. | 
| 195 | */ | 
| 196 | QValueAxis::QValueAxis(QObject *parent) | 
| 197 |     : QAbstractAxis(*(new QValueAxisPrivate), parent) | 
| 198 | {} | 
| 199 |  | 
| 200 | /*! | 
| 201 |     \internal | 
| 202 | */ | 
| 203 | QValueAxis::QValueAxis(QValueAxisPrivate &d, QObject *parent) | 
| 204 |     : QAbstractAxis(d, parent) | 
| 205 | {} | 
| 206 |  | 
| 207 | /*! | 
| 208 |     Destroys the object. | 
| 209 | */ | 
| 210 | QValueAxis::~QValueAxis() | 
| 211 | { | 
| 212 | } | 
| 213 |  | 
| 214 | void QValueAxis::setMin(qreal min) | 
| 215 | { | 
| 216 |     Q_D(QValueAxis); | 
| 217 |     setRange(min, max: qMax(a: d->m_max, b: min)); | 
| 218 | } | 
| 219 |  | 
| 220 | qreal QValueAxis::min() const | 
| 221 | { | 
| 222 |     Q_D(const QValueAxis); | 
| 223 |     return d->m_min; | 
| 224 | } | 
| 225 |  | 
| 226 | void QValueAxis::setMax(qreal max) | 
| 227 | { | 
| 228 |     Q_D(QValueAxis); | 
| 229 |     setRange(min: qMin(a: d->m_min, b: max), max); | 
| 230 | } | 
| 231 |  | 
| 232 | qreal QValueAxis::max() const | 
| 233 | { | 
| 234 |     Q_D(const QValueAxis); | 
| 235 |     return d->m_max; | 
| 236 | } | 
| 237 |  | 
| 238 | /*! | 
| 239 |   Sets the range from \a min to \a max on the axis. | 
| 240 |   If \a min is greater than \a max, this function returns without making any changes. | 
| 241 | */ | 
| 242 | void QValueAxis::setRange(qreal min, qreal max) | 
| 243 | { | 
| 244 |     Q_D(QValueAxis); | 
| 245 |     d->setRange(min,max); | 
| 246 |     emit update(); | 
| 247 | } | 
| 248 |  | 
| 249 | void QValueAxis::setSubTickCount(qsizetype count) | 
| 250 | { | 
| 251 |     Q_D(QValueAxis); | 
| 252 |     if (d->m_subTickCount != count && count >= 0) { | 
| 253 |         d->m_subTickCount = count; | 
| 254 |         emit update(); | 
| 255 |         emit subTickCountChanged(subTickCount: count); | 
| 256 |     } | 
| 257 | } | 
| 258 |  | 
| 259 | qsizetype QValueAxis::subTickCount() const | 
| 260 | { | 
| 261 |     Q_D(const QValueAxis); | 
| 262 |     return d->m_subTickCount; | 
| 263 | } | 
| 264 |  | 
| 265 | void QValueAxis::setTickAnchor(qreal anchor) | 
| 266 | { | 
| 267 |     Q_D(QValueAxis); | 
| 268 |     if (d->m_tickAnchor != anchor) { | 
| 269 |         d->m_tickAnchor = anchor; | 
| 270 |         emit update(); | 
| 271 |         emit tickAnchorChanged(tickAnchor: anchor); | 
| 272 |     } | 
| 273 | } | 
| 274 |  | 
| 275 | qreal QValueAxis::tickAnchor() const | 
| 276 | { | 
| 277 |     Q_D(const QValueAxis); | 
| 278 |     return d->m_tickAnchor; | 
| 279 | } | 
| 280 |  | 
| 281 | void QValueAxis::setTickInterval(qreal interval) | 
| 282 | { | 
| 283 |     Q_D(QValueAxis); | 
| 284 |     if (d->m_tickInterval != interval) { | 
| 285 |         d->m_tickInterval = interval; | 
| 286 |         emit update(); | 
| 287 |         emit tickIntervalChanged(tickInterval: interval); | 
| 288 |     } | 
| 289 | } | 
| 290 |  | 
| 291 | qreal QValueAxis::tickInterval() const | 
| 292 | { | 
| 293 |     Q_D(const QValueAxis); | 
| 294 |     return d->m_tickInterval; | 
| 295 | } | 
| 296 |  | 
| 297 | void QValueAxis::setLabelFormat(const QString &format) | 
| 298 | { | 
| 299 |     Q_D(QValueAxis); | 
| 300 |     d->m_format = format; | 
| 301 |     emit update(); | 
| 302 |     emit labelFormatChanged(format); | 
| 303 | } | 
| 304 |  | 
| 305 | QString QValueAxis::labelFormat() const | 
| 306 | { | 
| 307 |     Q_D(const QValueAxis); | 
| 308 |     return d->m_format; | 
| 309 | } | 
| 310 |  | 
| 311 | void QValueAxis::setLabelDecimals(int decimals) | 
| 312 | { | 
| 313 |     Q_D(QValueAxis); | 
| 314 |     if (d->m_decimals != decimals) { | 
| 315 |         d->m_decimals = decimals; | 
| 316 |         emit update(); | 
| 317 |         emit labelDecimalsChanged(decimals); | 
| 318 |     } | 
| 319 | } | 
| 320 |  | 
| 321 | int QValueAxis::labelDecimals() const | 
| 322 | { | 
| 323 |     Q_D(const QValueAxis); | 
| 324 |     return d->m_decimals; | 
| 325 | } | 
| 326 |  | 
| 327 | /*! | 
| 328 |   Returns the type of the axis. | 
| 329 | */ | 
| 330 | QAbstractAxis::AxisType QValueAxis::type() const | 
| 331 | { | 
| 332 |     return QAbstractAxis::AxisType::Value; | 
| 333 | } | 
| 334 |  | 
| 335 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | 
| 336 |  | 
| 337 | QValueAxisPrivate::QValueAxisPrivate() | 
| 338 |     : m_min(0) | 
| 339 |     , m_max(10) | 
| 340 |     , m_subTickCount(0) | 
| 341 |     , m_format() | 
| 342 |     , m_decimals(-1) | 
| 343 |     , m_tickAnchor(0.0) | 
| 344 |     , m_tickInterval(0.0) | 
| 345 | {} | 
| 346 |  | 
| 347 | QValueAxisPrivate::~QValueAxisPrivate() {} | 
| 348 |  | 
| 349 | void QValueAxisPrivate::setMin(const QVariant &min) | 
| 350 | { | 
| 351 |     Q_Q(QValueAxis); | 
| 352 |     bool ok; | 
| 353 |     qreal value = min.toReal(ok: &ok); | 
| 354 |     if (ok) | 
| 355 |         q->setMin(value); | 
| 356 | } | 
| 357 |  | 
| 358 | void QValueAxisPrivate::setMax(const QVariant &max) | 
| 359 | { | 
| 360 |     Q_Q(QValueAxis); | 
| 361 |     bool ok; | 
| 362 |     qreal value = max.toReal(ok: &ok); | 
| 363 |     if (ok) | 
| 364 |         q->setMax(value); | 
| 365 | } | 
| 366 |  | 
| 367 | void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max) | 
| 368 | { | 
| 369 |     Q_Q(QValueAxis); | 
| 370 |     bool ok1; | 
| 371 |     bool ok2; | 
| 372 |     qreal value1 = min.toReal(ok: &ok1); | 
| 373 |     qreal value2 = max.toReal(ok: &ok2); | 
| 374 |     if (ok1 && ok2) | 
| 375 |         q->setRange(min: value1, max: value2); | 
| 376 | } | 
| 377 |  | 
| 378 | void QValueAxisPrivate::setRange(qreal min, qreal max) | 
| 379 | { | 
| 380 |     Q_Q(QValueAxis); | 
| 381 |     bool changed = false; | 
| 382 |  | 
| 383 |     if (min > max) | 
| 384 |         return; | 
| 385 |  | 
| 386 |     if (!isValidValue(x: min, y: max)) { | 
| 387 |         qWarning(msg: "Attempting to set invalid range for value axis: [%f - %f]" , min, max); | 
| 388 |         return; | 
| 389 |     } | 
| 390 |  | 
| 391 |     if (m_min != min) { | 
| 392 |         m_min = min; | 
| 393 |         changed = true; | 
| 394 |         emit q->minChanged(min); | 
| 395 |     } | 
| 396 |  | 
| 397 |     if (m_max != max) { | 
| 398 |         m_max = max; | 
| 399 |         changed = true; | 
| 400 |         emit q->maxChanged(max); | 
| 401 |     } | 
| 402 |  | 
| 403 |     if (changed) | 
| 404 |         emit q->rangeChanged(min, max); | 
| 405 | } | 
| 406 |  | 
| 407 | QT_END_NAMESPACE | 
| 408 |  | 
| 409 | #include "moc_qvalueaxis.cpp" | 
| 410 |  |