@@ -94,6 +94,48 void initTitleTextStyle(QCPItemText &text) noexcept | |||
|
94 | 94 | text.position->setCoords(0.5, 0); |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | /** | |
|
98 | * Returns the cell index (x or y) of a colormap according to the coordinate passed in parameter. | |
|
99 | * This method handles the fact that a colormap axis can be logarithmic or linear. | |
|
100 | * @param colormap the colormap for which to calculate the index | |
|
101 | * @param coord the coord to convert to cell index | |
|
102 | * @param xCoord calculates the x index if true, calculates y index if false | |
|
103 | * @return the cell index | |
|
104 | */ | |
|
105 | int colorMapCellIndex(const QCPColorMap &colormap, double coord, bool xCoord) | |
|
106 | { | |
|
107 | // Determines the axis of the colormap according to xCoord, and whether it is logarithmic or not | |
|
108 | auto isLogarithmic = (xCoord ? colormap.keyAxis() : colormap.valueAxis())->scaleType() | |
|
109 | == QCPAxis::stLogarithmic; | |
|
110 | ||
|
111 | if (isLogarithmic) { | |
|
112 | // For a logarithmic axis we can't use the conversion method of colormap, so we calculate | |
|
113 | // the index manually based on the position of the coordinate on the axis | |
|
114 | ||
|
115 | // Gets the axis range and the number of values between range bounds to calculate the step | |
|
116 | // between each value of the range | |
|
117 | auto range = xCoord ? colormap.data()->keyRange() : colormap.data()->valueRange(); | |
|
118 | auto nbValues = (xCoord ? colormap.data()->keySize() : colormap.data()->valueSize()) - 1; | |
|
119 | auto valueStep | |
|
120 | = (std::log10(range.upper) - std::log10(range.lower)) / static_cast<double>(nbValues); | |
|
121 | ||
|
122 | // According to the coord position, calculates the closest index in the range | |
|
123 | return std::round((std::log10(coord) - std::log10(range.lower)) / valueStep); | |
|
124 | } | |
|
125 | else { | |
|
126 | // For a linear axis, we use the conversion method of colormap | |
|
127 | int index; | |
|
128 | if (xCoord) { | |
|
129 | colormap.data()->coordToCell(coord, 0., &index, nullptr); | |
|
130 | } | |
|
131 | else { | |
|
132 | colormap.data()->coordToCell(0., coord, nullptr, &index); | |
|
133 | } | |
|
134 | ||
|
135 | return index; | |
|
136 | } | |
|
137 | } | |
|
138 | ||
|
97 | 139 | } // namespace |
|
98 | 140 | |
|
99 | 141 | struct VisualizationGraphRenderingDelegate::VisualizationGraphRenderingDelegatePrivate { |
@@ -226,6 +268,12 void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexce | |||
|
226 | 268 | // Gets x and y coords |
|
227 | 269 | auto x = colorMap->keyAxis()->pixelToCoord(eventPos.x()); |
|
228 | 270 | auto y = colorMap->valueAxis()->pixelToCoord(eventPos.y()); |
|
271 | ||
|
272 | // Calculates x and y cell indexes, and retrieves the underlying value | |
|
273 | auto xCellIndex = colorMapCellIndex(*colorMap, x, true); | |
|
274 | auto yCellIndex = colorMapCellIndex(*colorMap, y, false); | |
|
275 | auto value = colorMap->data()->cell(xCellIndex, yCellIndex); | |
|
276 | ||
|
229 | 277 | } |
|
230 | 278 | |
|
231 | 279 | if (!tooltip.isEmpty()) { |
General Comments 0
You need to be logged in to leave comments.
Login now