summaryrefslogtreecommitdiffstats
path: root/src/charts/scatterchart/qscatterseries.cpp
blob: 1467a83931eef59c4da0f127f45ab0433c6e46c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCharts/QScatterSeries>
#include <private/qscatterseries_p.h>
#include <private/scatterchartitem_p.h>
#include <private/chartdataset_p.h>
#include <private/charttheme_p.h>
#include <private/scatteranimation_p.h>
#include <private/qchart_p.h>

/*!
    \class QScatterSeries
    \inmodule QtCharts
    \brief The QScatterSeries class presents data in scatter charts.

    The scatter data is displayed as a collection of points on the chart. For
    each point, two values are specified that determine its position on the
    horizontal axis and the vertical axis.

    \image examples_scatterchart.png

    The following code snippet illustrates how to create a basic scatter chart:
    \code
    QScatterSeries* series = new QScatterSeries();
    series->append(0, 6);
    series->append(2, 4);
    ...
    chart->addSeries(series);
    \endcode

    For more information, see \l{ScatterChart Example} and
    \l {Scatter Interactions Example}.
*/
/*!
    \qmltype ScatterSeries
    \instantiates QScatterSeries
    \inqmlmodule QtCharts

    \inherits XYSeries

    \brief The ScatterSeries type presents data in scatter charts.

    The scatter data is displayed as a collection of points on the chart. For
    each point, two values are specified that determine its position on the
    horizontal axis and the vertical axis.

    \image examples_qmlchart5.png

    The following QML code shows how to create a chart with two simple scatter
    series:
    \snippet qmlchart/qml/qmlchart/View5.qml 1

    For more information, see \l{Qml Charts Example}.
*/

/*!
    \enum QScatterSeries::MarkerShape

    This enum value describes the shape used when rendering marker items.

    \value MarkerShapeCircle
           The marker is a circle. This is the default value.
    \value MarkerShapeRectangle
           The marker is a rectangle.
    \value MarkerShapeRotatedRectangle
           The marker is a rotated rectangle.
    \value MarkerShapeTriangle
           The marker is a triangle.
    \value MarkerShapeStar
           The marker is a star.
    \value MarkerShapePentagon
           The marker is a pentagon.
*/

/*!
    \property QScatterSeries::brush
    \brief The brush used to draw the scatter series markers.

    The brush can be an image that can be created using QPainterPath,
    for example.
*/

/*!
    \qmlproperty brush ScatterSeries::brush
    The brush used to draw the scatter series markers.
*/

/*!
    \property QScatterSeries::color
    \brief The color used to fill the series markers.

    This is a convenience property for modifying the color of the brush.
    \sa QScatterSeries::brush()
*/

/*!
    \property QScatterSeries::borderColor
    \brief The color used to draw the marker borders.

    This is a convenience property for modifying the color of the pen.
    \sa QScatterSeries::pen()
*/

/*!
    \qmlproperty int ScatterSeries::count
    The number of data points in the series.
*/

/*!
    \qmlproperty color ScatterSeries::borderColor
    The color used to draw the marker borders.
*/

/*!
    \qmlproperty real ScatterSeries::borderWidth
    The width of the border line. By default, the width is 2.0.
*/

/*!
    \property QScatterSeries::markerShape
    \brief The shape of the marker used to render the points in the series.

    The default shape is MarkerShapeCircle.

    \sa MarkerShape
*/
/*!
    \qmlproperty enumeration ScatterSeries::markerShape

    The shape used when rendering marker items:

    \value ScatterSeries.MarkerShapeCircle
           The marker is a circle. This is the default value.
    \value ScatterSeries.MarkerShapeRectangle
           The marker is a rectangle.
*/

/*!
    \property QScatterSeries::markerSize
    \brief The size of the marker used to render the points in the series.

    \sa QXYSeries::setMarkerSize
*/
/*!
    \qmlproperty real ScatterSeries::markerSize
    The size of the marker used to render the points in the series.
*/

/*!
    \qmlproperty string ScatterSeries::brushFilename
    The name of the file used as a brush for the series.
*/

/*!
    \fn void QScatterSeries::colorChanged(QColor color)
    This signal is emitted when the fill (brush) color changes to \a color.
*/

/*!
    \fn void QScatterSeries::borderColorChanged(QColor color)
    This signal is emitted when the line (pen) color changes to \a color.
*/

/*!
    \fn void QScatterSeries::markerShapeChanged(MarkerShape shape)
    This signal is emitted when the marker shape changes to \a shape.
*/

/*!
    \fn void QScatterSeries::markerSizeChanged(qreal size)
    This signal is emitted when the marker size changes to \a size.
*/

QT_BEGIN_NAMESPACE

/*!
    Constructs a series object that is a child of \a parent.
*/
QScatterSeries::QScatterSeries(QObject *parent)
    : QXYSeries(*new QScatterSeriesPrivate(this), parent)
{
    // Emit QScatterSeries' markerSizeChanged signal as it's not the same as
    // QXYSeries' markerSizeChanged
    connect(this, &QXYSeries::markerSizeChanged, this, &QScatterSeries::markerSizeChanged);
}

