qscatterseries.cpp
298 lines
| 6.6 KiB
| text/x-c
|
CppLexer
Tero Ahola
|
r42 | #include "qscatterseries.h" | ||
Tero Ahola
|
r194 | #include "scatterseries_p.h" | ||
Tero Ahola
|
r42 | #include "qchart.h" | ||
Tero Ahola
|
r300 | /*! | ||
\class QScatterSeries | ||||
\brief QtCommercial Chart series API for showing scatter series. | ||||
Tero Ahola
|
r42 | |||
Tero Ahola
|
r300 | \mainclass | ||
Example on how to create a chart with scatter series: | ||||
\snippet ../example/scatter/main.cpp 1 | ||||
The example code would result the following: | ||||
\image scatter_example1.jpg | ||||
Tero Ahola
|
r397 | |||
To customize the graphical representation of the series, you can modify pen, brush, shape and | ||||
size of the marker items. For example: | ||||
\snippet ../example/scatter/main.cpp 3 | ||||
Would present your scatter markers as big rectangles with opaque, uglyish green outlines and | ||||
opaque red filling instead of the beatiful markers defined by the chart's theme: | ||||
\image scatter_example_custom.jpg | ||||
Tero Ahola
|
r300 | */ | ||
Tero Ahola
|
r42 | |||
Tero Ahola
|
r261 | /*! | ||
\enum QScatterSeries::MarkerShape | ||||
This enum describes the shape used when rendering marker items. | ||||
\value MarkerShapeDefault | ||||
\value MarkerShapeX | ||||
\value MarkerShapeRectangle | ||||
\value MarkerShapeTiltedRectangle | ||||
\value MarkerShapeTriangle | ||||
\value MarkerShapeCircle | ||||
*/ | ||||
Tero Ahola
|
r260 | /*! | ||
Tero Ahola
|
r300 | \fn QChartSeriesType QScatterSeries::type() const | ||
\brief Returns QChartSeries::SeriesTypeScatter. | ||||
*/ | ||||
Tero Ahola
|
r260 | |||
Tero Ahola
|
r300 | /*! | ||
Tero Ahola
|
r394 | \fn void QScatterSeries::clicked(QPointF coordinate) | ||
User clicked the scatter series. Note that the \a coordinate is the chart coordinate that the | ||||
click occurred on; not necessarily a data point coordinate. To find the corresponding (closest) | ||||
data point you can use closestPoint(). | ||||
Tero Ahola
|
r300 | */ | ||
Tero Ahola
|
r260 | |||
Tero Ahola
|
r300 | /*! | ||
\fn void QScatterSeries::changed() | ||||
\brief TODO | ||||
*/ | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
QScatterSeriesPrivate::QScatterSeriesPrivate() : | ||||
m_data(QList<QPointF>()), | ||||
Tero Ahola
|
r375 | m_markerPen(QPen(QColor::Invalid)), | ||
m_markerBrush(QBrush(QColor::Invalid)), | ||||
Tero Ahola
|
r397 | m_markerShape(QScatterSeries::MarkerShapeDefault), | ||
m_markerSize(9.0) | ||||
Tero Ahola
|
r300 | { | ||
} | ||||
Tero Ahola
|
r261 | /*! | ||
Constructs a series object which is a child of \a parent. | ||||
*/ | ||||
Tero Ahola
|
r158 | QScatterSeries::QScatterSeries(QObject *parent) : | ||
Michal Klocek
|
r360 | QSeries(parent), | ||
Tero Ahola
|
r158 | d(new QScatterSeriesPrivate()) | ||
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 | { | ||
Tero Ahola
|
r158 | delete d; | ||
Tero Ahola
|
r48 | } | ||
Tero Ahola
|
r358 | /*! | ||
Add single data point with \a x and \a y coordinates to the series. | ||||
*/ | ||||
void QScatterSeries::add(qreal x, qreal y) | ||||
{ | ||||
d->m_data.append(QPointF(x, y)); | ||||
emit changed(); | ||||
} | ||||
Tero Ahola
|
r260 | /*! | ||
Tero Ahola
|
r300 | Add single data point with \a value to the series. | ||
Tero Ahola
|
r260 | */ | ||
Tero Ahola
|
r358 | void QScatterSeries::add(QPointF value) | ||
Tero Ahola
|
r61 | { | ||
Tero Ahola
|
r180 | d->m_data.append(value); | ||
emit changed(); | ||||
} | ||||
Tero Ahola
|
r260 | /*! | ||
Tero Ahola
|
r300 | Add list of \a points to the series. | ||
*/ | ||||
Tero Ahola
|
r358 | void QScatterSeries::add(QList<QPointF> points) | ||
Tero Ahola
|
r300 | { | ||
Tero Ahola
|
r301 | d->m_data.append(points); | ||
Tero Ahola
|
r300 | emit changed(); | ||
} | ||||
Tero Ahola
|
r260 | |||
Tero Ahola
|
r300 | /*! | ||
Stream operator for adding a data point with \a value to the series. | ||||
Tero Ahola
|
r358 | \sa add() | ||
Tero Ahola
|
r260 | |||
Tero Ahola
|
r300 | For example: | ||
Tero Ahola
|
r397 | \snippet ../example/scatter/main.cpp 2 | ||
Tero Ahola
|
r260 | */ | ||
Tero Ahola
|
r180 | QScatterSeries& QScatterSeries::operator << (const QPointF &value) | ||
{ | ||||
d->m_data.append(value); | ||||
Tero Ahola
|
r179 | emit changed(); | ||
Tero Ahola
|
r180 | return *this; | ||
Tero Ahola
|
r179 | } | ||
Tero Ahola
|
r158 | |||
Tero Ahola
|
r260 | /*! | ||
Tero Ahola
|
r300 | Stream operator for adding a list of points to the series. | ||
Tero Ahola
|
r358 | \sa add() | ||
Tero Ahola
|
r300 | */ | ||
QScatterSeries& QScatterSeries::operator << (QList<QPointF> value) | ||||
{ | ||||
d->m_data.append(value); | ||||
emit changed(); | ||||
return *this; | ||||
} | ||||
/*! | ||||
Replaces the data of the series with the given list of data \a points. | ||||
Tero Ahola
|
r260 | */ | ||
Tero Ahola
|
r300 | void QScatterSeries::setData(QList<QPointF> points) | ||
Tero Ahola
|
r179 | { | ||
Tero Ahola
|
r301 | d->m_data = points; | ||
Tero Ahola
|
r158 | emit changed(); | ||
Tero Ahola
|
r61 | } | ||
Tero Ahola
|
r260 | /*! | ||
Tero Ahola
|
r261 | Returns the current list of data points of the series. | ||
Tero Ahola
|
r260 | */ | ||
Tero Ahola
|
r158 | QList<QPointF> QScatterSeries::data() | ||
Tero Ahola
|
r64 | { | ||
Tero Ahola
|
r158 | return d->m_data; | ||
Tero Ahola
|
r64 | } | ||
Tero Ahola
|
r394 | /*! | ||
Tero Ahola
|
r395 | Replaces the point at \a index with \a newPoint. Returns true if \a index is a valid position | ||
in the series data, false otherwise. | ||||
Tero Ahola
|
r394 | */ | ||
Tero Ahola
|
r395 | bool QScatterSeries::replace(int index, QPointF newPoint) | ||
Tero Ahola
|
r394 | { | ||
Tero Ahola
|
r395 | if (index >= 0 && index < d->m_data.count()) { | ||
d->m_data.replace(index, newPoint); | ||||
emit changed(); | ||||
return true; | ||||
} | ||||
return false; | ||||
} | ||||
/*! | ||||
Remove the data point at \a index. Returns true if a point was removed, false if the point | ||||
at \a index does not exist on the series. | ||||
*/ | ||||
bool QScatterSeries::removeAt(int index) | ||||
{ | ||||
if (index >=0 && index < d->m_data.count()) { | ||||
d->m_data.removeAt(index); | ||||
Tero Ahola
|
r394 | emit changed(); | ||
return true; | ||||
} | ||||
return false; | ||||
} | ||||
/*! | ||||
Remove all occurrences of \a point from the series and returns the number of points removed. | ||||
*/ | ||||
int QScatterSeries::removeAll(QPointF point) | ||||
{ | ||||
int count = d->m_data.removeAll(point); | ||||
emit changed(); | ||||
return count; | ||||
} | ||||
/*! | ||||
Remove all data points from the series. | ||||
*/ | ||||
void QScatterSeries::clear() | ||||
{ | ||||
d->m_data.clear(); | ||||
emit changed(); | ||||
} | ||||
/*! | ||||
Returns the index of the data point that is closest to \a coordinate. If several data points | ||||
are at the same distance from the \a coordinate, returns the last one. If no points exist, | ||||
returns -1. | ||||
*/ | ||||
int QScatterSeries::closestPoint(QPointF coordinate) | ||||
{ | ||||
qreal distance(-1); | ||||
int pointIndex(-1); | ||||
for (int i(0); i < d->m_data.count(); i++) { | ||||
QPointF dataPoint = d->m_data.at(i); | ||||
QPointF difference = dataPoint - coordinate; | ||||
if (i == 0 || difference.manhattanLength() <= distance) { | ||||
distance = difference.manhattanLength(); | ||||
pointIndex = i; | ||||
} | ||||
} | ||||
return pointIndex; | ||||
} | ||||
Tero Ahola
|
r260 | /*! | ||
Tero Ahola
|
r300 | Overrides the default pen used for drawing a marker item with a user defined \a pen. The | ||
default pen is defined by chart theme setting. | ||||
Tero Ahola
|
r261 | |||
Tero Ahola
|
r358 | \sa setBrush() | ||
Tero Ahola
|
r300 | \sa QChart::setChartTheme() | ||
Tero Ahola
|
r260 | */ | ||
Tero Ahola
|
r358 | void QScatterSeries::setPen(QPen pen) | ||
Tero Ahola
|
r75 | { | ||
Tero Ahola
|
r179 | d->m_markerPen = pen; | ||
Tero Ahola
|
r75 | } | ||
Tero Ahola
|
r260 | /*! | ||
Tero Ahola
|
r261 | Returns the pen used for drawing markers. | ||
Tero Ahola
|
r260 | */ | ||
Tero Ahola
|
r358 | QPen QScatterSeries::pen() | ||
Tero Ahola
|
r42 | { | ||
Tero Ahola
|
r179 | return d->m_markerPen; | ||
Tero Ahola
|
r42 | } | ||
Tero Ahola
|
r261 | /*! | ||
Tero Ahola
|
r300 | Overrides the default brush of the marker items with a user defined \a brush. The default brush | ||
is defined by chart theme setting. | ||||
Tero Ahola
|
r261 | |||
Tero Ahola
|
r358 | \sa setPen() | ||
Tero Ahola
|
r300 | \sa QChart::setChartTheme() | ||
Tero Ahola
|
r261 | */ | ||
Tero Ahola
|
r358 | void QScatterSeries::setBrush(QBrush brush) | ||
Tero Ahola
|
r195 | { | ||
d->m_markerBrush = brush; | ||||
} | ||||
Tero Ahola
|
r261 | /*! | ||
Returns the brush used for drawing markers. | ||||
*/ | ||||
Tero Ahola
|
r358 | QBrush QScatterSeries::brush() | ||
Tero Ahola
|
r195 | { | ||
return d->m_markerBrush; | ||||
} | ||||
Tero Ahola
|
r261 | /*! | ||
Tero Ahola
|
r300 | Overrides the default shape of the marker items with a user defined \a shape. The default shape | ||
is defined by chart theme setting. | ||||
Tero Ahola
|
r261 | */ | ||
Tero Ahola
|
r358 | void QScatterSeries::setShape(MarkerShape shape) | ||
Tero Ahola
|
r195 | { | ||
d->m_markerShape = shape; | ||||
} | ||||
Tero Ahola
|
r261 | /*! | ||
Returns the shape used for drawing markers. | ||||
*/ | ||||
Tero Ahola
|
r358 | QScatterSeries::MarkerShape QScatterSeries::shape() | ||
Tero Ahola
|
r195 | { | ||
return (QScatterSeries::MarkerShape) d->m_markerShape; | ||||
} | ||||
Tero Ahola
|
r397 | /*! | ||
Returns the size of the marker items. | ||||
*/ | ||||
qreal QScatterSeries::size() | ||||
{ | ||||
return d->m_markerSize; | ||||
} | ||||
/*! | ||||
Set the \a size of the marker items. The default size is 9.0. | ||||
*/ | ||||
void QScatterSeries::setSize(qreal size) | ||||
{ | ||||
d->m_markerSize = size; | ||||
emit changed(); | ||||
} | ||||
Tero Ahola
|
r42 | #include "moc_qscatterseries.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||