1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include <QtCharts/QSplineSeries> |
5 | #include <private/qsplineseries_p.h> |
6 | #include <private/splinechartitem_p.h> |
7 | #include <private/chartdataset_p.h> |
8 | #include <private/charttheme_p.h> |
9 | #include <private/splineanimation_p.h> |
10 | #include <private/qchart_p.h> |
11 | |
12 | /*! |
13 | \class QSplineSeries |
14 | \inmodule QtCharts |
15 | \brief The QSplineSeries class presents data as spline charts. |
16 | |
17 | A spline series stores the data points and the segment control points needed |
18 | by QPainterPath to draw a spline. The control points are automatically |
19 | calculated when the data changes. The algorithm computes the points so that |
20 | the normal spline can be drawn. |
21 | |
22 | \image examples_splinechart.png |
23 | |
24 | The following code snippet illustrates how to create a basic spline chart: |
25 | \code |
26 | QSplineSeries* series = new QSplineSeries(); |
27 | series->append(0, 6); |
28 | series->append(2, 4); |
29 | ... |
30 | chart->addSeries(series); |
31 | \endcode |
32 | */ |
33 | /*! |
34 | \qmltype SplineSeries |
35 | \instantiates QSplineSeries |
36 | \inqmlmodule QtCharts |
37 | |
38 | \inherits XYSeries |
39 | |
40 | \brief Presents data as spline charts. |
41 | |
42 | A spline series stores the data points and the segment control points needed |
43 | by QPainterPath to draw a spline. The control points are automatically |
44 | calculated when the data changes. The algorithm computes the points so that |
45 | the normal spline can be drawn. |
46 | |
47 | \image examples_qmlchart3.png |
48 | |
49 | The following QML code shows how to create a simple spline chart: |
50 | \snippet qmlchartsgallery/qml/SplineSeries.qml 1 |
51 | */ |
52 | |
53 | /*! |
54 | \qmlproperty real SplineSeries::width |
55 | The width of the line. By default, the width is 2.0. |
56 | */ |
57 | |
58 | /*! |
59 | \qmlproperty Qt::PenStyle SplineSeries::style |
60 | Controls the style of the line. Set to one of \l{Qt::NoPen}{Qt.NoPen}, |
61 | \l{Qt::SolidLine}{Qt.SolidLine}, \l{Qt::DashLine}{Qt.DashLine}, \l{Qt::DotLine}{Qt.DotLine}, |
62 | \l{Qt::DashDotLine}{Qt.DashDotLine}, or \l{Qt::DashDotDotLine}{Qt.DashDotDotLine}. |
63 | Using \l{Qt::CustomDashLine}{Qt.CustomDashLine} is not supported in the QML API. |
64 | By default, the style is Qt.SolidLine. |
65 | |
66 | \sa Qt::PenStyle |
67 | */ |
68 | |
69 | /*! |
70 | \qmlproperty Qt::PenCapStyle SplineSeries::capStyle |
71 | Controls the cap style of the line. Set to one of \l{Qt::FlatCap}{Qt.FlatCap}, |
72 | \l{Qt::SquareCap}{Qt.SquareCap} or \l{Qt::RoundCap}{Qt.RoundCap}. By |
73 | default, the cap style is Qt.SquareCap. |
74 | |
75 | \sa Qt::PenCapStyle |
76 | */ |
77 | |
78 | /*! |
79 | \qmlproperty int SplineSeries::count |
80 | The number of data points in the series. |
81 | */ |
82 | |
83 | QT_BEGIN_NAMESPACE |
84 | |
85 | /*! |
86 | Constructs an empty series object that is a child of \a parent. |
87 | When the series object is added to a QChart instance, the ownerships is |
88 | transferred. |
89 | */ |
90 | |
91 | QSplineSeries::QSplineSeries(QObject *parent) |
92 | : QLineSeries(*new QSplineSeriesPrivate(this), parent) |
93 | { |
94 | } |
95 | |
96 | /*! |
97 | Deletes the spline series. |
98 | */ |
99 | QSplineSeries::~QSplineSeries() |
100 | { |
101 | Q_D(QSplineSeries); |
102 | if (d->m_chart) |
103 | d->m_chart->removeSeries(series: this); |
104 | } |
105 | |
106 | /*! |
107 | \reimp |
108 | */ |
109 | QAbstractSeries::SeriesType QSplineSeries::type() const |
110 | { |
111 | return QAbstractSeries::SeriesTypeSpline; |
112 | } |
113 | |
114 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
115 | |
116 | QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries *q) |
117 | : QLineSeriesPrivate(q) |
118 | { |
119 | } |
120 | |
121 | void QSplineSeriesPrivate::initializeGraphics(QGraphicsItem* parent) |
122 | { |
123 | Q_Q(QSplineSeries); |
124 | SplineChartItem *spline = new SplineChartItem(q,parent); |
125 | m_item.reset(p: spline); |
126 | QAbstractSeriesPrivate::initializeGraphics(parent); |
127 | } |
128 | |
129 | void QSplineSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced) |
130 | { |
131 | Q_Q(QSplineSeries); |
132 | const QList<QColor> colors = theme->seriesColors(); |
133 | |
134 | if (forced || QChartPrivate::defaultPen() == m_pen) { |
135 | QPen pen; |
136 | pen.setColor(colors.at(i: index % colors.size())); |
137 | pen.setWidthF(2); |
138 | q->setPen(pen); |
139 | } |
140 | |
141 | if (forced || QChartPrivate::defaultPen().color() == m_pointLabelsColor) { |
142 | QColor color = theme->labelBrush().color(); |
143 | q->setPointLabelsColor(color); |
144 | } |
145 | } |
146 | |
147 | void QSplineSeriesPrivate::initializeAnimations(QChart::AnimationOptions options, |
148 | int duration, QEasingCurve &curve) |
149 | { |
150 | SplineChartItem *item = static_cast<SplineChartItem *>(m_item.get()); |
151 | Q_ASSERT(item); |
152 | if (item->animation()) |
153 | item->animation()->stopAndDestroyLater(); |
154 | |
155 | if (options.testFlag(flag: QChart::SeriesAnimations)) |
156 | item->setAnimation(new SplineAnimation(item, duration, curve)); |
157 | else |
158 | item->setAnimation(0); |
159 | QAbstractSeriesPrivate::initializeAnimations(options, duration, curve); |
160 | } |
161 | |
162 | QT_END_NAMESPACE |
163 | |
164 | #include "moc_qsplineseries.cpp" |
165 | #include "moc_qsplineseries_p.cpp" |
166 | |