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/qscatterseries.h>
6#include <private/qxypoint_p.h>
7#include <private/qscatterseries_p.h>
8#include <private/qgraphsview_p.h>
9#include <QtCore/qloggingcategory.h>
10
11QT_BEGIN_NAMESPACE
12
13/*!
14 \class QScatterSeries
15 \inmodule QtGraphs
16 \ingroup graphs_2D
17 \brief The QScatterSeries class presents data in scatter graphs.
18
19 The scatter data is displayed as a collection of points on the graph. For
20 each point, two values are specified that determine its position on the
21 horizontal axis and the vertical axis.
22*/
23/*!
24 \qmltype ScatterSeries
25 \nativetype QScatterSeries
26 \inqmlmodule QtGraphs
27 \ingroup graphs_qml_2D
28 \inherits XYSeries
29
30 \brief The ScatterSeries type presents data in scatter graphs.
31
32 The scatter data is displayed as a collection of points on the graph. For
33 each point, two values are specified that determine its position on the
34 horizontal axis and the vertical axis.
35
36 \image graphs2d-scatter.png
37
38 You can represent scatter data by creating a ScatterSeries inside
39 GraphsView. The data can be added to the graph by creating XYPoints as
40 children for the ScatterSeries that define the x and y values of each
41 point.
42
43 \code
44 GraphsView {
45 anchors.fill: parent
46 axisX: ValueAxis {
47 max: 3
48 }
49 axisY: ValueAxis {
50 max: 3
51 }
52
53 ScatterSeries {
54 color: "#00ff00"
55 XYPoint { x: 0.5; y: 0.5 }
56 XYPoint { x: 1; y: 1 }
57 XYPoint { x: 2; y: 2 }
58 XYPoint { x: 2.5; y: 1.5 }
59 }
60 }
61 \endcode
62
63 Multiple scatter graphs can be created by adding multiple ScatterSeries
64 as children of GraphsView. In such cases only one series should define
65 the axis used as multiple definitions only override the earlier ones.
66
67 \code
68 GraphsView {
69 anchors.fill: parent
70 ScatterSeries {
71 color: "#00ff00"
72 axisX: ValueAxis {
73 max: 3
74 }
75 axisY: ValueAxis {
76 max: 3
77 }
78
79 XYPoint { x: 0.5; y: 0.5 }
80 XYPoint { x: 1; y: 1 }
81 XYPoint { x: 2; y: 2 }
82 XYPoint { x: 2.5; y: 1.5 }
83 }
84
85 ScatterSeries {
86 color: "#ff0000"
87 XYPoint { x: 0.5; y: 3 }
88 XYPoint { x: 1; y: 2 }
89 XYPoint { x: 2; y: 2.5 }
90 XYPoint { x: 2.5; y: 1 }
91 }
92 }
93 \endcode
94*/
95
96/*!
97 \qmlproperty Component ScatterSeries::pointDelegate
98 Marks points with the given QML component.
99
100 \code
101 pointDelegate: Image {
102 source: "images/happy_box.png"
103 }
104 \endcode
105*/
106
107QScatterSeries::QScatterSeries(QObject *parent)
108 : QXYSeries(*(new QScatterSeriesPrivate()), parent)
109{}
110
111QScatterSeries::~QScatterSeries() {}
112
113QScatterSeries::QScatterSeries(QScatterSeriesPrivate &dd, QObject *parent)
114 : QXYSeries(dd, parent)
115{}
116
117void QScatterSeries::componentComplete()
118{
119 Q_D(QScatterSeries);
120
121 for (auto *child : children()) {
122 if (auto point = qobject_cast<QXYPoint *>(object: child)) {
123 append(x: point->x(), y: point->y());
124 qCDebug(lcSeries2D, "append points x: %f, y: %f to scatterSeries",
125 point->x(),
126 point->y());
127 }
128 }
129
130 if (d->m_graphTransition)
131 d->m_graphTransition->initialize();
132
133 qCDebug(lcEvents2D, "QScatterSeries::componentComplete");
134
135 QAbstractSeries::componentComplete();
136}
137
138QAbstractSeries::SeriesType QScatterSeries::type() const
139{
140 return QAbstractSeries::SeriesType::Scatter;
141}
142
143QScatterSeriesPrivate::QScatterSeriesPrivate()
144 : QXYSeriesPrivate(QAbstractSeries::SeriesType::Scatter)
145{
146}
147
148QT_END_NAMESPACE
149

source code of qtgraphs/src/graphs2d/scatterchart/qscatterseries.cpp