Skip to content
Permalink
Browse files

Render axis for stacked bar diagram

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 4235b76bb7d77677f9738177fd26bf23b82e0558
Showing with 48 additions and 0 deletions.
  1. +48 −0 src/core/diagram/qgsstackedbardiagram.cpp
@@ -17,6 +17,7 @@
#include "qgsdiagramrenderer.h"
#include "qgsrendercontext.h"
#include "qgsexpression.h"
#include "qgssymbollayerutils.h"

#include <QPainter>

@@ -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;
}

@@ -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 );
@@ -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.
You can’t perform that action at this time.