Skip to content

Commit

Permalink
Render axis for stacked bar diagram
Browse files Browse the repository at this point in the history
This adds support for rendering the axis on stacked bar diagrams if an axis has
been set. The implementation is based on the axis rendering for the histogram
diagram.

Fixes #34915
  • Loading branch information
dminor authored and nyalldawson committed Jul 14, 2020
1 parent 15a4c39 commit 4235b76
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/core/diagram/qgsstackedbardiagram.cpp
Expand Up @@ -17,6 +17,7 @@
#include "qgsdiagramrenderer.h"
#include "qgsrendercontext.h"
#include "qgsexpression.h"
#include "qgssymbollayerutils.h"

#include <QPainter>

Expand Down Expand Up @@ -88,6 +89,13 @@ QSizeF QgsStackedBarDiagram::diagramSize( const QgsFeature &feature, const QgsRe
}
}

if ( s.showAxis() && s.axisLineSymbol() )
{
const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c ) / painterUnitConversionScale;
size.setWidth( size.width() + 2 * maxBleed );
size.setHeight( size.height() + 2 * maxBleed );
}

return size;
}

Expand Down Expand Up @@ -201,6 +209,15 @@ void QgsStackedBarDiagram::renderDiagram( const QgsFeature &feature, QgsRenderCo
double baseX = position.x();
double baseY = position.y();

if ( s.showAxis() && s.axisLineSymbol() )
{
// if showing axis, the diagram position needs shifting from the default base x so that the axis
// line stroke sits within the desired label engine rect (otherwise we risk overlaps of the axis line stroke)
const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c );
baseX += maxBleed;
baseY -= maxBleed;
}

mPen.setColor( s.penColor );
setPenWidth( mPen, s, c );
p->setPen( mPen );
Expand Down Expand Up @@ -235,4 +252,35 @@ void QgsStackedBarDiagram::renderDiagram( const QgsFeature &feature, QgsRenderCo

currentOffset += length + spacing;
}

if ( s.showAxis() && s.axisLineSymbol() )
{
s.axisLineSymbol()->startRender( c );
QPolygonF axisPoints;
switch ( s.diagramOrientation )
{
case QgsDiagramSettings::Up:
axisPoints << QPointF( baseX, baseY - scaledMaxVal ) << QPointF( baseX, baseY ) << QPointF( baseX + scaledWidth, baseY );
break;

case QgsDiagramSettings::Down:
axisPoints << QPointF( baseX, baseY ) << QPointF( baseX, baseY - scaledMaxVal ) << QPointF( baseX + scaledWidth, baseY - scaledMaxVal );
break;

case QgsDiagramSettings::Right:
axisPoints << QPointF( baseX + scaledMaxVal, baseY - scaledWidth)
<< QPointF( baseX, baseY - scaledWidth )
<< QPointF( baseX, baseY );
break;

case QgsDiagramSettings::Left:
axisPoints << QPointF( baseX, baseY - scaledWidth )
<< QPointF( baseX + scaledMaxVal, baseY - scaledWidth )
<< QPointF( baseX + scaledMaxVal, baseY );
break;
}

s.axisLineSymbol()->renderPolyline( axisPoints, nullptr, c );
s.axisLineSymbol()->stopRender( c );
}
}

0 comments on commit 4235b76

Please sign in to comment.