Skip to content

Commit

Permalink
[tests] Subdiagram position in vertically stacked diagrams (iterating…
Browse files Browse the repository at this point in the history
… subdiagrams backwards)
  • Loading branch information
gacarrillor committed Aug 21, 2024
1 parent 56564cb commit 8fe10fa
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ Adds a subdiagram to the stacked diagram object along with its corresponding set
or more to the top (if stacked diagram is vertical).
%End

QList< QgsDiagram * > subDiagrams() const;
QList< QgsDiagram * > subDiagrams( const QgsDiagramSettings &s ) const;
%Docstring
Returns an ordered list with the subdiagrams of the stacked diagram object.
If the stacked diagram orientation is vertical, the list is returned backwards.

:param s: stacked diagram settings
%End

QgsDiagramSettings *subDiagramSettings( const QgsDiagram *diagram ) const;
Expand Down
5 changes: 4 additions & 1 deletion python/core/auto_generated/diagram/qgsstackeddiagram.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ Adds a subdiagram to the stacked diagram object along with its corresponding set
or more to the top (if stacked diagram is vertical).
%End

QList< QgsDiagram * > subDiagrams() const;
QList< QgsDiagram * > subDiagrams( const QgsDiagramSettings &s ) const;
%Docstring
Returns an ordered list with the subdiagrams of the stacked diagram object.
If the stacked diagram orientation is vertical, the list is returned backwards.

:param s: stacked diagram settings
%End

QgsDiagramSettings *subDiagramSettings( const QgsDiagram *diagram ) const;
Expand Down
22 changes: 18 additions & 4 deletions src/core/diagram/qgsstackeddiagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,26 @@ void QgsStackedDiagram::addSubDiagram( QgsDiagram *diagram, QgsDiagramSettings *
mSubDiagrams.append( DiagramData{diagram, s} );
}

