| 310 | } |
| 311 | |
| 312 | QList<PlotViewWidget::TickValue> |
| 313 | PlotViewWidget::getAxisTicksToShow(const Axis axis, Range<double> visibleRange) const |
| 314 | { |
| 315 | const auto &properties = this->propertiesAxis[(axis == Axis::X) ? 0 : 1]; |
| 316 | const auto axisVector = (axis == Axis::X) ? QPointF(1, 0) : QPointF(0, -1); |
| 317 | const auto axisLengthInPixels = |
| 318 | QPointF::dotProduct(properties.line.p2() - properties.line.p1(), axisVector); |
| 319 | |
| 320 | const auto rangeWidth = visibleRange.max - visibleRange.min; |
| 321 | if (rangeWidth == 0) |
| 322 | return {}; |
| 323 | |
| 324 | const int minPixelDistanceBetweenValues = 50; |
| 325 | const auto nrTicksToShowMax = double(axisLengthInPixels) / minPixelDistanceBetweenValues; |
| 326 | |
| 327 | double factorMajor = 1.0; |
| 328 | while (factorMajor * 10 * rangeWidth < nrTicksToShowMax) |
| 329 | factorMajor *= 10; |
| 330 | while (factorMajor * rangeWidth > nrTicksToShowMax) |
| 331 | factorMajor /= 10; |
| 332 | |
| 333 | double factorMinor = factorMajor; |
| 334 | while (factorMinor * rangeWidth * 2 < nrTicksToShowMax) |
| 335 | factorMinor *= 2; |
| 336 | |
| 337 | DEBUG_PLOT("PlotViewWidget::getAxisTicksToShow rangeWidth " |
| 338 | << rangeWidth << " nrTicksToShowMax " << nrTicksToShowMax << " factorMajor " |
| 339 | << factorMajor << " factorMinor " << factorMinor); |
| 340 | |
| 341 | /* Get the actual values to show between min and max. However, we want some more values to the |
| 342 | * left and the right for correct display of the tick labels. Unfortunately we don't know how wide |
| 343 | * the labels will be so we will just use a worst case assumption and add half the width of the |
| 344 | * view left and right. |
| 345 | */ |
| 346 | auto rangeToReturn = visibleRange; |
| 347 | rangeToReturn.min -= rangeWidth / 2; |
| 348 | rangeToReturn.max += rangeWidth / 2; |
| 349 | const auto plotRange = this->getVisibleRange(axis); |
| 350 | if (plotRange) |
| 351 | { |
| 352 | rangeToReturn.min = std::max(rangeToReturn.min, plotRange->min); |
| 353 | rangeToReturn.max = std::min(rangeToReturn.max, plotRange->max); |
| 354 | } |
| 355 | |
| 356 | auto getValuesForFactor = [rangeToReturn](double factor) { |
| 357 | QList<double> values; |
| 358 | int min = std::ceil(rangeToReturn.min * factor); |
| 359 | int max = std::floor(rangeToReturn.max * factor); |
| 360 | for (int i = min; i <= max; i++) |
| 361 | values.append(double(i) / factor); |
| 362 | return values; |
| 363 | }; |
| 364 | |
| 365 | auto valuesMajor = getValuesForFactor(factorMajor); |
| 366 | auto valuesMinor = getValuesForFactor(factorMinor); |
| 367 | |
| 368 | QList<TickValue> values; |
| 369 | for (auto v : valuesMinor) |
no test coverage detected