QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsstackedbardiagram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstackedbardiagram.cpp
3  ---------------------
4  begin : November 2019
5  copyright : (C) 2019 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsstackedbardiagram.h"
17 #include "qgsdiagramrenderer.h"
18 #include "qgsrendercontext.h"
19 #include "qgsexpression.h"
20 #include "qgssymbollayerutils.h"
21 #include "qgslinesymbol.h"
22 
23 #include <QPainter>
24 
26 {
27  mCategoryBrush.setStyle( Qt::SolidPattern );
28  mPen.setStyle( Qt::SolidLine );
29 }
30 
32 {
33  return new QgsStackedBarDiagram( *this );
34 }
35 
37 {
38  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
39  return QSizeF(); // invalid value range => zero size
40 
41  QVariant attrVal;
43  {
44  QgsExpressionContext expressionContext = c.expressionContext();
45  if ( !feature.fields().isEmpty() )
46  expressionContext.setFields( feature.fields() );
47  expressionContext.setFeature( feature );
48 
49  QgsExpression *expression = getExpression( is.classificationAttributeExpression, expressionContext );
50  attrVal = expression->evaluate( &expressionContext );
51  }
52  else
53  {
54  attrVal = feature.attribute( is.classificationField );
55  }
56 
57  bool ok = false;
58  double value = fabs( attrVal.toDouble( &ok ) );
59  if ( !ok )
60  {
61  return QSizeF(); //zero size if attribute is missing
62  }
63 
64  QSizeF size = sizeForValue( value, s, is );
65 
66  // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
67  // a conversion factor to painter units...
68  // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
69  double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
70 
71  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
72  mApplySpacingAdjust = true;
73 
74  switch ( s.diagramOrientation )
75  {
78  {
79  const double totalBarLength = size.height() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 );
80  size = QSizeF( s.barWidth, totalBarLength );
81  break;
82  }
83 
86  {
87  const double totalBarLength = size.width() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 );
88  size = QSizeF( totalBarLength, s.barWidth );
89  break;
90  }
91  }
92 
93  if ( s.showAxis() && s.axisLineSymbol() )
94  {
95  const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c ) / painterUnitConversionScale;
96  size.setWidth( size.width() + 2 * maxBleed );
97  size.setHeight( size.height() + 2 * maxBleed );
98  }
99 
100  return size;
101 }
102 
104 {
105  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
106  return s.minimumSize; // invalid value range => zero size
107 
108  // Scale, if extension is smaller than the specified minimum
109  if ( value < s.minimumSize )
110  {
111  value = s.minimumSize;
112  }
113 
114  double scaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
115  return value * scaleFactor;
116 }
117 
119 {
120  return DIAGRAM_NAME_STACKED;
121 }
122 
124 {
125  Q_UNUSED( c )
126  QSizeF size;
127 
128  if ( attributes.isEmpty() )
129  {
130  return QSizeF(); //zero size if no attributes
131  }
132 
133  // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
134  // a conversion factor to painter units...
135  // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
136  double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
137 
138  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
139 
140  switch ( s.diagramOrientation )
141  {
144  size.scale( s.barWidth, s.size.height() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ), Qt::IgnoreAspectRatio );
145  break;
146 
149  size.scale( s.size.width() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ), s.barWidth, Qt::IgnoreAspectRatio );
150  break;
151  }
152 
153  return size;
154 }
155 
156 void QgsStackedBarDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
157 {
158  QPainter *p = c.painter();
159  if ( !p )
160  {
161  return;
162  }
163 
164  QList< QPair<double, QColor> > values;
165  QList< QPair<double, QColor> > negativeValues;
166 
167  QgsExpressionContext expressionContext = c.expressionContext();
168  expressionContext.setFeature( feature );
169  if ( !feature.fields().isEmpty() )
170  expressionContext.setFields( feature.fields() );
171 
172  values.reserve( s.categoryAttributes.size() );
173  double total = 0;
174  double negativeTotal = 0;
175 
176  QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
177  for ( const QString &cat : std::as_const( s.categoryAttributes ) )
178  {
179  QgsExpression *expression = getExpression( cat, expressionContext );
180  double currentVal = expression->evaluate( &expressionContext ).toDouble();
181  total += fabs( currentVal );
182  if ( currentVal >= 0 )
183  {
184  values.push_back( qMakePair( currentVal, *colIt ) );
185  }
186  else
187  {
188  negativeTotal += currentVal;
189  negativeValues.push_back( qMakePair( -currentVal, *colIt ) );
190  }
191  ++colIt;
192  }
193 
194 
195  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() );
196  const double totalSpacing = std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ) * spacing;
197 
198  double scaledMaxVal = 0;
199  switch ( s.diagramOrientation )
200  {
203  scaledMaxVal = sizePainterUnits( s.size.height(), s, c );
204  break;
205 
208  scaledMaxVal = sizePainterUnits( s.size.width(), s, c );
209  break;
210  }
211  if ( mApplySpacingAdjust )
212  scaledMaxVal -= totalSpacing;
213 
214  double axisOffset = 0;
215  if ( !negativeValues.isEmpty() )
216  {
217  axisOffset = -negativeTotal / total * scaledMaxVal + ( negativeValues.size() - 1 ) * spacing;
218  }
219  double scaledWidth = sizePainterUnits( s.barWidth, s, c );
220 
221  double baseX = position.x();
222  double baseY = position.y();
223 
224  if ( s.showAxis() && s.axisLineSymbol() )
225  {
226  // if showing axis, the diagram position needs shifting from the default base x so that the axis
227  // line stroke sits within the desired label engine rect (otherwise we risk overlaps of the axis line stroke)
228  const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c );
229  baseX += maxBleed;
230  baseY -= maxBleed;
231  }
232 
233  mPen.setColor( s.penColor );
234  setPenWidth( mPen, s, c );
235  p->setPen( mPen );
236 
237  while ( !negativeValues.isEmpty() )
238  {
239  values.push_front( negativeValues.takeLast() );
240  }
241 
242  double currentOffset = 0;
243  QList< QPair<double, QColor> >::const_iterator valIt = values.constBegin();
244  for ( ; valIt != values.constEnd(); ++valIt )
245  {
246  double length = valIt->first / total * scaledMaxVal;
247 
248  QColor brushColor( valIt->second );
249  brushColor.setAlphaF( brushColor.alphaF() * s.opacity );
250  mCategoryBrush.setColor( brushColor );
251  p->setBrush( mCategoryBrush );
252 
253  switch ( s.diagramOrientation )
254  {
256  p->drawRect( QRectF( baseX, baseY - currentOffset, scaledWidth, length * -1 ) );
257  break;
258 
260  p->drawRect( QRectF( baseX, baseY + currentOffset - scaledMaxVal - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), scaledWidth, length ) );
261  break;
262 
264  p->drawRect( QRectF( baseX + currentOffset, baseY - scaledWidth, length, scaledWidth ) );
265  break;
266 
268  p->drawRect( QRectF( baseX + scaledMaxVal - currentOffset + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), baseY - scaledWidth, 0 - length, scaledWidth ) );
269  break;
270  }
271 
272  currentOffset += length + spacing;
273  }
274 
275  if ( s.showAxis() && s.axisLineSymbol() )
276  {
277  s.axisLineSymbol()->startRender( c );
278  QPolygonF axisPoints;
279  switch ( s.diagramOrientation )
280  {
282  axisPoints << QPointF( baseX, baseY - scaledMaxVal - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
283  << QPointF( baseX, baseY - axisOffset )
284  << QPointF( baseX + scaledWidth, baseY - axisOffset );
285  break;
286 
288  axisPoints << QPointF( baseX, baseY )
289  << QPointF( baseX, baseY - scaledMaxVal - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) + axisOffset )
290  << QPointF( baseX + scaledWidth, baseY - scaledMaxVal - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) + axisOffset );
291  break;
292 
294  axisPoints << QPointF( baseX + scaledMaxVal + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), baseY - scaledWidth )
295  << QPointF( baseX + axisOffset, baseY - scaledWidth )
296  << QPointF( baseX + axisOffset, baseY );
297  break;
298 
300  axisPoints << QPointF( baseX, baseY - scaledWidth )
301  << QPointF( baseX + scaledMaxVal + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) - axisOffset, baseY - scaledWidth )
302  << QPointF( baseX + scaledMaxVal + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) - axisOffset, baseY );
303  break;
304  }
305 
306  s.axisLineSymbol()->renderPolyline( axisPoints, nullptr, c );
307  s.axisLineSymbol()->stopRender( c );
308  }
309 }
QgsDiagram::getExpression
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition: qgsdiagram.cpp:38
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsDiagramInterpolationSettings::upperSize
QSizeF upperSize
Definition: qgsdiagramrenderer.h:666
QgsDiagram::sizeForValue
QSizeF sizeForValue(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is) const
Returns the scaled size of a diagram for a value, respecting the specified diagram interpolation sett...
Definition: qgsdiagram.cpp:81
QgsDiagramInterpolationSettings::lowerValue
double lowerValue
Definition: qgsdiagramrenderer.h:667
QgsFields::isEmpty
bool isEmpty() const
Checks whether the container is empty.
Definition: qgsfields.cpp:128
QgsDiagramSettings::categoryColors
QList< QColor > categoryColors
Definition: qgsdiagramrenderer.h:421
QgsDiagramSettings::penColor
QColor penColor
Definition: qgsdiagramrenderer.h:451
qgsexpression.h
QgsDiagramSettings::Up
@ Up
Definition: qgsdiagramrenderer.h:394
QgsStackedBarDiagram::legendSize
double legendSize(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is) const override
Returns the size of the legend item for the diagram corresponding to a specified value.
Definition: qgsstackedbardiagram.cpp:103
qgssymbollayerutils.h
QgsDiagramSettings::diagramOrientation
DiagramOrientation diagramOrientation
Definition: qgsdiagramrenderer.h:454
QgsStackedBarDiagram::clone
QgsStackedBarDiagram * clone() const override
Returns an instance that is equivalent to this one.
Definition: qgsstackedbardiagram.cpp:31
QgsDiagramSettings::minimumSize
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
Definition: qgsdiagramrenderer.h:487
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsExpressionContext::setFields
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Definition: qgsexpressioncontext.cpp:587
QgsDiagramSettings::spacingMapUnitScale
const QgsMapUnitScale & spacingMapUnitScale() const
Returns the map unit scale for the content spacing.
Definition: qgsdiagramrenderer.h:552
QgsDiagramInterpolationSettings::classificationField
QString classificationField
Name of the field for classification.
Definition: qgsdiagramrenderer.h:671
QgsDiagramSettings::opacity
double opacity
Opacity, from 0 (transparent) to 1.0 (opaque)
Definition: qgsdiagramrenderer.h:458
QgsDiagramSettings::Down
@ Down
Definition: qgsdiagramrenderer.h:395
QgsDiagramInterpolationSettings::upperValue
double upperValue
Definition: qgsdiagramrenderer.h:668
QgsDiagramSettings::spacing
double spacing() const
Returns the spacing between diagram contents.
Definition: qgsdiagramrenderer.h:500
QgsDiagramInterpolationSettings::lowerSize
QSizeF lowerSize
Definition: qgsdiagramrenderer.h:665
QgsDiagram::setPenWidth
void setPenWidth(QPen &pen, const QgsDiagramSettings &s, const QgsRenderContext &c)
Changes the pen width to match the current settings and rendering context.
Definition: qgsdiagram.cpp:49
QgsSymbol::stopRender
void stopRender(QgsRenderContext &context)
Ends the rendering process.
Definition: qgssymbol.cpp:842
QgsStackedBarDiagram::renderDiagram
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position) override
Draws the diagram at the given position (in pixel coordinates)
Definition: qgsstackedbardiagram.cpp:156
QgsDiagram::sizePainterUnits
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Definition: qgsdiagram.cpp:55
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2265
QgsDiagramSettings::Left
@ Left
Definition: qgsdiagramrenderer.h:396
qgsrendercontext.h
qgsstackedbardiagram.h
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:327
QgsDiagramSettings::Right
@ Right
Definition: qgsdiagramrenderer.h:397
QgsSymbol::startRender
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
Definition: qgssymbol.cpp:794
qgsdiagramrenderer.h
QgsDiagramInterpolationSettings
Additional diagram settings for interpolated size rendering.
Definition: qgsdiagramrenderer.h:662
QgsStackedBarDiagram::diagramName
QString diagramName() const override
Gets a descriptive name for this diagram type.
Definition: qgsstackedbardiagram.cpp:118
QgsStackedBarDiagram::diagramSize
QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s) override
Returns the size in map units the diagram will use to render.
Definition: qgsstackedbardiagram.cpp:123
QgsExpression::evaluate
QVariant evaluate()
Evaluate the feature and return the result.
Definition: qgsexpression.cpp:350
QgsDiagramInterpolationSettings::classificationAttributeIsExpression
bool classificationAttributeIsExpression
Definition: qgsdiagramrenderer.h:674
QgsStackedBarDiagram::QgsStackedBarDiagram
QgsStackedBarDiagram()
Definition: qgsstackedbardiagram.cpp:25
QgsDiagramSettings::size
QSizeF size
Definition: qgsdiagramrenderer.h:425
c
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Definition: porting_processing.dox:1
QgsDiagramSettings::barWidth
double barWidth
Definition: qgsdiagramrenderer.h:455
QgsDiagramSettings::showAxis
bool showAxis() const
Returns true if the diagram axis should be shown.
Definition: qgsdiagramrenderer.cpp:830
QgsStackedBarDiagram
A stacked bar chart diagram.
Definition: qgsstackedbardiagram.h:43
QgsDiagramSettings::axisLineSymbol
QgsLineSymbol * axisLineSymbol() const
Returns the line symbol to use for rendering axis in diagrams.
Definition: qgsdiagramrenderer.cpp:819
QgsLineSymbol::renderPolyline
void renderPolyline(const QPolygonF &points, const QgsFeature *f, QgsRenderContext &context, int layer=-1, bool selected=false)
Renders the symbol along the line joining points, using the given render context.
Definition: qgslinesymbol.cpp:232
QgsDiagramSettings::sizeType
QgsUnitTypes::RenderUnit sizeType
Diagram size unit.
Definition: qgsdiagramrenderer.h:430
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsFeature::fields
QgsFields fields
Definition: qgsfeature.h:70
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsSymbolLayerUtils::estimateMaxSymbolBleed
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
Definition: qgssymbollayerutils.cpp:934
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
DIAGRAM_NAME_STACKED
#define DIAGRAM_NAME_STACKED
Definition: qgsstackedbardiagram.h:19
QgsDiagramSettings::spacingUnit
QgsUnitTypes::RenderUnit spacingUnit() const
Returns the units for the content spacing.
Definition: qgsdiagramrenderer.h:532
QgsDiagramSettings::categoryAttributes
QList< QString > categoryAttributes
Definition: qgsdiagramrenderer.h:422
QgsDiagramInterpolationSettings::classificationAttributeExpression
QString classificationAttributeExpression
Definition: qgsdiagramrenderer.h:673
QgsDiagramSettings
Stores the settings for rendering a single diagram.
Definition: qgsdiagramrenderer.h:381
qgslinesymbol.h
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:525