QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 
21 #include <QPainter>
22 
24 {
25  mCategoryBrush.setStyle( Qt::SolidPattern );
26  mPen.setStyle( Qt::SolidLine );
27 }
28 
30 {
31  return new QgsStackedBarDiagram( *this );
32 }
33 
35 {
36  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
37  return QSizeF(); // invalid value range => zero size
38 
39  QVariant attrVal;
41  {
42  QgsExpressionContext expressionContext = c.expressionContext();
43  if ( !feature.fields().isEmpty() )
44  expressionContext.setFields( feature.fields() );
45  expressionContext.setFeature( feature );
46 
47  QgsExpression *expression = getExpression( is.classificationAttributeExpression, expressionContext );
48  attrVal = expression->evaluate( &expressionContext );
49  }
50  else
51  {
52  attrVal = feature.attribute( is.classificationField );
53  }
54 
55  bool ok = false;
56  double value = attrVal.toDouble( &ok );
57  if ( !ok )
58  {
59  return QSizeF(); //zero size if attribute is missing
60  }
61 
62  QSizeF size = sizeForValue( value, s, is );
63 
64  // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
65  // a conversion factor to painter units...
66  // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
67  double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
68 
69  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
70  mApplySpacingAdjust = true;
71 
72  switch ( s.diagramOrientation )
73  {
76  {
77  const double totalBarLength = size.height() + spacing * std::max( 0, s.categoryAttributes.size() - 1 );
78  size = QSizeF( s.barWidth, totalBarLength );
79  break;
80  }
81 
84  {
85  const double totalBarLength = size.width() + spacing * std::max( 0, s.categoryAttributes.size() - 1 );
86  size = QSizeF( totalBarLength, s.barWidth );
87  break;
88  }
89  }
90 
91  return size;
92 }
93 
95 {
96  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
97  return s.minimumSize; // invalid value range => zero size
98 
99  // Scale, if extension is smaller than the specified minimum
100  if ( value < s.minimumSize )
101  {
102  value = s.minimumSize;
103  }
104 
105  double scaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
106  return value * scaleFactor;
107 }
108 
110 {
111  return DIAGRAM_NAME_STACKED;
112 }
113 
115 {
116  Q_UNUSED( c )
117  QSizeF size;
118 
119  if ( attributes.isEmpty() )
120  {
121  return QSizeF(); //zero size if no attributes
122  }
123 
124  double totalSum = 0;
125  for ( int i = 0; i < attributes.count(); ++i )
126  {
127  totalSum += attributes.at( i ).toDouble();
128  }
129 
130  // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
131  // a conversion factor to painter units...
132  // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
133  double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
134 
135  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
136 
137  switch ( s.diagramOrientation )
138  {
141  size.scale( s.barWidth, s.size.height() + spacing * std::max( 0, s.categoryAttributes.size() - 1 ), Qt::IgnoreAspectRatio );
142  break;
143 
146  size.scale( s.size.width() + spacing * std::max( 0, s.categoryAttributes.size() - 1 ), s.barWidth, Qt::IgnoreAspectRatio );
147  break;
148  }
149 
150  return size;
151 }
152 
153 void QgsStackedBarDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
154 {
155  QPainter *p = c.painter();
156  if ( !p )
157  {
158  return;
159  }
160 
161  QList<double> values;
162 
163  QgsExpressionContext expressionContext = c.expressionContext();
164  expressionContext.setFeature( feature );
165  if ( !feature.fields().isEmpty() )
166  expressionContext.setFields( feature.fields() );
167 
168  values.reserve( s.categoryAttributes.size() );
169  double total = 0;
170  for ( const QString &cat : qgis::as_const( s.categoryAttributes ) )
171  {
172  QgsExpression *expression = getExpression( cat, expressionContext );
173  double currentVal = expression->evaluate( &expressionContext ).toDouble();
174  values.push_back( currentVal );
175  total += currentVal;
176  }
177 
178  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() );
179  const double totalSpacing = std::max( 0, s.categoryAttributes.size() - 1 ) * spacing;
180 
181  double scaledMaxVal = 0;
182  switch ( s.diagramOrientation )
183  {
186  scaledMaxVal = sizePainterUnits( s.size.height(), s, c );
187  break;
188 
191  scaledMaxVal = sizePainterUnits( s.size.width(), s, c );
192  break;
193  }
194  if ( mApplySpacingAdjust )
195  scaledMaxVal -= totalSpacing;
196 
197  double currentOffset = 0;
198  double scaledWidth = sizePainterUnits( s.barWidth, s, c );
199 
200 
201  double baseX = position.x();
202  double baseY = position.y();
203 
204  mPen.setColor( s.penColor );
205  setPenWidth( mPen, s, c );
206  p->setPen( mPen );
207 
208  QList<double>::const_iterator valIt = values.constBegin();
209  QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
210  for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
211  {
212  double length = *valIt / total * scaledMaxVal;
213 
214  mCategoryBrush.setColor( *colIt );
215  p->setBrush( mCategoryBrush );
216 
217  switch ( s.diagramOrientation )
218  {
220  p->drawRect( QRectF( baseX, baseY - currentOffset, scaledWidth, length * -1 ) );
221  break;
222 
224  p->drawRect( QRectF( baseX, baseY + currentOffset - scaledMaxVal - spacing * std::max( 0, values.size() - 1 ), scaledWidth, length ) );
225  break;
226 
228  p->drawRect( QRectF( baseX + currentOffset, baseY - scaledWidth, length, scaledWidth ) );
229  break;
230 
232  p->drawRect( QRectF( baseX + scaledMaxVal - currentOffset + spacing * std::max( 0, values.size() - 1 ), baseY - scaledWidth, 0 - length, scaledWidth ) );
233  break;
234  }
235 
236  currentOffset += length + spacing;
237  }
238 }
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
Class for parsing and evaluation of expressions (formerly called "search strings").
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
QString diagramName() const override
Gets a descriptive name for this diagram type.
const QgsMapUnitScale & spacingMapUnitScale() const
Returns the map unit scale for the content spacing.
A stacked bar chart diagram.
QgsUnitTypes::RenderUnit spacingUnit() const
Returns the units for the content spacing.
QList< QString > categoryAttributes
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
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
QVariant evaluate()
Evaluate the feature and return the result.
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.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
QgsFields fields
Definition: qgsfeature.h:66
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
DiagramOrientation diagramOrientation
QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s) override
Returns the size in map units the diagram will use to render.
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
Definition: qgsdiagram.cpp:37
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
double spacing() const
Returns the spacing between diagram contents.
#define DIAGRAM_NAME_STACKED
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
QgsUnitTypes::RenderUnit sizeType
Diagram size unit.
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
QgsExpressionContext & expressionContext()
Gets the expression context.
Additional diagram settings for interpolated size rendering.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsStackedBarDiagram * clone() const override
Returns an instance that is equivalent to this one.
QString classificationField
Name of the field for classification.
bool isEmpty() const
Checks whether the container is empty.
Definition: qgsfields.cpp:128
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position) override
Draws the diagram at the given position (in pixel coordinates)
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
A vector of attributes.
Definition: qgsattributes.h:57
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262
QList< QColor > categoryColors
Stores the settings for rendering a single diagram.