/*!
    Deletes the scatter series.

    \note Adding the series to QChart transfers the ownership to the chart.
*/
QScatterSeries::~QScatterSeries()
{
    Q_D(QScatterSeries);
    if (d->m_chart)
        d->m_chart->removeSeries(this);
}

/*!
    \reimp
*/
QAbstractSeries::SeriesType QScatterSeries::type() const
{
    return QAbstractSeries::SeriesTypeScatter;
}

/*!
    \reimp
*/
void QScatterSeries::setPen(const QPen &pen)
{
    Q_D(QXYSeries);
    if (d->m_pen != pen) {
        bool emitColorChanged = d->m_pen.color() != pen.color();
        d->m_pen = pen;
        emit d->seriesUpdated();
        if (emitColorChanged)
            emit borderColorChanged(pen.color());
    }
}

/*!
    \reimp
*/
void QScatterSeries::setBrush(const QBrush &brush)
{
    Q_D(QScatterSeries);
    if (d->m_brush != brush) {
        bool emitColorChanged = d->m_brush.color() != brush.color();
        d->m_brush = brush;
        emit d->seriesUpdated();
        if (emitColorChanged)
            emit colorChanged(brush.color());
    }
}

QBrush QScatterSeries::brush() const
{
    Q_D(const QScatterSeries);
    if (d->m_brush == QChartPrivate::defaultBrush())
        return QBrush();
    else
        return d->m_brush;
}

void QScatterSeries::setColor(const QColor &color)
{
    QBrush b = brush();
    if (b == QChartPrivate::defaultBrush())
        b = QBrush();
    if (b == QBrush())
        b.setStyle(Qt::SolidPattern);
    b.setColor(color);
    setBrush(b);
}

QColor QScatterSeries::color() const
{
    return brush().color();
}

void QScatterSeries::setBorderColor(const QColor &color)
{
    QPen p = pen();
    if (p == QChartPrivate::defaultPen())
        p = QPen();
    p.setColor(color);
    setPen(p);
}

QColor QScatterSeries::borderColor() const
{
    return pen().color();
}

QScatterSeries::MarkerShape QScatterSeries::markerShape() const
{
    Q_D(const QScatterSeries);
    return d->m_shape;
}

void QScatterSeries::setMarkerShape(MarkerShape shape)
{
    Q_D(QScatterSeries);
    if (d->m_shape != shape) {
        d->m_shape = shape;
        emit d->seriesUpdated();
        emit markerShapeChanged(shape);
    }
}

qreal QScatterSeries::markerSize() const
{
    // markerSize has moved to QXYSeries, but this method needs to remain for API compatibility.
    return QXYSeries::markerSize();
}
void QScatterSeries::setMarkerSize(qreal size)
{
    // markerSize has moved to QXYSeries, but this method needs to remain for API compatibility.
    QXYSeries::setMarkerSize(size);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

QScatterSeriesPrivate::QScatterSeriesPrivate(QScatterSeries *q)
    : QXYSeriesPrivate(q),
      m_shape(QScatterSeries::MarkerShapeCircle)
{
}

void QScatterSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
{
    Q_Q(QScatterSeries);
    ScatterChartItem *scatter = new ScatterChartItem(q,parent);
    m_item.reset(scatter);
    QAbstractSeriesPrivate::initializeGraphics(parent);
}

void QScatterSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
{
    Q_Q(QScatterSeries);
    const QList<QColor> colors = theme->seriesColors();
    const QList<QGradient> gradients = theme->seriesGradients();

    if (forced || QChartPrivate::defaultPen() == m_pen) {
        QPen pen;
        pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0));
        pen.setWidthF(2);
        q->setPen(pen);
    }

    if (forced || QChartPrivate::defaultBrush() == m_brush) {
        QBrush brush(colors.at(index % colors.size()));
        q->setBrush(brush);
    }

    if (forced || QChartPrivate::defaultPen().color() == m_pointLabelsColor) {
        QColor color = theme->labelBrush().color();
        q->setPointLabelsColor(color);
    }
}

void QScatterSeriesPrivate::initializeAnimations(QChart::AnimationOptions options, int duration,
                                                 QEasingCurve &curve)
{
    ScatterChartItem *item = static_cast<ScatterChartItem *>(m_item.data());
    Q_ASSERT(item);

    if (item->animation())
        item->animation()->stopAndDestroyLater();

    if (options.testFlag(QChart::SeriesAnimations))
        item->setAnimation(new ScatterAnimation(item, duration, curve));
    else
        item->setAnimation(0);

    QAbstractSeriesPrivate::initializeAnimations(options, duration, curve);
}

QT_END_NAMESPACE

#include "moc_qscatterseries.cpp"