QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
22 #include <QPainter>
23 
25 {
26  mCategoryBrush.setStyle( Qt::SolidPattern );
27  mPen.setStyle( Qt::SolidLine );
28 }
29 
31 {
32  return new QgsStackedBarDiagram( *this );
33 }
34 
36 {
37  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
38  return QSizeF(); // invalid value range => zero size
39 
40  QVariant attrVal;
42  {
43  QgsExpressionContext expressionContext = c.expressionContext();
44  if ( !feature.fields().isEmpty() )
45  expressionContext.setFields( feature.fields() );
46  expressionContext.setFeature( feature );
47 
48  QgsExpression *expression = getExpression( is.classificationAttributeExpression, expressionContext );
49  attrVal = expression->evaluate( &expressionContext );
50  }
51  else
52  {
53  attrVal = feature.attribute( is.classificationField );
54  }
55 
56  bool ok = false;
57  double value = fabs( attrVal.toDouble( &ok ) );
58  if ( !ok )
59  {
60  return QSizeF(); //zero size if attribute is missing
61  }
62 
63  QSizeF size = sizeForValue( value, s, is );
64 
65  // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
66  // a conversion factor to painter units...
67  // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
68  double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
69 
70  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
71  mApplySpacingAdjust = true;
72 
73  switch ( s.diagramOrientation )
74  {
77  {
78  const double totalBarLength = size.height() + spacing * std::max( 0, s.categoryAttributes.size() - 1 );
79  size = QSizeF( s.barWidth, totalBarLength );
80  break;
81  }
82 
85  {
86  const double totalBarLength = size.width() + spacing * std::max( 0, s.categoryAttributes.size() - 1 );
87  size = QSizeF( totalBarLength, s.barWidth );
88  break;
89  }
90  }
91 
92  if ( s.showAxis() && s.axisLineSymbol() )
93  {
94  const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c ) / painterUnitConversionScale;
95  size.setWidth( size.width() + 2 * maxBleed );
96  size.setHeight( size.height() + 2 * maxBleed );
97  }
98 
99  return size;
100 }
101 
103 {
104  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
105  return s.minimumSize; // invalid value range => zero size
106 
107  // Scale, if extension is smaller than the specified minimum
108  if ( value < s.minimumSize )
109  {
110  value = s.minimumSize;
111  }
112 
113  double scaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
114  return value * scaleFactor;
115 }
116 
118 {
119  return DIAGRAM_NAME_STACKED;
120 }
121 
123 {
124  Q_UNUSED( c )
125  QSizeF size;
126 
127  if ( attributes.isEmpty() )
128  {
129  return QSizeF(); //zero size if no attributes
130  }
131 
132  // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
133  // a conversion factor to painter units...
134  // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
135  double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
136 
137  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
138 
139  switch ( s.diagramOrientation )
140  {
143  size.scale( s.barWidth, s.size.height() + spacing * std::max( 0, s.categoryAttributes.size() - 1 ), Qt::IgnoreAspectRatio );
144  break;
145 
148  size.scale( s.size.width() + spacing * std::max( 0, s.categoryAttributes.size() - 1 ), s.barWidth, Qt::IgnoreAspectRatio );
149  break;
150  }
151 
152  return size;
153 }
154 
155 void QgsStackedBarDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
156 {
157  QPainter *p = c.painter();
158  if ( !p )
159  {
160  return;
161  }
162 
163  QList< QPair<double, QColor> > values;
164  QList< QPair<double, QColor> > negativeValues;
165 
166  QgsExpressionContext expressionContext = c.expressionContext();
167  expressionContext.setFeature( feature );
168  if ( !feature.fields().isEmpty() )
169  expressionContext.setFields( feature.fields() );
170 
171  values.reserve( s.categoryAttributes.size() );
172  double total = 0;
173  double negativeTotal = 0;
174 
175  QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
176  for ( const QString &cat : qgis::as_const( s.categoryAttributes ) )
177  {
178  QgsExpression *expression = getExpression( cat, expressionContext );
179  double currentVal = expression->evaluate( &expressionContext ).toDouble();
180  total += fabs( currentVal );
181  if ( currentVal >= 0 )
182  {
183  values.push_back( qMakePair( currentVal, *colIt ) );
184  }
185  else
186  {
187  negativeTotal += currentVal;
188  negativeValues.push_back( qMakePair( -currentVal, *colIt ) );
189  }
190  ++colIt;
191  }
192 
193 
194  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() );
195  const double totalSpacing = std::max( 0, s.categoryAttributes.size() - 1 ) * spacing;
196 
197  double scaledMaxVal = 0;
198  switch ( s.diagramOrientation )
199  {
202  scaledMaxVal = sizePainterUnits( s.size.height(), s, c );
203  break;
204 
207  scaledMaxVal = sizePainterUnits( s.size.width(), s, c );
208  break;
209  }
210  if ( mApplySpacingAdjust )
211  scaledMaxVal -= totalSpacing;
212 
213  double axisOffset = 0;
214  if ( !negativeValues.isEmpty() )
215  {
216  axisOffset = -negativeTotal / total * scaledMaxVal + ( negativeValues.size() - 1 ) * spacing;
217  }
218  double scaledWidth = sizePainterUnits( s.barWidth, s, c );
219 
220  double baseX = position.x();
221  double baseY = position.y();
222 
223  if ( s.showAxis() && s.axisLineSymbol() )
224  {
225  // if showing axis, the diagram position needs shifting from the default base x so that the axis
226  // line stroke sits within the desired label engine rect (otherwise we risk overlaps of the axis line stroke)
227  const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c );
228  baseX += maxBleed;
229  baseY -= maxBleed;
230  }
231 
232  mPen.setColor( s.penColor );
233  setPenWidth( mPen, s, c );
234  p->setPen( mPen );
235 
236  while ( !negativeValues.isEmpty() )
237  {
238  values.push_front( negativeValues.takeLast() );
239  }
240 
241  double currentOffset = 0;
242  QList< QPair<double, QColor> >::const_iterator valIt = values.constBegin();
243  for ( ; valIt != values.constEnd(); ++valIt )
244  {
245  double length = valIt->first / total * scaledMaxVal;
246 
247  mCategoryBrush.setColor( valIt->second );
248  p->setBrush( mCategoryBrush );
249 
250  switch ( s.diagramOrientation )
251  {
253  p->drawRect( QRectF( baseX, baseY - currentOffset, scaledWidth, length * -1 ) );
254  break;
255 
257  p->drawRect( QRectF( baseX, baseY + currentOffset - scaledMaxVal - spacing * std::max( 0, values.size() - 1 ), scaledWidth, length ) );
258  break;
259 
261  p->drawRect( QRectF( baseX + currentOffset, baseY - scaledWidth, length, scaledWidth ) );
262  break;
263 
265  p->drawRect( QRectF( baseX + scaledMaxVal - currentOffset + spacing * std::max( 0, values.size() - 1 ), baseY - scaledWidth, 0 - length, scaledWidth ) );
266  break;
267  }
268 
269  currentOffset += length + spacing;
270  }
271 
272  if ( s.showAxis() && s.axisLineSymbol() )
273  {
274  s.axisLineSymbol()->startRender( c );
275  QPolygonF axisPoints;
276  switch ( s.diagramOrientation )
277  {
279  axisPoints << QPointF( baseX, baseY - scaledMaxVal - spacing * std::max( 0, values.size() - 1 ) )
280  << QPointF( baseX, baseY - axisOffset )
281  << QPointF( baseX + scaledWidth, baseY - axisOffset );
282  break;
283 
285  axisPoints << QPointF( baseX, baseY )
286  << QPointF( baseX, baseY - scaledMaxVal - spacing * std::max( 0, values.size() - 1 ) + axisOffset )
287  << QPointF( baseX + scaledWidth, baseY - scaledMaxVal - spacing * std::max( 0, values.size() - 1 ) + axisOffset );
288  break;
289 
291  axisPoints << QPointF( baseX + scaledMaxVal + spacing * std::max( 0, values.size() - 1 ), baseY - scaledWidth )
292  << QPointF( baseX + axisOffset, baseY - scaledWidth )
293  << QPointF( baseX + axisOffset, baseY );
294  break;
295 
297  axisPoints << QPointF( baseX, baseY - scaledWidth )
298  << QPointF( baseX + scaledMaxVal + spacing * std::max( 0, values.size() - 1 ) - axisOffset, baseY - scaledWidth )
299  << QPointF( baseX + scaledMaxVal + spacing * std::max( 0, values.size() - 1 ) - axisOffset, baseY );
300  break;
301  }
302 
303  s.axisLineSymbol()->renderPolyline( axisPoints, nullptr, c );
304  s.axisLineSymbol()->stopRender( c );
305  }
306 }
QgsDiagram::getExpression
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition: qgsdiagram.cpp:37
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsDiagramInterpolationSettings::upperSize
QSizeF upperSize
Definition: qgsdiagramrenderer.h:665
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:80
QgsDiagramInterpolationSettings::lowerValue
double lowerValue
Definition: qgsdiagramrenderer.h:666
QgsFields::isEmpty
bool isEmpty() const
Checks whether the container is empty.
Definition: qgsfields.cpp:128
QgsDiagramSettings::categoryColors
QList< QColor > categoryColors
Definition: qgsdiagramrenderer.h:420
QgsDiagramSettings::penColor
QColor penColor
Definition: qgsdiagramrenderer.h:450
qgsexpression.h
QgsDiagramSettings::Up
@ Up
Definition: qgsdiagramrenderer.h:393
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:102
qgssymbollayerutils.h
QgsDiagramSettings::diagramOrientation
DiagramOrientation diagramOrientation
Definition: qgsdiagramrenderer.h:453
QgsStackedBarDiagram::clone
QgsStackedBarDiagram * clone() const override
Returns an instance that is equivalent to this one.
Definition: qgsstackedbardiagram.cpp:30
QgsDiagramSettings::minimumSize
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
Definition: qgsdiagramrenderer.h:486
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsExpressionContext::setFields
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Definition: qgsexpressioncontext.cpp:553
QgsDiagramSettings::spacingMapUnitScale
const QgsMapUnitScale & spacingMapUnitScale() const
Returns the map unit scale for the content spacing.
Definition: qgsdiagramrenderer.h:551
QgsDiagramInterpolationSettings::classificationField
QString classificationField
Name of the field for classification.
Definition: qgsdiagramrenderer.h:670
QgsDiagramSettings::Down
@ Down
Definition: qgsdiagramrenderer.h:394
QgsDiagramInterpolationSettings::upperValue
double upperValue
Definition: qgsdiagramrenderer.h:667
QgsDiagramSettings::spacing
double spacing() const
Returns the spacing between diagram contents.
Definition: qgsdiagramrenderer.h:499
QgsDiagramInterpolationSettings::lowerSize
QSizeF lowerSize
Definition: qgsdiagramrenderer.h:664
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:48
QgsSymbol::stopRender
void stopRender(QgsRenderContext &context)
Ends the rendering process.
Definition: qgssymbol.cpp:479
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:155
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:54
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:315
QgsDiagramSettings::Left
@ Left
Definition: qgsdiagramrenderer.h:395
qgsrendercontext.h
qgsstackedbardiagram.h
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:264
QgsDiagramSettings::Right
@ Right
Definition: qgsdiagramrenderer.h:396
QgsSymbol::startRender
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
Definition: qgssymbol.cpp:454
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:117
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:122
QgsExpression::evaluate
QVariant evaluate()
Evaluate the feature and return the result.
Definition: qgsexpression.cpp:346
QgsDiagramInterpolationSettings::classificationAttributeIsExpression
bool classificationAttributeIsExpression
Definition: qgsdiagramrenderer.h:673
QgsStackedBarDiagram::QgsStackedBarDiagram
QgsStackedBarDiagram()
Definition: qgsstackedbardiagram.cpp:24
QgsDiagramSettings::size
QSizeF size
Definition: qgsdiagramrenderer.h:424
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:454
QgsDiagramSettings::showAxis
bool showAxis() const
Returns true if the diagram axis should be shown.
Definition: qgsdiagramrenderer.cpp:815
QgsStackedBarDiagram
A stacked bar chart diagram.
Definition: qgsstackedbardiagram.h:44
QgsDiagramSettings::axisLineSymbol
QgsLineSymbol * axisLineSymbol() const
Returns the line symbol to use for rendering axis in diagrams.
Definition: qgsdiagramrenderer.cpp:804
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: qgssymbol.cpp:2125
QgsDiagramSettings::sizeType
QgsUnitTypes::RenderUnit sizeType
Diagram size unit.
Definition: qgsdiagramrenderer.h:429
QgsAttributes
A vector of attributes.
Definition: qgsattributes.h:58
QgsFeature::fields
QgsFields fields
Definition: qgsfeature.h:66
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsSymbolLayerUtils::estimateMaxSymbolBleed
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
Definition: qgssymbollayerutils.cpp:822
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
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:531
QgsDiagramSettings::categoryAttributes
QList< QString > categoryAttributes
Definition: qgsdiagramrenderer.h:421
QgsDiagramInterpolationSettings::classificationAttributeExpression
QString classificationAttributeExpression
Definition: qgsdiagramrenderer.h:672
QgsDiagramSettings
Stores the settings for rendering a single diagram.
Definition: qgsdiagramrenderer.h:381
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:521