QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
17#include "qgsdiagramrenderer.h"
18#include "qgsexpression.h"
19#include "qgslinesymbol.h"
20#include "qgsrendercontext.h"
21#include "qgssymbollayerutils.h"
22
23#include <QPainter>
24
25const QString QgsHistogramDiagram::DIAGRAM_NAME_HISTOGRAM = QStringLiteral( "Histogram" );
26
28{
29 mCategoryBrush.setStyle( Qt::SolidPattern );
30 mPen.setStyle( Qt::SolidLine );
31 mScaleFactor = 0;
32}
33
38
40{
41 QSizeF size;
42 if ( feature.attributeCount() == 0 )
43 {
44 return size; //zero size if no attributes
45 }
46
47 if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
48 return size; // invalid value range => zero size
49
50 double maxValue = 0;
51
52 QgsExpressionContext expressionContext = c.expressionContext();
53 expressionContext.setFeature( feature );
54 if ( !feature.fields().isEmpty() )
55 expressionContext.setFields( feature.fields() );
56
57 for ( const QString &cat : std::as_const( s.categoryAttributes ) )
58 {
59 QgsExpression *expression = getExpression( cat, expressionContext );
60 maxValue = std::max( expression->evaluate( &expressionContext ).toDouble(), maxValue );
61 }
62
63 // Scale, if extension is smaller than the specified minimum
64 if ( maxValue < s.minimumSize )
65 {
66 maxValue = s.minimumSize;
67 }
68
69 // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
70 // a conversion factor to painter units...
71 // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
72 double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
73
74 const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
75
76 switch ( s.diagramOrientation )
77 {
80 mScaleFactor = ( ( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue ) );
81 size.scale( s.barWidth * s.categoryAttributes.size() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ), maxValue * mScaleFactor, Qt::IgnoreAspectRatio );
82 break;
83
86 mScaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
87 size.scale( maxValue * mScaleFactor, s.barWidth * s.categoryAttributes.size() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ), Qt::IgnoreAspectRatio );
88 break;
89 }
90
91 if ( s.showAxis() && s.axisLineSymbol() )
92 {
93 const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c ) / painterUnitConversionScale;
94 size.setWidth( size.width() + 2 * maxBleed );
95 size.setHeight( size.height() + 2 * maxBleed );
96 }
97
98 return size;
99}
100
102{
103 if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
104 return s.minimumSize; // invalid value range => zero size
105
106 // Scale, if extension is smaller than the specified minimum
107 if ( value < s.minimumSize )
108 {
109 value = s.minimumSize;
110 }
111
112 double scaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
113 return value * scaleFactor;
114}
115
120
122{
123 QSizeF size;
124
125 if ( attributes.isEmpty() )
126 {
127 return QSizeF(); //zero size if no attributes
128 }
129
130 double maxValue = attributes.at( 0 ).toDouble();
131
132 for ( int i = 0; i < attributes.count(); ++i )
133 {
134 maxValue = std::max( attributes.at( i ).toDouble(), maxValue );
135 }
136
137 // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
138 // a conversion factor to painter units...
139 // TODO QGIS 4.0 -- these methods should all use painter units, dependent on the render context scaling...
140 double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
141
142 const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
143
144 switch ( s.diagramOrientation )
145 {
148 mScaleFactor = maxValue / s.size.height();
149 size.scale( s.barWidth * s.categoryColors.size() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ), s.size.height(), Qt::IgnoreAspectRatio );
150 break;
151
154 mScaleFactor = maxValue / s.size.width();
155 size.scale( s.size.width(), s.barWidth * s.categoryColors.size() + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 ), Qt::IgnoreAspectRatio );
156 break;
157 }
158
159 if ( s.showAxis() && s.axisLineSymbol() )
160 {
161 const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c ) / painterUnitConversionScale;
162 size.setWidth( size.width() + 2 * maxBleed );
163 size.setHeight( size.height() + 2 * maxBleed );
164 }
165
166 return size;
167}
168
169void QgsHistogramDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
170{
171 QPainter *p = c.painter();
172 if ( !p )
173 {
174 return;
175 }
176
177 QList<double> values;
178 double maxValue = 0;
179
180 QgsExpressionContext expressionContext = c.expressionContext();
181 expressionContext.setFeature( feature );
182 if ( !feature.fields().isEmpty() )
183 expressionContext.setFields( feature.fields() );
184
185 values.reserve( s.categoryAttributes.size() );
186 for ( const QString &cat : std::as_const( s.categoryAttributes ) )
187 {
188 QgsExpression *expression = getExpression( cat, expressionContext );
189 double currentVal = expression->evaluate( &expressionContext ).toDouble();
190 values.push_back( currentVal );
191 maxValue = std::max( currentVal, maxValue );
192 }
193
194 double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );
195
196 double currentOffset = 0;
197 double scaledWidth = sizePainterUnits( s.barWidth, s, c );
198
199 const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() );
200
201 double baseX = position.x();
202 double baseY = position.y();
203
204 if ( s.showAxis() && s.axisLineSymbol() )
205 {
206 // if showing axis, the diagram position needs shifting from the default base x so that the axis
207 // line stroke sits within the desired label engine rect (otherwise we risk overlaps of the axis line stroke)
208 const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c );
209 baseX += maxBleed;
210 baseY -= maxBleed;
211 }
212
213 mPen.setColor( s.penColor );
214 setPenWidth( mPen, s, c );
215 p->setPen( mPen );
216
217 QList<double>::const_iterator valIt = values.constBegin();
218 QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
219 for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
220 {
221 double length = sizePainterUnits( *valIt * mScaleFactor, s, c );
222
223 QColor brushColor( *colIt );
224 brushColor.setAlphaF( brushColor.alphaF() * s.opacity );
225 mCategoryBrush.setColor( brushColor );
226 p->setBrush( mCategoryBrush );
227
228 switch ( s.diagramOrientation )
229 {
231 p->drawRect( QRectF( baseX + currentOffset, baseY, scaledWidth, length * -1 ) );
232 break;
233
235 p->drawRect( QRectF( baseX + currentOffset, baseY - scaledMaxVal, scaledWidth, length ) );
236 break;
237
239 p->drawRect( QRectF( baseX, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) + currentOffset, length, scaledWidth ) );
240 break;
241
243 p->drawRect( QRectF( baseX + scaledMaxVal, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) + currentOffset, 0 - length, scaledWidth ) );
244 break;
245 }
246
247 currentOffset += scaledWidth + spacing;
248 }
249
250 if ( s.showAxis() && s.axisLineSymbol() )
251 {
253 QPolygonF axisPoints;
254 switch ( s.diagramOrientation )
255 {
257 axisPoints << QPointF( baseX, baseY - scaledMaxVal ) << QPointF( baseX, baseY ) << QPointF( baseX + scaledWidth * values.size() + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), baseY );
258 break;
259
261 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 );
262 break;
263
265 axisPoints << QPointF( baseX + scaledMaxVal, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
266 << QPointF( baseX, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
267 << QPointF( baseX, baseY );
268 break;
269
271 axisPoints << QPointF( baseX, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
272 << QPointF( baseX + scaledMaxVal, baseY - scaledWidth * values.size() - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
273 << QPointF( baseX + scaledMaxVal, baseY );
274 break;
275 }
276
277 s.axisLineSymbol()->renderPolyline( axisPoints, nullptr, c );
279 }
280}
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.
Handles 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
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:6607