From 846f29bd29edc488f0637664e4f5ffd848f9eaae Mon Sep 17 00:00:00 2001 From: Dimitrios Apostolou Date: Mon, 10 May 2021 16:20:41 +0200 Subject: Add comments and clarify some code Change-Id: Ib0280e18d8e788631ae3c11674398c2725cbafa6 Reviewed-by: Miikka Heikkinen (cherry picked from commit b1e5084099c28c0b937f4f9ab070d6d5556cf54d) Reviewed-by: Qt Cherry-pick Bot --- src/charts/axis/chartaxiselement.cpp | 6 +++++- src/charts/axis/horizontalaxis.cpp | 13 ++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/charts/axis/chartaxiselement.cpp b/src/charts/axis/chartaxiselement.cpp index 72ea2ec2..f6ff050a 100644 --- a/src/charts/axis/chartaxiselement.cpp +++ b/src/charts/axis/chartaxiselement.cpp @@ -419,7 +419,11 @@ QStringList ChartAxisElement::createValueLabels(qreal min, qreal max, int ticks, return labels; if (format.isEmpty()) { - int n = qMax(int(-qFloor(std::log10((max - min) / (ticks - 1)))), 0) + 1; + // Calculate how many decimal digits are needed to show difference between ticks, + // for example tick marks 1.002 and 1.003 have a difference of 0.001 and need 3 decimals. + // For differences >= 1 (positive log10) use always 1 decimal. + double l10 = std::log10((max - min) / (ticks - 1)); + int n = qMax(int(-qFloor(l10)), 0) + 1; if (tickType == QValueAxis::TicksFixed) { for (int i = 0; i < ticks; i++) { qreal value = min + (i * (max - min) / (ticks - 1)); diff --git a/src/charts/axis/horizontalaxis.cpp b/src/charts/axis/horizontalaxis.cpp index 7e92b891..f5c78b78 100644 --- a/src/charts/axis/horizontalaxis.cpp +++ b/src/charts/axis/horizontalaxis.cpp @@ -102,7 +102,6 @@ void HorizontalAxis::updateGeometry() else if (axis()->alignment() == Qt::AlignBottom) arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top()); - qreal width = 0; const QLatin1String ellipsis("..."); //title @@ -133,6 +132,8 @@ void HorizontalAxis::updateGeometry() QList lines = gridItems(); QList shades = shadeItems(); + qreal last_label_max_x = 0; + for (int i = 0; i < layout.size(); ++i) { //items QGraphicsLineItem *gridItem = static_cast(lines.at(i)); @@ -160,6 +161,7 @@ void HorizontalAxis::updateGeometry() labelItem->setHtml(text); } else { qreal labelWidth = axisRect.width() / layout.count() - (2 * labelPadding()); + // Replace digits with ellipsis "..." if number does not fit QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text, axis()->labelsAngle(), labelWidth, @@ -255,13 +257,14 @@ void HorizontalAxis::updateGeometry() labelItem->setPos(labelPos.toPoint()); //label overlap detection - compensate one pixel for rounding errors - if ((labelItem->pos().x() < width && labelItem->toPlainText() == ellipsis) || forceHide || - (labelItem->pos().x() + (widthDiff / 2.0)) < (axisRect.left() - 1.0) || - (labelItem->pos().x() + (widthDiff / 2.0) - 1.0) > axisRect.right()) { + if ((labelItem->pos().x() < last_label_max_x && labelItem->toPlainText() == ellipsis) + || forceHide + || (labelItem->pos().x() + (widthDiff / 2.0)) < (axisRect.left() - 1.0) + || (labelItem->pos().x() + (widthDiff / 2.0) - 1.0) > axisRect.right()) { labelItem->setVisible(false); } else { labelItem->setVisible(true); - width = boundingRect.width() + labelItem->pos().x(); + last_label_max_x = boundingRect.width() + labelItem->pos().x(); } //shades -- cgit v1.2.3