KD Chart API Documentation  3.1
CartesianCoordinateTransformation.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Chart library.
4 **
5 ** SPDX-FileCopyrightText: 2001 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
11 #ifndef CARTESIANCOORDINATETRANSFORMATION_H
12 #define CARTESIANCOORDINATETRANSFORMATION_H
13 
14 #include <QList>
15 #include <QPointF>
16 #include <QRectF>
17 
18 #include "KDChartZoomParameters.h"
19 
20 #include <cmath>
21 #include <limits>
22 
23 namespace KDChart {
24 
25 // FIXME: if this struct is used more often, we need to make it a class
26 // with proper accessor methods:
27 
32 {
34  {
35  }
36 
39 
41 
42  QTransform transform;
43  QTransform backTransform;
44  // a logarithmic scale cannot cross zero, so we have to know which side we are on.
45  bool isPositiveX = true;
46  bool isPositiveY = true;
47 
48  qreal logTransform(qreal value, bool isPositiveRange) const
49  {
50  if (isPositiveRange) {
51  return log10(value);
52  } else {
53  return -log10(-value);
54  }
55  }
56 
57  qreal logTransformBack(qreal value, bool wasPositive) const
58  {
59  if (wasPositive) {
60  return pow(10.0, value);
61  } else {
62  return -pow(10.0, -value);
63  }
64  }
65 
66  void updateTransform(const QRectF &constDataRect, const QRectF &screenRect)
67  {
68  QRectF dataRect = constDataRect;
70  // the data will be scaled by logTransform() later, so scale its bounds as well
71  isPositiveX = dataRect.left() >= 0.0;
72  dataRect.setLeft(logTransform(dataRect.left(), isPositiveX));
73  dataRect.setRight(logTransform(dataRect.right(), isPositiveX));
74  }
76  isPositiveY = dataRect.top() >= 0.0;
77  dataRect.setTop(logTransform(dataRect.top(), isPositiveY));
78  dataRect.setBottom(logTransform(dataRect.bottom(), isPositiveY));
79  }
80 
81  transform.reset();
82  // read the following transformation sequence from bottom to top(!)
83  transform.translate(screenRect.left(), screenRect.bottom());
84  transform.scale(screenRect.width(), screenRect.height());
85 
86  // TODO: mirror in case of "reverse" axes?
87 
88  // transform into screen space
89  transform.translate(0.5, -0.5);
91  transform.translate(-zoom.xCenter, 1.0 - zoom.yCenter);
92  // zoom
93  transform.scale(1.0 / dataRect.width(), 1.0 / dataRect.height());
94  transform.translate(-dataRect.left(), -dataRect.bottom());
95  // transform into the unit square
96 
97  backTransform = transform.inverted();
98  }
99 
100  // convert data space point to screen point
101  inline QPointF translate(const QPointF &dataPoint) const
102  {
103  QPointF data = dataPoint;
105  data.setX(logTransform(data.x(), isPositiveX));
106  }
108  data.setY(logTransform(data.y(), isPositiveY));
109  }
110 
111  return transform.map(data);
112  }
113 
114  // convert screen point to data space point
115  inline const QPointF translateBack(const QPointF &screenPoint) const
116  {
117  QPointF ret = backTransform.map(screenPoint);
119  ret.setX(logTransformBack(ret.x(), isPositiveX));
120  }
122  ret.setY(logTransformBack(ret.y(), isPositiveY));
123  }
124  return ret;
125  }
126 };
127 
128 typedef QList<CoordinateTransformation> CoordinateTransformationList;
129 }
130 
131 #endif
QList< CoordinateTransformation > CoordinateTransformationList
const QPointF translateBack(const QPointF &screenPoint) const
qreal logTransformBack(qreal value, bool wasPositive) const
QPointF translate(const QPointF &dataPoint) const
void updateTransform(const QRectF &constDataRect, const QRectF &screenRect)
qreal logTransform(qreal value, bool isPositiveRange) const
CartesianCoordinatePlane::AxesCalcMode axesCalcModeY
CartesianCoordinatePlane::AxesCalcMode axesCalcModeX

© 2001 Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-chart/
Generated by doxygen 1.9.1