qareaseries.cpp
454 lines
| 12.4 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
|
r794 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r421 | #include "qareaseries.h" | ||
|
r938 | #include "qareaseries_p.h" | ||
|
r421 | #include "qlineseries.h" | ||
|
r943 | #include "areachartitem_p.h" | ||
|
r2275 | #include "abstractdomain_p.h" | ||
|
r943 | #include "chartdataset_p.h" | ||
#include "charttheme_p.h" | ||||
|
r1805 | #include "qvalueaxis.h" | ||
|
r2177 | #include "qarealegendmarker.h" | ||
|
r2273 | #include "qchart_p.h" | ||
|
r2177 | |||
|
r421 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
/*! | ||||
\class QAreaSeries | ||||
\brief The QAreaSeries class is used for making area charts. | ||||
\mainclass | ||||
|
r2097 | An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line | ||
is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance, | ||||
which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line. | ||||
|
r1515 | In that case QAreaSeries should be initiated with two QLineSeries instances. Please note terms "upper" and "lower" boundary can be misleading in cases | ||
|
r2097 | where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled. | ||
|
r421 | |||
|
r995 | See the \l {AreaChart Example} {area chart example} to learn how to create a simple area chart. | ||
\image examples_areachart.png | ||||
|
r421 | */ | ||
|
r1491 | /*! | ||
\qmlclass AreaSeries QAreaSeries | ||||
The following QML shows how to create a simple area chart: | ||||
\snippet ../demos/qmlchart/qml/qmlchart/View4.qml 1 | ||||
\beginfloatleft | ||||
\image demos_qmlchart4.png | ||||
\endfloat | ||||
\clearfloat | ||||
*/ | ||||
|
r421 | |||
/*! | ||||
|
r1462 | \property QAreaSeries::upperSeries | ||
\brief The upper one of the two line series used to define area series boundaries. | ||||
|
r421 | */ | ||
|
r1491 | /*! | ||
\qmlproperty LineSeries AreaSeries::upperSeries | ||||
The upper one of the two line series used to define area series boundaries. | ||||
*/ | ||||
|
r421 | |||
/*! | ||||
|
r1462 | \property QAreaSeries::lowerSeries | ||
The lower one of the two line series used to define are series boundaries. Note if | ||||
QAreaSeries was counstucted wihtout a\ lowerSeries this is null. | ||||
|
r421 | */ | ||
|
r1491 | /*! | ||
\qmlproperty LineSeries AreaSeries::lowerSeries | ||||
The lower one of the two line series used to define are series boundaries. Note if | ||||
AreaSeries was counstucted wihtout a\ lowerSeries this is null. | ||||
*/ | ||||
|
r421 | |||
|
r1481 | /*! | ||
\property QAreaSeries::color | ||||
Fill (brush) color of the series. This is a convenience property for modifying the color of brush. | ||||
\sa QAreaSeries::brush() | ||||
*/ | ||||
|
r1491 | /*! | ||
\qmlproperty color AreaSeries::color | ||||
Fill (brush) color of the series. | ||||
*/ | ||||
|
r1481 | |||
/*! | ||||
\property QAreaSeries::borderColor | ||||
Line (pen) color of the series. This is a convenience property for modifying the color of pen. | ||||
\sa QAreaSeries::pen() | ||||
*/ | ||||
|
r1491 | /*! | ||
\qmlproperty color AreaSeries::borderColor | ||||
Line (pen) color of the series. | ||||
*/ | ||||
|
r1481 | |||
|
r1906 | /*! | ||
\qmlproperty real AreaSeries::borderWidth | ||||
The width of the border line. By default the width is 2.0. | ||||
*/ | ||||
|
r421 | /*! | ||
\fn QPen QAreaSeries::pen() const | ||||
\brief Returns the pen used to draw line for this series. | ||||
\sa setPen() | ||||
*/ | ||||
/*! | ||||
\fn QPen QAreaSeries::brush() const | ||||
\brief Returns the brush used to draw line for this series. | ||||
\sa setBrush() | ||||
*/ | ||||
|
r1481 | /*! | ||
\fn void QAreaSeries::colorChanged(QColor color) | ||||
\brief Signal is emitted when the fill (brush) color has changed to \a color. | ||||
*/ | ||||
|
r1491 | /*! | ||
|
r1531 | \qmlsignal AreaSeries::onColorChanged(color color) | ||
|
r1491 | Signal is emitted when the fill (brush) color has changed to \a color. | ||
*/ | ||||
|
r1481 | |||
/*! | ||||
\fn void QAreaSeries::borderColorChanged(QColor color) | ||||
\brief Signal is emitted when the line (pen) color has changed to \a color. | ||||
*/ | ||||
|
r421 | /*! | ||
|
r1531 | \qmlsignal AreaSeries::onBorderColorChanged(color color) | ||
|
r1491 | Signal is emitted when the line (pen) color has changed to \a color. | ||
|
r421 | */ | ||
|
r574 | /*! | ||
\fn void QAreaSeries::clicked(const QPointF& point) | ||||
\brief Signal is emitted when user clicks the \a point on area chart. | ||||
*/ | ||||
|
r1491 | /*! | ||
|
r1531 | \qmlsignal AreaSeries::onClicked(QPointF point) | ||
|
r1491 | Signal is emitted when user clicks the \a point on area chart. | ||
*/ | ||||
|
r574 | |||
|
r2362 | /*! | ||
\fn void QAreaSeries::hovered(const QPointF &point, bool state) | ||||
This signal is emitted when user has hovered over or away from the series. \a point shows the origin (coordinate) | ||||
of the hover event. \a state is true when user has hovered over the series and false when hover has moved away from | ||||
the series. | ||||
*/ | ||||
/*! | ||||
\qmlsignal AreaSeries::onHovered(point point, bool state) | ||||
This signal is emitted when user has hovered over or away from the series. \a point shows the origin (coordinate) | ||||
of the hover event. \a state is true when user has hovered over the series and false when hover has moved away from | ||||
the series. | ||||
*/ | ||||
|
r973 | /*! | ||
\fn void QAreaSeries::selected() | ||||
The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be | ||||
implemented by the user of QAreaSeries API. | ||||
*/ | ||||
|
r1491 | /*! | ||
|
r1531 | \qmlsignal AreaSeries::onSelected() | ||
|
r1491 | The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be | ||
implemented by the user of AreaSeries API. | ||||
*/ | ||||
|
r973 | |||
|
r421 | /*! | ||
|
r940 | \fn void QAreaSeriesPrivate::updated() | ||
|
r421 | \brief \internal | ||
*/ | ||||
/*! | ||||
Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a | ||||
upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead. | ||||
|
r974 | When series object is added to QChartView or QChart instance ownerships is transferred. | ||
|
r421 | */ | ||
|
r761 | QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries) | ||
|
r2097 | : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries, lowerSeries, this), upperSeries) | ||
|
r421 | { | ||
} | ||||
|
r761 | |||
|
r1202 | /*! | ||
|
r1351 | Constructs area series object without upper or lower series with \a parent object. | ||
|
r1202 | */ | ||
QAreaSeries::QAreaSeries(QObject *parent) | ||||
: QAbstractSeries(*new QAreaSeriesPrivate(0, 0, this), parent) | ||||
{ | ||||
} | ||||
|
r421 | /*! | ||
|
r1515 | Destroys the object. | ||
|
r421 | */ | ||
QAreaSeries::~QAreaSeries() | ||||
{ | ||||
|
r1727 | Q_D(QAreaSeries); | ||
|
r2273 | if (d->m_chart) | ||
d->m_chart->removeSeries(this); | ||||
|
r421 | } | ||
|
r1345 | /*! | ||
Returns QChartSeries::SeriesTypeArea. | ||||
*/ | ||||
|
r1107 | QAbstractSeries::SeriesType QAreaSeries::type() const | ||
|
r938 | { | ||
|
r988 | return QAbstractSeries::SeriesTypeArea; | ||
|
r938 | } | ||
|
r1369 | /*! | ||
Sets the \a series that is to be used as the area chart upper series. | ||||
*/ | ||||
|
r2104 | void QAreaSeries::setUpperSeries(QLineSeries *series) | ||
|
r1202 | { | ||
Q_D(QAreaSeries); | ||||
|
r2273 | if(d->m_upperSeries!=series){ | ||
|
r2407 | d->m_upperSeries = series; | ||
//TODO: | ||||
|
r2273 | } | ||
|
r1202 | } | ||
|
r2104 | QLineSeries *QAreaSeries::upperSeries() const | ||
|
r938 | { | ||
Q_D(const QAreaSeries); | ||||
return d->m_upperSeries; | ||||
} | ||||
|
r1369 | /*! | ||
Sets the \a series that is to be used as the area chart lower series. | ||||
*/ | ||||
|
r2104 | void QAreaSeries::setLowerSeries(QLineSeries *series) | ||
|
r1202 | { | ||
Q_D(QAreaSeries); | ||||
d->m_lowerSeries = series; | ||||
} | ||||
|
r2104 | QLineSeries *QAreaSeries::lowerSeries() const | ||
|
r938 | { | ||
Q_D(const QAreaSeries); | ||||
return d->m_lowerSeries; | ||||
} | ||||
|
r421 | /*! | ||
Sets \a pen used for drawing area outline. | ||||
*/ | ||||
|
r761 | void QAreaSeries::setPen(const QPen &pen) | ||
|
r421 | { | ||
|
r938 | Q_D(QAreaSeries); | ||
if (d->m_pen != pen) { | ||||
d->m_pen = pen; | ||||
emit d->updated(); | ||||
|
r571 | } | ||
|
r421 | } | ||
|
r938 | QPen QAreaSeries::pen() const | ||
{ | ||||
Q_D(const QAreaSeries); | ||||
return d->m_pen; | ||||
} | ||||
|
r421 | /*! | ||
Sets \a brush used for filling the area. | ||||
*/ | ||||
|
r743 | void QAreaSeries::setBrush(const QBrush &brush) | ||
|
r421 | { | ||
|
r938 | Q_D(QAreaSeries); | ||
if (d->m_brush != brush) { | ||||
|
r1933 | bool emitColorChanged = brush.color() != d->m_brush.color(); | ||
|
r938 | d->m_brush = brush; | ||
emit d->updated(); | ||||
|
r1933 | if (emitColorChanged) | ||
emit colorChanged(brush.color()); | ||||
|
r571 | } | ||
|
r421 | } | ||
|
r938 | |||
QBrush QAreaSeries::brush() const | ||||
{ | ||||
Q_D(const QAreaSeries); | ||||
return d->m_brush; | ||||
} | ||||
|
r1481 | |||
void QAreaSeries::setColor(const QColor &color) | ||||
{ | ||||
QBrush b = brush(); | ||||
|
r1933 | if (b == QBrush()) | ||
b.setStyle(Qt::SolidPattern); | ||||
b.setColor(color); | ||||
setBrush(b); | ||||
|
r1481 | } | ||
QColor QAreaSeries::color() const | ||||
{ | ||||
return brush().color(); | ||||
} | ||||
void QAreaSeries::setBorderColor(const QColor &color) | ||||
{ | ||||
QPen p = pen(); | ||||
if (p.color() != color) { | ||||
p.setColor(color); | ||||
setPen(p); | ||||
emit borderColorChanged(color); | ||||
} | ||||
} | ||||
QColor QAreaSeries::borderColor() const | ||||
{ | ||||
return pen().color(); | ||||
} | ||||
|
r421 | /*! | ||
Sets if data points are \a visible and should be drawn on line. | ||||
*/ | ||||
void QAreaSeries::setPointsVisible(bool visible) | ||||
{ | ||||
|
r938 | Q_D(QAreaSeries); | ||
if (d->m_pointsVisible != visible) { | ||||
d->m_pointsVisible = visible; | ||||
emit d->updated(); | ||||
|
r571 | } | ||
|
r421 | } | ||
|
r1491 | /*! | ||
Returns if the points are drawn for this series. | ||||
\sa setPointsVisible() | ||||
*/ | ||||
|
r938 | bool QAreaSeries::pointsVisible() const | ||
{ | ||||
Q_D(const QAreaSeries); | ||||
return d->m_pointsVisible; | ||||
} | ||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
|
r2104 | QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries, QAreaSeries *q) | ||
|
r2097 | : QAbstractSeriesPrivate(q), | ||
m_upperSeries(upperSeries), | ||||
m_lowerSeries(lowerSeries), | ||||
m_pointsVisible(false) | ||||
|
r938 | { | ||
|
r973 | } | ||
|
r938 | |||
|
r2273 | void QAreaSeriesPrivate::initializeDomain() | ||
|
r943 | { | ||
Q_Q(QAreaSeries); | ||||
|
r2273 | qreal minX(domain()->minX()); | ||
qreal minY(domain()->minY()); | ||||
qreal maxX(domain()->maxX()); | ||||
qreal maxY(domain()->maxY()); | ||||
|
r943 | |||
|
r2104 | QLineSeries *upperSeries = q->upperSeries(); | ||
QLineSeries *lowerSeries = q->lowerSeries(); | ||||
|
r943 | |||
|
r1057 | const QList<QPointF>& points = upperSeries->points(); | ||
|
r2097 | for (int i = 0; i < points.count(); i++) { | ||
|
r1057 | qreal x = points[i].x(); | ||
qreal y = points[i].y(); | ||||
|
r943 | minX = qMin(minX, x); | ||
minY = qMin(minY, y); | ||||
maxX = qMax(maxX, x); | ||||
maxY = qMax(maxY, y); | ||||
} | ||||
|
r2097 | if (lowerSeries) { | ||
|
r1057 | |||
|
r1059 | const QList<QPointF>& points = lowerSeries->points(); | ||
|
r1057 | |||
|
r2097 | for (int i = 0; i < points.count(); i++) { | ||
|
r1057 | qreal x = points[i].x(); | ||
qreal y = points[i].y(); | ||||
|
r943 | minX = qMin(minX, x); | ||
minY = qMin(minY, y); | ||||
maxX = qMax(maxX, x); | ||||
maxY = qMax(maxY, y); | ||||
|
r2097 | } | ||
} | ||||
|
r943 | |||
|
r2273 | domain()->setRange(minX, maxX, minY, maxY); | ||
|
r943 | } | ||
|
r2273 | void QAreaSeriesPrivate::initializeGraphics(QGraphicsItem* parent) | ||
|
r943 | { | ||
Q_Q(QAreaSeries); | ||||
|
r2273 | AreaChartItem *area = new AreaChartItem(q,parent); | ||
m_item.reset(area); | ||||
QAbstractSeriesPrivate::initializeGraphics(parent); | ||||
} | ||||
void QAreaSeriesPrivate::initializeAnimations(QChart::AnimationOptions options) | ||||
{ | ||||
Q_Q(QAreaSeries); | ||||
AreaChartItem *area = static_cast<AreaChartItem *>(m_item.data()); | ||||
if (options.testFlag(QChart::SeriesAnimations)) { | ||||
|
r1217 | area->upperLineItem()->setAnimation(new XYAnimation(area->upperLineItem())); | ||
|
r2097 | if (q->lowerSeries()) | ||
|
r2273 | area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem())); | ||
}else{ | ||||
area->upperLineItem()->setAnimation(0); | ||||
if (q->lowerSeries()) | ||||
area->lowerLineItem()->setAnimation(0); | ||||
|
r943 | } | ||
|
r2273 | QAbstractSeriesPrivate::initializeAnimations(options); | ||
|
r943 | } | ||
|
r2163 | QList<QLegendMarker*> QAreaSeriesPrivate::createLegendMarkers(QLegend* legend) | ||
{ | ||||
|
r2177 | Q_Q(QAreaSeries); | ||
|
r2163 | QList<QLegendMarker*> list; | ||
|
r2177 | return list << new QAreaLegendMarker(q,legend); | ||
|
r2163 | } | ||
|
r1545 | |||
|
r2273 | |||
void QAreaSeriesPrivate::initializeAxes() | ||||
|
r1545 | { | ||
|
r2273 | |||
|
r1545 | } | ||
|
r1695 | QAbstractAxis::AxisType QAreaSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const | ||
|
r1588 | { | ||
|
r1695 | Q_UNUSED(orientation); | ||
|
r1818 | return QAbstractAxis::AxisTypeValue; | ||
|
r1545 | } | ||
|
r2273 | QAbstractAxis* QAreaSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const | ||
{ | ||||
Q_UNUSED(orientation); | ||||
return 0; | ||||
} | ||||
void QAreaSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced) | ||||
{ | ||||
Q_Q(QAreaSeries); | ||||
QPen pen; | ||||
QBrush brush; | ||||
const QList<QGradient> gradients = theme->seriesGradients(); | ||||
const QList<QColor> colors = theme->seriesColors(); | ||||
if (forced || pen == m_pen) { | ||||
pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0)); | ||||
pen.setWidthF(2); | ||||
q->setPen(pen); | ||||
} | ||||
if (forced || brush == m_brush) { | ||||
QBrush brush(colors.at(index % colors.size())); | ||||
q->setBrush(brush); | ||||
} | ||||
} | ||||
|
r421 | #include "moc_qareaseries.cpp" | ||
|
r938 | #include "moc_qareaseries_p.cpp" | ||
|
r421 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||