qscatterseries.cpp
333 lines
| 8.8 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
|
r2776 | ** Copyright (C) 2015 The Qt Company Ltd | ||
|
r794 | ** All rights reserved. | ||
|
r2776 | ** For any questions to The Qt Company, please use contact form at http://qt.io | ||
|
r794 | ** | ||
|
r2740 | ** This file is part of the Qt Charts module. | ||
|
r794 | ** | ||
|
r2740 | ** Licensees holding valid commercial license for Qt may use this file in | ||
** accordance with the Qt License Agreement provided with the Software | ||||
** or, alternatively, in accordance with the terms contained in a written | ||||
|
r2776 | ** agreement between you and The Qt Company. | ||
|
r794 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
|
r2740 | ** contact form at http://qt.io | ||
|
r794 | ** | ||
****************************************************************************/ | ||||
|
r2714 | #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> | ||||
|
r42 | |||
|
r300 | /*! | ||
\class QScatterSeries | ||||
|
r2639 | \inmodule Qt Charts | ||
|
r481 | \brief The QScatterSeries class is used for making scatter charts. | ||
|
r42 | |||
|
r481 | The scatter data is displayed as a collection of points on the chart. Each point determines the position on the horizontal axis | ||
and the vertical axis. | ||||
|
r300 | |||
|
r995 | \image examples_scatterchart.png | ||
|
r300 | |||
|
r481 | Creating basic scatter chart is simple: | ||
\code | ||||
QScatterSeries* series = new QScatterSeries(); | ||||
|
r796 | series->append(0, 6); | ||
series->append(2, 4); | ||||
|
r481 | ... | ||
|
r995 | chart->addSeries(series); | ||
|
r481 | \endcode | ||
|
r300 | */ | ||
|
r1491 | /*! | ||
|
r2639 | \qmltype ScatterSeries | ||
\instantiates QScatterSeries | ||||
|
r2712 | \inqmlmodule QtCharts | ||
|
r1491 | |||
|
r2712 | \inherits XYSeries | ||
\brief The ScatterSeries type is used for making scatter charts. | ||||
The following QML shows how to create a chart with two simple scatter series: | ||||
\snippet qmlchart/qml/qmlchart/View5.qml 1 | ||||
|
r1491 | |||
|
r2712 | \beginfloatleft | ||
\image examples_qmlchart5.png | ||||
\endfloat | ||||
\clearfloat | ||||
|
r1491 | */ | ||
|
r42 | |||
|
r261 | /*! | ||
\enum QScatterSeries::MarkerShape | ||||
This enum describes the shape used when rendering marker items. | ||||
\value MarkerShapeCircle | ||||
|
r541 | \value MarkerShapeRectangle | ||
|
r261 | */ | ||
|
r2681 | /*! | ||
\property QScatterSeries::brush | ||||
Brush used to draw the series. | ||||
*/ | ||||
|
r1341 | /*! | ||
|
r1481 | \property QScatterSeries::color | ||
Fill (brush) color of the series. This is a convenience property for modifying the color of brush. | ||||
\sa QScatterSeries::brush() | ||||
|
r1341 | */ | ||
|
r1349 | /*! | ||
|
r1481 | \property QScatterSeries::borderColor | ||
Line (pen) color of the series. This is a convenience property for modifying the color of pen. | ||||
\sa QScatterSeries::pen() | ||||
|
r1349 | */ | ||
|
r2115 | /*! | ||
\qmlproperty color ScatterSeries::borderColor | ||||
Border (pen) color of the series. | ||||
*/ | ||||
|
r1349 | |||
|
r1904 | /*! | ||
\qmlproperty real ScatterSeries::borderWidth | ||||
The width of the border line. By default the width is 2.0. | ||||
*/ | ||||
|
r1349 | /*! | ||
|
r1491 | \property QScatterSeries::markerShape | ||
Defines the shape of the marker used to draw the points in the series. The default shape is MarkerShapeCircle. | ||||
|
r1349 | */ | ||
|
r1341 | /*! | ||
|
r1491 | \qmlproperty MarkerShape ScatterSeries::markerShape | ||
Defines the shape of the marker used to draw the points in the series. One of ScatterSeries | ||||
ScatterSeries.MarkerShapeCircle or ScatterSeries.MarkerShapeRectangle. | ||||
The default shape is ScatterSeries.MarkerShapeCircle. | ||||
*/ | ||||
|
r1341 | |||
|
r1491 | /*! | ||
\property QScatterSeries::markerSize | ||||
Defines the size of the marker used to draw the points in the series. The default size is 15.0. | ||||
*/ | ||||
/*! | ||||
\qmlproperty real ScatterSeries::markerSize | ||||
Defines the size of the marker used to draw the points in the series. The default size is 15.0. | ||||
|
r1481 | */ | ||
|
r2681 | /*! | ||
\qmlproperty QString ScatterSeries::brushFilename | ||||
The name of the file used as a brush for the series. | ||||
*/ | ||||
|
r1481 | /*! | ||
\fn void QScatterSeries::colorChanged(QColor color) | ||||
|
r1491 | Signal is emitted when the fill (brush) color has changed to \a color. | ||
|
r1481 | */ | ||
/*! | ||||
\fn void QScatterSeries::borderColorChanged(QColor color) | ||||
|
r1491 | Signal is emitted when the line (pen) color has changed to \a color. | ||
*/ | ||||
/*! | ||||
\qmlsignal ScatterSeries::borderColorChanged(color color) | ||||
Signal is emitted when the line (pen) color has changed to \a color. | ||||
|
r1341 | */ | ||
|
r260 | /*! | ||
|
r2520 | \fn QAbstractSeries::SeriesType QScatterSeries::type() const | ||
Returns QAbstractSeries::SeriesTypeScatter. | ||||
|
r1107 | \sa QAbstractSeries, SeriesType | ||
|
r300 | */ | ||
|
r260 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r300 | |||
|
r261 | /*! | ||
Constructs a series object which is a child of \a parent. | ||||
*/ | ||||
|
r2097 | QScatterSeries::QScatterSeries(QObject *parent) | ||
: QXYSeries(*new QScatterSeriesPrivate(this), parent) | ||||
|
r42 | { | ||
} | ||||
|
r260 | /*! | ||
|
r261 | Destroys the object. Note that adding series to QChart transfers the ownership to the chart. | ||
|
r260 | */ | ||
|
r158 | QScatterSeries::~QScatterSeries() | ||
|
r42 | { | ||
|
r1270 | Q_D(QScatterSeries); | ||
|
r2273 | if (d->m_chart) | ||
d->m_chart->removeSeries(this); | ||||
|
r48 | } | ||
|
r1107 | QAbstractSeries::SeriesType QScatterSeries::type() const | ||
|
r938 | { | ||
|
r988 | return QAbstractSeries::SeriesTypeScatter; | ||
|
r938 | } | ||
|
r1651 | /*! | ||
Sets \a pen used for drawing points' border on the chart. If the pen is not defined, the | ||||
pen from chart theme is used. | ||||
\sa QChart::setTheme() | ||||
*/ | ||||
|
r1537 | 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->updated(); | ||||
if (emitColorChanged) | ||||
emit borderColorChanged(pen.color()); | ||||
} | ||||
} | ||||
|
r1651 | /*! | ||
Sets \a brush used for drawing points on the chart. If the brush is not defined, brush | ||||
from chart theme setting is used. | ||||
\sa QChart::setTheme() | ||||
*/ | ||||
|
r1537 | 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->updated(); | ||||
if (emitColorChanged) | ||||
emit colorChanged(brush.color()); | ||||
} | ||||
} | ||||
|
r2681 | QBrush QScatterSeries::brush() const | ||
{ | ||||
Q_D(const QScatterSeries); | ||||
if (d->m_brush == QChartPrivate::defaultBrush()) | ||||
return QBrush(); | ||||
else | ||||
return d->m_brush; | ||||
} | ||||
|
r1481 | void QScatterSeries::setColor(const QColor &color) | ||
{ | ||||
QBrush b = brush(); | ||||
|
r2670 | if (b == QChartPrivate::defaultBrush()) | ||
b = QBrush(); | ||||
|
r1933 | if (b == QBrush()) | ||
b.setStyle(Qt::SolidPattern); | ||||
b.setColor(color); | ||||
setBrush(b); | ||||
|
r1481 | } | ||
QColor QScatterSeries::color() const | ||||
{ | ||||
return brush().color(); | ||||
} | ||||
void QScatterSeries::setBorderColor(const QColor &color) | ||||
{ | ||||
QPen p = pen(); | ||||
|
r2670 | if (p == QChartPrivate::defaultPen()) | ||
p = QPen(); | ||||
p.setColor(color); | ||||
setPen(p); | ||||
|
r1481 | } | ||
QColor QScatterSeries::borderColor() const | ||||
{ | ||||
return pen().color(); | ||||
} | ||||
|
r1276 | QScatterSeries::MarkerShape QScatterSeries::markerShape() const | ||
|
r195 | { | ||
|
r938 | Q_D(const QScatterSeries); | ||
return d->m_shape; | ||||
|
r195 | } | ||
|
r1276 | void QScatterSeries::setMarkerShape(MarkerShape shape) | ||
|
r195 | { | ||
|
r938 | Q_D(QScatterSeries); | ||
if (d->m_shape != shape) { | ||||
d->m_shape = shape; | ||||
|
r1481 | emit d->updated(); | ||
|
r573 | } | ||
|
r195 | } | ||
|
r1276 | qreal QScatterSeries::markerSize() const | ||
|
r397 | { | ||
|
r938 | Q_D(const QScatterSeries); | ||
return d->m_size; | ||||
|
r397 | } | ||
|
r1276 | void QScatterSeries::setMarkerSize(qreal size) | ||
|
r397 | { | ||
|
r938 | Q_D(QScatterSeries); | ||
|
r2242 | if (!qFuzzyCompare(d->m_size, size)) { | ||
|
r938 | d->m_size = size; | ||
|
r1481 | emit d->updated(); | ||
|
r573 | } | ||
|
r397 | } | ||
|
r938 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
r2104 | QScatterSeriesPrivate::QScatterSeriesPrivate(QScatterSeries *q) | ||
: QXYSeriesPrivate(q), | ||||
m_shape(QScatterSeries::MarkerShapeCircle), | ||||
m_size(15.0) | ||||
|
r938 | { | ||
|
r1537 | } | ||
|
r938 | |||
|
r2273 | void QScatterSeriesPrivate::initializeGraphics(QGraphicsItem* parent) | ||
|
r943 | { | ||
Q_Q(QScatterSeries); | ||||
|
r2273 | 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(); | ||||
|
r2516 | if (forced || QChartPrivate::defaultPen() == m_pen) { | ||
QPen pen; | ||||
|
r2273 | pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0)); | ||
pen.setWidthF(2); | ||||
q->setPen(pen); | ||||
} | ||||
|
r2516 | if (forced || QChartPrivate::defaultBrush() == m_brush) { | ||
|
r2273 | QBrush brush(colors.at(index % colors.size())); | ||
q->setBrush(brush); | ||||
} | ||||
|
r2689 | |||
if (forced || QChartPrivate::defaultPen().color() == m_pointLabelsColor) { | ||||
QColor color = theme->labelBrush().color(); | ||||
q->setPointLabelsColor(color); | ||||
} | ||||
|
r943 | } | ||
|
r2804 | void QScatterSeriesPrivate::initializeAnimations(QChart::AnimationOptions options, int duration, | ||
QEasingCurve &curve) | ||||
|
r2492 | { | ||
ScatterChartItem *item = static_cast<ScatterChartItem *>(m_item.data()); | ||||
Q_ASSERT(item); | ||||
|
r2555 | if (item->animation()) | ||
item->animation()->stopAndDestroyLater(); | ||||
|
r2492 | if (options.testFlag(QChart::SeriesAnimations)) | ||
|
r2804 | item->setAnimation(new ScatterAnimation(item, duration, curve)); | ||
|
r2492 | else | ||
item->setAnimation(0); | ||||
|
r2804 | QAbstractSeriesPrivate::initializeAnimations(options, duration, curve); | ||
|
r2492 | } | ||
|
r1276 | #include "moc_qscatterseries.cpp" | ||
|
r938 | |||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||