QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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#include "qgslinesymbol.h"
21
22#include <QPainter>
23
24const QString QgsHistogramDiagram::DIAGRAM_NAME_HISTOGRAM = QStringLiteral( "Histogram" );
25
27{
28 mCategoryBrush.setStyle( Qt::SolidPattern );
29 mPen.setStyle( Qt::SolidLine );
30 mScaleFactor = 0;
31}
32
37
39{
40 QSizeF size;
41 if ( feature.attributeCount() == 0 )
42 {
43 return size; //zero size if no attributes
44 }
45
46 if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
47 return size; // invalid value range => zero size
48
49 double maxValue = 0;
50
51 QgsExpressionContext expressionContext = c.expressionContext();
52 expressionContext.setFeature( feature );
53 if ( !feature.fields().isEmpty() )
54 expressionContext.setFields( feature.fields() );
55
56 for ( const QString &cat : std::as_const( s.categoryAttributes ) )
57 {
58 QgsExpression *expression = getExpression( cat, expressionContext );
59 maxValue = std::max( expression->evaluate( &expressionContext ).toDouble(), maxValue );
60 }
61
62 // Scale, if extension is smaller than the specified minimum
63 if ( maxValue < s.minimumSize )
64 {
65 maxValue = s.minimumSize;
66 }
67
68 // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
69 // a conversion factor to painter units...
70 // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
71 double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
72
73 const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
74
75 switch ( s.diagramOrientation )
76 {
79 mScaleFactor = ( ( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue ) );
80 size.scale( s.barWidth * s.categoryAttributes.size() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ), maxValue * mScaleFactor, Qt::IgnoreAspectRatio );
81 break;
82
85 mScaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
86 size.scale( maxValue * mScaleFactor, s.barWidth * s.categoryAttributes.size() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ), Qt::IgnoreAspectRatio );
87 break;
88 }
89
90 if ( s.showAxis() && s.axisLineSymbol() )
91 {
92 const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c ) / painterUnitConversionScale;
93 size.setWidth( size.width() + 2 * maxBleed );
94 size.setHeight( size.height() + 2 * maxBleed );
95 }
96
97 return size;
98}
99
101{
102 if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
103 return s.minimumSize; // invalid value range => zero size
104
105 // Scale, if extension is smaller than the specified minimum
106 if ( value < s.minimumSize )
107 {
108 value = s.minimumSize;
109 }
110
111 double scaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
112 return value * scaleFactor;
113}
114
119
121{
122 QSizeF size;
123
124 if ( attributes.isEmpty() )
125 {
126 return QSizeF(); //zero size if no attributes
127 }
128
129 double maxValue = attributes.at( 0 ).toDouble();
130
131 for ( int i = 0; i < attributes.count(); ++i )
132 {
133 maxValue = std::max( attributes.at( i ).toDouble(), maxValue );
134 }
135
136 // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
137 // a conversion factor to painter units...
138 // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
139 double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
140
141 const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
142
143 switch ( s.diagramOrientation )
144 {
147 mScaleFactor = maxValue / s.size.height();
148 size.scale( s.barWidth * s.categoryColors.size() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ), s.size.height(), Qt::IgnoreAspectRatio );
149 break;
150
153 mScaleFactor = maxValue / s.size.width();
154 size.scale( s.size.width(), s.barWidth * s.categoryColors.size() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ), Qt::IgnoreAspectRatio );
155 break;
156 }
157
158 if ( s.showAxis() && s.axisLineSymbol() )
159 {
160 const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c ) / painterUnitConversionScale;
161 size.setWidth( size.width() + 2 * maxBleed );
162 size.setHeight( size.height() + 2 * maxBleed );
163 }
164
165 return size;
166}
167
168void QgsHistogramDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
169{
170 QPainter *p = c.painter();
171 if ( !p )
172 {
173 return;
174 }
175
176 QList<double> values;
177 double maxValue = 0;
178
179 QgsExpressionContext expressionContext = c.expressionContext();
180 expressionContext.setFeature( feature );
181 if ( !feature.fields().isEmpty() )
182 expressionContext.setFields( feature.fields() );
183
184 values.reserve( s.categoryAttributes.size() );
185 for ( const QString &cat : std::as_const( s.categoryAttributes ) )
186 {
187 QgsExpression *expression = getExpression( cat, expressionContext );
188 double currentVal = expression->evaluate( &expressionContext ).toDouble();
189 values.push_back( currentVal );
190 maxValue = std::max( currentVal, maxValue );
191 }
192
193 double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );
194
195 double currentOffset = 0;
196 double scaledWidth = sizePainterUnits( s.barWidth, s, c );
197
198 const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() );
199
200 double baseX = position.x();
201 double baseY = position.y();
202
203 if ( s.showAxis() && s.axisLineSymbol() )
204 {
205 // if showing axis, the diagram position needs shifting from the default base x so that the axis
206 // line stroke sits within the desired label engine rect (otherwise we risk overlaps of the axis line stroke)
207 const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c );
208 baseX += maxBleed;
209 baseY -= maxBleed;
210 }
211
212 mPen.setColor( s.penColor );
213 setPenWidth( mPen, s, c );
214 p->setPen( mPen );
215
216 QList<double>::const_iterator valIt = values.constBegin();
217 QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
218 for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
219 {
220 double length = sizePainterUnits( *valIt * mScaleFactor, s, c );
221
222 QColor brushColor( *colIt );
223 brushColor.setAlphaF( brushColor.alphaF() * s.opacity );
224 mCategoryBrush.setColor( brushColor );
225 p->setBrush( mCategoryBrush );
226
227 switch ( s.diagramOrientation )
228 {
230 p->drawRect( QRectF( baseX + currentOffset, baseY, scaledWidth, length * -1 ) );
231 break;
232
234 p->drawRect( QRectF( baseX + currentOffset, baseY - scaledMaxVal, scaledWidth, length ) );
235 break;
236
238 p->drawRect( QRectF( baseX, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) + currentOffset, length, scaledWidth ) );
239 break;
240
242 p->drawRect( QRectF( baseX + scaledMaxVal, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) + currentOffset, 0 - length, scaledWidth ) );
243 break;
244 }
245
246 currentOffset += scaledWidth + spacing;
247 }
248
249 if ( s.showAxis() && s.axisLineSymbol() )
250 {
252 QPolygonF axisPoints;
253 switch ( s.diagramOrientation )
254 {
256 axisPoints << QPointF( baseX, baseY - scaledMaxVal ) << QPointF( baseX, baseY ) << QPointF( baseX + scaledWidth * values.size() + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), baseY );
257 break;
258
260 axisPoints << QPointF( baseX, baseY ) << QPointF( baseX, baseY - scaledMaxVal ) << QPointF( baseX + scaledWidth * values.size() + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), baseY - scaledMaxVal );
261 break;
262
264 axisPoints << QPointF( baseX + scaledMaxVal, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
265 << QPointF( baseX, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
266 << QPointF( baseX, baseY );
267 break;
268
270 axisPoints << QPointF( baseX, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
271 << QPointF( baseX + scaledMaxVal, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
272 << QPointF( baseX + scaledMaxVal, baseY );
273 break;
274 }
275
276 s.axisLineSymbol()->renderPolyline( axisPoints, nullptr, c );
278 }
279}
A vector of attributes.
Additional diagram settings for interpolated size rendering.
Stores the settings for rendering a single diagram.
bool showAxis() const
Returns true if the diagram axis should be shown.
double opacity
Opacity, from 0 (transparent) to 1.0 (opaque)
QgsLineSymbol * axisLineSymbol() const
Returns the line symbol to use for rendering axis in diagrams.
Qgis::RenderUnit sizeType
Diagram size unit.
QList< QString > categoryAttributes
DiagramOrientation diagramOrientation
double spacing() const
Returns the spacing between diagram contents.
QList< QColor > categoryColors
const QgsMapUnitScale & spacingMapUnitScale() const
Returns the map unit scale for the content spacing.
double minimumSize
Scale diagrams smaller than mMinimumSize to mMinimumSize.
Qgis::RenderUnit spacingUnit() const
Returns the units for the content spacing.
void setPenWidth(QPen &pen, const QgsDiagramSettings &s, const QgsRenderContext &c)
Changes the pen width to match the current settings and rendering context.
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
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:58
QgsFields fields
Definition qgsfeature.h:68
int attributeCount() const
Returns the number of attributes attached to the feature.
bool isEmpty
Definition qgsfields.h:49
A histogram style diagram.
double legendSize(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &interpolationSettings) 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.
static const QString DIAGRAM_NAME_HISTOGRAM
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.
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.
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
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:5958