KQuickCharts

LineChart.h
1/*
2 * This file is part of KQuickCharts
3 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#ifndef LINECHART_H
9#define LINECHART_H
10
11#include <memory>
12
13#include <qqmlregistration.h>
14
15#include "XYChart.h"
16
17class LineChartNode;
18
19/**
20 * An attached property that is exposed to point delegates created in line charts.
21 *
22 * \sa LineChart::pointDelegate
23 */
25{
27 QML_ANONYMOUS
28
29public:
31
32 /**
33 * The value at the current point.
34 */
35 Q_PROPERTY(QVariant value READ value NOTIFY valueChanged)
36 QVariant value() const;
37 void setValue(const QVariant &value);
38 Q_SIGNAL void valueChanged();
39
40 /**
41 * The color at the current point.
42 */
43 Q_PROPERTY(QColor color READ color NOTIFY colorChanged)
44 QColor color() const;
45 void setColor(const QColor &color);
46 Q_SIGNAL void colorChanged();
47
48 /**
49 * The name at the current point.
50 */
51 Q_PROPERTY(QString name READ name NOTIFY nameChanged)
52 QString name() const;
53 void setName(const QString &newName);
54 Q_SIGNAL void nameChanged();
55
56 /**
57 * The short name at the current point.
58 */
59 Q_PROPERTY(QString shortName READ shortName NOTIFY shortNameChanged)
60 QString shortName() const;
61 void setShortName(const QString &newShortName);
62 Q_SIGNAL void shortNameChanged();
63
64private:
65 QVariant m_value;
66 QColor m_color;
67 QString m_name;
68 QString m_shortName;
69};
70
71/**
72 * A line chart.
73 *
74 * ## Usage example
75 *
76 * \snippet snippets/linechart.qml example
77 *
78 * \image html linechart.png "The resulting line chart."
79 */
80class QUICKCHARTS_EXPORT LineChart : public XYChart
81{
82 Q_OBJECT
83 QML_ELEMENT
84 QML_ATTACHED(LineChartAttached)
85
86public:
87 explicit LineChart(QQuickItem *parent = nullptr);
88
89 /**
90 * Interpolate the values in the chart so that the lines become smoothed.
91 */
92 Q_PROPERTY(bool interpolate READ interpolate WRITE setInterpolate NOTIFY interpolateChanged)
93 bool interpolate() const;
94 void setInterpolate(bool newInterpolate);
95 Q_SIGNAL void interpolateChanged();
96 /**
97 * The width of a line in the chart.
98 */
99 Q_PROPERTY(qreal lineWidth READ lineWidth WRITE setLineWidth NOTIFY lineWidthChanged)
100 qreal lineWidth() const;
101 void setLineWidth(qreal width);
102 Q_SIGNAL void lineWidthChanged();
103 /**
104 * The opacity of the area below a line.
105 *
106 * The default is 0.0. Note that if fillColorSource is set, this value is
107 * ignored.
108 */
109 Q_PROPERTY(qreal fillOpacity READ fillOpacity WRITE setFillOpacity NOTIFY fillOpacityChanged)
110 qreal fillOpacity() const;
111 void setFillOpacity(qreal opacity);
112 Q_SIGNAL void fillOpacityChanged();
113 /**
114 * A data source that supplies color values for the line charts' fill area.
115 *
116 * If this is not set (the default), the normal color source will be used,
117 * with the fillOpacity used as its opacity.
118 */
119 Q_PROPERTY(ChartDataSource *fillColorSource READ fillColorSource WRITE setFillColorSource NOTIFY fillColorSourceChanged)
120 ChartDataSource *fillColorSource() const;
121 void setFillColorSource(ChartDataSource *newFillColorSource);
122 Q_SIGNAL void fillColorSourceChanged();
123 /**
124 * A delegate that will be placed at each line chart point.
125 *
126 * When this is not null, the specified component will be used to
127 * instantiate an object for each point in the chart. These objects will
128 * then be placed centered at positions corresponding to the points on the
129 * chart. Each instance will have access to the attached properties of
130 * LineChartAttached through LineChart attached object.
131 *
132 * \note The component assigned to this property is expected to create a
133 * QQuickItem, since the created object needs to be positioned.
134 */
135 Q_PROPERTY(QQmlComponent *pointDelegate READ pointDelegate WRITE setPointDelegate NOTIFY pointDelegateChanged)
136 QQmlComponent *pointDelegate() const;
137 void setPointDelegate(QQmlComponent *newPointDelegate);
138 Q_SIGNAL void pointDelegateChanged();
139
140 static LineChartAttached *qmlAttachedProperties(QObject *object)
141 {
142 return new LineChartAttached(object);
143 }
144
145protected:
146 void updatePolish() override;
147 QSGNode *updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *data) override;
148 void onDataChanged() override;
149 void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
150
151private:
152 void updateLineNode(LineChartNode *node, ChartDataSource *valueSource, const QColor &lineColor, const QColor &fillColor, qreal lineWidth);
153 void createPointDelegates(const QList<QVector2D> &values, int sourceIndex);
154 void updatePointDelegate(QQuickItem *delegate, const QVector2D &position, const QVariant &value, int sourceIndex);
155
156 bool m_interpolate = false;
157 qreal m_lineWidth = 1.0;
158 qreal m_fillOpacity = 0.0;
159 bool m_rangeInvalid = true;
160 ChartDataSource *m_fillColorSource = nullptr;
162 QQmlComponent *m_pointDelegate = nullptr;
164};
165
166#endif // LINECHART_H
Abstract base class for data sources.
virtual void onDataChanged()=0
Called when the data of a value source changes.
An attached property that is exposed to point delegates created in line charts.
Definition LineChart.h:25
QVariant value
The value at the current point.
Definition LineChart.h:35
QString shortName
The short name at the current point.
Definition LineChart.h:59
QColor color
The color at the current point.
Definition LineChart.h:43
QString name
The name at the current point.
Definition LineChart.h:51
A line chart.
Definition LineChart.h:81
A base class for Charts that are based on an X/Y grid.
Definition XYChart.h:33
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALQ_SIGNAL
QObject * parent() const const
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
virtual QSGNode * updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData)
virtual void updatePolish()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:23 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.