QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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  mCategoryBrush.setColor( valIt->second );
249  p->setBrush( mCategoryBrush );
250 
251  switch ( s.diagramOrientation )
252  {
254  p->drawRect( QRectF( baseX, baseY - currentOffset, scaledWidth, length * -1 ) );
255  break;
256 
258  p->drawRect( QRectF( baseX, baseY + currentOffset - scaledMaxVal - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), scaledWidth, length ) );
259  break;
260 
262  p->drawRect( QRectF( baseX + currentOffset, baseY - scaledWidth, length, scaledWidth ) );
263  break;
264 
266  p->drawRect( QRectF( baseX + scaledMaxVal - currentOffset + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), baseY - scaledWidth, 0 - length, scaledWidth ) );
267  break;
268  }
269 
270  currentOffset += length + spacing;
271  }
272 
273  if ( s.showAxis() && s.axisLineSymbol() )
274  {
275  s.axisLineSymbol()->startRender( c );
276  QPolygonF axisPoints;
277  switch ( s.diagramOrientation )
278  {
280  axisPoints << QPointF( baseX, baseY - scaledMaxVal - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
281  << QPointF( baseX, baseY - axisOffset )
282  << QPointF( baseX + scaledWidth, baseY - axisOffset );
283  break;
284 
286  axisPoints << QPointF( baseX, baseY )
287  << QPointF( baseX, baseY - scaledMaxVal - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) + axisOffset )
288  << QPointF( baseX + scaledWidth, baseY - scaledMaxVal - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) + axisOffset );
289  break;
290 
292  axisPoints << QPointF( baseX + scaledMaxVal + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), baseY - scaledWidth )
293  << QPointF( baseX + axisOffset, baseY - scaledWidth )
294  << QPointF( baseX + axisOffset, baseY );
295  break;
296 
298  axisPoints << QPointF( baseX, baseY - scaledWidth )
299  << QPointF( baseX + scaledMaxVal + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) - axisOffset, baseY - scaledWidth )
300  << QPointF( baseX + scaledMaxVal + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) - axisOffset, baseY );
301  break;
302  }
303 
304  s.axisLineSymbol()->renderPolyline( axisPoints, nullptr, c );
305  s.axisLineSymbol()->stopRender( c );
306  }
307 }
A vector of attributes.
Definition: qgsattributes.h:58
Additional diagram settings for interpolated size rendering.
QString classificationField
Name of the field for classification.
Stores the settings for rendering a single diagram.
QgsUnitTypes::RenderUnit spacingUnit() const
Returns the units for the content spacing.
QgsUnitTypes::RenderUnit sizeType
Diagram size unit.
bool showAxis() const
Returns true if the diagram axis should be shown.
QgsLineSymbol * axisLineSymbol() const
Returns the line symbol to use for rendering axis in diagrams.
QList< QString > categoryAttributes
DiagramOrientation diagramOrientation
double spacing() const
Returns the spacing between diagram contents.
const QgsMapUnitScale & spacingMapUnitScale() const
Returns the map unit scale for the content spacing.
QList< QColor > categoryColors
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
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
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
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition: qgsdiagram.cpp:38
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
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
QVariant evaluate()
Evaluate the feature and return the result.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsFields fields
Definition: qgsfeature.h:66
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:320
bool isEmpty() const
Checks whether the container is empty.
Definition: qgsfields.cpp:128
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.
Contains information about the context of a rendering operation.
A stacked bar chart diagram.
QgsStackedBarDiagram * clone() const override
Returns an instance that is equivalent to this one.
QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s) override
Returns the size in map units the diagram will use to render.
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.
QString diagramName() const override
Gets a descriptive name for this diagram type.
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position) override
Draws the diagram at the given position (in pixel coordinates)
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
void stopRender(QgsRenderContext &context)
Ends the rendering process.
Definition: qgssymbol.cpp:516
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
Definition: qgssymbol.cpp:489
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
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:1578
#define DIAGRAM_NAME_STACKED