From 75ed5c9858506bea31a772cf7d7fa8edd8037e45 2012-11-26 12:58:47 From: Marek Rosa Date: 2012-11-26 12:58:47 Subject: [PATCH] Log domains zoomin, zoomout and move added --- diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 39e23d2..c2d0874 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -33,6 +33,9 @@ #include "qpieseries.h" #include "chartitem_p.h" #include "xydomain_p.h" +#include "xlogydomain_p.h" +#include "logxydomain_p.h" +#include "logxlogydomain_p.h" #ifndef QT_ON_ARM #include "qdatetimeaxis.h" diff --git a/src/domain/abstractdomain.cpp b/src/domain/abstractdomain.cpp index e583bb2..889aece 100644 --- a/src/domain/abstractdomain.cpp +++ b/src/domain/abstractdomain.cpp @@ -52,29 +52,6 @@ QSizeF AbstractDomain::size() const return m_size; } -//void AbstractDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) -//{ -// bool axisXChanged = false; -// bool axisYChanged = false; - -// if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) { -// m_minX = minX; -// m_maxX = maxX; -// axisXChanged = true; -// emit rangeHorizontalChanged(m_minX, m_maxX); -// } - -// if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) { -// m_minY = minY; -// m_maxY = maxY; -// axisYChanged = true; -// emit rangeVerticalChanged(m_minY, m_maxY); -// } - -// if (axisXChanged || axisYChanged) -// emit updated(); -//} - void AbstractDomain::setRangeX(qreal min, qreal max) { setRange(min, max, m_minY, m_maxY); @@ -122,89 +99,6 @@ bool AbstractDomain::isEmpty() const return qFuzzyIsNull(spanX()) || qFuzzyIsNull(spanY()) || m_size.isEmpty() ; } -void AbstractDomain::zoomIn(const QRectF &rect) -{ - qreal dx = spanX() / m_size.width(); - qreal dy = spanY() / m_size.height(); - - qreal maxX = m_maxX; - qreal minX = m_minX; - qreal minY = m_minY; - qreal maxY = m_maxY; - - maxX = minX + dx * rect.right(); - minX = minX + dx * rect.left(); - minY = maxY - dy * rect.bottom(); - maxY = maxY - dy * rect.top(); - - setRange(minX, maxX, minY, maxY); -} - -void AbstractDomain::zoomOut(const QRectF &rect) -{ - qreal dx = spanX() / rect.width(); - qreal dy = spanY() / rect.height(); - - qreal maxX = m_maxX; - qreal minX = m_minX; - qreal minY = m_minY; - qreal maxY = m_maxY; - - minX = maxX - dx * rect.right(); - maxX = minX + dx * m_size.width(); - maxY = minY + dy * rect.bottom(); - minY = maxY - dy * m_size.height(); - - setRange(minX, maxX, minY, maxY); -} - -void AbstractDomain::move(qreal dx, qreal dy) -{ - qreal x = spanX() / m_size.width(); - qreal y = spanY() / m_size.height(); - - qreal maxX = m_maxX; - qreal minX = m_minX; - qreal minY = m_minY; - qreal maxY = m_maxY; - - if (dx != 0) { - minX = minX + x * dx; - maxX = maxX + x * dx; - } - if (dy != 0) { - minY = minY + y * dy; - maxY = maxY + y * dy; - } - setRange(minX, maxX, minY, maxY); -} - -//QPointF AbstractDomain::calculateGeometryPoint(const QPointF &point) const -//{ -// const qreal deltaX = m_size.width() / (m_maxX - m_minX); -// const qreal deltaY = m_size.height() / (m_maxY - m_minY); -// qreal x = (point.x() - m_minX) * deltaX; -// qreal y = (point.y() - m_minY) * -deltaY + m_size.height(); -// return QPointF(x, y); -//} - -//QVector AbstractDomain::calculateGeometryPoints(const QList& vector) const -//{ -// const qreal deltaX = m_size.width() / (m_maxX - m_minX); -// const qreal deltaY = m_size.height() / (m_maxY - m_minY); - -// QVector result; -// result.resize(vector.count()); - -// for (int i = 0; i < vector.count(); ++i) { -// qreal x = (vector[i].x() - m_minX) * deltaX; -// qreal y = (vector[i].y() - m_minY) * -deltaY + m_size.height(); -// result[i].setX(x); -// result[i].setY(y); -// } -// return result; -//} - QPointF AbstractDomain::calculateDomainPoint(const QPointF &point) const { const qreal deltaX = m_size.width() / (m_maxX - m_minX); diff --git a/src/domain/abstractdomain_p.h b/src/domain/abstractdomain_p.h index 701252a..957508e 100644 --- a/src/domain/abstractdomain_p.h +++ b/src/domain/abstractdomain_p.h @@ -72,9 +72,9 @@ public: friend bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const AbstractDomain &domain1, const AbstractDomain &domain2); friend QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const AbstractDomain &domain); - void zoomIn(const QRectF &rect); - void zoomOut(const QRectF &rect); - void move(qreal dx, qreal dy); + virtual void zoomIn(const QRectF &rect) = 0; + virtual void zoomOut(const QRectF &rect) = 0; + virtual void move(qreal dx, qreal dy) = 0; virtual QPointF calculateGeometryPoint(const QPointF &point) const = 0; virtual QPointF calculateDomainPoint(const QPointF &point) const = 0; diff --git a/src/domain/logxlogydomain.cpp b/src/domain/logxlogydomain.cpp index 011bb55..7d46c44 100644 --- a/src/domain/logxlogydomain.cpp +++ b/src/domain/logxlogydomain.cpp @@ -68,58 +68,46 @@ void LogXLogYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) void LogXLogYDomain::zoomIn(const QRectF &rect) { - qreal dx = spanX() / m_size.width(); - qreal dy = spanY() / m_size.height(); + qreal newLogMinX = rect.left() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX; + qreal newLogMaxX = rect.right() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX; + qreal minX = qPow(m_logBaseX, newLogMinX); + qreal maxX = qPow(m_logBaseX, newLogMaxX); - qreal maxX = m_maxX; - qreal minX = m_minX; - qreal minY = m_minY; - qreal maxY = m_maxY; - - maxX = minX + dx * rect.right(); - minX = minX + dx * rect.left(); - minY = maxY - dy * rect.bottom(); - maxY = maxY - dy * rect.top(); + qreal newLogMinY = m_logMaxY - rect.bottom() * (m_logMaxY - m_logMinY) / m_size.height(); + qreal newLogMaxY = m_logMaxY - rect.top() * (m_logMaxY - m_logMinY) / m_size.height(); + qreal minY = qPow(m_logBaseY, newLogMinY); + qreal maxY = qPow(m_logBaseY, newLogMaxY); setRange(minX, maxX, minY, maxY); } void LogXLogYDomain::zoomOut(const QRectF &rect) { - qreal dx = spanX() / rect.width(); - qreal dy = spanY() / rect.height(); - - qreal maxX = m_maxX; - qreal minX = m_minX; - qreal minY = m_minY; - qreal maxY = m_maxY; - - minX = maxX - dx * rect.right(); - maxX = minX + dx * m_size.width(); - maxY = minY + dy * rect.bottom(); - minY = maxY - dy * m_size.height(); + qreal ratioX = m_size.width()/rect.width(); + qreal newLogMinX = m_logMinX - (m_logMaxX - m_logMinX) / ratioX; + qreal newLogMaxX = m_logMaxX + (m_logMaxX - m_logMinX) / ratioX; + qreal minX = qPow(m_logBaseX, newLogMinX); + qreal maxX = qPow(m_logBaseX, newLogMaxX); + + qreal ratioY = m_size.height()/rect.height(); + qreal newLogMinY = m_logMaxY - (m_logMaxY - m_logMinY) / ratioY; + qreal newLogMaxY = m_logMaxY + (m_logMaxY - m_logMinY) / ratioY; + qreal minY = qPow(m_logBaseY, newLogMinY); + qreal maxY = qPow(m_logBaseY, newLogMaxY); setRange(minX, maxX, minY, maxY); } void LogXLogYDomain::move(qreal dx, qreal dy) { - qreal x = spanX() / m_size.width(); - qreal y = spanY() / m_size.height(); + qreal stepX = dx * qAbs(m_logMaxX - m_logMinX) / m_size.width(); + qreal minX = qPow(m_logBaseX, m_logMinX + stepX); + qreal maxX = qPow(m_logBaseX, m_logMaxX + stepX); - qreal maxX = m_maxX; - qreal minX = m_minX; - qreal minY = m_minY; - qreal maxY = m_maxY; + qreal stepY = dy * qAbs(m_logMaxY - m_logMinY) / m_size.height(); + qreal minY = qPow(m_logBaseY, m_logMinY + stepY); + qreal maxY = qPow(m_logBaseY, m_logMaxY + stepY); - if (dx != 0) { - minX = minX + x * dx; - maxX = maxX + x * dx; - } - if (dy != 0) { - minY = minY + y * dy; - maxY = maxY + y * dy; - } setRange(minX, maxX, minY, maxY); } diff --git a/src/domain/logxydomain.cpp b/src/domain/logxydomain.cpp index 62459d5..31e6760 100644 --- a/src/domain/logxydomain.cpp +++ b/src/domain/logxydomain.cpp @@ -64,16 +64,15 @@ void LogXYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) void LogXYDomain::zoomIn(const QRectF &rect) { - qreal dx = spanX() / m_size.width(); - qreal dy = spanY() / m_size.height(); + qreal newLogMinX = rect.left() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX; + qreal newLogMaxX = rect.right() * (m_logMaxX - m_logMinX) / m_size.width() + m_logMinX; + qreal minX = qPow(m_logBaseX, newLogMinX); + qreal maxX = qPow(m_logBaseX, newLogMaxX); - qreal maxX = m_maxX; - qreal minX = m_minX; + qreal dy = spanY() / m_size.height(); qreal minY = m_minY; qreal maxY = m_maxY; - maxX = minX + dx * rect.right(); - minX = minX + dx * rect.left(); minY = maxY - dy * rect.bottom(); maxY = maxY - dy * rect.top(); @@ -82,16 +81,16 @@ void LogXYDomain::zoomIn(const QRectF &rect) void LogXYDomain::zoomOut(const QRectF &rect) { - qreal dx = spanX() / rect.width(); - qreal dy = spanY() / rect.height(); + qreal ratioX = m_size.width()/rect.width(); + qreal newLogMinX = m_logMinX - (m_logMaxX - m_logMinX) / ratioX; + qreal newLogMaxX = m_logMaxX + (m_logMaxX - m_logMinX) / ratioX; + qreal minX = qPow(m_logBaseX, newLogMinX); + qreal maxX = qPow(m_logBaseX, newLogMaxX); - qreal maxX = m_maxX; - qreal minX = m_minX; + qreal dy = spanY() / rect.height(); qreal minY = m_minY; qreal maxY = m_maxY; - minX = maxX - dx * rect.right(); - maxX = minX + dx * m_size.width(); maxY = minY + dy * rect.bottom(); minY = maxY - dy * m_size.height(); @@ -100,18 +99,14 @@ void LogXYDomain::zoomOut(const QRectF &rect) void LogXYDomain::move(qreal dx, qreal dy) { - qreal x = spanX() / m_size.width(); - qreal y = spanY() / m_size.height(); + qreal stepX = dx * qAbs(m_logMaxX - m_logMinX) / m_size.width(); + qreal minX = qPow(m_logBaseX, m_logMinX + stepX); + qreal maxX = qPow(m_logBaseX, m_logMaxX + stepX); - qreal maxX = m_maxX; - qreal minX = m_minX; + qreal y = spanY() / m_size.height(); qreal minY = m_minY; qreal maxY = m_maxY; - if (dx != 0) { - minX = minX + x * dx; - maxX = maxX + x * dx; - } if (dy != 0) { minY = minY + y * dy; maxY = maxY + y * dy; diff --git a/src/domain/xlogydomain.cpp b/src/domain/xlogydomain.cpp index 7bda41e..fb2f9ee 100644 --- a/src/domain/xlogydomain.cpp +++ b/src/domain/xlogydomain.cpp @@ -64,17 +64,16 @@ void XLogYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) void XLogYDomain::zoomIn(const QRectF &rect) { qreal dx = spanX() / m_size.width(); - qreal dy = spanY() / m_size.height(); - qreal maxX = m_maxX; qreal minX = m_minX; - qreal minY = m_minY; - qreal maxY = m_maxY; maxX = minX + dx * rect.right(); minX = minX + dx * rect.left(); - minY = maxY - dy * rect.bottom(); - maxY = maxY - dy * rect.top(); + + qreal newLogMinY = m_logMaxY - rect.bottom() * (m_logMaxY - m_logMinY) / m_size.height(); + qreal newLogMaxY = m_logMaxY - rect.top() * (m_logMaxY - m_logMinY) / m_size.height(); + qreal minY = qPow(m_logBaseY, newLogMinY); + qreal maxY = qPow(m_logBaseY, newLogMaxY); setRange(minX, maxX, minY, maxY); } @@ -82,17 +81,17 @@ void XLogYDomain::zoomIn(const QRectF &rect) void XLogYDomain::zoomOut(const QRectF &rect) { qreal dx = spanX() / rect.width(); - qreal dy = spanY() / rect.height(); - qreal maxX = m_maxX; qreal minX = m_minX; - qreal minY = m_minY; - qreal maxY = m_maxY; minX = maxX - dx * rect.right(); maxX = minX + dx * m_size.width(); - maxY = minY + dy * rect.bottom(); - minY = maxY - dy * m_size.height(); + + qreal ratioY = m_size.height()/rect.height(); + qreal newLogMinY = m_logMaxY - (m_logMaxY - m_logMinY) / ratioY; + qreal newLogMaxY = m_logMaxY + (m_logMaxY - m_logMinY) / ratioY; + qreal minY = qPow(m_logBaseY, newLogMinY); + qreal maxY = qPow(m_logBaseY, newLogMaxY); setRange(minX, maxX, minY, maxY); } @@ -100,21 +99,18 @@ void XLogYDomain::zoomOut(const QRectF &rect) void XLogYDomain::move(qreal dx, qreal dy) { qreal x = spanX() / m_size.width(); - qreal y = spanY() / m_size.height(); - qreal maxX = m_maxX; qreal minX = m_minX; - qreal minY = m_minY; - qreal maxY = m_maxY; if (dx != 0) { minX = minX + x * dx; maxX = maxX + x * dx; } - if (dy != 0) { - minY = minY + y * dy; - maxY = maxY + y * dy; - } + + qreal stepY = dy * qAbs(m_logMaxY - m_logMinY) / m_size.height(); + qreal minY = qPow(m_logBaseY, m_logMinY + stepY); + qreal maxY = qPow(m_logBaseY, m_logMaxY + stepY); + setRange(minX, maxX, minY, maxY); }