QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 double minValue = 0;
55 double tempValue = 0;
56 double valueRange = 0;
57
58 QgsExpressionContext expressionContext = c.expressionContext();
59 expressionContext.setFeature( feature );
60 if ( !feature.fields().isEmpty() )
61 expressionContext.setFields( feature.fields() );
62
63 for ( const QString &cat : std::as_const( s.categoryAttributes ) )
64 {
65 QgsExpression *expression = getExpression( cat, expressionContext );
66 tempValue = expression->evaluate( &expressionContext ).toDouble();
67 maxValue = std::max( tempValue, maxValue );
68 minValue = std::min( tempValue, minValue );
69 }
70
71 // Account for negative values
72 valueRange = maxValue + ( minValue * -1 );
73
74 // Scale, if extension is smaller than the specified minimum
75 if ( valueRange < s.minimumSize )
76 {
77 valueRange = s.minimumSize;
78 }
79
80 // eh - this method returns size in unknown units ...! We'll have to fake it and use a rough estimation of
81 // a conversion factor to painter units...
82 // TODO QGIS 5.0 -- these methods should all use painter units, dependent on the render context scaling...
83 double painterUnitConversionScale = c.convertToPainterUnits( 1, s.sizeType );
84
85 const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() ) / painterUnitConversionScale;
86
87 switch ( s.diagramOrientation )
88 {
91 {
92 mScaleFactor = ( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue );
93 double scaleWidth = s.barWidth * static_cast<double>( s.categoryAttributes.size() ) + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() ) - 1 );
94 size.scale( scaleWidth, valueRange * mScaleFactor, Qt::IgnoreAspectRatio );
95 break;
96 }
97
100 {
101 mScaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
102 double scaleHeight = s.barWidth * static_cast<double>( s.categoryAttributes.size() ) + spacing * std::max( 0, static_cast<int>( s.categoryAttributes.size() - 1 ) );
103 size.scale( valueRange * mScaleFactor, scaleHeight, Qt::IgnoreAspectRatio );
104 break;
105 }
106 }
107
108 if ( s.showAxis() && s.axisLineSymbol() )
109 {
110 const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c ) / painterUnitConversionScale;
111 size.setWidth( size.width() + 2 * maxBleed );
112 size.setHeight( size.height() + 2 * maxBleed );
113 }
114
115 return size;
116}
117
119{
120 if ( qgsDoubleNear( is.upperValue, is.lowerValue ) )
121 return s.minimumSize; // invalid value range => zero size
122
123 // Scale, if extension is smaller than the specified minimum
124 if ( value < s.minimumSize )
125 {
126 value = s.minimumSize;
127 }
128
129 double scaleFactor = ( ( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) );
130 return value * scaleFactor;
131}
132
137
139{
140 // Since histograms only support interpolated size,
141 // we only keep this method for compatibility reasons.
142 return QSizeF();
143}
144
145void QgsHistogramDiagram::renderDiagram( const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position )
146{
147 QPainter *p = c.painter();
148 if ( !p )
149 {
150 return;
151 }
152
153 QList<double> values;
154 double maxValue = 0;
155 double minValue = 0;
156
157 QgsExpressionContext expressionContext = c.expressionContext();
158 expressionContext.setFeature( feature );
159 if ( !feature.fields().isEmpty() )
160 expressionContext.setFields( feature.fields() );
161
162 values.reserve( s.categoryAttributes.size() );
163 for ( const QString &cat : std::as_const( s.categoryAttributes ) )
164 {
165 QgsExpression *expression = getExpression( cat, expressionContext );
166 double currentVal = expression->evaluate( &expressionContext ).toDouble();
167 values.push_back( currentVal );
168 maxValue = std::max( currentVal, maxValue );
169 minValue = std::min( currentVal, minValue );
170 }
171
172 double scaledMaxVal = sizePainterUnits( maxValue * mScaleFactor, s, c );
173 double scaledMinVal = sizePainterUnits( minValue * mScaleFactor, s, c ) * -1;
174
175 double currentOffset = 0;
176 double scaledWidth = sizePainterUnits( s.barWidth, s, c );
177
178 const double spacing = c.convertToPainterUnits( s.spacing(), s.spacingUnit(), s.spacingMapUnitScale() );
179
180 double baseX = position.x();
181 double baseY = position.y();
182
183 if ( s.showAxis() && s.axisLineSymbol() )
184 {
185 // if showing axis, the diagram position needs shifting from the default base x so that the axis
186 // line stroke sits within the desired label engine rect (otherwise we risk overlaps of the axis line stroke)
187 const double maxBleed = QgsSymbolLayerUtils::estimateMaxSymbolBleed( s.axisLineSymbol(), c );
188 baseX += maxBleed;
189 baseY -= maxBleed;
190 }
191
192 // Special bases
193 double baseYTop = baseY - scaledMinVal;
194 double baseYDown = baseY - scaledMaxVal;
195 double baseXRight = baseX + scaledMinVal;
196 double baseXLeft = baseX + scaledMaxVal;
197
198 mPen.setColor( s.penColor );
199 setPenWidth( mPen, s, c );
200 p->setPen( mPen );
201
202 QList<double>::const_iterator valIt = values.constBegin();
203 QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
204 for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
205 {
206 double length = sizePainterUnits( *valIt * mScaleFactor, s, c );
207
208 QColor brushColor( *colIt );
209 brushColor.setAlphaF( brushColor.alphaF() * static_cast<float>( s.opacity ) );
210 mCategoryBrush.setColor( brushColor );
211 p->setBrush( mCategoryBrush );
212
213 switch ( s.diagramOrientation )
214 {
216 p->drawRect( QRectF( baseX + currentOffset, baseYTop, scaledWidth, length * -1 ) );
217 break;
218
220 p->drawRect( QRectF( baseX + currentOffset, baseYDown, scaledWidth, length ) );
221 break;
222
224 p->drawRect(
225 QRectF( baseXRight, baseY - scaledWidth * static_cast<double>( values.size() ) - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) + currentOffset, length, scaledWidth )
226 );
227 break;
228
230 p->drawRect(
231 QRectF( baseXLeft, baseY - scaledWidth * static_cast<double>( values.size() ) - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) + currentOffset, 0 - length, scaledWidth )
232 );
233 break;
234 }
235
236 currentOffset += scaledWidth + spacing;
237 }
238
239 if ( s.showAxis() && s.axisLineSymbol() )
240 {
242 QPolygonF axisPoints;
243 switch ( s.diagramOrientation )
244 {
246 axisPoints
247 << QPointF( baseX, baseYTop - scaledMaxVal )
248 << QPointF( baseX, baseYTop )
249 << QPointF( baseX + scaledWidth * static_cast<double>( values.size() ) + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), baseYTop );
250 break;
251
253 axisPoints
254 << QPointF( baseX, baseYDown + scaledMaxVal )
255 << QPointF( baseX, baseYDown )
256 << QPointF( baseX + scaledWidth * static_cast<double>( values.size() ) + spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ), baseYDown );
257 break;
258
260 axisPoints
261 << QPointF( baseXRight + scaledMaxVal, baseY - scaledWidth * static_cast<double>( values.size() ) - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
262 << QPointF( baseXRight, baseY - scaledWidth * static_cast<double>( values.size() ) - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
263 << QPointF( baseXRight, baseY );
264 break;
265
267 axisPoints
268 << QPointF( baseXLeft - scaledMaxVal, baseY - scaledWidth * static_cast<double>( values.size() ) - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
269 << QPointF( baseXLeft, baseY - scaledWidth * static_cast<double>( values.size() ) - spacing * std::max( 0, static_cast<int>( values.size() ) - 1 ) )
270 << QPointF( baseXLeft, baseY );
271 break;
272 }
273
274 s.axisLineSymbol()->renderPolyline( axisPoints, nullptr, c );
276 }
277}
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:6975