1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include <private/logxlogydomain_p.h>
31#include <private/qabstractaxis_p.h>
32#include <QtCharts/QLogValueAxis>
33#include <QtCore/QtMath>
34#include <cmath>
35
36QT_CHARTS_BEGIN_NAMESPACE
37
38LogXLogYDomain::LogXLogYDomain(QObject *parent)
39 : AbstractDomain(parent),
40 m_logLeftX(0),
41 m_logRightX(1),
42 m_logBaseX(10),
43 m_logLeftY(0),
44 m_logRightY(1),
45 m_logBaseY(10)
46{
47}
48
49LogXLogYDomain::~LogXLogYDomain()
50{
51}
52
53void LogXLogYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
54{
55 bool axisXChanged = false;
56 bool axisYChanged = false;
57
58 adjustLogDomainRanges(min&: minX, max&: maxX);
59 adjustLogDomainRanges(min&: minY, max&: maxY);
60
61 if (!qFuzzyIsNull(d: m_minX - minX) || !qFuzzyIsNull(d: m_maxX - maxX)) {
62 m_minX = minX;
63 m_maxX = maxX;
64 axisXChanged = true;
65 qreal logMinX = std::log10(x: m_minX) / std::log10(x: m_logBaseX);
66 qreal logMaxX = std::log10(x: m_maxX) / std::log10(x: m_logBaseX);
67 m_logLeftX = logMinX < logMaxX ? logMinX : logMaxX;
68 m_logRightX = logMinX > logMaxX ? logMinX : logMaxX;
69 if(!m_signalsBlocked)
70 emit rangeHorizontalChanged(min: m_minX, max: m_maxX);
71 }
72
73 if (!qFuzzyIsNull(d: m_minY - minY) || !qFuzzyIsNull(d: m_maxY - maxY)) {
74 m_minY = minY;
75 m_maxY = maxY;
76 axisYChanged = true;
77 qreal logMinY = std::log10(x: m_minY) / std::log10(x: m_logBaseY);
78 qreal logMaxY = std::log10(x: m_maxY) / std::log10(x: m_logBaseY);
79 m_logLeftY = logMinY < logMaxY ? logMinY : logMaxY;
80 m_logRightY = logMinY > logMaxY ? logMinY : logMaxY;
81 if (!m_signalsBlocked)
82 emit rangeVerticalChanged(min: m_minY, max: m_maxY);
83 }
84
85 if (axisXChanged || axisYChanged)
86 emit updated();
87}
88
89void LogXLogYDomain::zoomIn(const QRectF &rect)
90{
91 storeZoomReset();
92 QRectF fixedRect = fixZoomRect(rect);
93 qreal logLeftX = fixedRect.left() * (m_logRightX - m_logLeftX) / m_size.width() + m_logLeftX;
94 qreal logRightX = fixedRect.right() * (m_logRightX - m_logLeftX) / m_size.width() + m_logLeftX;
95 qreal leftX = qPow(x: m_logBaseX, y: logLeftX);
96 qreal rightX = qPow(x: m_logBaseX, y: logRightX);
97 qreal minX = leftX < rightX ? leftX : rightX;
98 qreal maxX = leftX > rightX ? leftX : rightX;
99
100 qreal logLeftY = m_logRightY - fixedRect.bottom() * (m_logRightY - m_logLeftY) / m_size.height();
101 qreal logRightY = m_logRightY - fixedRect.top() * (m_logRightY - m_logLeftY) / m_size.height();
102 qreal leftY = qPow(x: m_logBaseY, y: logLeftY);
103 qreal rightY = qPow(x: m_logBaseY, y: logRightY);
104 qreal minY = leftY < rightY ? leftY : rightY;
105 qreal maxY = leftY > rightY ? leftY : rightY;
106
107 setRange(minX, maxX, minY, maxY);
108}
109
110void LogXLogYDomain::zoomOut(const QRectF &rect)
111{
112 storeZoomReset();
113 QRectF fixedRect = fixZoomRect(rect);
114 const qreal factorX = m_size.width() / fixedRect.width();
115 const qreal factorY = m_size.height() / fixedRect.height();
116
117 qreal logLeftX = m_logLeftX + (m_logRightX - m_logLeftX) / 2 * (1 - factorX);
118 qreal logRightX = m_logLeftX + (m_logRightX - m_logLeftX) / 2 * (1 + factorX);
119 qreal leftX = qPow(x: m_logBaseX, y: logLeftX);
120 qreal rightX = qPow(x: m_logBaseX, y: logRightX);
121 qreal minX = leftX < rightX ? leftX : rightX;
122 qreal maxX = leftX > rightX ? leftX : rightX;
123
124 qreal newLogMinY = m_logLeftY + (m_logRightY - m_logLeftY) / 2 * (1 - factorY);
125 qreal newLogMaxY = m_logLeftY + (m_logRightY - m_logLeftY) / 2 * (1 + factorY);
126 qreal leftY = qPow(x: m_logBaseY, y: newLogMinY);
127 qreal rightY = qPow(x: m_logBaseY, y: newLogMaxY);
128 qreal minY = leftY < rightY ? leftY : rightY;
129 qreal maxY = leftY > rightY ? leftY : rightY;
130
131 if (logRightX > m_size.width() || newLogMaxY > m_size.height())
132 return;
133
134 if (qIsInf(d: maxX) || qIsInf(d: maxY))
135 return;
136
137 setRange(minX, maxX, minY, maxY);
138}
139
140void LogXLogYDomain::move(qreal dx, qreal dy)
141{
142 if (m_reverseX)
143 dx = -dx;
144 if (m_reverseY)
145 dy = -dy;
146
147 qreal stepX = dx * qAbs(t: m_logRightX - m_logLeftX) / m_size.width();
148 qreal leftX = qPow(x: m_logBaseX, y: m_logLeftX + stepX);
149 qreal rightX = qPow(x: m_logBaseX, y: m_logRightX + stepX);
150 qreal minX = leftX < rightX ? leftX : rightX;
151 qreal maxX = leftX > rightX ? leftX : rightX;
152
153 qreal stepY = dy * (m_logRightY - m_logLeftY) / m_size.height();
154 qreal leftY = qPow(x: m_logBaseY, y: m_logLeftY + stepY);
155 qreal rightY = qPow(x: m_logBaseY, y: m_logRightY + stepY);
156 qreal minY = leftY < rightY ? leftY : rightY;
157 qreal maxY = leftY > rightY ? leftY : rightY;
158
159 setRange(minX, maxX, minY, maxY);
160}
161
162QPointF LogXLogYDomain::calculateGeometryPoint(const QPointF &point, bool &ok) const
163{
164 const qreal deltaX = m_size.width() / qAbs(t: m_logRightX - m_logLeftX);
165 const qreal deltaY = m_size.height() / qAbs(t: m_logRightY - m_logLeftY);
166 qreal x(0);
167 qreal y(0);
168 if (point.x() > 0 && point.y() > 0) {
169 x = ((std::log10(x: point.x()) / std::log10(x: m_logBaseX)) - m_logLeftX) * deltaX;
170 y = ((std::log10(x: point.y()) / std::log10(x: m_logBaseY)) - m_logLeftY) * deltaY;
171 ok = true;
172 } else {
173 qWarning() << "Logarithms of zero and negative values are undefined.";
174 ok = false;
175 if (point.x() > 0)
176 x = ((std::log10(x: point.x()) / std::log10(x: m_logBaseX)) - m_logLeftX) * deltaX;
177 else
178 x = 0;
179 if (point.y() > 0)
180 y = ((std::log10(x: point.y()) / std::log10(x: m_logBaseY)) - m_logLeftY) * deltaY;
181 else
182 y = 0;
183 }
184 if (m_reverseX)
185 x = m_size.width() - x;
186 if (!m_reverseY)
187 y = m_size.height() - y;
188 return QPointF(x, y);
189}
190
191QVector<QPointF> LogXLogYDomain::calculateGeometryPoints(const QVector<QPointF> &vector) const
192{
193 const qreal deltaX = m_size.width() / qAbs(t: m_logRightX - m_logLeftX);
194 const qreal deltaY = m_size.height() / qAbs(t: m_logRightY - m_logLeftY);
195
196 QVector<QPointF> result;
197 result.resize(size: vector.count());
198
199 for (int i = 0; i < vector.count(); ++i) {
200 if (vector[i].x() > 0 && vector[i].y() > 0) {
201 qreal x = ((std::log10(x: vector[i].x()) / std::log10(x: m_logBaseX)) - m_logLeftX) * deltaX;
202 if (m_reverseX)
203 x = m_size.width() - x;
204 qreal y = ((std::log10(x: vector[i].y()) / std::log10(x: m_logBaseY)) - m_logLeftY) * deltaY;
205 if (!m_reverseY)
206 y = m_size.height() - y;
207 result[i].setX(x);
208 result[i].setY(y);
209 } else {
210 qWarning() << "Logarithms of zero and negative values are undefined.";
211 return QVector<QPointF>();
212 }
213 }
214 return result;
215}
216
217QPointF LogXLogYDomain::calculateDomainPoint(const QPointF &point) const
218{
219 const qreal deltaX = m_size.width() / qAbs(t: m_logRightX - m_logLeftX);
220 const qreal deltaY = m_size.height() / qAbs(t: m_logRightY - m_logLeftY);
221 qreal x = m_reverseX ? (m_size.width() - point.x()) : point.x();
222 x = qPow(x: m_logBaseX, y: m_logLeftX + x / deltaX);
223 qreal y = m_reverseY ? point.y() : (m_size.height() - point.y());
224 y = qPow(x: m_logBaseY, y: m_logLeftY + y / deltaY);
225 return QPointF(x, y);
226}
227
228bool LogXLogYDomain::attachAxis(QAbstractAxis *axis)
229{
230 AbstractDomain::attachAxis(axis);
231 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(object: axis);
232
233 if (logAxis && logAxis->orientation() == Qt::Vertical) {
234 QObject::connect(sender: logAxis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleVerticalAxisBaseChanged(qreal)));
235 handleVerticalAxisBaseChanged(baseY: logAxis->base());
236 }
237
238 if (logAxis && logAxis->orientation() == Qt::Horizontal) {
239 QObject::connect(sender: logAxis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
240 handleHorizontalAxisBaseChanged(baseX: logAxis->base());
241 }
242
243 return true;
244}
245
246bool LogXLogYDomain::detachAxis(QAbstractAxis *axis)
247{
248 AbstractDomain::detachAxis(axis);
249 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(object: axis);
250
251 if (logAxis && logAxis->orientation() == Qt::Vertical)
252 QObject::disconnect(sender: logAxis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleVerticalAxisBaseChanged(qreal)));
253
254 if (logAxis && logAxis->orientation() == Qt::Horizontal)
255 QObject::disconnect(sender: logAxis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
256
257 return true;
258}
259
260void LogXLogYDomain::handleVerticalAxisBaseChanged(qreal baseY)
261{
262 m_logBaseY = baseY;
263 qreal logMinY = std::log10(x: m_minY) / std::log10(x: m_logBaseY);
264 qreal logMaxY = std::log10(x: m_maxY) / std::log10(x: m_logBaseY);
265 m_logLeftY = logMinY < logMaxY ? logMinY : logMaxY;
266 m_logRightY = logMinY > logMaxY ? logMinY : logMaxY;
267 emit updated();
268}
269
270void LogXLogYDomain::handleHorizontalAxisBaseChanged(qreal baseX)
271{
272 m_logBaseX = baseX;
273 qreal logMinX = std::log10(x: m_minX) / std::log10(x: m_logBaseX);
274 qreal logMaxX = std::log10(x: m_maxX) / std::log10(x: m_logBaseX);
275 m_logLeftX = logMinX < logMaxX ? logMinX : logMaxX;
276 m_logRightX = logMinX > logMaxX ? logMinX : logMaxX;
277 emit updated();
278}
279
280// operators
281
282bool Q_AUTOTEST_EXPORT operator== (const LogXLogYDomain &domain1, const LogXLogYDomain &domain2)
283{
284 return (qFuzzyIsNull(d: domain1.m_maxX - domain2.m_maxX)
285 && qFuzzyIsNull(d: domain1.m_maxY - domain2.m_maxY)
286 && qFuzzyIsNull(d: domain1.m_minX - domain2.m_minX)
287 && qFuzzyIsNull(d: domain1.m_minY - domain2.m_minY));
288}
289
290
291bool Q_AUTOTEST_EXPORT operator!= (const LogXLogYDomain &domain1, const LogXLogYDomain &domain2)
292{
293 return !(domain1 == domain2);
294}
295
296
297QDebug Q_AUTOTEST_EXPORT operator<<(QDebug dbg, const LogXLogYDomain &domain)
298{
299#ifdef QT_NO_TEXTSTREAM
300 Q_UNUSED(domain)
301#else
302 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
303#endif
304 return dbg.maybeSpace();
305}
306
307QT_CHARTS_END_NAMESPACE
308
309#include "moc_logxlogydomain_p.cpp"
310

source code of qtcharts/src/charts/domain/logxlogydomain.cpp