qscatterseries.cpp
291 lines
| 6.5 KiB
| text/x-c
|
CppLexer
|
r42 | #include "qscatterseries.h" | ||
|
r194 | #include "scatterseries_p.h" | ||
|
r42 | #include "qchart.h" | ||
|
r300 | /*! | ||
\class QScatterSeries | ||||
\brief QtCommercial Chart series API for showing scatter series. | ||||
|
r42 | |||
|
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 | ||||
*/ | ||||
|
r42 | |||
|
r261 | /*! | ||
\enum QScatterSeries::MarkerShape | ||||
This enum describes the shape used when rendering marker items. | ||||
\value MarkerShapeDefault | ||||
\value MarkerShapePoint | ||||
\value MarkerShapeX | ||||
\value MarkerShapeRectangle | ||||
\value MarkerShapeTiltedRectangle | ||||
\value MarkerShapeTriangle | ||||
\value MarkerShapeCircle | ||||
*/ | ||||
|
r260 | /*! | ||
|
r300 | \fn QChartSeriesType QScatterSeries::type() const | ||
\brief Returns QChartSeries::SeriesTypeScatter. | ||||
*/ | ||||
|
r260 | |||
|
r300 | /*! | ||
|
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(). | ||||
|
r300 | */ | ||
|
r260 | |||
|
r300 | /*! | ||
\fn void QScatterSeries::changed() | ||||
\brief TODO | ||||
*/ | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
QScatterSeriesPrivate::QScatterSeriesPrivate() : | ||||
m_data(QList<QPointF>()), | ||||
|
r375 | m_markerPen(QPen(QColor::Invalid)), | ||
m_markerBrush(QBrush(QColor::Invalid)), | ||||
|
r300 | m_markerShape(QScatterSeries::MarkerShapeDefault) | ||
{ | ||||
} | ||||
|
r261 | /*! | ||
Constructs a series object which is a child of \a parent. | ||||
*/ | ||||
|
r158 | QScatterSeries::QScatterSeries(QObject *parent) : | ||
|
r360 | QSeries(parent), | ||
|
r158 | d(new QScatterSeriesPrivate()) | ||
|
r42 | { | ||
} | ||||
|
r260 | /*! | ||
|
r261 | Destroys the object. Note that adding series to QChart transfers the ownership to the chart. | ||
|
r260 | */ | ||
|
r158 | QScatterSeries::~QScatterSeries() | ||
|
r42 | { | ||
|
r158 | delete d; | ||
|
r48 | } | ||
|
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(); | ||||
} | ||||
|
r260 | /*! | ||
|
r300 | Add single data point with \a value to the series. | ||
|
r260 | */ | ||
|
r358 | void QScatterSeries::add(QPointF value) | ||
|
r61 | { | ||
|
r180 | d->m_data.append(value); | ||
emit changed(); | ||||
} | ||||
|
r260 | /*! | ||
|
r300 | Add list of \a points to the series. | ||
*/ | ||||
|
r358 | void QScatterSeries::add(QList<QPointF> points) | ||
|
r300 | { | ||
|
r301 | d->m_data.append(points); | ||
|
r300 | emit changed(); | ||
} | ||||
|
r260 | |||
|
r300 | /*! | ||
Stream operator for adding a data point with \a value to the series. | ||||
|
r358 | \sa add() | ||
|
r260 | |||
|
r300 | For example: | ||
\snippet ../example/scatter/main.cpp 3 | ||||
|
r260 | */ | ||
|
r180 | QScatterSeries& QScatterSeries::operator << (const QPointF &value) | ||
{ | ||||
d->m_data.append(value); | ||||
|
r179 | emit changed(); | ||
|
r180 | return *this; | ||
|
r179 | } | ||
|
r158 | |||
|
r260 | /*! | ||
|
r300 | Stream operator for adding a list of points to the series. | ||
|
r358 | \sa add() | ||
|
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. | ||||
|
r260 | */ | ||
|
r300 | void QScatterSeries::setData(QList<QPointF> points) | ||
|
r179 | { | ||
|
r301 | d->m_data = points; | ||
|
r158 | emit changed(); | ||
|
r61 | } | ||
|
r260 | /*! | ||
|
r261 | Returns the current list of data points of the series. | ||
|
r260 | */ | ||
|
r158 | QList<QPointF> QScatterSeries::data() | ||
|
r64 | { | ||
|
r158 | return d->m_data; | ||
|
r64 | } | ||
|
r394 | /*! | ||
|
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. | ||||
|
r394 | */ | ||
|
r395 | bool QScatterSeries::replace(int index, QPointF newPoint) | ||
|
r394 | { | ||
|
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); | ||||
|
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; | ||||
} | ||||
|
r260 | /*! | ||
|
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. | ||||
|
r261 | |||
For example: | ||||
|
r300 | \snippet ../example/scatter/main.cpp 5 | ||
|
r261 | |||
|
r300 | Would present your scatter markers with an opaque, uglyish green outlines instead of the | ||
beatiful markers defined by the chart's theme: | ||||
|
r261 | \image scatter_example_pen.jpg | ||
|
r358 | \sa setBrush() | ||
|
r300 | \sa QChart::setChartTheme() | ||
|
r260 | */ | ||
|
r358 | void QScatterSeries::setPen(QPen pen) | ||
|
r75 | { | ||
|
r179 | d->m_markerPen = pen; | ||
|
r75 | } | ||
|
r260 | /*! | ||
|
r261 | Returns the pen used for drawing markers. | ||
|
r260 | */ | ||
|
r358 | QPen QScatterSeries::pen() | ||
|
r42 | { | ||
|
r179 | return d->m_markerPen; | ||
|
r42 | } | ||
|
r261 | /*! | ||
|
r300 | Overrides the default brush of the marker items with a user defined \a brush. The default brush | ||
is defined by chart theme setting. | ||||
|
r261 | |||
For example: | ||||
|
r300 | \snippet ../example/scatter/main.cpp 4 | ||
|
r261 | |||
Would fill your scatter markers with an opaque red color: | ||||
\image scatter_example_brush.jpg | ||||
|
r358 | \sa setPen() | ||
|
r300 | \sa QChart::setChartTheme() | ||
|
r261 | */ | ||
|
r358 | void QScatterSeries::setBrush(QBrush brush) | ||
|
r195 | { | ||
d->m_markerBrush = brush; | ||||
} | ||||
|
r261 | /*! | ||
Returns the brush used for drawing markers. | ||||
*/ | ||||
|
r358 | QBrush QScatterSeries::brush() | ||
|
r195 | { | ||
return d->m_markerBrush; | ||||
} | ||||
|
r261 | /*! | ||
|
r300 | Overrides the default shape of the marker items with a user defined \a shape. The default shape | ||
is defined by chart theme setting. | ||||
|
r261 | |||
For example: | ||||
|
r300 | \snippet ../example/scatter/main.cpp 6 | ||
|
r261 | |||
Would make your scatter marker items rectangle: | ||||
\image scatter_example_shape.jpg | ||||
*/ | ||||
|
r358 | void QScatterSeries::setShape(MarkerShape shape) | ||
|
r195 | { | ||
d->m_markerShape = shape; | ||||
} | ||||
|
r261 | /*! | ||
Returns the shape used for drawing markers. | ||||
*/ | ||||
|
r358 | QScatterSeries::MarkerShape QScatterSeries::shape() | ||
|
r195 | { | ||
return (QScatterSeries::MarkerShape) d->m_markerShape; | ||||
} | ||||
|
r42 | #include "moc_qscatterseries.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||