diff --git a/ApplicationLibCode/ProjectDataModel/CorrelationPlots/RimRftTornadoPlot.cpp b/ApplicationLibCode/ProjectDataModel/CorrelationPlots/RimRftTornadoPlot.cpp index f1a5edef41..8954e632b3 100644 --- a/ApplicationLibCode/ProjectDataModel/CorrelationPlots/RimRftTornadoPlot.cpp +++ b/ApplicationLibCode/ProjectDataModel/CorrelationPlots/RimRftTornadoPlot.cpp @@ -48,6 +48,12 @@ CAF_PDM_SOURCE_INIT( RimRftTornadoPlot, "RftTornadoPlot" ); +namespace +{ +constexpr double axisMinAbsolute = -0.7; +constexpr double anchorMargin = 0.03; +} // namespace + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -225,8 +231,8 @@ void RimRftTornadoPlot::updateAxes() if ( m_showAbsoluteValues() ) { m_plotWidget->setAxisTitleText( RiuPlotAxis::defaultBottom(), "Pearson Correlation Coefficient ABS" ); - // Use a small negative margin so bar label text (anchored at x=0) remains visible - m_plotWidget->setAxisRange( RiuPlotAxis::defaultBottom(), -0.15, 1.0 ); + // Leave room left of zero for the bar labels + m_plotWidget->setAxisRange( RiuPlotAxis::defaultBottom(), axisMinAbsolute, 1.0 ); } else { @@ -320,6 +326,10 @@ void RimRftTornadoPlot::onLoadDataAndUpdate() m_lastCorrelations = addDataToChartBuilder( chartBuilder ); const int labelSize = caf::FontTools::absolutePointSize( RiaPreferences::current()->defaultPlotFontSize(), m_labelFontSize() ); chartBuilder.setLabelFontSize( labelSize ); + // Labels start at the left edge of the plot, in the empty space left of the bars. The anchor must be slightly inside + // the axis range, as Qwt skips scale items positioned outside the canvas. Only used for absolute values, as signed + // values have bars on both sides of zero. + if ( m_showAbsoluteValues() ) chartBuilder.setBarTextAnchor( axisMinAbsolute + anchorMargin ); RiuRftCorrelationPlotTools::addHorizontalBarChart( m_plotWidget, chartBuilder, m_showOnlyTopNCorrelations() ? m_topNFilterCount() : -1 ); RimCorrelationBarChartTools::highlightSelectedParameterBar( m_plotWidget, m_selectedParameter, diff --git a/ApplicationLibCode/UserInterface/AnalysisPlots/RiuGroupedBarChartBuilder.cpp b/ApplicationLibCode/UserInterface/AnalysisPlots/RiuGroupedBarChartBuilder.cpp index e46f6eaf28..3b7282efc8 100644 --- a/ApplicationLibCode/UserInterface/AnalysisPlots/RiuGroupedBarChartBuilder.cpp +++ b/ApplicationLibCode/UserInterface/AnalysisPlots/RiuGroupedBarChartBuilder.cpp @@ -367,6 +367,8 @@ void RiuGroupedBarChartBuilder::setBarColor( const QColor& color ) //-------------------------------------------------------------------------------------------------- void RiuGroupedBarChartBuilder::addBarChartToPlot( QwtPlot* plot, Qt::Orientation barOrientation, int maxBarCount ) { + const bool showBarTextAtAnchor = m_barTextAnchor.has_value() && barOrientation == Qt::Horizontal; + const double majGroupSpacing = 1.6; const double midGroupSpacing = 0.5; const double minGroupSpacing = 0.2; @@ -629,8 +631,19 @@ void RiuGroupedBarChartBuilder::addBarChartToPlot( QwtPlot* plot, Qt::Orientatio barPoints->push_back( { currentBarPosition, barDef.m_value } ); if ( !barDef.m_barText.isEmpty() ) { + QString barText = barDef.m_barText; + if ( showBarTextAtAnchor ) + { + // Long labels are shortened in the middle to limit the space used inside the plot + const int maxLabelLength = 26; + if ( barText.size() > maxLabelLength ) + { + barText = barText.left( 10 ) + QString::fromUtf8( "…" ) + barText.right( maxLabelLength - 11 ); + } + } + positionedBarLabels[currentBarPosition] = - RiuBarChartScaleDraw::RiuBarChartTick( QwtScaleDiv::MinorTick, barDef.m_barText, barDef.m_value < 0.0 ); + RiuBarChartScaleDraw::RiuBarChartTick( QwtScaleDiv::MinorTick, barText, !showBarTextAtAnchor && barDef.m_value < 0.0 ); } // Increment the bar position for the next bar @@ -797,12 +810,19 @@ void RiuGroupedBarChartBuilder::addBarChartToPlot( QwtPlot* plot, Qt::Orientatio QwtScaleDraw::Alignment alignment = QwtScaleDraw::TopScale; double labelRotation = -90.0; Qt::Alignment labelAlignment = Qt::AlignVCenter | Qt::AlignRight; + double scalePosition = 0.0; if ( barOrientation == Qt::Horizontal ) { alignment = QwtScaleDraw::RightScale; labelRotation = 0.0; } + if ( showBarTextAtAnchor ) + { + // Labels start at the anchor position and extend to the right + scalePosition = *m_barTextAnchor; + } + QwtScaleDiv barTextScaleDiv( 0, currentBarPosition ); { QList onBarTickPositions; @@ -826,7 +846,7 @@ void RiuGroupedBarChartBuilder::addBarChartToPlot( QwtPlot* plot, Qt::Orientatio barTextScaleDrawer->setLabelRotation( labelRotation ); barTextScaleDrawer->setLabelAlignment( labelAlignment ); - QwtPlotScaleItem* barTextScale = new QwtPlotScaleItem( alignment, 0.0 ); + QwtPlotScaleItem* barTextScale = new QwtPlotScaleItem( alignment, scalePosition ); barTextScale->setScaleDraw( barTextScaleDrawer ); barTextScale->setScaleDiv( barTextScaleDiv ); barTextScale->attach( plot ); @@ -842,6 +862,14 @@ void RiuGroupedBarChartBuilder::setLabelFontSize( int labelPointSize ) m_labelPointSize = labelPointSize; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuGroupedBarChartBuilder::setBarTextAnchor( double valueAxisPosition ) +{ + m_barTextAnchor = valueAxisPosition; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationLibCode/UserInterface/AnalysisPlots/RiuGroupedBarChartBuilder.h b/ApplicationLibCode/UserInterface/AnalysisPlots/RiuGroupedBarChartBuilder.h index 74a232c244..1224fba684 100644 --- a/ApplicationLibCode/UserInterface/AnalysisPlots/RiuGroupedBarChartBuilder.h +++ b/ApplicationLibCode/UserInterface/AnalysisPlots/RiuGroupedBarChartBuilder.h @@ -25,6 +25,7 @@ #include #include +#include #include class QwtPlot; @@ -50,6 +51,10 @@ public: void addBarChartToPlot( QwtPlot* plot, Qt::Orientation orientation, int maxBarCount = -1 ); void setLabelFontSize( int labelPointSize ); + // For horizontal bar charts: left-align the bar texts at the given value axis position instead of + // placing them next to the bars + void setBarTextAnchor( double valueAxisPosition ); + QString plotContentAsText() const; private: @@ -91,6 +96,7 @@ private: bool m_isSortingByMaxValueInGroups; int m_labelPointSize; - bool m_useBarColor = false; - QColor m_barColor; + std::optional m_barTextAnchor; + bool m_useBarColor = false; + QColor m_barColor; };