diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2021-10-11 17:24:18 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2021-10-13 16:30:24 +0200 |
commit | f0f43a2bd551662cb25535b4f7291e3ced11889c (patch) | |
tree | bd156ee2c2c7d291d5f6c4c55e3b259795fdd4eb /src | |
parent | 8321f6f0b358f23649169a84293ce3c4c6e3c9cc (diff) |
Extract precision-needed calculation to a function
Two of the three places doing the calculation neglected to consider
the case of only one tick (division by ticks - 1 is then a problem)
and only one of these explained what the calculation was about.
Package it in a little function and explain what it's about a little
more clearly, then call it from all three users and avoid repetition.
Change-Id: I5826e37522a785d50ba103976be33f4c06ce1dea
Reviewed-by: Dimitrios Apostolou <jimis@qt.io>
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/charts/axis/chartaxiselement.cpp | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/src/charts/axis/chartaxiselement.cpp b/src/charts/axis/chartaxiselement.cpp index 692400ea..f0ed39fd 100644 --- a/src/charts/axis/chartaxiselement.cpp +++ b/src/charts/axis/chartaxiselement.cpp @@ -476,6 +476,21 @@ void ChartAxisElement::prepareColorScale(const qreal width, const qreal height) } } +static int precisionDigits(qreal min, qreal max, int ticks) +{ + // How many digits of each tick value should we display after the decimal point ? + // For example tick marks 1.002 and 1.003 have a difference of 0.001 and need 3 decimals. + if (ticks > 1) { + // Number of digits after decimal that *don't* change between ticks: + const int gap = -qFloor(std::log10((max - min) / (ticks - 1))); + if (gap > 0) + return gap + 1; + } + // We want at least one digit after the decimal point even when digits + // before are changing, or when we only have a single tick-mark: + return 1; +} + QStringList ChartAxisElement::createValueLabels(qreal min, qreal max, int ticks, qreal tickInterval, qreal tickAnchor, QValueAxis::TickType tickType, @@ -487,11 +502,7 @@ QStringList ChartAxisElement::createValueLabels(qreal min, qreal max, int ticks, return labels; if (format.isEmpty()) { - // 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(-qFloor(l10), 0) + 1; + const int n = precisionDigits(min, max, ticks); if (tickType == QValueAxis::TicksFixed) { for (int i = 0; i < ticks; i++) { qreal value = min + (i * (max - min) / (ticks - 1)); @@ -557,10 +568,7 @@ QStringList ChartAxisElement::createLogValueLabels(qreal min, qreal max, qreal b const int firstTick = qCeil(qLn(base > 1 ? min : max) / qLn(base)); if (format.isEmpty()) { - int n = 0; - if (ticks > 1) - n = qMax(-qFloor(std::log10((max - min) / (ticks - 1))), 0); - n++; + const int n = precisionDigits(min, max, ticks); for (int i = firstTick; i < ticks + firstTick; i++) { qreal value = qPow(base, i); labels << presenter()->numberToString(value, 'f', n); @@ -616,7 +624,7 @@ QStringList ChartAxisElement::createColorLabels(qreal min, qreal max, int ticks) if (max <= min || ticks < 1) return labels; - int n = qMax(int(-qFloor(std::log10((max - min) / (ticks - 1)))), 0) + 1; + const int n = precisionDigits(min, max, ticks); for (int i = 0; i < ticks; ++i) { qreal value = min + (i * (max - min) / (ticks - 1)); labels << presenter()->numberToString(value, 'f', n); |