QList< QgsDiagram * > QgsStackedDiagram::subDiagrams() const
QList< QgsDiagram * > QgsStackedDiagram::subDiagrams( const QgsDiagramSettings &s ) const
{
QList< QgsDiagram * > diagrams;
for ( const auto &item : std::as_const( mSubDiagrams ) )

if ( s.stackedDiagramMode == QgsDiagramSettings::Horizontal )
{
for ( const auto &item : std::as_const( mSubDiagrams ) )
{
diagrams.append( item.diagram );
}
}
else
{
diagrams.append( item.diagram );
// We'll draw vertical diagrams backwards,
// so we return the subdiagrams in reverse order
QList< DiagramData >::const_reverse_iterator iter = mSubDiagrams.rbegin();
for ( ; iter != mSubDiagrams.rend(); ++iter )
{
diagrams.append( iter->diagram );
}
}
return diagrams;
}
Expand Down Expand Up @@ -71,7 +85,7 @@ void QgsStackedDiagram::subDiagramPosition( QPointF &newPos, const QgsRenderCont
}
else
{
newPos += QPointF( 0, size.height() + spacing );
newPos -= QPointF( 0, size.height() + spacing );
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/core/diagram/qgsstackeddiagram.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ class CORE_EXPORT QgsStackedDiagram : public QgsDiagram SIP_NODEFAULTCTORS
*/
void addSubDiagram( QgsDiagram *diagram, QgsDiagramSettings *s );

//! Returns an ordered list with the subdiagrams of the stacked diagram object.
QList< QgsDiagram * > subDiagrams() const;
/**
* Returns an ordered list with the subdiagrams of the stacked diagram object.
* If the stacked diagram orientation is vertical, the list is returned backwards.
* \param s stacked diagram settings
*/
QList< QgsDiagram * > subDiagrams( const QgsDiagramSettings &s ) const;

//! Returns the settings associated to the \a diagram.
QgsDiagramSettings *subDiagramSettings( const QgsDiagram *diagram ) const;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsdiagramrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ void QgsDiagramRenderer::renderDiagram( const QgsFeature &feature, QgsRenderCont
{
// Iterate subdiagrams and render them individually
QgsStackedDiagram *stackedDiagram = qgis::down_cast< QgsStackedDiagram *>( mDiagram.get() );
QList< QgsDiagram * > subDiagrams = stackedDiagram->subDiagrams();
QList< QgsDiagram * > subDiagrams = stackedDiagram->subDiagrams( s );
QPointF newPos = pos; // Each subdiagram will have its own newPos

for ( const auto &subDiagram : std::as_const( subDiagrams ) )
Expand Down
78 changes: 78 additions & 0 deletions tests/src/core/testqgsstackeddiagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,84 @@ class TestQgsStackedDiagram : public QgsTest
QGSVERIFYRENDERMAPSETTINGSCHECK( "stackedhistograms", "stackedhistograms", *mMapSettings, 200, 15 );
}

void testVerticallyStackedHistograms()
{
// Histogram 1
QgsDiagramSettings ds1;
QColor col1 = Qt::blue;
QColor col2 = Qt::red;
QColor col3 = Qt::yellow;
QColor col4 = Qt::green;
col1.setAlphaF( 0.5 );
col2.setAlphaF( 0.5 );
col3.setAlphaF( 0.5 );
col4.setAlphaF( 0.5 );
ds1.categoryColors = QList<QColor>() << col1 << col2 << col3 << col4;
ds1.categoryAttributes = QList<QString>() << QStringLiteral( "\"maennlich_ab_65\"" ) << QStringLiteral( "\"maennlich_18_64\"" ) << QStringLiteral( "\"maennlich_6_17\"" ) << QStringLiteral( "\"maennlich_unter_6\"" ); //#spellok
ds1.minimumScale = -1;
ds1.maximumScale = -1;
ds1.minimumSize = 0;
ds1.penColor = Qt::black;
ds1.penWidth = .5;
ds1.scaleByArea = true;
ds1.sizeType = Qgis::RenderUnit::Millimeters;
ds1.rotationOffset = 0;
ds1.diagramOrientation = QgsDiagramSettings::Up;

// Histogram 2
QgsDiagramSettings ds2;
col1 = Qt::blue;
col2 = Qt::red;
col3 = Qt::yellow;
col4 = Qt::green;
col1.setAlphaF( 0.5 );
col2.setAlphaF( 0.5 );
col3.setAlphaF( 0.5 );
col4.setAlphaF( 0.5 );
ds2.categoryColors = QList<QColor>() << col1 << col2 << col3 << col4;
ds2.categoryAttributes = QList<QString>() << QStringLiteral( "\"weiblich_ab_65\"" ) << QStringLiteral( "\"weiblich_18_64\"" ) << QStringLiteral( "\"weiblich_6_17\"" ) << QStringLiteral( "\"weiblich_unter_6\"" ); //#spellok
ds2.minimumScale = -1;
ds2.maximumScale = -1;
ds2.minimumSize = 0;
ds2.penColor = Qt::black;
ds2.penWidth = .5;
ds2.scaleByArea = true;
ds2.sizeType = Qgis::RenderUnit::Millimeters;
ds2.rotationOffset = 0;
ds2.diagramOrientation = QgsDiagramSettings::Down;

QgsDiagramSettings ds;
ds.stackedDiagramMode = QgsDiagramSettings::Vertical;
ds.categoryAttributes = ds1.categoryAttributes + ds2.categoryAttributes;
ds.setStackedDiagramSpacingUnit( Qgis::RenderUnit::Pixels );
ds.setStackedDiagramSpacing( 0 );

QgsStackedDiagram *stackedDiagram = new QgsStackedDiagram();
stackedDiagram->addSubDiagram( new QgsHistogramDiagram(), &ds1 );
stackedDiagram->addSubDiagram( new QgsHistogramDiagram(), &ds2 );

QgsLinearlyInterpolatedDiagramRenderer *dr = new QgsLinearlyInterpolatedDiagramRenderer();
dr->setLowerValue( 0.0 );
dr->setLowerSize( QSizeF( 0.0, 0.0 ) );
dr->setUpperValue( 15000 );
dr->setUpperSize( QSizeF( 20, 20 ) );
dr->setClassificationField( QStringLiteral( "max(\"maennlich_18_64\", \"maennlich_ab_65\", \"weiblich_unter_6\", \"weiblich_6_17\", \"weiblich_18_64\", \"weiblich_ab_65\")" ) ); //#spellok
dr->setDiagram( stackedDiagram );
dr->setDiagramSettings( ds );
mPointsLayer->setDiagramRenderer( dr );

QgsDiagramLayerSettings dls = QgsDiagramLayerSettings();
dls.setPlacement( QgsDiagramLayerSettings::OverPoint );
dls.setShowAllDiagrams( true );
mPointsLayer->setDiagramLayerSettings( dls );

const QgsRectangle extent( 9.7, 53.5, 9.95, 53.6 );
mMapSettings->setExtent( extent );
mMapSettings->setFlag( Qgis::MapSettingsFlag::ForceVectorOutput );
mMapSettings->setOutputDpi( 96 );
QGSVERIFYRENDERMAPSETTINGSCHECK( "verticallystackedhistograms", "verticallystackedhistograms", *mMapSettings, 200, 15 );
}

void testStackedHistogramsWithSpacing()
{
// Histogram 1
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8fe10fa

Please sign in to comment.