| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "graphs2d/xychart/qxyseries_p.h" |
| 5 | #include <QtGraphs/qlineseries.h> |
| 6 | #include <private/qgraphsview_p.h> |
| 7 | #include <private/qlineseries_p.h> |
| 8 | #include <private/qxypoint_p.h> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | /*! |
| 13 | \class QLineSeries |
| 14 | \inmodule QtGraphs |
| 15 | \ingroup graphs_2D |
| 16 | \brief The QLineSeries class presents data in line graphs. |
| 17 | |
| 18 | A line graph is used to show information as a series of data points |
| 19 | connected by straight lines. |
| 20 | */ |
| 21 | /*! |
| 22 | \qmltype LineSeries |
| 23 | \nativetype QLineSeries |
| 24 | \inqmlmodule QtGraphs |
| 25 | \ingroup graphs_qml_2D |
| 26 | \inherits XYSeries |
| 27 | |
| 28 | \brief Presents data in line graphs. |
| 29 | |
| 30 | A line graph is used to show information as a series of data points |
| 31 | connected by straight lines. |
| 32 | |
| 33 | \image graphs2d-line.png |
| 34 | |
| 35 | LineSeries uses mostly the same API as ScatterSeries so see ScatterSeries |
| 36 | documentation for further usage examples. |
| 37 | |
| 38 | \sa ScatterSeries |
| 39 | */ |
| 40 | |
| 41 | /*! |
| 42 | \qmlproperty real LineSeries::width |
| 43 | The width of the line. By default, the width is 2.0. Widths lower than 0 |
| 44 | are invalid and are automatically set to 0. |
| 45 | */ |
| 46 | |
| 47 | /*! |
| 48 | \qmlproperty Qt::PenCapStyle LineSeries::capStyle |
| 49 | Controls the cap style of the line. Set to one of \l{Qt::FlatCap}{Qt.FlatCap}, |
| 50 | \l{Qt::SquareCap}{Qt.SquareCap} or \l{Qt::RoundCap}{Qt.RoundCap}. By |
| 51 | default the cap style is Qt.SquareCap. Invalid values are automatically set |
| 52 | to the default value. |
| 53 | |
| 54 | \sa Qt::PenCapStyle |
| 55 | */ |
| 56 | |
| 57 | /*! |
| 58 | \qmlproperty Component LineSeries::pointDelegate |
| 59 | Marks the point with the given QML component. |
| 60 | |
| 61 | \code |
| 62 | pointDelegate: Image { |
| 63 | source: "images/happy_box.png" |
| 64 | } |
| 65 | \endcode |
| 66 | */ |
| 67 | |
| 68 | /*! |
| 69 | \qmlsignal LineSeries::widthChanged() |
| 70 | This signal is emitted when the line series width changes. |
| 71 | */ |
| 72 | |
| 73 | /*! |
| 74 | \qmlsignal LineSeries::capStyleChanged() |
| 75 | This signal is emitted when the line series cap style changes. |
| 76 | */ |
| 77 | |
| 78 | QLineSeries::QLineSeries(QObject *parent) |
| 79 | : QXYSeries(*(new QLineSeriesPrivate()), parent) |
| 80 | {} |
| 81 | |
| 82 | QLineSeries::~QLineSeries() {} |
| 83 | |
| 84 | QLineSeries::QLineSeries(QLineSeriesPrivate &dd, QObject *parent) |
| 85 | : QXYSeries(dd, parent) |
| 86 | {} |
| 87 | |
| 88 | void QLineSeries::componentComplete() |
| 89 | { |
| 90 | Q_D(QLineSeries); |
| 91 | |
| 92 | for (auto *child : children()) { |
| 93 | if (auto point = qobject_cast<QXYPoint *>(object: child)) { |
| 94 | append(x: point->x(), y: point->y()); |
| 95 | qCDebug(lcSeries2D, "append points x: %.1f, y: %.1f to lineSeries" , |
| 96 | point->x(), |
| 97 | point->y()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (d->m_graphTransition) |
| 102 | d->m_graphTransition->initialize(); |
| 103 | |
| 104 | qCDebug(lcEvents2D) << "QLineSeries::componentComplete." ; |
| 105 | |
| 106 | QAbstractSeries::componentComplete(); |
| 107 | } |
| 108 | |
| 109 | QAbstractSeries::SeriesType QLineSeries::type() const |
| 110 | { |
| 111 | return QAbstractSeries::SeriesType::Line; |
| 112 | } |
| 113 | |
| 114 | QLineSeriesPrivate::QLineSeriesPrivate() |
| 115 | : QXYSeriesPrivate(QAbstractSeries::SeriesType::Line) |
| 116 | {} |
| 117 | |
| 118 | qreal QLineSeries::width() const |
| 119 | { |
| 120 | Q_D(const QLineSeries); |
| 121 | return d->m_width; |
| 122 | } |
| 123 | |
| 124 | void QLineSeries::setWidth(qreal newWidth) |
| 125 | { |
| 126 | Q_D(QLineSeries); |
| 127 | if (newWidth < 0.0) |
| 128 | newWidth = 0.0; |
| 129 | if (qFuzzyCompare(p1: d->m_width, p2: newWidth)) { |
| 130 | qCDebug(lcProperties2D, "QLineSeries::setWidth. Set value width is already %f" , |
| 131 | newWidth); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | d->m_width = newWidth; |
| 136 | emit widthChanged(); |
| 137 | emit update(); |
| 138 | } |
| 139 | |
| 140 | Qt::PenCapStyle QLineSeries::capStyle() const |
| 141 | { |
| 142 | Q_D(const QLineSeries); |
| 143 | return d->m_capStyle; |
| 144 | } |
| 145 | |
| 146 | void QLineSeries::setCapStyle(Qt::PenCapStyle newCapStyle) |
| 147 | { |
| 148 | Q_D(QLineSeries); |
| 149 | Qt::PenCapStyle validCapStyle = newCapStyle; |
| 150 | if (validCapStyle != Qt::PenCapStyle::FlatCap && validCapStyle != Qt::PenCapStyle::SquareCap |
| 151 | && validCapStyle != Qt::PenCapStyle::RoundCap |
| 152 | && validCapStyle != Qt::PenCapStyle::MPenCapStyle) { |
| 153 | validCapStyle = Qt::PenCapStyle::SquareCap; |
| 154 | } |
| 155 | if (d->m_capStyle == validCapStyle) { |
| 156 | qCDebug(lcProperties2D) << "QLineSeries::setCapStyle. CapStyle is already set to:" |
| 157 | << newCapStyle; |
| 158 | return; |
| 159 | } |
| 160 | d->m_capStyle = validCapStyle; |
| 161 | emit capStyleChanged(); |
| 162 | emit update(); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /*! |
| 167 | \qmlmethod LineSeries::dataPointCoordinatesAt(real x, real y) |
| 168 | Returns \a x and \a y rendercoordinates converted into data point |
| 169 | coordinates. |
| 170 | */ |
| 171 | /*! |
| 172 | Returns \a x and \a y rendercoordinates converted into data point |
| 173 | coordinates. |
| 174 | |
| 175 | */ |
| 176 | QPointF QLineSeries::dataPointCoordinatesAt(qreal x, qreal y) |
| 177 | { |
| 178 | Q_D(QLineSeries); |
| 179 | |
| 180 | auto oPoint = d->m_graph->getDataPointCoordinates(series: this, x, y); |
| 181 | return oPoint; |
| 182 | } |
| 183 | |
| 184 | QT_END_NAMESPACE |
| 185 | |