qvalueaxis.cpp
435 lines
| 10.9 KiB
| text/x-c
|
CppLexer
Michal Klocek
|
r1540 | /**************************************************************************** | ||
** | ||||
Miikka Heikkinen
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
Michal Klocek
|
r1540 | ** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
Marek Rosa
|
r1805 | #include "qvalueaxis.h" | ||
#include "qvalueaxis_p.h" | ||||
#include "chartvalueaxisx_p.h" | ||||
#include "chartvalueaxisy_p.h" | ||||
Marek Rosa
|
r2275 | #include "abstractdomain_p.h" | ||
Miikka Heikkinen
|
r2483 | #include "polarchartvalueaxisangular_p.h" | ||
#include "polarchartvalueaxisradial_p.h" | ||||
Michal Klocek
|
r1725 | #include "chartdataset_p.h" | ||
Michal Klocek
|
r2273 | #include "chartpresenter_p.h" | ||
#include "charttheme_p.h" | ||||
Michal Klocek
|
r1725 | |||
Michal Klocek
|
r1540 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Marek Rosa
|
r1547 | /*! | ||
Marek Rosa
|
r1804 | \class QValueAxis | ||
\brief The QValueAxis class is used for manipulating chart's axis. | ||||
Marek Rosa
|
r1547 | \mainclass | ||
Marek Rosa
|
r1812 | ValueAxis can be setup to show axis line with tick marks, grid lines and shades. | ||
Marek Rosa
|
r1881 | Values of axis are drawn to position of ticks. | ||
Example code on how to use QValueAxis. | ||||
\code | ||||
QChartView *chartView = new QChartView; | ||||
QLineSeries *series = new QLineSeries; | ||||
// ... | ||||
Marek Rosa
|
r1882 | chartView->chart()->addSeries(series); | ||
Marek Rosa
|
r1881 | QValueAxis *axisX = new QValueAxis; | ||
axisX->setRange(10, 20.5); | ||||
axisX->setTickCount(10); | ||||
axisX->setLabelFormat("%.2f"); | ||||
Marek Rosa
|
r2108 | chartView->chart()->setAxisX(axisX, series); | ||
Marek Rosa
|
r1881 | \endcode | ||
Marek Rosa
|
r1547 | */ | ||
/*! | ||||
Marek Rosa
|
r1812 | \qmlclass ValueAxis QValueAxis | ||
Tero Ahola
|
r1869 | \inherits AbstractAxis | ||
Marek Rosa
|
r1812 | \brief The ValueAxis element is used for manipulating chart's axes | ||
Marek Rosa
|
r1547 | |||
sauimone
|
r1639 | ValueAxis can be setup to show axis line with tick marks, grid lines and shades. | ||
Values of axis are drawn to position of ticks | ||||
Marek Rosa
|
r1547 | |||
Miikka Heikkinen
|
r2494 | Example about using ValueAxis: | ||
Marek Rosa
|
r1547 | \code | ||
ChartView { | ||||
Marek Rosa
|
r1812 | ValueAxis { | ||
sauimone
|
r1639 | id: xAxis | ||
min: 0 | ||||
max: 10 | ||||
} | ||||
// Add a few series... | ||||
Marek Rosa
|
r1547 | } | ||
\endcode | ||||
*/ | ||||
/*! | ||||
Marek Rosa
|
r1804 | \property QValueAxis::min | ||
Marek Rosa
|
r1547 | Defines the minimum value on the axis. | ||
Marek Rosa
|
r1730 | When setting this property the max is adjusted if necessary, to ensure that the range remains valid. | ||
Marek Rosa
|
r1547 | */ | ||
/*! | ||||
Marek Rosa
|
r1812 | \qmlproperty real ValueAxis::min | ||
Marek Rosa
|
r1547 | Defines the minimum value on the axis. | ||
Marek Rosa
|
r1730 | When setting this property the max is adjusted if necessary, to ensure that the range remains valid. | ||
Marek Rosa
|
r1547 | */ | ||
/*! | ||||
Marek Rosa
|
r1804 | \property QValueAxis::max | ||
Marek Rosa
|
r1547 | Defines the maximum value on the axis. | ||
Marek Rosa
|
r1730 | When setting this property the min is adjusted if necessary, to ensure that the range remains valid. | ||
Marek Rosa
|
r1547 | */ | ||
/*! | ||||
Marek Rosa
|
r1812 | \qmlproperty real ValueAxis::max | ||
Marek Rosa
|
r1547 | Defines the maximum value on the axis. | ||
Marek Rosa
|
r1730 | When setting this property the min is adjusted if necessary, to ensure that the range remains valid. | ||
Marek Rosa
|
r1547 | */ | ||
Michal Klocek
|
r2308 | /*! | ||
\property QValueAxis::tickCount | ||||
Defines the number of ticks on the axis. This indicates how many grid lines are draw on the chart. | ||||
The default value is 5, and it can not be below 2. | ||||
*/ | ||||
/*! | ||||
\qmlproperty real ValueAxis::tickCount | ||||
Defines the number of ticks on the axis. This indicates how many grid lines are draw on the chart. | ||||
Miikka Heikkinen
|
r2494 | The default value is 5, and it can not be below 2. | ||
Michal Klocek
|
r2308 | */ | ||
Marek Rosa
|
r1855 | /*! | ||
\property QValueAxis::labelFormat | ||||
Marek Rosa
|
r2339 | Defines the label format of the axis. | ||
Marek Rosa
|
r2268 | Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, c | ||
See QString::sprintf() for additional details. | ||||
Marek Rosa
|
r1855 | */ | ||
/*! | ||||
\qmlproperty real ValueAxis::labelFormat | ||||
Marek Rosa
|
r2339 | Defines the label format of the axis. | ||
Marek Rosa
|
r2268 | Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, c | ||
See QString::sprintf() for additional details. | ||||
Marek Rosa
|
r1855 | */ | ||
Marek Rosa
|
r1547 | /*! | ||
Marek Rosa
|
r1804 | \fn void QValueAxis::minChanged(qreal min) | ||
Marek Rosa
|
r1547 | Axis emits signal when \a min of axis has changed. | ||
*/ | ||||
sauimone
|
r1654 | /*! | ||
Marek Rosa
|
r1812 | \qmlsignal ValueAxis::onMinChanged(real min) | ||
sauimone
|
r1654 | Axis emits signal when \a min of axis has changed. | ||
*/ | ||||
Marek Rosa
|
r1547 | |||
/*! | ||||
Marek Rosa
|
r1804 | \fn void QValueAxis::maxChanged(qreal max) | ||
Marek Rosa
|
r1547 | Axis emits signal when \a max of axis has changed. | ||
*/ | ||||
sauimone
|
r1654 | /*! | ||
Marek Rosa
|
r1812 | \qmlsignal ValueAxis::onMaxChanged(real max) | ||
sauimone
|
r1654 | Axis emits signal when \a max of axis has changed. | ||
*/ | ||||
Marek Rosa
|
r1547 | |||
/*! | ||||
Michal Klocek
|
r2308 | \fn void QValueAxis::tickCountChanged(int tickCount) | ||
Miikka Heikkinen
|
r2494 | Axis emits signal when \a tickCount of axis has changed. | ||
Marek Rosa
|
r1547 | */ | ||
sauimone
|
r1575 | /*! | ||
Michal Klocek
|
r2308 | \qmlsignal ValueAxis::tickCountChanged(int tickCount) | ||
Miikka Heikkinen
|
r2494 | Axis emits signal when \a tickCount of axis has changed. | ||
sauimone
|
r1575 | */ | ||
/*! | ||||
Michal Klocek
|
r2308 | \fn void QValueAxis::rangeChanged(qreal min, qreal max) | ||
Axis emits signal when \a min or \a max of axis has changed. | ||||
sauimone
|
r1575 | */ | ||
Marek Rosa
|
r2339 | /*! | ||
\fn void QValueAxis::labelFormatChanged(const QString &format) | ||||
Axis emits signal when \a format of axis labels has changed. | ||||
*/ | ||||
/*! | ||||
\qmlsignal ValueAxis::labelFormatChanged(const QString &format) | ||||
Miikka Heikkinen
|
r2494 | Axis emits signal when \a format of axis labels has changed. | ||
Marek Rosa
|
r2339 | */ | ||
sauimone
|
r1575 | /*! | ||
Marek Rosa
|
r1804 | \property QValueAxis::niceNumbersEnabled | ||
Michal Klocek
|
r2325 | \obsolete | ||
Using this function can lead to unexpected behavior. Use applyNiceNumbers() instead. | ||||
sauimone
|
r1575 | */ | ||
/*! | ||||
Marek Rosa
|
r1812 | \qmlproperty bool ValueAxis::niceNumbersEnabled | ||
Michal Klocek
|
r2325 | \obsolete | ||
Using this function can lead to unexpected behavior. Use applyNiceNumbers() instead. | ||||
sauimone
|
r1575 | */ | ||
Marek Rosa
|
r1636 | /*! | ||
Constructs an axis object which is a child of \a parent. | ||||
*/ | ||||
Marek Rosa
|
r1804 | QValueAxis::QValueAxis(QObject *parent) : | ||
Jani Honkonen
|
r2097 | QAbstractAxis(*new QValueAxisPrivate(this), parent) | ||
Michal Klocek
|
r1540 | { | ||
} | ||||
Marek Rosa
|
r1638 | /*! | ||
\internal | ||||
*/ | ||||
Jani Honkonen
|
r2097 | QValueAxis::QValueAxis(QValueAxisPrivate &d, QObject *parent) | ||
: QAbstractAxis(d, parent) | ||||
Michal Klocek
|
r1540 | { | ||
} | ||||
Marek Rosa
|
r1638 | /*! | ||
Destroys the object | ||||
*/ | ||||
Marek Rosa
|
r1804 | QValueAxis::~QValueAxis() | ||
Michal Klocek
|
r1540 | { | ||
Marek Rosa
|
r1804 | Q_D(QValueAxis); | ||
Michal Klocek
|
r2273 | if (d->m_chart) | ||
d->m_chart->removeAxis(this); | ||||
Michal Klocek
|
r1540 | } | ||
Marek Rosa
|
r1804 | void QValueAxis::setMin(qreal min) | ||
Michal Klocek
|
r1540 | { | ||
Marek Rosa
|
r1804 | Q_D(QValueAxis); | ||
Marek Rosa
|
r1728 | setRange(min, qMax(d->m_max, min)); | ||
Michal Klocek
|
r1540 | } | ||
Marek Rosa
|
r1804 | qreal QValueAxis::min() const | ||
Michal Klocek
|
r1540 | { | ||
Marek Rosa
|
r1804 | Q_D(const QValueAxis); | ||
Michal Klocek
|
r1540 | return d->m_min; | ||
} | ||||
Marek Rosa
|
r1804 | void QValueAxis::setMax(qreal max) | ||
Michal Klocek
|
r1540 | { | ||
Marek Rosa
|
r1804 | Q_D(QValueAxis); | ||
Marek Rosa
|
r1728 | setRange(qMin(d->m_min, max), max); | ||
Michal Klocek
|
r1540 | } | ||
Marek Rosa
|
r1804 | qreal QValueAxis::max() const | ||
Michal Klocek
|
r1540 | { | ||
Marek Rosa
|
r1804 | Q_D(const QValueAxis); | ||
Michal Klocek
|
r1540 | return d->m_max; | ||
} | ||||
/*! | ||||
Sets range from \a min to \a max on the axis. | ||||
Marek Rosa
|
r1730 | If min is greater than max then this function returns without making any changes. | ||
Michal Klocek
|
r1540 | */ | ||
Marek Rosa
|
r1804 | void QValueAxis::setRange(qreal min, qreal max) | ||
Michal Klocek
|
r1540 | { | ||
Marek Rosa
|
r1804 | Q_D(QValueAxis); | ||
Michal Klocek
|
r2273 | d->setRange(min,max); | ||
Michal Klocek
|
r1540 | } | ||
Marek Rosa
|
r1807 | void QValueAxis::setTickCount(int count) | ||
Michal Klocek
|
r1540 | { | ||
Marek Rosa
|
r1804 | Q_D(QValueAxis); | ||
Jani Honkonen
|
r2097 | if (d->m_tickCount != count && count >= 2) { | ||
Michal Klocek
|
r1698 | d->m_tickCount = count; | ||
Michal Klocek
|
r2273 | emit tickCountChanged(count); | ||
Michal Klocek
|
r1540 | } | ||
} | ||||
Marek Rosa
|
r1807 | int QValueAxis::tickCount() const | ||
Michal Klocek
|
r1540 | { | ||
Marek Rosa
|
r1804 | Q_D(const QValueAxis); | ||
Michal Klocek
|
r1698 | return d->m_tickCount; | ||
Michal Klocek
|
r1540 | } | ||
Marek Rosa
|
r1804 | void QValueAxis::setNiceNumbersEnabled(bool enable) | ||
Michal Klocek
|
r1540 | { | ||
Michal Klocek
|
r2314 | Q_D(QValueAxis); | ||
Michal Klocek
|
r2325 | qWarning()<<"This function is depreciated, it can lead to unexpected behavior.Use applyNiceNumbers(). "; | ||
Michal Klocek
|
r2314 | if(enable) { | ||
QObject::connect(this,SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(applyNiceNumbers())); | ||||
QObject::connect(this,SIGNAL(tickCountChanged(int)),this,SLOT(applyNiceNumbers())); | ||||
applyNiceNumbers(); | ||||
} | ||||
else { | ||||
QObject::disconnect(this,SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(applyNiceNumbers())); | ||||
QObject::disconnect(this,SIGNAL(tickCountChanged(int)),this,SLOT(applyNiceNumbers())); | ||||
} | ||||
Michal Klocek
|
r2315 | d->m_niceNumbersEnabled=enable; | ||
Michal Klocek
|
r1540 | } | ||
Michal Klocek
|
r2273 | bool QValueAxis::niceNumbersEnabled() const | ||
Michal Klocek
|
r1540 | { | ||
Michal Klocek
|
r2314 | Q_D(const QValueAxis); | ||
Michal Klocek
|
r2273 | qWarning()<<"This function is depreciated.Use applyNiceNumbers()."; | ||
Michal Klocek
|
r2314 | return d->m_niceNumbersEnabled; | ||
Michal Klocek
|
r1540 | } | ||
Marek Rosa
|
r1854 | void QValueAxis::setLabelFormat(const QString &format) | ||
{ | ||||
Q_D(QValueAxis); | ||||
d->m_format = format; | ||||
Marek Rosa
|
r2319 | emit labelFormatChanged(format); | ||
Marek Rosa
|
r1854 | } | ||
QString QValueAxis::labelFormat() const | ||||
{ | ||||
Q_D(const QValueAxis); | ||||
return d->m_format; | ||||
} | ||||
Marek Rosa
|
r1637 | /*! | ||
Returns the type of the axis | ||||
*/ | ||||
Marek Rosa
|
r1804 | QAbstractAxis::AxisType QValueAxis::type() const | ||
Michal Klocek
|
r1540 | { | ||
Marek Rosa
|
r1818 | return AxisTypeValue; | ||
Michal Klocek
|
r1540 | } | ||
Michal Klocek
|
r2325 | /*! | ||
This method modifies range and number of ticks on the axis to look "nice". Algorithm considers numbers that | ||||
can be expressed as form of 1*10^n, 2* 10^n or 5*10^n as a nice numbers. These numbers are used for spacing the ticks. | ||||
This method will modify the current range and number of ticks. | ||||
Marek Rosa
|
r2339 | \sa setRange(), setTickCount() | ||
Michal Klocek
|
r2325 | */ | ||
Michal Klocek
|
r2273 | void QValueAxis::applyNiceNumbers() | ||
{ | ||||
Q_D(QValueAxis); | ||||
Michal Klocek
|
r2314 | if(d->m_applying) return; | ||
Michal Klocek
|
r2273 | qreal min = d->m_min; | ||
qreal max = d->m_max; | ||||
int ticks = d->m_tickCount; | ||||
Marek Rosa
|
r2275 | AbstractDomain::looseNiceNumbers(min,max,ticks); | ||
Michal Klocek
|
r2314 | d->m_applying=true; | ||
Michal Klocek
|
r2273 | d->setRange(min,max); | ||
setTickCount(ticks); | ||||
Michal Klocek
|
r2314 | d->m_applying=false; | ||
Michal Klocek
|
r2273 | } | ||
Michal Klocek
|
r1540 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
Jani Honkonen
|
r2104 | QValueAxisPrivate::QValueAxisPrivate(QValueAxis *q) | ||
: QAbstractAxisPrivate(q), | ||||
m_min(0), | ||||
m_max(0), | ||||
m_tickCount(5), | ||||
Michal Klocek
|
r2314 | m_format(QString::null), | ||
m_applying(false), | ||||
m_niceNumbersEnabled(false) | ||||
Michal Klocek
|
r1540 | { | ||
} | ||||
Marek Rosa
|
r1804 | QValueAxisPrivate::~QValueAxisPrivate() | ||
Michal Klocek
|
r1540 | { | ||
} | ||||
Marek Rosa
|
r1804 | void QValueAxisPrivate::setMin(const QVariant &min) | ||
Michal Klocek
|
r1544 | { | ||
Marek Rosa
|
r1804 | Q_Q(QValueAxis); | ||
sauimone
|
r1578 | bool ok; | ||
qreal value = min.toReal(&ok); | ||||
Jani Honkonen
|
r2097 | if (ok) | ||
q->setMin(value); | ||||
Michal Klocek
|
r1544 | } | ||
Marek Rosa
|
r1804 | void QValueAxisPrivate::setMax(const QVariant &max) | ||
Michal Klocek
|
r1544 | { | ||
Marek Rosa
|
r1804 | Q_Q(QValueAxis); | ||
sauimone
|
r1578 | bool ok; | ||
qreal value = max.toReal(&ok); | ||||
Jani Honkonen
|
r2097 | if (ok) | ||
q->setMax(value); | ||||
Michal Klocek
|
r1544 | } | ||
Marek Rosa
|
r1804 | void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max) | ||
Michal Klocek
|
r1544 | { | ||
Marek Rosa
|
r1804 | Q_Q(QValueAxis); | ||
sauimone
|
r1578 | bool ok1; | ||
bool ok2; | ||||
qreal value1 = min.toReal(&ok1); | ||||
qreal value2 = max.toReal(&ok2); | ||||
Jani Honkonen
|
r2097 | if (ok1 && ok2) | ||
q->setRange(value1, value2); | ||||
Michal Klocek
|
r1544 | } | ||
Michal Klocek
|
r2273 | void QValueAxisPrivate::setRange(qreal min, qreal max) | ||
Michal Klocek
|
r1556 | { | ||
Michal Klocek
|
r2273 | Q_Q(QValueAxis); | ||
bool changed = false; | ||||
Michal Klocek
|
r1556 | |||
Michal Klocek
|
r2273 | if (min > max) | ||
return; | ||||
if (!qFuzzyCompare(m_min,min)) { | ||||
m_min = min; | ||||
changed = true; | ||||
emit q->minChanged(min); | ||||
} | ||||
if (!qFuzzyCompare(m_max,max)) { | ||||
m_max = max; | ||||
changed = true; | ||||
emit q->maxChanged(max); | ||||
Michal Klocek
|
r1661 | } | ||
Michal Klocek
|
r2273 | if (changed) { | ||
emit rangeChanged(min,max); | ||||
Michal Klocek
|
r2314 | emit q->rangeChanged(min, max); | ||
Michal Klocek
|
r2273 | } | ||
} | ||||
Michal Klocek
|
r1698 | |||
Miikka Heikkinen
|
r2483 | void QValueAxisPrivate::initializeGraphics(QGraphicsItem *parent) | ||
Michal Klocek
|
r1698 | { | ||
Michal Klocek
|
r2273 | Q_Q(QValueAxis); | ||
Miikka Heikkinen
|
r2483 | ChartAxisElement *axis(0); | ||
if (m_chart->chartType() == QChart::ChartTypeCartesian) { | ||||
if (orientation() == Qt::Vertical) | ||||
axis = new ChartValueAxisY(q,parent); | ||||
if (orientation() == Qt::Horizontal) | ||||
axis = new ChartValueAxisX(q,parent); | ||||
} | ||||
if (m_chart->chartType() == QChart::ChartTypePolar) { | ||||
if (orientation() == Qt::Vertical) | ||||
axis = new PolarChartValueAxisRadial(q, parent); | ||||
if (orientation() == Qt::Horizontal) | ||||
axis = new PolarChartValueAxisAngular(q, parent); | ||||
} | ||||
Michal Klocek
|
r2273 | |||
m_item.reset(axis); | ||||
QAbstractAxisPrivate::initializeGraphics(parent); | ||||
Michal Klocek
|
r1698 | } | ||
Marek Rosa
|
r2275 | void QValueAxisPrivate::initializeDomain(AbstractDomain *domain) | ||
Michal Klocek
|
r1698 | { | ||
Michal Klocek
|
r2273 | if (orientation() == Qt::Vertical) { | ||
Miikka Heikkinen
|
r2483 | if (!qFuzzyIsNull(m_max - m_min)) | ||
Michal Klocek
|
r2273 | domain->setRangeY(m_min, m_max); | ||
Miikka Heikkinen
|
r2483 | else | ||
Michal Klocek
|
r2273 | setRange(domain->minY(), domain->maxY()); | ||
} | ||||
if (orientation() == Qt::Horizontal) { | ||||
Miikka Heikkinen
|
r2483 | if (!qFuzzyIsNull(m_max - m_min)) | ||
Michal Klocek
|
r2273 | domain->setRangeX(m_min, m_max); | ||
Miikka Heikkinen
|
r2483 | else | ||
Michal Klocek
|
r2273 | setRange(domain->minX(), domain->maxX()); | ||
Michal Klocek
|
r1698 | } | ||
} | ||||
Marek Rosa
|
r1805 | #include "moc_qvalueaxis.cpp" | ||
#include "moc_qvalueaxis_p.cpp" | ||||
Michal Klocek
|
r1540 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||