From 6fc7ac20b7044253a34bb8e1a158bbc95900ceb0 2012-07-19 09:33:08 From: Marek Rosa Date: 2012-07-19 09:33:08 Subject: [PATCH] Added QDateTimeAxis --- diff --git a/src/axis/axis.pri b/src/axis/axis.pri index 5ac74da..ce49d17 100644 --- a/src/axis/axis.pri +++ b/src/axis/axis.pri @@ -3,12 +3,14 @@ INCLUDEPATH += $$PWD \ $$PWD/valuesaxis \ $$PWD/categoriesaxis \ - $$PWD/intervalsaxis + $$PWD/intervalsaxis \ + $$PWD/datetimeaxis DEPENDPATH += $$PWD \ $$PWD/valuesaxis \ $$PWD/categoriesaxis \ - $$PWD/intervalsaxis + $$PWD/intervalsaxis \ + $$PWD/datetimeaxis SOURCES += \ $$PWD/chartaxis.cpp \ @@ -21,7 +23,10 @@ SOURCES += \ $$PWD/categoriesaxis/qbarcategoriesaxis.cpp \ $$PWD/intervalsaxis/chartintervalsaxisx.cpp \ $$PWD/intervalsaxis/chartintervalsaxisy.cpp \ - $$PWD/intervalsaxis/qintervalsaxis.cpp + $$PWD/intervalsaxis/qintervalsaxis.cpp \ + $$PWD/datetimeaxis/chartdatetimeaxisx.cpp \ + $$PWD/datetimeaxis/chartdatetimeaxisy.cpp \ + $$PWD/datetimeaxis/qdatetimeaxis.cpp PRIVATE_HEADERS += \ $$PWD/chartaxis_p.h \ @@ -34,10 +39,14 @@ PRIVATE_HEADERS += \ $$PWD/categoriesaxis/qbarcategoriesaxis_p.h \ $$PWD/intervalsaxis/chartintervalsaxisx_p.h \ $$PWD/intervalsaxis/chartintervalsaxisy_p.h \ - $$PWD/intervalsaxis/qintervalsaxis_p.h + $$PWD/intervalsaxis/qintervalsaxis_p.h \ + $$PWD/datetimeaxis/chartdatetimeaxisx_p.h \ + $$PWD/datetimeaxis/chartdatetimeaxisy_p.h \ + $$PWD/datetimeaxis/qdatetimeaxis_p.h PUBLIC_HEADERS += \ $$PWD/qabstractaxis.h \ $$PWD/valuesaxis/qvaluesaxis.h \ $$PWD/categoriesaxis/qbarcategoriesaxis.h \ - $$PWD/intervalsaxis/qintervalsaxis.h + $$PWD/intervalsaxis/qintervalsaxis.h \ + $$PWD/datetimeaxis/qdatetimeaxis.h diff --git a/src/axis/chartaxis.cpp b/src/axis/chartaxis.cpp index 30183d6..809c346 100644 --- a/src/axis/chartaxis.cpp +++ b/src/axis/chartaxis.cpp @@ -27,6 +27,7 @@ #include #include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE diff --git a/src/axis/datetimeaxis/chartdatetimeaxisx.cpp b/src/axis/datetimeaxis/chartdatetimeaxisx.cpp new file mode 100644 index 0000000..37bec16 --- /dev/null +++ b/src/axis/datetimeaxis/chartdatetimeaxisx.cpp @@ -0,0 +1,138 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** 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$ +** +****************************************************************************/ + +#include "chartdatetimeaxisx_p.h" +#include "qabstractaxis.h" +#include "chartpresenter_p.h" +#include "chartanimator_p.h" +#include "qdatetimeaxis.h" +#include +#include +#include +#include +#include + +static int label_padding = 5; + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +ChartDateTimeAxisX::ChartDateTimeAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), +m_tickCount(0) +{ +} + +ChartDateTimeAxisX::~ChartDateTimeAxisX() +{ +} + +void ChartDateTimeAxisX::createLabels(QStringList &labels,qreal min, qreal max, int ticks) +{ + Q_ASSERT(max>min); + Q_ASSERT(ticks>1); + + QDateTimeAxis *axis = qobject_cast(m_chartAxis); + + int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); + n++; + for (int i=0; i< ticks; i++) { + qreal value = min + (i * (max - min)/ (ticks-1)); + labels << QDateTime::fromMSecsSinceEpoch(value).toString(axis->formatString()); + } +} + +QVector ChartDateTimeAxisX::calculateLayout() const +{ + Q_ASSERT(m_tickCount>=2); + + QVector points; + points.resize(m_tickCount); + + const qreal deltaX = m_rect.width()/(m_tickCount-1); + for (int i = 0; i < m_tickCount; ++i) { + int x = i * deltaX + m_rect.left(); + points[i] = x; + } + return points; +} + +void ChartDateTimeAxisX::updateGeometry() +{ + const QVector& layout = ChartAxis::layout(); + + m_minWidth = 0; + m_minHeight = 0; + + if(layout.isEmpty()) return; + + QStringList ticksList; + + createLabels(ticksList,m_min,m_max,layout.size()); + + QList lines = m_grid->childItems(); + QList labels = m_labels->childItems(); + QList shades = m_shades->childItems(); + QList axis = m_axis->childItems(); + + Q_ASSERT(labels.size() == ticksList.size()); + Q_ASSERT(layout.size() == ticksList.size()); + + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); + + qreal width = 0; + for (int i = 0; i < layout.size(); ++i) { + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); + QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); + labelItem->setText(ticksList.at(i)); + const QRectF& rect = labelItem->boundingRect(); + QPointF center = rect.center(); + labelItem->setTransformOriginPoint(center.x(), center.y()); + labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); + + if(labelItem->pos().x()<=width){ + labelItem->setVisible(false); + lineItem->setVisible(false); + }else{ + labelItem->setVisible(true); + lineItem->setVisible(true); + width=rect.width()+labelItem->pos().x(); + } + m_minWidth+=rect.width(); + m_minHeight=qMax(rect.height(),m_minHeight); + + if ((i+1)%2 && i>1) { + QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); + rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); + } + lineItem = static_cast(axis.at(i+1)); + lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); + } +} + +void ChartDateTimeAxisX::handleAxisUpdated() +{ + //TODO:: fix this + QDateTimeAxis* axis = qobject_cast(m_chartAxis); + m_tickCount = axis->ticksCount(); + ChartAxis::handleAxisUpdated(); +} + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/datetimeaxis/chartdatetimeaxisx_p.h b/src/axis/datetimeaxis/chartdatetimeaxisx_p.h new file mode 100644 index 0000000..aeb78da --- /dev/null +++ b/src/axis/datetimeaxis/chartdatetimeaxisx_p.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial Chart API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef CHARTDATETIMEAXISX_H +#define CHARTDATETIMEAXISX_H + +#include "chartaxis_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QAbstractAxis; +class ChartPresenter; + +class ChartDateTimeAxisX : public ChartAxis +{ +public: + ChartDateTimeAxisX(QAbstractAxis *axis, ChartPresenter *presenter); + ~ChartDateTimeAxisX(); + + AxisType axisType() const { return X_AXIS;} + +protected: + void createLabels(QStringList &labels,qreal min, qreal max, int ticks); + void handleAxisUpdated(); + QVector calculateLayout() const; + void updateGeometry(); + +private: + int m_tickCount; + +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif /* CHARTDATETIMEAXISX_H */ diff --git a/src/axis/datetimeaxis/chartdatetimeaxisy.cpp b/src/axis/datetimeaxis/chartdatetimeaxisy.cpp new file mode 100644 index 0000000..fadc6a8 --- /dev/null +++ b/src/axis/datetimeaxis/chartdatetimeaxisy.cpp @@ -0,0 +1,144 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** 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$ +** +****************************************************************************/ + +#include "chartdatetimeaxisy_p.h" +#include "qabstractaxis.h" +#include "chartpresenter_p.h" +#include "chartanimator_p.h" +#include "qdatetimeaxis.h" +#include +#include +#include +#include +#include + +static int label_padding = 5; + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +ChartDateTimeAxisY::ChartDateTimeAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), +m_tickCount(0) +{ +} + +ChartDateTimeAxisY::~ChartDateTimeAxisY() +{ +} + +void ChartDateTimeAxisY::createLabels(QStringList &labels,qreal min, qreal max, int ticks) +{ + Q_ASSERT(max>min); + Q_ASSERT(ticks>1); + + QDateTimeAxis *axis = qobject_cast(m_chartAxis); + + int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); + n++; + for (int i=0; i< ticks; i++) { + qreal value = min + (i * (max - min)/ (ticks-1)); + labels << QDateTime::fromMSecsSinceEpoch(value).toString(axis->formatString()); + } +} + +QVector ChartDateTimeAxisY::calculateLayout() const +{ + Q_ASSERT(m_tickCount>=2); + + QVector points; + points.resize(m_tickCount); + + const qreal deltaY = m_rect.height()/(m_tickCount-1); + for (int i = 0; i < m_tickCount; ++i) { + int y = i * -deltaY + m_rect.bottom(); + points[i] = y; + } + + return points; +} + +void ChartDateTimeAxisY::updateGeometry() +{ + const QVector &layout = ChartAxis::layout(); + m_minWidth = 0; + m_minHeight = 0; + + if(layout.isEmpty()) return; + + QStringList ticksList; + + createLabels(ticksList,m_min,m_max,layout.size()); + + QList lines = m_grid->childItems(); + QList labels = m_labels->childItems(); + QList shades = m_shades->childItems(); + QList axis = m_axis->childItems(); + + Q_ASSERT(labels.size() == ticksList.size()); + Q_ASSERT(layout.size() == ticksList.size()); + + qreal height = 2*m_rect.bottom(); + + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); + + for (int i = 0; i < layout.size(); ++i) { + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); + QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); + + labelItem->setText(ticksList.at(i)); + const QRectF& rect = labelItem->boundingRect(); + + QPointF center = rect.center(); + labelItem->setTransformOriginPoint(center.x(), center.y()); + labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y()); + + if(labelItem->pos().y()+rect.height()>height) { + labelItem->setVisible(false); + lineItem->setVisible(false); + } + else { + labelItem->setVisible(true); + lineItem->setVisible(true); + height=labelItem->pos().y(); + } + + m_minWidth=qMax(rect.width()+label_padding,m_minWidth); + m_minHeight+=rect.height(); + + if ((i+1)%2 && i>1) { + QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); + rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); + } + lineItem = static_cast(axis.at(i+1)); + lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); + } +} + +void ChartDateTimeAxisY::handleAxisUpdated() +{ + //TODO:: fix this + QDateTimeAxis* axis = qobject_cast(m_chartAxis); + m_tickCount = axis->ticksCount(); + ChartAxis::handleAxisUpdated(); +} + + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/datetimeaxis/chartdatetimeaxisy_p.h b/src/axis/datetimeaxis/chartdatetimeaxisy_p.h new file mode 100644 index 0000000..2c11688 --- /dev/null +++ b/src/axis/datetimeaxis/chartdatetimeaxisy_p.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial Chart API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef CHARTDATETIMEAXISY_H +#define CHARTDATETIMEAXISY_H + +#include "chartaxis_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QAbstractAxis; +class ChartPresenter; + +class ChartDateTimeAxisY : public ChartAxis +{ +public: + ChartDateTimeAxisY(QAbstractAxis *axis, ChartPresenter *presenter); + ~ChartDateTimeAxisY(); + + AxisType axisType() const { return Y_AXIS;} + +protected: + void createLabels(QStringList &labels,qreal min, qreal max, int ticks); + QVector calculateLayout() const; + void updateGeometry(); + void handleAxisUpdated(); +private: + int m_tickCount; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif /* CHARTDATETIMEAXISY_H */ diff --git a/src/axis/datetimeaxis/qdatetimeaxis.cpp b/src/axis/datetimeaxis/qdatetimeaxis.cpp new file mode 100644 index 0000000..9d4e70d --- /dev/null +++ b/src/axis/datetimeaxis/qdatetimeaxis.cpp @@ -0,0 +1,323 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** 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$ +** +****************************************************************************/ + +#include "qdatetimeaxis.h" +#include "qdatetimeaxis_p.h" +#include "chartdatetimeaxisx_p.h" +#include "chartdatetimeaxisy_p.h" +#include "domain_p.h" +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +/*! + \class QDateTimeAxis + \brief The QDateTimeAxis class is used for manipulating chart's axis. + \mainclass + + ValuesAxis can be setup to show axis line with tick marks, grid lines and shades. + Values of axis are drawn to position of ticks +*/ + +/*! + \qmlclass ValuesAxis QDateTimeAxis + \brief The ValuesAxis element is used for manipulating chart's axes + + ValueAxis can be setup to show axis line with tick marks, grid lines and shades. + Values of axis are drawn to position of ticks + + To access Axes you can use ChartView API. For example: + \code + ChartView { + ValuesAxis { + id: xAxis + min: 0 + max: 10 + } + // Add a few series... + } + \endcode +*/ + +/*! + \property QDateTimeAxis::min + Defines the minimum value on the axis. +*/ +/*! + \qmlproperty real ValuesAxis::min + Defines the minimum value on the axis. +*/ + +/*! + \property QDateTimeAxis::max + Defines the maximum value on the axis. +*/ +/*! + \qmlproperty real ValuesAxis::max + Defines the maximum value on the axis. +*/ + +/*! + \fn void QDateTimeAxis::minChanged(qreal min) + Axis emits signal when \a min of axis has changed. +*/ +/*! + \qmlsignal ValuesAxis::onMinChanged(real min) + Axis emits signal when \a min of axis has changed. +*/ + +/*! + \fn void QDateTimeAxis::maxChanged(qreal max) + Axis emits signal when \a max of axis has changed. +*/ +/*! + \qmlsignal ValuesAxis::onMaxChanged(real max) + Axis emits signal when \a max of axis has changed. +*/ + +/*! + \fn void QDateTimeAxis::rangeChanged(qreal min, qreal max) + Axis emits signal when \a min or \a max of axis has changed. +*/ + +/*! + \property QDateTimeAxis::ticksCount + The number of tick marks for the axis. +*/ + +/*! + \qmlproperty int ValuesAxis::ticksCount + The number of tick marks for the axis. +*/ + +/*! + \property QDateTimeAxis::niceNumbersEnabled + Whether the nice numbers algorithm is enabled or not for the axis. +*/ + +/*! + \qmlproperty bool ValuesAxis::niceNumbersEnabled + Whether the nice numbers algorithm is enabled or not for the axis. +*/ + +/*! + Constructs an axis object which is a child of \a parent. +*/ +QDateTimeAxis::QDateTimeAxis(QObject *parent) : + QAbstractAxis(*new QDateTimeAxisPrivate(this),parent) +{ + +} + +/*! + \internal +*/ +QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent) +{ + +} + +/*! + Destroys the object +*/ +QDateTimeAxis::~QDateTimeAxis() +{ + +} + +void QDateTimeAxis::setMin(QDateTime min) +{ + Q_D(QDateTimeAxis); + setRange(min,d->m_max); +} + +QDateTime QDateTimeAxis::min() const +{ + Q_D(const QDateTimeAxis); + return d->m_min; +} + +void QDateTimeAxis::setMax(QDateTime max) +{ + Q_D(QDateTimeAxis); + setRange(d->m_min,max); +} + +QDateTime QDateTimeAxis::max() const +{ + Q_D(const QDateTimeAxis); + return d->m_max; +} + +/*! + Sets range from \a min to \a max on the axis. +*/ +void QDateTimeAxis::setRange(QDateTime min, QDateTime max) +{ + Q_D(QDateTimeAxis); + bool changed = false; + + if (d->m_min != min) { + d->m_min = min; + changed = true; + emit minChanged(min); + } + + if (d->m_max != max) { + d->m_max = max; + changed = true; + emit maxChanged(max); + } + + // if(d->m_niceNumbers) d->looseNiceNumbers(d->m_min, d->m_max, d->m_tickCount); + + if (changed) { + emit rangeChanged(d->m_min,d->m_max); + d->emitUpdated(); + } +} + +void QDateTimeAxis::setFormatString(QString format) +{ + Q_D(QDateTimeAxis); + d->m_format = format; +} + +QString QDateTimeAxis::formatString() const +{ + Q_D(const QDateTimeAxis); + return d->m_format; +} + +/*! + Sets \a count for ticks on the axis. +*/ +void QDateTimeAxis::setTicksCount(int count) +{ + Q_D(QDateTimeAxis); + if (d->m_tickCount != count && count >=2) { + d->m_tickCount = count; + d->emitUpdated(); + } +} + +/*! + \fn int QDateTimeAxis::ticksCount() const + Return number of ticks on the axis +*/ +int QDateTimeAxis::ticksCount() const +{ + Q_D(const QDateTimeAxis); + return d->m_tickCount; +} + +/*! + Returns the type of the axis +*/ +QAbstractAxis::AxisType QDateTimeAxis::type() const +{ + return AxisTypeDateTime; +} + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +QDateTimeAxisPrivate::QDateTimeAxisPrivate(QDateTimeAxis* q): + QAbstractAxisPrivate(q), + m_tickCount(5) +{ + m_min = QDateTime::fromMSecsSinceEpoch(0); + m_max = QDateTime::fromMSecsSinceEpoch(0); + m_format = "hh:mm:ss\ndd-mm-yyyy"; +} + +QDateTimeAxisPrivate::~QDateTimeAxisPrivate() +{ + +} + +void QDateTimeAxisPrivate::handleDomainUpdated() +{ + Q_Q(QDateTimeAxis); + Domain* domain = qobject_cast(sender()); + Q_ASSERT(domain); + + if(orientation()==Qt::Horizontal){ + q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX())); + }else if(orientation()==Qt::Vertical){ + q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY())); + } +} + + +void QDateTimeAxisPrivate::setMin(const QVariant &min) +{ + Q_Q(QDateTimeAxis); + if (min.canConvert(QVariant::DateTime)) + q->setMin(min.toDateTime()); +} + +void QDateTimeAxisPrivate::setMax(const QVariant &max) +{ + + Q_Q(QDateTimeAxis); + if (max.canConvert(QVariant::DateTime)) + q->setMax(max.toDateTime()); +} + +void QDateTimeAxisPrivate::setRange(const QVariant &min, const QVariant &max) +{ + Q_Q(QDateTimeAxis); + if (min.canConvert(QVariant::DateTime) && max.canConvert(QVariant::DateTime)) + q->setRange(min.toDateTime(), max.toDateTime()); +} + +ChartAxis* QDateTimeAxisPrivate::createGraphics(ChartPresenter* presenter) +{ + Q_Q(QDateTimeAxis); + if(m_orientation == Qt::Vertical){ + return new ChartDateTimeAxisY(q,presenter); + }else{ + return new ChartDateTimeAxisX(q,presenter); + } + +} + +void QDateTimeAxisPrivate::intializeDomain(Domain* domain) +{ + Q_Q(QDateTimeAxis); + if(m_max == m_min) { + if(m_orientation==Qt::Vertical){ + q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY())); + }else{ + q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX())); + } + } else { + if(m_orientation==Qt::Vertical){ + domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch()); + }else{ + domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch()); + } + } +} + +#include "moc_qdatetimeaxis.cpp" +#include "moc_qdatetimeaxis_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/datetimeaxis/qdatetimeaxis.h b/src/axis/datetimeaxis/qdatetimeaxis.h new file mode 100644 index 0000000..74ad3d0 --- /dev/null +++ b/src/axis/datetimeaxis/qdatetimeaxis.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** 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$ +** +****************************************************************************/ + +#ifndef QDATETIMEAXIS_H +#define QDATETIMEAXIS_H + +#include "qabstractaxis.h" + +class QDateTime; + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QDateTimeAxisPrivate; + +class QTCOMMERCIALCHART_EXPORT QDateTimeAxis : public QAbstractAxis +{ + Q_OBJECT + Q_PROPERTY(int ticksCount READ ticksCount WRITE setTicksCount) + Q_PROPERTY(QDateTime min READ min WRITE setMin NOTIFY minChanged) + Q_PROPERTY(QDateTime max READ max WRITE setMax NOTIFY maxChanged) + +public: + explicit QDateTimeAxis(QObject *parent = 0); + ~QDateTimeAxis(); + +protected: + QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent = 0); + +public: + AxisType type() const; + + //range handling + void setMin(QDateTime min); + QDateTime min() const; + void setMax(QDateTime max); + QDateTime max() const; + void setRange(QDateTime min, QDateTime max); + + void setFormatString(QString format); + QString formatString() const; + + //ticks handling + void setTicksCount(int count); + int ticksCount() const; + +Q_SIGNALS: + void minChanged(QDateTime min); + void maxChanged(QDateTime max); + void rangeChanged(QDateTime min, QDateTime max); + +private: + Q_DECLARE_PRIVATE(QDateTimeAxis) + Q_DISABLE_COPY(QDateTimeAxis) +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QDATETIMEAXIS_H diff --git a/src/axis/datetimeaxis/qdatetimeaxis_p.h b/src/axis/datetimeaxis/qdatetimeaxis_p.h new file mode 100644 index 0000000..b61775b --- /dev/null +++ b/src/axis/datetimeaxis/qdatetimeaxis_p.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** 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$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial Chart API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef QVALUESAXIS_P_H +#define QVALUESAXIS_P_H + +#include "qdatetimeaxis.h" +#include "qabstractaxis_p.h" +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QDateTimeAxisPrivate : public QAbstractAxisPrivate +{ + Q_OBJECT + public: + QDateTimeAxisPrivate(QDateTimeAxis *q); + ~QDateTimeAxisPrivate(); + + public: + ChartAxis* createGraphics(ChartPresenter* presenter); + void intializeDomain(Domain* domain); + void handleDomainUpdated(); + qreal min(){ return m_min.toMSecsSinceEpoch(); } + qreal max(){ return m_max.toMSecsSinceEpoch(); } + + protected: + void setMin(const QVariant &min); + void setMax(const QVariant &max); + void setRange(const QVariant &min, const QVariant &max); + int ticksCount() const; + + protected: + QDateTime m_min; + QDateTime m_max; + int m_tickCount; + QString m_format; + Q_DECLARE_PUBLIC(QDateTimeAxis) +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QVALUESAXIS_P_H diff --git a/src/axis/qabstractaxis.h b/src/axis/qabstractaxis.h index 82a3d38..a74bb68 100644 --- a/src/axis/qabstractaxis.h +++ b/src/axis/qabstractaxis.h @@ -51,7 +51,8 @@ public: AxisTypeNoAxis = 0x0, AxisTypeValues = 0x1, AxisTypeCategories = 0x2, - AxisTypeIntervals = 0x3 + AxisTypeIntervals = 0x3, + AxisTypeDateTime = 0x4 }; Q_DECLARE_FLAGS(AxisTypes, AxisType) diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index ce4b9c1..342bfd2 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -23,6 +23,8 @@ #include "qvaluesaxis.h" #include "qbarcategoriesaxis.h" #include "qvaluesaxis_p.h" +#include "qintervalsaxis.h" +#include "qdatetimeaxis.h" #include "qabstractseries_p.h" #include "qabstractbarseries.h" #include "qstackedbarseries.h" @@ -122,20 +124,20 @@ void ChartDataSet::createDefaultAxes() QMapIterator i(m_seriesDomainMap); while (i.hasNext()) { - i.next(); - removeAxes(i.key()); + i.next(); + removeAxes(i.key()); } i.toFront(); while (i.hasNext()) { - i.next(); - QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); - QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); - if(axisX) typeX&=axisX->type(); - else typeX|=i.key()->d_ptr->defaultAxisType(Qt::Horizontal); - if(axisY) typeY&=axisY->type(); - else typeY|=i.key()->d_ptr->defaultAxisType(Qt::Vertical); + i.next(); + QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); + QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); + if(axisX) typeX&=axisX->type(); + else typeX|=i.key()->d_ptr->defaultAxisType(Qt::Horizontal); + if(axisY) typeY&=axisY->type(); + else typeY|=i.key()->d_ptr->defaultAxisType(Qt::Vertical); } createAxes(typeX,Qt::Horizontal); @@ -174,13 +176,19 @@ QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type,Qt::Orienta QAbstractAxis* axis =0; switch(type) { - case QAbstractAxis::AxisTypeValues: + case QAbstractAxis::AxisTypeValues: axis = new QValuesAxis(this); break; - case QAbstractAxis::AxisTypeCategories: + case QAbstractAxis::AxisTypeCategories: axis = new QBarCategoriesAxis(this); break; - default: + case QAbstractAxis::AxisTypeIntervals: + axis = new QIntervalsAxis(this); + break; + case QAbstractAxis::AxisTypeDateTime: + axis = new QDateTimeAxis(this); + break; + default: axis = 0; break; } @@ -289,12 +297,12 @@ void ChartDataSet::blockAxisSignals(bool enabled) { QMapIterator i(m_seriesDomainMap); while (i.hasNext()) { - i.next(); - QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); - QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); - if(axisX) axisX->d_ptr->blockSignals(enabled); - if(axisY) axisY->d_ptr->blockSignals(enabled); - } + i.next(); + QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); + QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); + if(axisX) axisX->d_ptr->blockSignals(enabled); + if(axisY) axisY->d_ptr->blockSignals(enabled); + } } int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type) @@ -302,8 +310,8 @@ int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type) int count=0; QMapIterator i(m_seriesDomainMap); while (i.hasNext()) { - i.next(); - if(i.key()->type()==type) count++; + i.next(); + if(i.key()->type()==type) count++; } return count; } @@ -323,13 +331,13 @@ QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const { if(series == 0) { - QMapIterator i(m_seriesAxisXMap); + QMapIterator i(m_seriesAxisXMap); - while (i.hasNext()) { - i.next(); - if(i.value()->isVisible()) return i.value(); - } - return 0; + while (i.hasNext()) { + i.next(); + if(i.value()->isVisible()) return i.value(); + } + return 0; } return m_seriesAxisXMap.value(series); }