mirror of
https://github.com/OPM/ResInsight.git
synced 2026-08-27 05:37:21 -05:00
#14563 Fix truncated display of axis values for summary data
The 'g' number format interprets the precision as the number of significant digits, not as the number of decimals. Using the number of decimals directly caused tick labels to be rounded to too few digits, so 10.5 was displayed as 11 and the same label could appear twice. Add the number of integer digits to the precision when the automatic number format is used.
This commit is contained in:
@@ -58,30 +58,7 @@ public:
|
||||
m_numberFormat = numberFormat;
|
||||
}
|
||||
|
||||
QwtText label( double value ) const override
|
||||
{
|
||||
if ( qFuzzyCompare( scaledValue( value ) + 1.0, 1.0 ) ) value = 0.0;
|
||||
|
||||
return QString::number( scaledValue( value ), numberFormat(), m_numberOfDecimals );
|
||||
}
|
||||
|
||||
private:
|
||||
char numberFormat() const
|
||||
{
|
||||
switch ( m_numberFormat )
|
||||
{
|
||||
case RimPlotAxisProperties::NUMBER_FORMAT_AUTO:
|
||||
return 'g';
|
||||
case RimPlotAxisProperties::NUMBER_FORMAT_DECIMAL:
|
||||
return 'f';
|
||||
case RimPlotAxisProperties::NUMBER_FORMAT_SCIENTIFIC:
|
||||
return 'e';
|
||||
default:
|
||||
return 'g';
|
||||
}
|
||||
}
|
||||
|
||||
double scaledValue( double value ) const { return value / m_scaleFactor; }
|
||||
QwtText label( double value ) const override { return axisValueText( value, m_scaleFactor, m_numberOfDecimals, m_numberFormat ); }
|
||||
|
||||
private:
|
||||
double m_scaleFactor;
|
||||
@@ -213,6 +190,32 @@ void applyAxisScaleDraw( RiuPlotWidget* plotWidget, RiuPlotAxis axis, const RimP
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Text for a single axis value, scaled by the given scale factor and formatted using the given number format
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString axisValueText( double value, double scaleFactor, int numberOfDecimals, RimPlotAxisProperties::NumberFormatType numberFormat )
|
||||
{
|
||||
double displayValue = scaleFactor != 0.0 ? value / scaleFactor : value;
|
||||
if ( qFuzzyCompare( displayValue + 1.0, 1.0 ) ) displayValue = 0.0;
|
||||
|
||||
char format = 'g';
|
||||
if ( numberFormat == RimPlotAxisProperties::NUMBER_FORMAT_DECIMAL ) format = 'f';
|
||||
if ( numberFormat == RimPlotAxisProperties::NUMBER_FORMAT_SCIENTIFIC ) format = 'e';
|
||||
|
||||
int precision = numberOfDecimals;
|
||||
if ( format == 'g' )
|
||||
{
|
||||
// The 'g' format interprets the precision as the number of significant digits, while the user specifies the
|
||||
// number of decimals. Add the number of digits in the integer part to display the requested number of decimals.
|
||||
const double absValue = std::abs( displayValue );
|
||||
const int integerDigits = absValue >= 1.0 ? static_cast<int>( std::floor( std::log10( absValue ) ) ) + 1 : 1;
|
||||
|
||||
precision = integerDigits + numberOfDecimals;
|
||||
}
|
||||
|
||||
return QString::number( displayValue, format, precision );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
class RimPlotAxisProperties;
|
||||
#include "RimPlotAxisProperties.h"
|
||||
|
||||
class RimPlotCurve;
|
||||
class RiuPlotAxis;
|
||||
class RiuPlotWidget;
|
||||
@@ -39,6 +40,7 @@ void updatePlotWidgetFromAxisProperties( RiuPlotWidget*
|
||||
|
||||
void applyAxisScaleDraw( RiuPlotWidget* plotWidget, RiuPlotAxis axis, const RimPlotAxisProperties* const axisProperties );
|
||||
QString scaleFactorText( const RimPlotAxisProperties* const axisProperties );
|
||||
QString axisValueText( double value, double scaleFactor, int numberOfDecimals, RimPlotAxisProperties::NumberFormatType numberFormat );
|
||||
QString axisTextForAddress( RifEclipseSummaryAddress address );
|
||||
std::string shortCalculationName( const std::string& calculationName );
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ set(SOURCE_UNITTEST_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/RigStimPlanModelTools-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RigWellPathIntersectionTools-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RimHistogramDataSource-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RimPlotAxisTools-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RimWellLogExtractionCurveImpl-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RivAnnotationTools-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RivPipeGeometryGenerator-Test.cpp
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "RimPlotAxisProperties.h"
|
||||
#include "Tools/RimPlotAxisTools.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// The tick values for the range 8e9 - 1.1e10 displayed using a scale factor of 1e9
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
static std::vector<double> tickValuesForScaledRange()
|
||||
{
|
||||
return { 8.0e9, 8.5e9, 9.0e9, 9.5e9, 10.0e9, 10.5e9, 11.0e9 };
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// The 'g' format used by the automatic number format interprets the precision as the number of
|
||||
/// significant digits. Using the number of decimals directly caused 10.5 to be displayed as 11, and
|
||||
/// the same label was displayed twice.
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RimPlotAxisTools, AutoFormatKeepsRequestedNumberOfDecimals )
|
||||
{
|
||||
const std::vector<QString> expectedTexts = { "8", "8.5", "9", "9.5", "10", "10.5", "11" };
|
||||
|
||||
const auto tickValues = tickValuesForScaledRange();
|
||||
ASSERT_EQ( expectedTexts.size(), tickValues.size() );
|
||||
|
||||
for ( size_t i = 0; i < tickValues.size(); i++ )
|
||||
{
|
||||
auto text = RimPlotAxisTools::axisValueText( tickValues[i], 1.0e9, 2, RimPlotAxisProperties::NUMBER_FORMAT_AUTO );
|
||||
|
||||
EXPECT_EQ( expectedTexts[i].toStdString(), text.toStdString() );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Trailing zeros are not displayed by the automatic number format, and increasing the number of
|
||||
/// decimals does not change the text for values that require fewer decimals
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RimPlotAxisTools, AutoFormatIsConcise )
|
||||
{
|
||||
const auto autoFormat = RimPlotAxisProperties::NUMBER_FORMAT_AUTO;
|
||||
|
||||
EXPECT_EQ( "10", RimPlotAxisTools::axisValueText( 10.0e9, 1.0e9, 2, autoFormat ).toStdString() );
|
||||
EXPECT_EQ( "10.5", RimPlotAxisTools::axisValueText( 10.5e9, 1.0e9, 4, autoFormat ).toStdString() );
|
||||
EXPECT_EQ( "1234.5", RimPlotAxisTools::axisValueText( 1234.5, 1.0, 2, autoFormat ).toStdString() );
|
||||
EXPECT_EQ( "0.25", RimPlotAxisTools::axisValueText( 0.25, 1.0, 2, autoFormat ).toStdString() );
|
||||
EXPECT_EQ( "-10.5", RimPlotAxisTools::axisValueText( -10.5, 1.0, 2, autoFormat ).toStdString() );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// The decimal and scientific formats interpret the precision as the number of decimals
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RimPlotAxisTools, DecimalAndScientificFormat )
|
||||
{
|
||||
EXPECT_EQ( "10.50", RimPlotAxisTools::axisValueText( 10.5e9, 1.0e9, 2, RimPlotAxisProperties::NUMBER_FORMAT_DECIMAL ).toStdString() );
|
||||
EXPECT_EQ( "10.5", RimPlotAxisTools::axisValueText( 10.5e9, 1.0e9, 1, RimPlotAxisProperties::NUMBER_FORMAT_DECIMAL ).toStdString() );
|
||||
|
||||
EXPECT_EQ( "1.05e+01", RimPlotAxisTools::axisValueText( 10.5e9, 1.0e9, 2, RimPlotAxisProperties::NUMBER_FORMAT_SCIENTIFIC ).toStdString() );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Values close to zero are displayed as zero, and a scale factor of one leaves the value unchanged
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RimPlotAxisTools, ZeroAndUnitScaleFactor )
|
||||
{
|
||||
const auto autoFormat = RimPlotAxisProperties::NUMBER_FORMAT_AUTO;
|
||||
|
||||
EXPECT_EQ( "0", RimPlotAxisTools::axisValueText( 0.0, 1.0e9, 2, autoFormat ).toStdString() );
|
||||
EXPECT_EQ( "0", RimPlotAxisTools::axisValueText( 1.0e-20, 1.0, 2, autoFormat ).toStdString() );
|
||||
|
||||
EXPECT_EQ( "8.5", RimPlotAxisTools::axisValueText( 8.5, 1.0, 2, autoFormat ).toStdString() );
|
||||
}
|
||||
Reference in New Issue
Block a user