qscatterseries.cpp
336 lines
| 8.8 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r794 | /**************************************************************************** | ||
** | ||||
Titta Heikkala
|
r2688 | ** Copyright (C) 2014 Digia Plc | ||
Jani Honkonen
|
r794 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
Miikka Heikkinen
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
Jani Honkonen
|
r794 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
Miikka Heikkinen
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
Jani Honkonen
|
r794 | ** Software or, alternatively, in accordance with the terms contained in | ||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
Titta Heikkala
|
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> | ||||
Tero Ahola
|
r42 | |||
Tero Ahola
|
r300 | /*! | ||
\class QScatterSeries | ||||
Titta Heikkala
|
r2639 | \inmodule Qt Charts | ||
Michal Klocek
|
r481 | \brief The QScatterSeries class is used for making scatter charts. | ||
Tero Ahola
|
r42 | |||
Tero Ahola
|
r300 | \mainclass | ||
Michal Klocek
|
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. | ||||
Tero Ahola
|
r300 | |||
Tero Ahola
|
r995 | \image examples_scatterchart.png | ||
Tero Ahola
|
r300 | |||
Michal Klocek
|
r481 | Creating basic scatter chart is simple: | ||
\code | ||||
QScatterSeries* series = new QScatterSeries(); | ||||
Jani Honkonen
|
r796 | series->append(0, 6); | ||
series->append(2, 4); | ||||
Michal Klocek
|
r481 | ... | ||
Tero Ahola
|
r995 | chart->addSeries(series); | ||
Michal Klocek
|
r481 | \endcode | ||
Tero Ahola
|
r300 | */ | ||
Tero Ahola
|
r1491 | /*! | ||
Titta Heikkala
|
r2639 | \qmltype ScatterSeries | ||
\instantiates QScatterSeries | ||||
Titta Heikkala
|
r2712 | \inqmlmodule QtCharts | ||
Tero Ahola
|
r1491 | |||
Titta Heikkala
|
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 | ||||
Tero Ahola
|
r1491 | |||
Titta Heikkala
|
r2712 | \beginfloatleft | ||
\image examples_qmlchart5.png | ||||
\endfloat | ||||
\clearfloat | ||||
Tero Ahola
|
r1491 | */ | ||
Tero Ahola
|
r42 | |||
Tero Ahola
|
r261 | /*! | ||
\enum QScatterSeries::MarkerShape | ||||
This enum describes the shape used when rendering marker items. | ||||
\value MarkerShapeCircle | ||||
Michal Klocek
|
r541 | \value MarkerShapeRectangle | ||
Tero Ahola
|
r261 | */ | ||
Titta Heikkala
|
r2681 | /*! | ||
\property QScatterSeries::brush | ||||
Brush used to draw the series. | ||||
*/ | ||||
Jani Honkonen
|
r1341 | /*! | ||
Tero Ahola
|
r1481 | \property QScatterSeries::color | ||
Fill (brush) color of the series. This is a convenience property for modifying the color of brush. | ||||
\sa QScatterSeries::brush() | ||||
Jani Honkonen
|
r1341 | */ | ||
Tero Ahola
|
r1349 | /*! | ||
Tero Ahola
|
r1481 | \property QScatterSeries::borderColor | ||
Line (pen) color of the series. This is a convenience property for modifying the color of pen. | ||||
\sa QScatterSeries::pen() | ||||
Tero Ahola
|
r1349 | */ | ||
Tero Ahola
|
r2115 | /*! | ||
\qmlproperty color ScatterSeries::borderColor | ||||
Border (pen) color of the series. | ||||
*/ | ||||
Tero Ahola
|
r1349 | |||
Tero Ahola
|
r1904 | /*! | ||
\qmlproperty real ScatterSeries::borderWidth | ||||
The width of the border line. By default the width is 2.0. | ||||
*/ | ||||
Tero Ahola
|
r1349 | /*! | ||
Tero Ahola
|
r1491 | \property QScatterSeries::markerShape | ||
Defines the shape of the marker used to draw the points in the series. The default shape is MarkerShapeCircle. | ||||
Tero Ahola
|
r1349 | */ | ||
Jani Honkonen
|
r1341 | /*! | ||
Tero Ahola
|
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. | ||||
*/ | ||||
Jani Honkonen
|
r1341 | |||
Tero Ahola
|
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. | ||||
Tero Ahola
|
r1481 | */ | ||
Titta Heikkala
|
r2681 | /*! | ||
\qmlproperty QString ScatterSeries::brushFilename | ||||
The name of the file used as a brush for the series. | ||||
*/ | ||||
Tero Ahola
|
r1481 | /*! | ||
\fn void QScatterSeries::colorChanged(QColor color) | ||||
Tero Ahola
|
r1491 | Signal is emitted when the fill (brush) color has changed to \a color. | ||
Tero Ahola
|
r1481 | */ | ||
/*! | ||||
\fn void QScatterSeries::borderColorChanged(QColor color) | ||||
Tero Ahola
|
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. | ||||
Jani Honkonen
|
r1341 | */ | ||
Tero Ahola
|
r260 | /*! | ||
Miikka Heikkinen
|
r2520 | \fn QAbstractSeries::SeriesType QScatterSeries::type() const | ||
Returns QAbstractSeries::SeriesTypeScatter. | ||||
Michal Klocek
|
r1107 | \sa QAbstractSeries, SeriesType | ||
Tero Ahola
|
r300 | */ | ||
Tero Ahola
|
r260 | |||
Titta Heikkala
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
Tero Ahola
|
r300 | |||
Tero Ahola
|
r261 | /*! | ||
Constructs a series object which is a child of \a parent. | ||||
*/ | ||||
Jani Honkonen
|
r2097 | QScatterSeries::QScatterSeries(QObject *parent) | ||
: QXYSeries(*new QScatterSeriesPrivate(this), parent) | ||||
Tero Ahola
|
r42 | { | ||
} | ||||
Tero Ahola
|
r260 | /*! | ||
Tero Ahola
|
r261 | Destroys the object. Note that adding series to QChart transfers the ownership to the chart. | ||
Tero Ahola
|
r260 | */ | ||
Tero Ahola
|
r158 | QScatterSeries::~QScatterSeries() | ||
Tero Ahola
|
r42 | { | ||
Michal Klocek
|
r1270 | Q_D(QScatterSeries); | ||
Michal Klocek
|
r2273 | if (d->m_chart) | ||
d->m_chart->removeSeries(this); | ||||
Tero Ahola
|
r48 | } | ||
Michal Klocek
|
r1107 | QAbstractSeries::SeriesType QScatterSeries::type() const | ||
Michal Klocek
|
r938 | { | ||
Tero Ahola
|
r988 | return QAbstractSeries::SeriesTypeScatter; | ||
Michal Klocek
|
r938 | } | ||
Marek Rosa
|
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() | ||||
*/ | ||||
Tero Ahola
|
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()); | ||||
} | ||||
} | ||||
Marek Rosa
|
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() | ||||
*/ | ||||
Tero Ahola
|
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()); | ||||
} | ||||
} | ||||
Titta Heikkala
|
r2681 | QBrush QScatterSeries::brush() const | ||
{ | ||||
Q_D(const QScatterSeries); | ||||
if (d->m_brush == QChartPrivate::defaultBrush()) | ||||
return QBrush(); | ||||
else | ||||
return d->m_brush; | ||||
} | ||||
Tero Ahola
|
r1481 | void QScatterSeries::setColor(const QColor &color) | ||
{ | ||||
QBrush b = brush(); | ||||
Titta Heikkala
|
r2670 | if (b == QChartPrivate::defaultBrush()) | ||
b = QBrush(); | ||||
Tero Ahola
|
r1933 | if (b == QBrush()) | ||
b.setStyle(Qt::SolidPattern); | ||||
b.setColor(color); | ||||
setBrush(b); | ||||
Tero Ahola
|
r1481 | } | ||
QColor QScatterSeries::color() const | ||||
{ | ||||
return brush().color(); | ||||
} | ||||
void QScatterSeries::setBorderColor(const QColor &color) | ||||
{ | ||||
QPen p = pen(); | ||||
Titta Heikkala
|
r2670 | if (p == QChartPrivate::defaultPen()) | ||
p = QPen(); | ||||
p.setColor(color); | ||||
setPen(p); | ||||
Tero Ahola
|
r1481 | } | ||
QColor QScatterSeries::borderColor() const | ||||
{ | ||||
return pen().color(); | ||||
} | ||||
Tero Ahola
|
r1276 | QScatterSeries::MarkerShape QScatterSeries::markerShape() const | ||
Tero Ahola
|
r195 | { | ||
Michal Klocek
|
r938 | Q_D(const QScatterSeries); | ||
return d->m_shape; | ||||
Tero Ahola
|
r195 | } | ||
Tero Ahola
|
r1276 | void QScatterSeries::setMarkerShape(MarkerShape shape) | ||
Tero Ahola
|
r195 | { | ||
Michal Klocek
|
r938 | Q_D(QScatterSeries); | ||
if (d->m_shape != shape) { | ||||
d->m_shape = shape; | ||||
Tero Ahola
|
r1481 | emit d->updated(); | ||
Michal Klocek
|
r573 | } | ||
Tero Ahola
|
r195 | } | ||
Tero Ahola
|
r1276 | qreal QScatterSeries::markerSize() const | ||
Tero Ahola
|
r397 | { | ||
Michal Klocek
|
r938 | Q_D(const QScatterSeries); | ||
return d->m_size; | ||||
Tero Ahola
|
r397 | } | ||
Tero Ahola
|
r1276 | void QScatterSeries::setMarkerSize(qreal size) | ||
Tero Ahola
|
r397 | { | ||
Michal Klocek
|
r938 | Q_D(QScatterSeries); | ||
Marek Rosa
|
r2242 | if (!qFuzzyCompare(d->m_size, size)) { | ||
Michal Klocek
|
r938 | d->m_size = size; | ||
Tero Ahola
|
r1481 | emit d->updated(); | ||
Michal Klocek
|
r573 | } | ||
Tero Ahola
|
r397 | } | ||
Michal Klocek
|
r938 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
Jani Honkonen
|
r2104 | QScatterSeriesPrivate::QScatterSeriesPrivate(QScatterSeries *q) | ||
: QXYSeriesPrivate(q), | ||||
m_shape(QScatterSeries::MarkerShapeCircle), | ||||
m_size(15.0) | ||||
Michal Klocek
|
r938 | { | ||
Tero Ahola
|
r1537 | } | ||
Michal Klocek
|
r938 | |||
Michal Klocek
|
r2273 | void QScatterSeriesPrivate::initializeGraphics(QGraphicsItem* parent) | ||
Michal Klocek
|
r943 | { | ||
Q_Q(QScatterSeries); | ||||
Michal Klocek
|
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(); | ||||
Miikka Heikkinen
|
r2516 | if (forced || QChartPrivate::defaultPen() == m_pen) { | ||
QPen pen; | ||||
Michal Klocek
|
r2273 | pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0)); | ||
pen.setWidthF(2); | ||||
q->setPen(pen); | ||||
} | ||||
Miikka Heikkinen
|
r2516 | if (forced || QChartPrivate::defaultBrush() == m_brush) { | ||
Michal Klocek
|
r2273 | QBrush brush(colors.at(index % colors.size())); | ||
q->setBrush(brush); | ||||
} | ||||
Titta Heikkala
|
r2689 | |||
if (forced || QChartPrivate::defaultPen().color() == m_pointLabelsColor) { | ||||
QColor color = theme->labelBrush().color(); | ||||
q->setPointLabelsColor(color); | ||||
} | ||||
Michal Klocek
|
r943 | } | ||
Miikka Heikkinen
|
r2492 | void QScatterSeriesPrivate::initializeAnimations(QChart::AnimationOptions options) | ||
{ | ||||
ScatterChartItem *item = static_cast<ScatterChartItem *>(m_item.data()); | ||||
Q_ASSERT(item); | ||||
Miikka Heikkinen
|
r2555 | if (item->animation()) | ||
item->animation()->stopAndDestroyLater(); | ||||
Miikka Heikkinen
|
r2492 | if (options.testFlag(QChart::SeriesAnimations)) | ||
item->setAnimation(new ScatterAnimation(item)); | ||||
else | ||||
item->setAnimation(0); | ||||
QAbstractSeriesPrivate::initializeAnimations(options); | ||||
} | ||||
Tero Ahola
|
r1276 | #include "moc_qscatterseries.cpp" | ||
Michal Klocek
|
r938 | |||
Titta Heikkala
|
r2712 | QT_CHARTS_END_NAMESPACE | ||