QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
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:369
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:94
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:29
QgsDiagramSettings::minimumSize
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
Definition: qgsdiagramrenderer.h:486
QgsRenderContext
Definition: qgsrendercontext.h:57
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
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:153
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:262
QgsDiagramSettings::Right
@ Right
Definition: qgsdiagramrenderer.h:396
qgsdiagramrenderer.h
QgsDiagramInterpolationSettings
Definition: qgsdiagramrenderer.h:661
QgsStackedBarDiagram::diagramName
QString diagramName() const override
Gets a descriptive name for this diagram type.
Definition: qgsstackedbardiagram.cpp:109
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:114
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:23
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
QgsStackedBarDiagram
Definition: qgsstackedbardiagram.h:43
QgsDiagramSettings::sizeType
QgsUnitTypes::RenderUnit sizeType
Diagram size unit.
Definition: qgsdiagramrenderer.h:429
QgsAttributes
Definition: qgsattributes.h:57
QgsFeature::fields
QgsFields fields
Definition: qgsfeature.h:70
QgsFeature
Definition: qgsfeature.h:55
QgsExpression
Definition: qgsexpression.h:113
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:380
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:521