1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <QtCharts/QLineSeries>
5#include <private/qlineseries_p.h>
6#include <private/linechartitem_p.h>
7#include <private/chartdataset_p.h>
8#include <private/charttheme_p.h>
9#include <private/qchart_p.h>
10QT_BEGIN_NAMESPACE
11
12/*!
13 \class QLineSeries
14 \inmodule QtCharts
15 \brief The QLineSeries class presents data in line charts.
16
17 A line chart is used to show information as a series of data points
18 connected by straight lines.
19
20 \image examples_linechart.png
21
22 Creating a basic line chart is simple:
23 \code
24 QLineSeries* series = new QLineSeries();
25 series->append(0, 6);
26 series->append(2, 4);
27 ...
28 chart->addSeries(series);
29 \endcode
30*/
31/*!
32 \qmltype LineSeries
33 \instantiates QLineSeries
34 \inqmlmodule QtCharts
35
36 \inherits XYSeries
37
38 \brief Presents data in line charts.
39
40 A line chart is used to show information as a series of data points
41 connected by straight lines.
42
43 The following QML example shows how to create a simple line chart:
44 \snippet qmlchartsgallery/qml/LineSeries.qml 1
45 \beginfloatleft
46 \image examples_qmlchart2.png
47 \endfloat
48 \clearfloat
49*/
50
51/*!
52 \qmlproperty int LineSeries::count
53 The number of data points in the series.
54*/
55
56/*!
57 \qmlproperty real LineSeries::width
58 The width of the line. By default, the width is 2.0.
59*/
60
61/*!
62 \qmlproperty Qt::PenStyle LineSeries::style
63 Controls the style of the line. Set to one of \l{Qt::NoPen}{Qt.NoPen},
64 \l{Qt::SolidLine}{Qt.SolidLine}, \l{Qt::DashLine}{Qt.DashLine}, \l{Qt::DotLine}{Qt.DotLine},
65 \l{Qt::DashDotLine}{Qt.DashDotLine}, or \l{Qt::DashDotDotLine}{Qt.DashDotDotLine}.
66 Using \l{Qt::CustomDashLine}{Qt.CustomDashLine} is not supported in the QML API.
67 By default, the style is Qt.SolidLine.
68
69 \sa Qt::PenStyle
70*/
71
72/*!
73 \qmlproperty Qt::PenCapStyle LineSeries::capStyle
74 Controls the cap style of the line. Set to one of \l{Qt::FlatCap}{Qt.FlatCap},
75 \l{Qt::SquareCap}{Qt.SquareCap} or \l{Qt::RoundCap}{Qt.RoundCap}. By
76 default the cap style is Qt.SquareCap.
77
78 \sa Qt::PenCapStyle
79*/
80
81/*!
82 Constructs an empty series object that is a child of \a parent.
83 When the series object is added to a QChartView or QChart instance, the ownership
84 is transferred.
85*/
86QLineSeries::QLineSeries(QObject *parent)
87 : QXYSeries(*new QLineSeriesPrivate(this), parent)
88{
89
90}
91
92/*!
93 \internal
94*/
95QLineSeries::QLineSeries(QLineSeriesPrivate &d, QObject *parent)
96 : QXYSeries(d, parent)
97{
98
99}
100/*!
101 Destroys the object. Series added to QChartView or QChart instances are owned by the
102 instances and deleted when the instances are destroyed.
103*/
104QLineSeries::~QLineSeries()
105{
106 Q_D(QLineSeries);
107 if (d->m_chart)
108 d->m_chart->removeSeries(series: this);
109}
110
111/*!
112 \reimp
113*/
114QAbstractSeries::SeriesType QLineSeries::type() const
115{
116 return QAbstractSeries::SeriesTypeLine;
117}
118
119/*
120QDebug operator<< (QDebug debug, const QLineSeries series)
121{
122 Q_ASSERT(series.d_func()->m_x.size() == series.d_func()->m_y.size());
123 int size = series.d_func()->m_x.size();
124 for (int i=0; i<size; i++) {
125 debug.nospace() << "(" << series.d_func()->m_x.at(i) << ',' << series.d_func()->m_y.at(i) << ") ";
126 }
127 return debug.space();
128}
129*/
130
131////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
132
133QLineSeriesPrivate::QLineSeriesPrivate(QLineSeries *q)
134 : QXYSeriesPrivate(q)
135{
136 m_markerSize = m_pen.widthF() * 1.5;
137}
138
139void QLineSeriesPrivate::initializeGraphics(QGraphicsItem *parent)
140{
141 Q_Q(QLineSeries);
142 LineChartItem *line = new LineChartItem(q,parent);
143 m_item.reset(p: line);
144 QAbstractSeriesPrivate::initializeGraphics(parent);
145}
146
147void QLineSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
148{
149 Q_Q(QLineSeries);
150 const QList<QColor> colors = theme->seriesColors();
151
152 if (forced || QChartPrivate::defaultPen() == m_pen) {
153 QPen pen;
154 pen.setColor(colors.at(i: index % colors.size()));
155 pen.setWidthF(2);
156 q->setPen(pen);
157 }
158
159 if (forced || QChartPrivate::defaultPen().color() == m_pointLabelsColor) {
160 QColor color = theme->labelBrush().color();
161 q->setPointLabelsColor(color);
162 }
163}
164
165QT_END_NAMESPACE
166
167#include "moc_qlineseries.cpp"
168

source code of qtcharts/src/charts/linechart/qlineseries.cpp