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