From c10eda1de14b8791c6a166f68819ab4f2daeb753 Mon Sep 17 00:00:00 2001 From: Keith Vetter Date: Wed, 21 Aug 2024 12:04:46 -0500 Subject: [PATCH] Clean up plot tic close to zero On 8/8/2024 on this commit: 8adaf0e6a8b77e338b81c27e2479799699a0ee47 I took out logic that formatted tics close to zero as zero and instead used the actual tic value no matter how close to zero. This introduced another issue when tic values were close to zero but actually meant to be zero. For example if Tics={-1e-17,1e-34,1e-17}, the plot should really have Tics={-1e-17,0,1e-17}. This commit fixes that issue by replacing the tic closest to zero with zero if appropriate. --- libkoviz/bookmodel.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/libkoviz/bookmodel.cpp b/libkoviz/bookmodel.cpp index 359eb29..f4d2c71 100644 --- a/libkoviz/bookmodel.cpp +++ b/libkoviz/bookmodel.cpp @@ -3231,6 +3231,31 @@ QList PlotBookModel::_calcTicSet(double aIn, double bIn, } } + // Clean up X by replacing single tic that is nearest zero with zero + // For example, if X={-1e-17,1e-34,1e-17}, make X={-1e-17,0,1e-17} + // Only replace tic if: + // 1. a <= 0 <= b + // 2. |X| > 1 + // 3. log10(tic) is less than another tic + if ( a <= 0 && b >= 0 && X.size() > 1 ) { + int idx_nearest_zero = 0; + double xmin = DBL_MAX; + for ( int i = 0; i < X.size(); ++i ) { + double x = X.at(i); + if ( qAbs(x) < xmin ) { + xmin = qAbs(x); + idx_nearest_zero = i; + } + } + if ( xmin != 0 ) { + int i = ( idx_nearest_zero == 0 ) ? 1 : 0; + double other_tic = X.at(i); + if ( log10(qAbs(xmin)) < log10(qAbs(other_tic))-1 ) { + X.replace(idx_nearest_zero,0.0); + } + } + } + // Shrink tic set until |X| <=7 // Tics are removed in favor of keeping tics "even" in order // to keep them the same while panning