1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <QtGraphs/qabstractseries.h>
5#include <private/qabstractseries_p.h>
6#include <private/qgraphsview_p.h>
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 \class QAbstractSeries
12 \inmodule QtGraphs
13 \ingroup graphs_2D
14 \brief The QAbstractSeries class is a base class for all Qt Graphs for 2D series.
15
16 Usually, the series type specific inherited classes are used instead of the base class.
17
18 \sa QLineSeries, QSplineSeries, QScatterSeries, QBarSeries, QXYSeries
19*/
20/*!
21 \qmltype AbstractSeries
22 \nativetype QAbstractSeries
23 \inqmlmodule QtGraphs
24 \ingroup graphs_qml_2D
25 \brief Base type for all Qt Graph series types.
26
27 This type cannot be instantiated directly. Instead, one of the following derived types
28 should be used to create a series: LineSeries, SplineSeries, BarSeries, or ScatterSeries.
29*/
30
31/*!
32 \class QLegendData
33 \inmodule QtGraphs
34 \ingroup graphs_2D
35 \brief The QLegendData struct contains information to display on a sets
36 legend marker.
37
38 The information needed to make a visual association between a set and a
39 marker include properties such as color, border color, and a name of a set.
40
41 \sa QLineSeries, QSplineSeries, QScatterSeries, QBarSeries, QXYSeries
42*/
43
44/*!
45 \qmltype LegendData
46 \nativetype QLegendData
47 \inqmlmodule QtGraphs
48 \ingroup graphs_qml__2D
49 \brief The LegendData struct contains information to display on a sets
50 legend marker.
51
52 The information needed to make a visual association between a set and a
53 marker include properties such as color, border color, and a name of a set.
54*/
55
56/*!
57 \property QLegendData::color
58 \brief A legend marker's color.
59*/
60/*!
61 \qmlproperty color LegendData::color
62 \brief A legend marker's color.
63*/
64
65/*!
66 \property QLegendData::borderColor
67 \brief A border color of a legend marker.
68*/
69/*!
70 \qmlproperty color LegendData::borderColor
71 \brief A border color of a legend marker.
72*/
73
74/*!
75 \property QLegendData::label
76 \brief A name of a legend marker.
77*/
78/*!
79 \qmlproperty string LegendData::label
80 \brief A name of a legend marker.
81*/
82
83/*!
84 \enum QAbstractSeries::SeriesType
85
86 This enum describes the type of the series.
87
88 \value Line A line graph.
89 \value Bar A bar graph.
90 \value Scatter A scatter graph.
91 \value Pie A pie graph.
92 \value Spline A spline graph.
93 \value Area An area graph.
94*/
95
96/*!
97 \property QAbstractSeries::type
98 \brief The type of the series.
99*/
100/*!
101 \qmlproperty enumeration AbstractSeries::type
102
103 The type of the series.
104
105 \value AbstractSeries.SeriesType.Line A line graph.
106 \value AbstractSeries.SeriesType.Bar A bar graph.
107 \value AbstractSeries.SeriesType.Scatter A scatter graph.
108 \value AbstractSeries.SeriesType.Pie A pie graph.
109 \value AbstractSeries.SeriesType.Spline A spline graph.
110 \value AbstractSeries.SeriesType.Area An area graph.
111*/
112
113/*!
114 \property QAbstractSeries::name
115 \brief The name of the series.
116
117 The name is displayed in the legend for the series and it supports HTML formatting.
118*/
119/*!
120 \qmlproperty string AbstractSeries::name
121 The name of the series. The name is displayed in the legend for the series and it
122 supports HTML formatting.
123*/
124
125/*!
126 \property QAbstractSeries::visible
127 \brief Visibility of the series.
128
129 The visibility used for this series. By default, \a visible is set to \c true.
130*/
131/*!
132 \qmlproperty bool AbstractSeries::visible
133 The visibility used for this series. By default, \a visible is set to \c true.
134*/
135
136/*!
137 \property QAbstractSeries::selectable
138 \brief Controls if the series is selectable.
139
140 Controls if the series can be selected with mouse/touch.
141 By default, \a selectable is set to \c false.
142*/
143/*!
144 \qmlproperty bool AbstractSeries::selectable
145 Controls if the series can be selected with mouse/touch.
146 By default, \a selectable is set to \c false.
147*/
148
149/*!
150 \property QAbstractSeries::hoverable
151 \brief Controls if the series is hoverable.
152
153 Controls if the series can be hovered with mouse/touch.
154 By default, \a hoverable is set to \c false.
155*/
156/*!
157 \qmlproperty bool AbstractSeries::hoverable
158 Controls if the series can be hovered with mouse/touch.
159 By default, \a hoverable is set to \c false.
160*/
161
162/*!
163 \property QAbstractSeries::opacity
164 \brief The opacity of the series.
165
166 By default, the opacity is 1.0. The valid values range from 0.0 (transparent) to 1.0 (opaque).
167*/
168/*!
169 \qmlproperty real AbstractSeries::opacity
170 The opacity of the series. By default, the opacity is 1.0.
171 The valid values range from 0.0 (transparent) to 1.0 (opaque).
172*/
173
174/*!
175 \property QAbstractSeries::valuesMultiplier
176 \brief Controls the series values effective visible value.
177
178 This variable can be used for animating the series values so they scale from 0 to actual value size.
179 By default, the valuesMultiplier is 1.0. The valid values range from 0.0 (height 0) to 1.0 (full value).
180*/
181/*!
182 \qmlproperty real AbstractSeries::valuesMultiplier
183 This variable can be used for animating the series values so they scale from 0 to actual value size.
184 By default, the valuesMultiplier is 1.0. The valid values range from 0.0 (height 0) to 1.0 (full value).
185*/
186
187/*!
188 \property QAbstractSeries::legendData
189 \brief Contains information needed to create a legend marker for a data set in a graph.
190 \sa QLegendData
191 */
192/*!
193 \qmlproperty list<LegendData> QAbstractSeries::legendData
194 Contains information needed to create a legend marker for a data set in a graph.
195*/
196
197/*!
198 \qmlsignal AbstractSeries::legendDataChanged()
199 This signal is emitted when legend data changes.
200*/
201
202/*!
203 \qmlsignal AbstractSeries::themeChanged()
204 This signal is emitted when the series theme changes.
205*/
206
207/*!
208 \qmlsignal AbstractSeries::nameChanged()
209 This signal is emitted when the series \l name changes.
210*/
211
212/*!
213 \qmlsignal AbstractSeries::visibleChanged()
214 This signal is emitted when the series visibility changes.
215*/
216
217/*!
218 \qmlsignal AbstractSeries::selectableChanged()
219 This signal is emitted when the series \l selectable changes.
220*/
221
222/*!
223 \qmlsignal AbstractSeries::hoverableChanged()
224 This signal is emitted when the series \l hoverable changes.
225*/
226
227/*!
228 \qmlsignal AbstractSeries::opacityChanged()
229 This signal is emitted when the \l opacity of the series changes.
230*/
231
232/*!
233 \qmlsignal AbstractSeries::valuesMultiplierChanged()
234 This signal is emitted when the valuesMultiplier of the series changes.
235*/
236
237/*!
238 \fn void QAbstractSeries::hoverEnter(const QString &seriesName, QPointF position, QPointF value)
239 This signal is emitted when the series hovering starts. The name of the series is in \a seriesName,
240 the mouse/touch position in \a position, and the series value in \a value.
241 \note This signal is only emitted when \l hoverable is set to true.
242*/
243
244/*!
245 \fn void QAbstractSeries::hoverExit(const QString &seriesName, QPointF position)
246 This signal is emitted when the series hovering ends. The name of the series is in \a seriesName,
247 and the mouse/touch position in \a position.
248 \note This signal is only emitted when \l hoverable is set to true.
249*/
250
251/*!
252 \fn void QAbstractSeries::hover(const QString &seriesName, QPointF position, QPointF value)
253 This signal is emitted when the series hovering changes. The name of the series is in \a seriesName,
254 the mouse/touch position in \a position, and the series value in \a value.
255 \note This signal is only emitted when \l hoverable is set to true.
256*/
257
258/*!
259 \internal
260 \brief Constructs QAbstractSeries object with \a parent.
261*/
262
263QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &dd, QObject *parent)
264 : QObject(dd, parent)
265{}
266
267/*!
268 \brief Virtual destructor for the graph series.
269*/
270QAbstractSeries::~QAbstractSeries()
271{
272 Q_D(QAbstractSeries);
273 if (d->m_graph)
274 d->m_graph->removeSeries(series: this);
275}
276
277QString QAbstractSeries::name() const
278{
279 Q_D(const QAbstractSeries);
280 return d->m_name;
281}
282
283void QAbstractSeries::setName(const QString &name)
284{
285 Q_D(QAbstractSeries);
286 if (name != d->m_name) {
287 d->m_name = name;
288 update();
289 emit nameChanged();
290 }
291}
292
293bool QAbstractSeries::isVisible() const
294{
295 Q_D(const QAbstractSeries);
296 return d->m_visible;
297}
298
299void QAbstractSeries::setVisible(bool visible)
300{
301 Q_D(QAbstractSeries);
302 if (visible != d->m_visible) {
303 d->m_visible = visible;
304 update();
305 emit visibleChanged();
306 }
307}
308
309bool QAbstractSeries::isSelectable() const
310{
311 Q_D(const QAbstractSeries);
312 return d->m_selectable;
313}
314
315void QAbstractSeries::setSelectable(bool selectable)
316{
317 Q_D(QAbstractSeries);
318 if (selectable != d->m_selectable) {
319 d->m_selectable = selectable;
320 update();
321 emit selectableChanged();
322 }
323}
324
325bool QAbstractSeries::isHoverable() const
326{
327 Q_D(const QAbstractSeries);
328 return d->m_hoverable;
329}
330
331void QAbstractSeries::setHoverable(bool hoverable)
332{
333 Q_D(QAbstractSeries);
334 if (hoverable != d->m_hoverable) {
335 d->m_hoverable = hoverable;
336 update();
337 emit hoverableChanged();
338 }
339}
340
341bool QAbstractSeries::hasLoaded() const
342{
343 Q_D(const QAbstractSeries);
344 return d->m_loaded;
345}
346
347qreal QAbstractSeries::opacity() const
348{
349 Q_D(const QAbstractSeries);
350 return d->m_opacity;
351}
352
353void QAbstractSeries::setOpacity(qreal opacity)
354{
355 Q_D(QAbstractSeries);
356 if (opacity != d->m_opacity) {
357 d->m_opacity = opacity;
358 update();
359 emit opacityChanged();
360 }
361}
362
363qreal QAbstractSeries::valuesMultiplier() const
364{
365 Q_D(const QAbstractSeries);
366 return d->m_valuesMultiplier;
367}
368
369void QAbstractSeries::setValuesMultiplier(qreal valuesMultiplier)
370{
371 Q_D(QAbstractSeries);
372 valuesMultiplier = std::clamp<qreal>(val: valuesMultiplier, lo: 0.0, hi: 1.0);
373 if (valuesMultiplier != d->m_valuesMultiplier) {
374 d->m_valuesMultiplier = valuesMultiplier;
375 update();
376 emit valuesMultiplierChanged();
377 }
378}
379
380/*!
381 Returns the graph that the series belongs to.
382
383 Set automatically when the series is added to the graph,
384 and unset when the series is removed from the graph.
385*/
386QGraphsView *QAbstractSeries::graph() const
387{
388 Q_D(const QAbstractSeries);
389 return d->m_graph;
390}
391
392void QAbstractSeries::setGraph(QGraphsView *graph)
393{
394 Q_D(QAbstractSeries);
395 d->m_graph = graph;
396 if (graph) {
397 switch (type()) {
398 case SeriesType::Bar:
399 graph->createBarsRenderer();
400 break;
401 case SeriesType::Scatter:
402 case SeriesType::Line:
403 case SeriesType::Spline:
404 graph->createPointRenderer();
405 break;
406 case SeriesType::Pie:
407 graph->createPieRenderer();
408 break;
409 case SeriesType::Area:
410 graph->createAreaRenderer();
411 break;
412 default:
413 break;
414 }
415 }
416}
417
418/*!
419 Sets the visibility of the series to \c true.
420
421 \sa setVisible(), isVisible()
422*/
423void QAbstractSeries::show()
424{
425 setVisible(true);
426}
427
428/*!
429 Sets the visibility of the series to \c false.
430
431 \sa setVisible(), isVisible()
432*/
433void QAbstractSeries::hide()
434{
435 setVisible(false);
436}
437
438const QList<QLegendData> QAbstractSeries::legendData() const
439{
440 Q_D(const QAbstractSeries);
441 return d->m_legendData;
442}
443
444QQmlListProperty<QObject> QAbstractSeries::seriesChildren()
445{
446 return QQmlListProperty<QObject>(this, 0, &QAbstractSeriesPrivate::appendSeriesChildren, 0, 0, 0);
447}
448
449void QAbstractSeries::classBegin()
450{
451}
452
453void QAbstractSeries::componentComplete()
454{
455 Q_D(QAbstractSeries);
456 d->m_loaded = true;
457}
458
459///////////////////////////////////////////////////////////////////////////////////////////////////
460
461QAbstractSeriesPrivate::QAbstractSeriesPrivate()
462{
463}
464
465QAbstractSeriesPrivate::~QAbstractSeriesPrivate()
466{
467}
468
469void QAbstractSeriesPrivate::setLegendData(const QList<QLegendData> &legendData)
470{
471 if (legendData.data() != m_legendData.data()) {
472 Q_Q(QAbstractSeries);
473 m_legendData = legendData;
474 emit q->legendDataChanged();
475 }
476}
477
478void QAbstractSeriesPrivate::clearLegendData()
479{
480 if (!m_legendData.empty()) {
481 Q_Q(QAbstractSeries);
482 m_legendData.clear();
483 emit q->legendDataChanged();
484 }
485}
486
487void QAbstractSeriesPrivate::appendSeriesChildren(QQmlListProperty<QObject> *list, QObject *element)
488{
489 // Empty implementation; the children are parsed in componentComplete instead
490 Q_UNUSED(list);
491 Q_UNUSED(element);
492}
493QT_END_NAMESPACE
494
495#include "moc_qabstractseries.cpp"
496#include "moc_qabstractseries_p.cpp"
497

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtgraphs/src/graphs2d/qabstractseries.cpp