QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgshistogramdiagram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshistogramdiagram.cpp
3  ---------------------
4  begin : August 2012
5  copyright : (C) 2012 by Matthias Kuhn
6  email : matthias at opengis dot ch
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 #include "qgshistogramdiagram.h"
16 #include "qgsdiagramrenderer.h"
17 #include "qgsrendercontext.h"
18 #include "qgsexpression.h"
19 #include "qgssymbollayerutils.h"
20 
21 #include <QPainter>
22 
24 {
25  mCategoryBrush.setStyle( Qt::SolidPattern );
26  mPen.setStyle( Qt::SolidLine );
27  mScaleFactor = 0;
28 }
29 
31 {
32  return new QgsHistogramDiagram( *this );
33 }
34 
36 {
37  QSizeF size;
38  if ( feature.attributes().isEmpty() )
39  {
40  return size; //zero size if no attributes
41  }
42 
43  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
44  return size; // invalid value range => zero size
45 
46  double maxValue = 0;
47 
48  QgsExpressionContext expressionContext = c.expressionContext();
49  expressionContext.setFeature( feature );
50  if ( !feature.fields().isEmpty() )
51  expressionContext.setFields( feature.fields() );
52 
53  for ( const QString &cat : qgis::as_const( s.categoryAttributes ) )
54  {
55  QgsExpression *expression = getExpression( cat, expressionContext );
56  maxValue = std::max( expression->evaluate( &expressionContext ).toDouble(), maxValue );
57  }
58 
59  // Scale, if extension is smaller than the specified minimum
60  if ( maxValue < s.minimumSize )
61  {
62  maxValue = s.minimumSize;
63  }
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 
72  switch ( s.diagramOrientation )
73  {
76  mScaleFactor = ( ( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue ) );
77  size.scale( s.barWidth * s.categoryAttributes.size() + spacing * std::max( 0, s.categoryAttributes.size() - 1 ), maxValue * mScaleFactor, Qt::IgnoreAspectRatio );
78  break;
79 
82  mScaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
83  size.scale( maxValue * mScaleFactor, s.barWidth * s.categoryAttributes.size() + spacing * std::max( 0, s.categoryAttributes.size() - 1 ), Qt::IgnoreAspectRatio );
84  break;
85  }
86 
87  if ( s.showAxis() && s.axisLineSymbol() )
88  {
89  const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c ) / painterUnitConversionScale;
90  size.setWidth( size.width() + 2 * maxBleed );
91  size.setHeight( size.height() + 2 * maxBleed );
92  }
93 
94  return size;
95 }
96 
98 {
99  if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
100  return s.minimumSize; // invalid value range => zero size
101 
102  // Scale, if extension is smaller than the specified minimum
103  if ( value < s.minimumSize )
104  {
105  value = s.minimumSize;
106  }
107 
108  double scaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
109  return value * scaleFactor;
110 }
111 
113 {
114  return DIAGRAM_NAME_HISTOGRAM;
115 }
116 
118 {
119  Q_UNUSED( c )
120  QSizeF size;
121 
122  if ( attributes.isEmpty() )
123  {
124  return QSizeF(); //zero size if no attributes
125  }
126 
127  double maxValue = attributes.at( 0 ).toDouble();
128 
129  for ( int i = 0; i < attributes.count(); ++i )
130  {
131  maxValue = std::max( attributes.at( i ).toDouble(), maxValue );
132  }
133 
134  // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
135  // a conversion factor to painter units...
136  // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
137  double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
138 
139  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
140 
141  switch ( s.diagramOrientation )
142  {
145  mScaleFactor = maxValue / s.size.height();
146  size.scale( s.barWidth * s.categoryColors.size() + spacing * std::max( 0, s.categoryAttributes.size() - 1 ), s.size.height(), Qt::IgnoreAspectRatio );
147  break;
148 
151  mScaleFactor = maxValue / s.size.width();
152  size.scale( s.size.width(), s.barWidth * s.categoryColors.size() + spacing * std::max( 0, s.categoryAttributes.size() - 1 ), Qt::IgnoreAspectRatio );
153  break;
154  }
155 
156  if ( s.showAxis() && s.axisLineSymbol() )
157  {
158  const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c ) / painterUnitConversionScale;
159  size.setWidth( size.width() + 2 * maxBleed );
160  size.setHeight( size.height() + 2 * maxBleed );
161  }
162 
163  return size;
164 }
165 
166 void QgsHistogramDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
167 {
168  QPainter *p = c.painter();
169  if ( !p )
170  {
171  return;
172  }
173 
174  QList<double> values;
175  double maxValue = 0;
176 
177  QgsExpressionContext expressionContext = c.expressionContext();
178  expressionContext.setFeature( feature );
179  if ( !feature.fields().isEmpty() )
180  expressionContext.setFields( feature.fields() );
181 
182  values.reserve( s.categoryAttributes.size() );
183  for ( const QString &cat : qgis::as_const( s.categoryAttributes ) )
184  {
185  QgsExpression *expression = getExpression( cat, expressionContext );
186  double currentVal = expression->evaluate( &expressionContext ).toDouble();
187  values.push_back( currentVal );
188  maxValue = std::max( currentVal, maxValue );
189  }
190 
191  double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );
192 
193  double currentOffset = 0;
194  double scaledWidth = sizePainterUnits( s.barWidth, s, c );
195 
196  const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() );
197 
198  double baseX = position.x();
199  double baseY = position.y();
200 
201  if ( s.showAxis() && s.axisLineSymbol() )
202  {
203  // if showing axis, the diagram position needs shifting from the default base x so that the axis
204  // line stroke sits within the desired label engine rect (otherwise we risk overlaps of the axis line stroke)
205  const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c );
206  baseX += maxBleed;
207  baseY -= maxBleed;
208  }
209 
210 
211  mPen.setColor( s.penColor );
212  setPenWidth( mPen, s, c );
213  p->setPen( mPen );
214 
215  QList<double>::const_iterator valIt = values.constBegin();
216  QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
217  for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
218  {
219  double length = sizePainterUnits( *valIt * mScaleFactor, s, c );
220 
221  mCategoryBrush.setColor( *colIt );
222  p->setBrush( mCategoryBrush );
223 
224  switch ( s.diagramOrientation )
225  {
227  p->drawRect( QRectF( baseX + currentOffset, baseY, scaledWidth, length * -1 ) );
228  break;
229 
231  p->drawRect( QRectF( baseX + currentOffset, baseY - scaledMaxVal, scaledWidth, length ) );
232  break;
233 
235  p->drawRect( QRectF( baseX, baseY - scaledWidth * values.size() - spacing * std::max( 0, values.size() - 1 ) + currentOffset, length, scaledWidth ) );
236  break;
237 
239  p->drawRect( QRectF( baseX + scaledMaxVal, baseY - scaledWidth * values.size() - spacing * std::max( 0, values.size() - 1 ) + currentOffset, 0 - length, scaledWidth ) );
240  break;
241  }
242 
243  currentOffset += scaledWidth + spacing;
244  }
245 
246  if ( s.showAxis() && s.axisLineSymbol() )
247  {
248  s.axisLineSymbol()->startRender( c );
249  QPolygonF axisPoints;
250  switch ( s.diagramOrientation )
251  {
253  axisPoints << QPointF( baseX, baseY - scaledMaxVal ) << QPointF( baseX, baseY ) << QPointF( baseX + scaledWidth * values.size() + spacing * std::max( 0, values.size() - 1 ), baseY );
254  break;
255 
257  axisPoints << QPointF( baseX, baseY ) << QPointF( baseX, baseY - scaledMaxVal ) << QPointF( baseX + scaledWidth * values.size() + spacing * std::max( 0, values.size() - 1 ), baseY - scaledMaxVal );
258  break;
259 
261  axisPoints << QPointF( baseX + scaledMaxVal, baseY - scaledWidth * values.size() - spacing * std::max( 0, values.size() - 1 ) )
262  << QPointF( baseX, baseY - scaledWidth * values.size() - spacing * std::max( 0, values.size() - 1 ) )
263  << QPointF( baseX, baseY );
264  break;
265 
267  axisPoints << QPointF( baseX, baseY - scaledWidth * values.size() - spacing * std::max( 0, values.size() - 1 ) )
268  << QPointF( baseX + scaledMaxVal, baseY - scaledWidth * values.size() - spacing * std::max( 0, values.size() - 1 ) )
269  << QPointF( baseX + scaledMaxVal, baseY );
270  break;
271  }
272 
273  s.axisLineSymbol()->renderPolyline( axisPoints, nullptr, c );
274  s.axisLineSymbol()->stopRender( c );
275  }
276 }
A vector of attributes.
Definition: qgsattributes.h:58
Additional diagram settings for interpolated size rendering.
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:48
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
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...
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 id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsAttributes attributes
Definition: qgsfeature.h:65
QgsFields fields
Definition: qgsfeature.h:66
bool isEmpty() const
Checks whether the container is empty.
Definition: qgsfields.cpp:128
A histogram style diagram.
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.
QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s) override
Returns the size in map units the diagram will use to render.
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)
QgsHistogramDiagram * clone() const override
Returns an instance that is equivalent to this one.
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:2225
Contains information about the context of a rendering operation.
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:507
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
Definition: qgssymbol.cpp:480
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:316
#define DIAGRAM_NAME_HISTOGRAM